Elm-serialize: Quickly and reliably encode and decode Elm values

I’ve released elm-serialize, a package that makes it quick and reliable to encode and decode Elm values.

This is an iteration on elm-codec-bytes which in turn is inspired by miniBill/elm-codec. All of these packages use what’s called a Codec to define encoders and decoders in one place. This saves you time typing and makes it harder for encoders and decoders to get out of sync.

Unlike elm-codec and elm-codec-bytes which encode to json and binary respectively, elm-serialize treats the encoding as an implementation detail. You can encode your data as Bytes or as a url safe string.

elm-codec and elm-codec-bytes also trade off some ease of use in order to support decoding data encoded from external sources. elm-serialize gives up support for decoding external data formats in order to make it as easy as possible to write codecs specifically for serializing and deserializing Elm values.

I recommend trying out this package if you have an app that needs to save and load data from local storage or if you have a server also written in Elm that communicates with your Elm app.

I’ve also released elm-geometry-serialize. It’s a collection of codecs for ianmackenzie/elm-geometry so that anyone using elm-geometry can get a head start if they want to try out elm-serialize.

Lastly, thanks to @drathier, @ianmackenzie, and @jfmengels for providing lots of feedback while I was making this.

21 Likes

I’ve added two new functions, Serialize.encodeToJson and Serialize.decodeFromJson.

These functions are useful when you want to serialize data as quickly as possible. Browsers have heavily optimized json functions so it’s actually faster than using Serialize.encodeToBytes. This is especially true if you need to send the data through ports since Bytes would have to be converted to List Int or String (this is what Serialize.encodeToString does already).

Additionally, thanks to elm-serialize not needing to generate human readable data, it’s pretty compact json as well. As an example, I took the following line of code module Serialize exposing(..), parsed it with elm-syntax and then encoded it both with a codec I had written and also with elm-syntax's built in json encoder.

The results look like this:

  • elm-syntax output:
    {"moduleDefinition":{"range":[1,1,1,30],"value":{"type":"normal","normal":{"moduleName":{"range":[1,8,1,17],"value":["Serialize"]},"exposingList":{"range":[1,18,1,30],"value":{"type":"all","all":[1,27,1,29]}}}}},"imports":[],"declarations":[],"comments":[]}

  • Serialize.encodeToJson output:
    [1,[[0,[[1,1],[1,31]],[0,[[0,[[1,8],[1,17]],["Serialize"]],[0,[[1,18],[1,31]],[0,[[1,28],[1,30]]]]]]],[],[],[]]]

Serialize.encodeToJson sticks to using json lists so it doesn’t need to waste characters on field names. In this case the output is about half as large as a result!

4 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.