Decode value that is sometimes an Int and other times a String your Codable
structs through property wrappers.
.package(url: "https://github.com/to4iki/ForcibleValue", from: "0.1.0")
pod 'ForcibleValue'
github "to4iki/ForcibleValue"
You can define a variable of forcible type to decode your structs.
struct User: Decodable {
@ForcibleString var name: String
@ForcibleInt var age: Int
@ForcibleDouble var height: Double
@ForcibleFloat var weight: Float
@ForcibleBool var isAdmin: Bool
}
let json = """
{
"name": 1234,
"age": "30",
"height": "172.3",
"weight": "60.0",
"isAdmin": 1
}
""".data(using: .utf8)
do {
let user = try JSONDecoder().decode(User.self, from: json!)
print(user) // User(_name: 1234, _age: 30, _height: 172.3, _weight: 60.0, _isAdmin: true)
} catch {
print(error)
}
ForcibleValue is released under the MIT license.
link |
Stars: 0 |
Last commit: 3 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics