简体   繁体   English

可编码 HAL JSON 类型

[英]Codable HAL JSON types

Does anyone have a process to manage HAL type JSON data?有人有管理 HAL 类型 JSON 数据的流程吗? The problem I'm running into is that all data requests will return a container that embeds it's actual type into an "_embedded" key.我遇到的问题是所有数据请求都将返回一个将其实际类型嵌入到“_embedded”键中的容器。 I'm struggling with figuring out how to decode out of this type since each embedded key may have multiple [Any HalTypes] assigned to it.我正在努力弄清楚如何从这种类型中解码出来,因为每个嵌入的键可能有多个 [Any HalTypes] 分配给它。 For example, if I make a request for a Menu Item or a Menu Category it will return the same overarching structure.例如,如果我请求菜单项或菜单类别,它将返回相同的总体结构。 The below JSON is for the Menu Category.下面的 JSON 用于菜单类别。

Eg,例如,

Model Model

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let category = try? newJSONDecoder().decode(Category.self, from: jsonData)

import Foundation

// MARK: - Category
struct Category: Codable {
    var embedded: Embedded?
    var links: CategoryLinksClass?
    var count, limit: Int?

    enum CodingKeys: String, CodingKey {
        case embedded = "_embedded"
        case links = "_links"
        case count, limit
    }
}

// MARK: - Embedded
struct Embedded: Codable {
    var categories: [CategoryElement]?
}

// MARK: - CategoryElement
struct CategoryElement: Codable {
    var links: CategoryLinks?
    var id: String?
    var level: Int?
    var name, posid: String?

    enum CodingKeys: String, CodingKey {
        case links = "_links"
        case id, level, name
        case posid = "pos_id"
    }
}

// MARK: - CategoryLinks
struct CategoryLinks: Codable {
    var linksSelf: Next?

    enum CodingKeys: String, CodingKey {
        case linksSelf = "self"
    }
}

// MARK: - Next
struct Next: Codable {
    var href: String?
    var type: String?
}

// MARK: - CategoryLinksClass
struct CategoryLinksClass: Codable {
    var next, linksSelf: Next?

    enum CodingKeys: String, CodingKey {
        case next
        case linksSelf = "self"
    }
}

JSON JSON

{
  "_embedded": {
    "categories": [
      {
        "_links": {
          "self": {
            "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/1001/",
            "type": "application/json; name=menu_category"
          }
        },
        "id": "1001",
        "level": 0,
        "name": "Entree",
        "pos_id": "1001"
      },
      {
        "_links": {
          "self": {
            "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/1002/",
            "type": "application/json; name=menu_category"
          }
        },
        "id": "1002",
        "level": 0,
        "name": "Appetizer",
        "pos_id": "1002"
      }
    ]
  },
  "_links": {
    "next": {
      "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/?limit=2&start=2",
      "type": "application/json; name=menu_category_list"
    },
    "self": {
      "href": "https://api.omnivore.io/1.0/locations/iE7e78GT/menu/categories/?limit=2",
      "type": "application/json; name=menu_category_list"
    }
  },
  "count": 2,
  "limit": 2
}

You could make the _embedded key accept a generic Codable struct instead of the particular type every time.您可以让_embedded键每次都接受一个通用的Codable结构而不是特定的类型。

struct Category<T: Codable>: Codable {
    var embedded: T?
    var links: CategoryLinksClass?
    var count, limit: Int?

    enum CodingKeys: String, CodingKey {
        case embedded = "_embedded"
        case links = "_links"
        case count, limit
    }
}

And create different models that are returned for the "_embedded" key.并创建为"_embedded"键返回的不同模型。

struct Embedded: Codable { ... }

struct Menu: Codable { ... }

And then provide the model type at the time of decoding like this:然后在解码时提供 model 类型,如下所示:

do { let decoded = JSONDecoder().decode(Category<Embedded>.self, from: data) 
} catch { print(error) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM