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

**URL:** <https://discourse.elm-lang.org/t/elm-serialize-quickly-and-reliably-encode-and-decode-elm-values/6112>\
**Category:** Show and Tell\
**Created:** [July 31, 2020, 10:24am UTC](https://discourse.elm-lang.org/t/elm-serialize-quickly-and-reliably-encode-and-decode-elm-values/6112 "2020-07-31T10:24:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [July 31, 2020, 10:24am UTC](https://discourse.elm-lang.org/t/elm-serialize-quickly-and-reliably-encode-and-decode-elm-values/6112/1 "2020-07-31T10:24:16Z")

</div>

I’ve released [`elm-serialize`](https://package.elm-lang.org/packages/MartinSStewart/elm-serialize/latest/), a package that makes it quick and reliable to encode and decode Elm values.

This is an iteration on [`elm-codec-bytes`](https://package.elm-lang.org/packages/MartinSStewart/elm-codec-bytes/latest/) which in turn is inspired by [`miniBill/elm-codec`](https://package.elm-lang.org/packages/miniBill/elm-codec/latest/). 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 `Byte`s 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`](https://package.elm-lang.org/packages/MartinSStewart/elm-geometry-serialize/latest/). It’s a collection of codecs for [`ianmackenzie/elm-geometry`](https://package.elm-lang.org/packages/ianmackenzie/elm-geometry/latest/) 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.

---

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [August 5, 2020, 8:56pm UTC](https://discourse.elm-lang.org/t/elm-serialize-quickly-and-reliably-encode-and-decode-elm-values/6112/2 "2020-08-05T20:56:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [August 15, 2020, 8:56pm UTC](https://discourse.elm-lang.org/t/elm-serialize-quickly-and-reliably-encode-and-decode-elm-values/6112/3 "2020-08-15T20:56:11Z")

</div>

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