Rich_Morin

Rich_Morin

Data serialization formats

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 (extensible data notation) is a subset of Clojure, extracted by 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 and eden’s Hex page.

JSON

JSON is a subset of JavaScript, extracted by 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 (JavaScript Object Notation for Linked Data) is a JSON-based method of encoding linked data. Thus, it can take the place of RDF-encoding formats such as N-Triples, RDF/XML, and Turtle.

TOML

TOML is an acronym for “Tom’s Obvious, Minimal Language”, referring to its creator, 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 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. For details, see transit_elixir’s Hex page.

YAML

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.

Most Liked

anuaralfetahe

anuaralfetahe

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.

D4no0

D4no0

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.

cmo

cmo

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

Rich_Morin

Rich_Morin

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, 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, 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:

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

Where Next?

Popular in Wikis Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
nicbet
Introduction Now that the language is picking up support and maturing nicely, I’d like to start a collection of common and recurring Elix...
New
axelson
With the new year I am looking at travel for this year and I’d love to base some travel around Elixir. So in the spirit of the 2017 threa...
New
Rich_Morin
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 persona...
New
swelham
Introduction After some discussion with a few other members we thought it might be an idea to start a thread where we can post about libr...
New
blackode
This is a wiki - anyone at Trust Level 1 or higher can help keep it updated. Elixir Pocket Syntax Uncommon Logical stuff of Elixir modul...
New
gmile
:httpc HTTP client is part of Erlang standard library, and as such can be easily used in Elixir code too. One particular advantage of usi...
New
BartOtten
A wiki for Doom Emacs Doom is a configuration framework for GNU Emacs. It focuses on performance and customizability. It can be a founda...
New
jdumont
Guide Using an iPad for web development can be easily split into two main parts: Setting up the iPad as a thin client Working in a remo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement