Skip to content

Instantly share code, notes, and snippets.

@bguidolim
Last active December 4, 2023 22:49
Show Gist options
  • Save bguidolim/7ac570e423172433353032e12bf9fb07 to your computer and use it in GitHub Desktop.
Save bguidolim/7ac570e423172433353032e12bf9fb07 to your computer and use it in GitHub Desktop.
import UIKit
let userJson = """
{
"id": "4yq6txdpfadhbaqnwp3",
"age": 5,
"name":"John Doe",
"properties": {
"dynamicKeyA": "1",
"dynamicKeyB": "2",
"dynamicKeyC": "3"
}
}
""".data(using: .utf8)!
typealias Properties = [String: String]
struct User: Decodable {
let id: String
let age: Int
let name: String
let properties: Properties
enum CodingKeys: String, CodingKey {
case id
case age
case name
case properties
}
}
extension Properties {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self = try container.decode(Properties.self)
}
}
let test = try JSONDecoder().decode(User.self, from: userJson)
print(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment