Last active
December 4, 2023 22:49
-
-
Save bguidolim/7ac570e423172433353032e12bf9fb07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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