JSON and JSON-RPC for Erlang
February 17th, 2007 tonyg
About a month ago, I wrote an implementation of RFC 4627, the JSON RFC, for Erlang. I also implemented JSON-RPC over HTTP, in the form of mod_jsonrpc, a plugin for Erlang’s built-in inets httpd. This makes accessing Erlang services from in-browser Javascript very comfortable and easy indeed.
Downloading the code:
- you can browse the code here
- a tarball is available here
- the mercurial repository holding the code can be retrieved with the command
hg clone http://hg.opensource.lshift.net/erlang-rfc4627/
Documentation is available, including notes on how to write a service and how to access it from javascript, and the curious may wish to see the code for an example Erlang JSON-RPC service and its corresponding javascript client.
The JSON codec uses a data type mapping suggested by Joe Armstrong, where strings map to binaries and arrays map to lists.
Coincidentally, on the very same day I started writing my JSON codec, Eric Merritt released his new JSON codec, Ktuo. If I’d seen that, I probably wouldn’t have started writing my own. At the time, the only other implementation I knew of was the json.erl included with yaws, which uses an awkward (to me) encoding and was, at the time I was using it, a bit buggy (decoding “[]” returned an incorrect value - it seems to have been fixed somewhere between yaws 1.64 and 1.68). To an extent, Eric’s rationale for a new JSON codec applies to mine, too, and my other excuse is that the data type mapping where strings become Erlang binaries is much more useful to my application. Your mileage may vary!
[Update: fixed broken links.]
Entry Filed under: Technology, Programming, Our Software, Erlang
7 Comments Add your own
1. Sam Ruby | September 5th, 2007 at 6:26 pm
I’m interested in proving patches to support utf-8. For more background (and a link to the first patch), see here.
2. tonyg | September 6th, 2007 at 10:40 am
Sam, that’s great stuff! Thank you. I’m very interested in integrating your utf-8 work - I’ll get in touch via email.
(Update: Er, or I would if I could find an email address for you! :-) I’ll leave a comment on your blog, instead.)
3. Sam Ruby | September 14th, 2007 at 2:01 pm
Email is required to post a comment, but even you can’t see it?
In any case, rubys at my weblog’s host.
4. Mikl Kurkov | October 11th, 2007 at 10:50 am
Hi!
Thanks for very good work. I’m interested in your jsonrpc implementation. It seems to work pretty good. But I don’t througly understand is it synchronyse or not. I mean if some rpc request takes long time to proceed will over requests wait for it or they can be proceed in parallel.
As I can see in current implementation you use genserver:call so it is not possible to proceed other request until previous will proceed. Also with function call - all system will wait current call to return.
Do you plan implementing async requests in this system. Or may be I misiing something.
5. rich | October 23rd, 2007 at 6:07 pm
there’s also ejson
6. Daniel Kwiecinski | January 8th, 2008 at 11:53 am
Hi,
How easy would be to convert mod_json into yaws appmod? Could you make any suggestion - clues to make this task easier?
Cheers,
Daniel
7. tonyg | January 14th, 2008 at 12:41 pm
Hi Daniel,
It ought not to be too difficult. It may need a bit of refactoring. The key differences will be separating out the inets-httpd specific way of retrieving the URL, query parameters, and POST body from the HTTP request. The main entry point that inets-httpd uses (after a few trivial calls) is do_rpc/1, and the main workers are parse_jsonrpc/2 and invoke_service_method/6. See also the section “Invoking JSON-RPC procedures from Erlang” in the manual.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed