Skip to content

Latest commit

 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON encoder/decoder

Erlangsters Repository Supported Erlang/OTP Versions Current Version License Build Status Documentation Link

A clean JSON encoder/decoder for the BEAM, meant to be unambiguous, not fast.

{ok, Data} = file:read_file("data.json").

Json = codec_json:decode(Data).
Encoded = codec_json:encode(Json).

For callers that treat malformed input as expected data, use the safe variants.

{ok, Json} = codec_json:try_decode(Data).
{ok, Encoded} = codec_json:try_encode(Json).

Written by the Erlangsters community and released under the MIT license.

Getting started

Include the public records when constructing or matching JSON values.

-include_lib("codec_json/include/codec_json.hrl").
Json = #json_object{
    members = [
        {<<"name">>, #json_string{value = <<"Ada">>}},
        {<<"active">>, #json_bool{value = true}},
        {<<"score">>, #json_number{literal = <<"1e400">>}},
        {<<"tags">>, #json_array{items = [#json_string{value = <<"math">>}]}},
        {<<"notes">>, #json_null{}}
    ]
},
Data = codec_json:encode(Json),
Json = codec_json:decode(Data).

The library targets RFC 8259. Numbers are stored as their exact JSON literals. Object members are stored as an ordered list, so duplicate names and source order are preserved.

A leading UTF-8 BOM is rejected. Nesting deeper than 512 is rejected. Encoded strings use a defined escape set, so decode-then-encode is not always byte-identical to the input.

Installing the library

To use codec-json in a rebar3 project, add it to your rebar.config.

{deps, [
  {codec_json, {git, "https://github.com/erlangsters/codec-json.git", {tag, "0.0.1"}}}
]}.

About

JSON encoder/decoder for the BEAM.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages