Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 9720df6605
Fetching contributors…

Cannot retrieve contributors at this time

98 lines (64 sloc) 3.048 kB
It is modified version on Bob Ippolito's <bob@mochimedia.com> mochijson2 module, one can find in excellent mochiweb, erlang webserver.
Yet another JSON (RFC 4627) library for Erlang. jsonerl works with binaries as strings, arrays as lists (without an {array, _}) wrapper and it only knows how to decode UTF-8 (and ASCII).
It has the following mapping:
Erlang data type: => JavaScript data type: => Erlang data type:
-----------------------------------------------------------------------------------------------
1. atom true boolean true atom true
2. atom false boolean false atom false
3. atom undefined null atom undefined
4. any other atom string binary holding string
5. list array list
6. number number number
7. binary string binary
8. proptuple object proptuple
-----------------------------------------------------------------------------------------------
Note: proptuple is result of passing proplist to list_to_atom bif ;)
This is concept of mine to nicely model javascript object without a need of tagging structures on erlang side.
Eg. JavaScript object
{
name: "Luc Besson",
year_of_birth: 1959,
city: "Paris",
photo: "http://is.gd/3pYJF",
movies: ["Taken","Bandidas","Taxi"]
}
results in
{
{<<"name">>, <<"Luc Besson">>},
{<<"year_of_birth">>, 1959},
{<<"city">>, <<"Paris">>},
{<<"photo">>, <<"http://is.gd/3pYJF">>},
{<<"movies">>, [<<"Taken">>,<<"Bandidas">>,<<"Taxi">>]}
}
-----------------------------------------------------------------------------------------------
Additionally there is utility macros delivered with jsonerl that helps turning erlang records to json and back.
For example:
-include("jsonerl.hrl").
-record(artist, {name, year_of_birth, city, photo, movies}).
Artist = #artist{name = <<"Luc Besson">>, year_of_birth = 1959, city = <<"Paris">>, photo = <<"http://is.gd/3pYJF">>, movies = [<<"Taken">>,<<"Bandidas">>,<<"Taxi">>]},
Json = ?record_to_json(artist, Artist),
Artist = ?json_to_record(artist, Json).
the resulting json will be:
{
name: "Luc Besson",
year_of_birth: 1959,
city: "Paris",
photo: "http://is.gd/3pYJF",
movies: ["Taken","Bandidas","Taxi"]
}
-record(man,{name,age}).
Man1 = #man{name = "Petr", age = 45},
Man2 = #man{name = "Elton", age = 27 },
ListOfMans = [Man1,Man2],
ListJson = ?list_record_to_json(man,ListOfMans)
the resulting json will be:
[
{
name:"Petr",
age:45
},
{
name:"Elton",
age:27
}
]
Jump to Line
Something went wrong with that request. Please try again.