The “I’m stupid” elm language nugget #15

In elm, when interacting with the server side of an app, I frequently need a pair of Json.Decoder and Json.Encoder for the same type. I’ve always disliked that these had to be separate. Why can’t I just specify both at the same time?

I wrote and published a package that helps with this:

The gist is that for most cases, you can specify almost every part of the encoding and decoding boilerplate just once, together:

import JsonCodec exposing (Codec)
type alias Session =
{ queue : List String
, playing : Maybe (String, Float)
, likeCategories : Dict String Int
-- A codec that can stand on its own and also be reused.
playingSerializer : Codec (Maybe (String,Float))
playingSerializer =
("u", JsonCodec.string)
("p", JsonCodec.float)
-- Simple codec built with composition.
serializer : Codec Session
serializer =
(\s -> (s.queue,s.playing,s.likeCategories))
("queue", JsonCodec.list JsonCodec.string)
("playing", playingSerializer)
("like", JsonCodec.dict

It has a similar enough naming convention to Json.Decode that it should be pretty easy to start using it. The way it builds codecs isn’t quite as general as Json.Decode but the advantage of being able to share encoding and decoding construction most of the time, IMO is worth it. You can still make JsonCodec.Codec values via the init function, which takes an arbitrary pair of decoder and encode function, for the hopefully rare cases where a serialization is very complicated.