Git Product home page Git Product logo

jsonerl's Introduction

It is modified version on Bob Ippolito's <[email protected]> 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
  }
]

  

jsonerl's People

Contributors

lambder avatar chemist avatar nox avatar

Watchers

edwardt avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.