[erlang-questions] New project: ZJ - A tiny JSON encoder/decoder

zxq9@REDACTED zxq9@REDACTED
Tue Jun 26 11:58:55 CEST 2018


With tuple calls gone and mochijson2 finally going extinct I found myself in need of a strings-as-strings JSON encoder/decoder in pure Erlang (not NIF-based). I wrote one yesterday. It is a single module that exports two functions: encode/1 and decode/1. Erlang didn't really *need* another JSON encoder/decoder, of course, but this one works exactly the way I need -- if anyone else finds it useful you're welcome to it:

https://gitlab.com/zxq9/zj

A few tradeoffs have to be made when writing a JSON encoder/decoder because JSON types don't map very well with Erlang, and everyone has different optimal performance profiles and data idioms within their programs. The mappings ZJ follows are below. Note that tuples map to JSON arrays, which means proplists do NOT map to JSON objects, Erlang maps do.

JSON -> Erlang:
 * Integer -> Integer
 * Float   -> Float
 * String  -> String
 * Object  -> Map
 * Array   -> List
 * true    -> true
 * false   -> false
 * null    -> null

Erlang -> JSON:
 * Integer -> Integer
 * Float   -> Float
 * Map     -> Object
 * List    -> Array
 * Tuple   -> Array
 * Binary  -> String
 * UTF-8   -> String
 * true    -> true
 * false   -> false
 * null    -> null





More information about the erlang-questions mailing list