3 ms·
This is a problem space that crystal-lang handles with beautiful elegance [1]. Include a single module, get sane behaviour out-of-the-box, and specify anything
by kimburgess 6y ago
This is a problem space that crystal-lang handles with beautiful elegance [1]. Include a single module, get sane behaviour out-of-the-box, and specify anything domain specific with a type annotations.
[1]: https://crystal-lang.org/api/0.35.1/JSON/Serializable.html https://crystal-lang.org/api/0.35.1/JSON/Serializable.html
- valenterry 6y agoHow does it look if you have two different json schemas/versions that are both valid (maybe an old one and a new one)?
- kimburgess 6y agoYou specify the type to deserialise to. This lets you be very concrete about this if you have versioned API endpoint or different data source. Alternatively if you deserialise into a type union, the first compatible type will be used. Finally, you can also use a discriminator field [1] to select an appropriate subtype. [1]: https://crystal-lang.org/api/0.35.1/JSON/Serializable.html#use_json_discriminator(field,mapping)-macro https://crystal-lang.org/api/0.35.1/JSON/Serializable.html#u...
- valenterry 6y agoI mean, what if the type I deserialize into is the same (my current domain object type), but I have to deserialize two (or more) different versions of json into it. I think what you linked is the case where my domain object type is either A or B - but that's not what I meant.