5 ms·
It means sometimes you'll get: { foo: { bar: "baz" }} and other times you'll get { foo: "something else" } Good luck!
by q3k 6y ago
It means sometimes you'll get:
{ foo: { bar: "baz" }}
and other times you'll get
{ foo: "something else" }
Good luck!
- maerF0x0 6y agoPolymorphic JSON is such a PITA for strongly typed languages. var data map[string]interface{} //.. switch t := data["foo"].(type) { case string: case interface{} } imagine that for every key...
- speedgoose 6y agoIn my humble opinion, Golang is pretty verbose in a bad way when it comes to JSON. Rust and the crate serde_json are also strongly typed and it's a lot better.
- q3k 6y agoHow would you express deserializing these 'polymorphic JSON' objects using serde/Rust?
- speedgoose 6y agoUsing a rust enum I guess.
- q3k 6y agoYou're right, this does seem to work [1]. I wasn't aware that serde would attempt to deserialize multiple enum variants until something matches. [1] - https://serde.rs/enum-representations.html#untagged https://serde.rs/enum-representations.html#untagged
- masklinn 6y agoYeah that's a rather common scheme out there so Serde does provide built-in support for this deserialisation. Probably better for deserialisation performances to use properly tagged enums, but if you don't have a choice Serde's got your back.
- deleted 6y ago[deleted]
- curryst 6y agoYeah, having done this, it's extremely verbose if you have all your error handling in there due to the verbosity of dealing with JSON and explicit return values instead of exceptions. By far my greatest annoyance is that []interface{} cannot be directly cast to []concreteType. Having to make a sized slice and type assert each value is annoying. Require validation of the values for even more "if err != nil" fun. Many Go advocates seem to consider the verbosity a feature, because it's explicit and forbids any clever-but-confusing tricks.
- sroussey 6y agoSo, like Apple and in app payments then...
- rmrfrmrf 6y ago{ foo: { length: 1.7976931348623157e+308 } }