# Data serialization formats

**URL:** <https://forum.elixirforum.com/t/data-serialization-formats/53818>\
**Category:** Wikis\
**Tags:** json, toml, yaml, serialization, edn, transit, ubf\
**Created:** [February 11, 2023, 2:44am UTC](https://forum.elixirforum.com/t/data-serialization-formats/53818 "2023-02-11T02:44:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rich\_Morin](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rich_morin/32/2020_2.png) [@Rich\_Morin](https://forum.elixirforum.com/u/Rich_Morin)\
**Post date:** [February 11, 2023, 2:44am UTC](https://forum.elixirforum.com/t/data-serialization-formats/53818/1 "2023-02-11T02:44:06Z")

</div>

I’d like to start a discussion of data serialization formats, in the context of Elixir. The rest of this note is a combination of personal opinions and links to useful resources; feel free to jump in with your own clues, pointers, reactions, war stories, etc. (ducking…)

# edn

[edn](https://en.wikipedia.org/wiki/Clojure#Extensible_Data_Notation) (extensible data notation) is a subset of [Clojure](https://en.wikipedia.org/wiki/Clojure), extracted by [Rich Hickey](https://en.wikipedia.org/wiki/Rich_Hickey). edn has a rich set of built-in data types, most of which are a good match for Elixir. In addition, it has a mechanism for extending this set with custom data types.

However, because edn is closely tied to Clojure, Transit (see below) may be a better choice for interoperability. For details, see edn’s [GitHub page](https://github.com/edn-format/edn) and eden’s [Hex page](https://hex.pm/packages/eden).

# JSON

[JSON](https://en.wikipedia.org/wiki/JSON) is a subset of [JavaScript](https://en.wikipedia.org/wiki/JavaScript), extracted by [Douglas Crockford](https://en.wikipedia.org/wiki/Douglas_Crockford). Although JSON shines at interoperability and standardization, it has very limited (and JavaScript-specific) data types. So, for example, I wouldn’t recommend it for cases where one needs to retain and/or transmit specific data types.

JSON is also poorly suited for generating human-readable documents. It’s possible to include comments by using data elements, but this is a hack. And, although it’s quite possible to format JSON nicely, many programmers don’t make the effort. So, a lot of JSON “in the wild” is difficult for humans to read.

[JSON-LD](https://en.wikipedia.org/wiki/JSON-LD) (JavaScript Object Notation for Linked Data) is a JSON-based method of encoding [linked data](https://en.wikipedia.org/wiki/Linked_data). Thus, it can take the place of [RDF](https://en.wikipedia.org/wiki/RDF)-encoding formats such as [N-Triples](https://en.wikipedia.org/wiki/N-Triples), [RDF/XML](https://en.wikipedia.org/wiki/RDF/XML), and [Turtle](https://en.wikipedia.org/wiki/Turtle_(syntax)).

# TOML

[TOML](https://en.wikipedia.org/wiki/TOML) is an acronym for “Tom’s Obvious, Minimal Language”, referring to its creator, [Tom Preston-Werner](https://en.wikipedia.org/wiki/Tom_Preston-Werner). Although TOML has very limited data types, it excels at generating human-readable documents.

Because the top of each “section” (i.e., sub-tree) can be encoded as a path, TOML works well for encoding deeply-nested hierarchical structures:

```
[a.b.c.d]
  e = 42

```

# Transit

[Transit](https://github.com/cognitect/transit-format) is conceptually similar to edn, in that it is an extensible format with strong data type capabilities. However, it is considerably less tied to the Clojure language. Also, its “wire format” uses JSON or [MessagePack](https://en.wikipedia.org/wiki/MessagePack). For details, see [transit\_elixir’s Hex page](https://hex.pm/packages/transit_elixir).

# YAML

[YAML](https://en.wikipedia.org/wiki/YAML) (“YAML Ain’t Markup Language”) is generally well suited to writing by humans, although the need for multiple levels of indentation can become an issue for deeply nested trees. Also, the syntax definition is rather large, so reading some YAML documents can be difficult. Finally, because YAML “in the wild” isn’t well standardized, interoperability can be an issue.

---

<div class="post-metadata">

**Author:** ![anuaralfetahe](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anuaralfetahe/32/29144_2.png) [@anuaralfetahe](https://forum.elixirforum.com/u/anuaralfetahe)\
**Post date:** [February 11, 2023, 9:31am UTC](https://forum.elixirforum.com/t/data-serialization-formats/53818/2 "2023-02-11T09:31:18Z")

</div>

I had to serialize Elixir data structures before pushing to kafka topic I used `:erlang.term_to_binary/1`. The performance was good and I was easily able to deserialize the data on the consumer side(also running Elixir). This function is also useful when working with C nifs.

I noticed that I can push raw Elixir data structures to RabbitMQ without any serialization at all. Probably because it is written in Erlang.

When dealing with different technologies I’ve found JSON the easiest to work with because of the wide use and support it has.

---

<div class="post-metadata">

**Author:** ![D4no0](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/32/33624_2.png) [@D4no0](https://forum.elixirforum.com/u/D4no0)\
**Post date:** [February 11, 2023, 2:21pm UTC](https://forum.elixirforum.com/t/data-serialization-formats/53818/3 "2023-02-11T14:21:54Z")

</div>

What about asn.1? It is part of OTP and it is a fully fledged standardised implementation for binary encoding/decoding. If you never used it, protobufs is basically a reimplementation of a small set of features from asn.1 .

While very few people use this protocol, I think it has very big potential in systems where data consistency matters.

The only issue currently is that using it from elixir is very hard, it needs a wrapper with updated documentation.

---

<div class="post-metadata">

**Author:** ![cmo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmo/32/19618_2.png) [@cmo](https://forum.elixirforum.com/u/cmo)\
**Post date:** [February 14, 2023, 12:34pm UTC](https://forum.elixirforum.com/t/data-serialization-formats/53818/4 "2023-02-14T12:34:58Z")

</div>

I use MessagePack for internal communication over HTTP because it is smaller than JSON and that sort of thing floats my boat.

---

<div class="post-metadata">

**Author:** ![Rich\_Morin](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rich_morin/32/2020_2.png) [@Rich\_Morin](https://forum.elixirforum.com/u/Rich_Morin)\
**Post date:** [February 23, 2023, 9:59pm UTC](https://forum.elixirforum.com/t/data-serialization-formats/53818/5 "2023-02-23T21:59:30Z")

</div>

Joe Armstrong had a lot to say about serialization formats, although he mostly couched it in terms of wire protocols, etc. In his talk, [The How and Why of Fitting Things Together](https://youtu.be/ed7A7r6DBsM), he talks about why we need a way to ensure that the bits going down the wire obey a “contract”.

At [about this point in the talk](https://youtu.be/ed7A7r6DBsM?t=1732), he talks about the need for formal protocol definitions and briefly mentions UBF, his (proposed) “Universal Binary Format”. When I went off to look for information on UBF, I found the following URLs:

- [Universal Binary Format: UBF 2.2 Home Page  
 ](https://ubf.github.io/ubf)

- [Universal Binary Format: UBF 2.2 User’s Guide  
 ](https://ubf.github.io/ubf/ubf-user-guide.en.html)

- [ubf/ubf](https://github.com/ubf/ubf) - [UNMAINTAINED] Universal Binary Format 2.2

The “UNMAINTAINED” tag is a bit troubling, but maybe it doesn’t reflect actual adoption. So, is anyone actually using UBF? If so, could you provide some useful feedback on it? Inquiring gnomes need to mine…

-r
