How to parse an empty dictionary object in Argo
wongzigii opened this issue · 1 comments
wongzigii commented
Seem like Argo cannot handle the empty dictionary situation corrrectly.
In this case, I try to parse this JSON into User
and then check if the userInfo
is empty.
Sample JSON
{
"name": "Li",
"userInfo": {}
}
Models
All models involved, including their Decodable
implementations.
struct User {
var name: String
var userInfo: UserInfo
}
extension User: Argo.Decodable {
public static func decode(_ json: JSON) -> Decoded<User> {
return curry(User.init)
<^> json <| "name"
<*> json <| "userInfo" // or json <|? "userInfo" both not work
}
}
Argo Version
example: Argo 3.0.0
Dependency Manager
examples: Carthage, Cocoapods, git submodules, copied code, etc.
gfontenot commented
Hey there!
We do support decoding dictionaries, assuming that the dictionary has String
keys and Decodable
values. It also needs to be a homomorphic dictionary, we can't decode to [String: Any]
. If that fits your use case, then you can use decodeObject
(or [String: T].decode
if you prefer). We don't expose an operator for it, but it's straightforward to plug in using flatMap
(or >>-
):
struct User {
var name: String
var userInfo: UserInfo
}
extension User: Argo.Decodable {
public static func decode(_ json: JSON) -> Decoded<User> {
return curry(User.init)
<^> json <| "name"
<*> (json <| "userInfo") >>- decodeObject
}
}