reazen / relude

FP-inspired prelude/standard library for ReasonML projects
https://reazen.github.io/relude
MIT License
267 stars 41 forks source link

JSON typeclasses/support #154

Open andywhite37 opened 5 years ago

andywhite37 commented 5 years ago

This might belong in another library (although I think the typeclasses and base impls might belong in relude core), but it would be cool to have a consistent answer to encoding and decoding JSON for arbitrary types. Maybe something similar to the Aeson ToJSON/FromJSON typeclasses, or similar things in purescript-argonaut, or other similar libs. We don't have the magic of implicit typelcasses, but we could at least provide default implementation modules for all of our types in a consistent way, to make it easier to compose together bigger encoders/decoders from component parts.

Here are some ideas I was playing around with:

https://sketch.sh/s/5FwJfBkuxRRbL1d62bDDRG/

The end goal would be to have a predictable and consistent way to expose JSON support for any type, like:

Relude.Int.JSON.encode(42);
Relude.Int.JSON.decode(`JInt(42));
let jsJson = Relude.JSON.toJsJson(...);
let reludeJSON = Relude.JSON.fromJsJson(...);

This also has the classic naming convention struggle of JSON vs Json...

andywhite37 commented 5 years ago

@mlml13 - I'm curious how this might fit in with bs-decode - would a decode function with a type like json => result('a, 'e) work seamlessly with bs-decode stuff? I believe most of our decoder functions in the wild are something like that.

andywhite37 commented 4 years ago

Now that we have a free applicative, these typeclasses would me more useful if we wanted to build a schema-type of library, where you could interpret a FreeAp-based schema into a ToJSON or FromJSON functor.

I think it would probably make sense to split all the relude JSON stuff off into a relude-json library, and then create a separate relude-schema library, that depends on relude and relude-json to provide the FromJSON/ToJSON support for schemas.

The goal would not be to replace bs-decode, but to just explore the idea of using typeclasses and functors for FreeAp interpretations of data schemas.