4 ms·
As some who doesn't speak haskell, how do you add type safety to this example, but using more precise types than string/object/array ?
by hershel 13y ago
As some who doesn't speak haskell, how do you add type safety to this example, but using more precise types than string/object/array ?
- tel 13y agoThere are already more precise types being inferred by the compiler and erased. Broadly, the "right" way to use typed JSON in Haskell is to write a JSON "schema" via FromJSON and ToJSON instances. This is fairly easy due to generic recursion in Haskell, but much more work than the python or lens examples. It's important to note that in order to interact with JSON Haskell uses an un typed microlanguage called aeson.Value which is the most direct translation to un typed JSON. The From/ToJSON code does a much more powerful lifting and lowering of Value into full Haskell types.
- Peaker 13y agoThere's Template Haskell support to auto-generate FromJSON/ToJSON instances, so if you don't care exactly how the JSON looks like, you can get type-safe JSON serialization/schema validation for free.
- tel 13y agoI personally find those wonderful in prototyping but it's rare that I control both ends of a JSON pipeline so it's important for me to write custom schema. One thing I've found really useful is generating ADTs which have structure that's driven by the exact format to make the TH derive a correct To/FromJSON instance and then exporting it abstractly and having lots of Lenses which specify a more Haskell-friendly access pattern.