spacer.png, 0 kB
Erlang JSON encoding & decoding

JSON has become one of the most used standard notations for data transport. How to use JSON in Erlang though? There is no standard library that does this. At work, we use Mochiweb (Erlang HTTP toolkit) and then specifically the module mochijson2.erl This module works great, but we made a few small patches to it (improved UTF8 handling and better tuple support). I'll try to post those patches at a later time.

  • %% @author Bob Ippolito
  • %% @copyright 2007 Mochi Media, Inc.
  • %% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works
  • %% with binaries as strings, arrays as lists (without an {array, _})
  • %% wrapper and it only knows how to decode UTF-8 (and ASCII).


Just grab the Mochiweb source tree, add it to your project and include the mochijson2.erl file. You can then simply encode and decode like this:

 


JsonEncoded = encode(E).
Decoded = encode(JsonEncoded).

 

Mochiweb is very useful, be sure to check it out!

 

 
Next >
spacer.png, 0 kB
rightborder
© 2006-2008 StartInChina.com - News and tips from Shenzhen, China