There was a discussion started by Levi in the elm slack, about Pickler Combinators. The paper is here:
Picklers in Haskell are like json encoders in Elm. The idea behind the pickler combinators, is that you use them to describe the structure of the data model, and from that description the encoders and decoders are automatically derived. This means you only have to describe the structure once, instead of writing as you often do a pair of encoding/decoding functions independently.
The idea had occurred to me before I heard of this pickler combinators paper, and I had a go at implementing it. Here it is:
I was hoping to go even further, and also be able to derive a json schema automatically too, as well as decoders that verified their input against a json schema.
To give an example of what this would look like in code:
type alias ObjectSimpleFields =
{ a : String
, b : Int
, c : Float
, d : Bool
}
objectSimpleFieldsDecoder =
object ObjectSimpleFields
|> with (field "a" .a string)
|> with (field "b" .b integer)
|> with (field "c" .c number)
|> with (field "d" .d boolean)
|> build
The idea is that there would be a DSL for describing the structure of whatever data model you are working with that pretty much mirrors its structure 1:1. From that you obtain some kind of Builder
representing that model, and that could then be converted to a json encoder or decoder or whatever.
What I managed to do so far was pretty hard going, and I am aware that I still have union types, recursion, tuples, lists and so on to tackle.
It would be interesting if anyone has some good ideas on how to progress this?
Also, with the picklers paper above it might be much easier to see how this can be done in Elm (it suggests how an implementation in ML could be written). Or indeed, has anyone else already tried to do this in Elm?