Create an example of a struct with a Codable field

I have the following struct, that was autogenerated from a JSON file:

struct EmergencyNumberModel: Codable, Identifiable {
    let id = UUID()
    let country: Country
    let ambulance: Ambulance
    let fire: Fire
    let police: Ambulance
    let dispatch: Dispatch
    let member112, localOnly: Bool
    let notes: NotesUnion <<<<
    let noData: Bool?
    let traffic: Traffic?

    enum CodingKeys: String, CodingKey {
        case country = "Country"
        case ambulance = "Ambulance"
        case fire = "Fire"
        case police = "Police"
        case dispatch = "Dispatch"
        case member112 = "Member_112"
        case localOnly = "LocalOnly"
        case notes = "Notes"
        case noData = "NoData"
        case traffic = "Traffic"
    }
}

And to use the information from the JSON in a view, I created a function that returns an example with the information:

static func example() -> EmergencyNumberModel {
        return EmergencyNumberModel(country: Country(name: "Japan", isoCode: "JP", isoNumeric: "392"),
 ambulance: Ambulance(all: ["119"], gsm: nil),
 fire: Fire(all: ["119"]),
 police: Ambulance(all: ["110"], gsm: nil),
 dispatch: Dispatch(all: [nil], fixed: nil, gsm: nil),
 member112: false,
 localOnly: false,
 notes: NotesUnion, <<<<<<
 noData: nil,
 traffic: Traffic(all: ["122"]))
}

But the notes field in the JSON sometimes is missing or is of different types, the notes in the JSON are like this:

"Notes": ""

or like this:

"Notes": false

And because of that the struct generator created this:

enum NotesUnion: Codable {
    case bool(Bool)
    case enumeration(NotesEnum)
    case null

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Bool.self) {
            self = .bool(x)
            return
        }
        if let x = try? container.decode(NotesEnum.self) {
            self = .enumeration(x)
            return
        }
        if container.decodeNil() {
            self = .null
            return
        }
        throw DecodingError.typeMismatch(NotesUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for NotesUnion"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .bool(let x):
            try container.encode(x)
        case .enumeration(let x):
            try container.encode(x)
        case .null:
            try container.encodeNil()
        }
    }
}

enum NotesEnum: String, Codable {
    case empty = ""
    case uninhabited = "Uninhabited"
}

I researched a bit and now I know that code is to convert between JSON and Swift types.

When I autocomplete the NotesUnion type, Xcode gives me this:

NotesUnion(from: Decoder)

Which type of Decoder I need to be able to generate the example to use it in a view?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum