0

Does anyone have a process to manage HAL type JSON data? 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. 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. 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.

E.g.,

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

{
  "_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
}

1 Answers1

1

You could make the _embedded key accept a generic Codable struct instead of the particular type every time.

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.

struct Embedded: Codable { ... }

struct Menu: Codable { ... }

And then provide the model type at the time of decoding like this:

do { let decoded = JSONDecoder().decode(Category<Embedded>.self, from: data) 
} catch { print(error) }
Frankenstein
  • 15,732
  • 4
  • 22
  • 47