Set of extension and custom classes to efficiently decode heterogeneous collections.


An extension for KeyedDecodingContainer class to decode a collection of heterogeneous types.


Start by creating an enum that has variants for the parsable type it must adhere to ClassFamily and define the type information discriminator.

enum DrinkFamily: String, ClassFamily {
    case drink = "drink"
    case beer = "beer"

    static var discriminator: Discriminator = .type
    typealias BaseType = Drink

    func getType() -> Drink.Type {
        switch self {
        case .beer:
            return Beer.self
        case .drink:
            return Drink.self

Later in your collection overload the init method to use our KeyedDecodingContainer extension.

class Bar: Decodable {
    let drinks: [Drink]

    private enum CodingKeys: String, CodingKey {
        case drinks

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        drinks = try container.decodeHeterogeneousArray(OfFamily: DrinkFamily.self, forKey: .drinks)

let barJson = """
        {"type": "drink", "description": "All natural"},
        {"type": "beer", "description": "best drunk on fridays after work", "alcohol_content": "5%"}
""".data(using: .utf8)!

let bar = try JSONDecoder().decode(Bar.self, from: barJson)

Otherwise follow the below example to decode an Hetegerenous json array object.

let drinksJson = """
    {"type": "drink", "description": "All natural"},
    {"type": "beer", "description": "best drunk on fridays after work", "alcohol_content": "5%"}
""".data(using: .utf8)!

let drinks = try JSONDecoder().decodeHeterogeneousArray(OfFamily: DrinkFamily.self, from: drinksJson)


In case you need to be able to decode heterogenous values — including nested arrays and dictionaries — then you need AnyDecodable:

let json = """
    {"type": "soda", "sugar_content": 5, "alcoholic_drink": false, "description": null}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let dictionary = try decoder.decode([String: AnyDecodable].self, from: json)


Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/abdalaliii/Unboxing.git")


Unboxing is available under the MIT license. See the LICENSE file for more info.


  • Swift Tools 5.1.0
View More Packages from this Author


  • None
Last updated: Sun Nov 19 2023 01:57:02 GMT-1000 (Hawaii-Aleutian Standard Time)