4 ms·
You don't need to write the decoder boilerplate manually to get all those benefits. For example, here's how you rename a JSON field while serializing/deseriali
by somenewacc 9y ago
You don't need to write the decoder boilerplate manually to get all those benefits.
For example, here's how you rename a JSON field while serializing/deserializing a data type in Rust:
https://play.rust-lang.org/?gist=1b382bc1572858841d5e392435de73f4&version=stable https://play.rust-lang.org/?gist=1b382bc1572858841d5e392435d...
You just annotate the field with #[serde(rename = "..")]. Here is a list of such annotations
https://serde.rs/field-attrs.html https://serde.rs/field-attrs.html
Serde is also generic in the serialization format; the example I linked uses serde_json, and was adapted from its README here https://github.com/serde-rs/json https://github.com/serde-rs/json
- MaxGabriel 9y agoSame for Haskell. This package provides common translations like snake_case to CamelCase: https://www.stackage.org/haddock/lts-9.0/aeson-casing-0.1.0.5/Data-Aeson-Casing.html https://www.stackage.org/haddock/lts-9.0/aeson-casing-0.1.0.... Giving you automatic encoders/decoders like so: instance ToJSON Person where toJSON = genericToJSON $ aesonPrefix snakeCase instance FromJSON Person where parseJSON = genericParseJSON $ aesonPrefix snakeCase And the implementation of that package is like 4 simple lines for snake case; it's totally doable on your own for whatever you need https://github.com/AndrewRademacher/aeson-casing/blob/260d18d62bbb57240b0b7b1e908c8a896ffd8d77/src/Data/Aeson/Casing/Internal.hs#L34-L40 https://github.com/AndrewRademacher/aeson-casing/blob/260d18... I haven't had to do snake_case to CamelCase with Aeson before, but I have dropped a prefix before, like "userName" -> "name", "userAge" -> "age", and it was pretty easy and well supported. Also I would note that this isn't as big of a deal for Haskell and Rust, because they're primarily backend languages, so they're more often sending out JSON in whatever form they please, rather than consuming it. In my experience the main consumers (Javascript on the web, Objective-C on iOS and Java on Android) use CamelCase anyway, so there's a natural compatibility.