Friday, April 20th, 2007

Flexjson: A DSL approach to JSON for Java

Category: Java, JavaScript, JSON

Charlie Hubbard has released Flexjson, a lightweight JSON library for Java. Most JSON serializers mimic object serialization libraries and try to serialize the entire object graph from the object being turned into JSON. This causes problems when you want a connected object model on your server, but you can’t send that object model to the client because the serialization library will try to send the entire object graph. This is terrible for performance, causes most JSON libraries to blow up if they encounter circular references, makes it difficult to follow good OO practices, and makes it nearly impossible to use libraries such as Hibernate. Other JSON libraries require you to create a lot of boiler plate code to translate your object representation into a JSON object.

Flexjson tries to solve both of these problems by using a DSL approach to sending JSON. You don’t have to create lots of boiler plate code or modify your object model to make it work.

Examples:

javascript

  1. return new JSONSerializer().include("hobbies", "favoriteMovies").serialize(person);
  2.  
  3. return new JSONSerializer().exclude("password").include("hobbies").serialize(person);

Posted by Dion Almaer at 7:34 am
12 Comments

+++--
3.9 rating from 27 votes

12 Comments »

Comments feed TrackBack URI

good job

Comment by kenji — April 20, 2007

Very nice. This addresses many problems that I have experienced when attempting to use JSON for large object trees.

Comment by mrcritcal — April 20, 2007

You should take a look at the JSPON spec. It is open extension to JSON to deal with difficulties of circular references, interactive transfer or large object graphs and more. I think this would be the most appropriate format for the serialization of complex object graphs.

Comment by Kris Zyp — April 20, 2007

I’ve been very busy today with lots of people giving me feedback (including some bug reports ;-). I just wanted to say thanks to everyone, and keep an eye out. I’ll be working on Flexjson some more very soon.

Comment by Charlie Hubbard — April 20, 2007

Very nice Charlie! Json-lib also has filtering capabilities, but your DSL-like one-liner looks very appealing ;-)

Comment by Andres Almiray — April 21, 2007

Nice. But why is this a DSL? Those letters get thrown around a lot and I’m not sure everything qualifies ..

Comment by ars — April 22, 2007

Andres,

It’s a fuzzy decision when something is a DSL or not. I started to respond to your question here, but it got kind of long so I posted it on my blog:

http://wrongnotes.blogspot.com/

If you’re interested on my thoughts about DSLs vs. API check it out. To sum it up. Flexjson is a really simple DSL. But it has a mini language in it for specifying the fields you want serialized.

Comment by Charlie Hubbard — April 22, 2007

You’re right Charlie, sometimes a not-yet mature DSL can be viewed as an API, but you got me confused, I never questioned flexjson’s DSL status ;-)

Comment by Andres Almiray — April 23, 2007

I love the idea of FlexJSON, and have been struggling just today with the issue of prototype’s toJSON() serializing entire objects, when all I really want are the data values, no Object methods. Is there a way in Javascript or prototype to limit what gets serialized?

Comment by Pete Allen — April 23, 2007

Try out ‘JSONObject’
-JSONStringer
-JSONTokener
-JSONArray

you can get it from here
http://json.org/

sudhir
http://www.jyog.com

Comment by sudhir — April 23, 2007

Andres,

Whoops. I guess that was ment for ars. The way the comments were layed out I guess I associated his response with your name. I guess the red link on your name just jumped out at me where ars is black, and I never noticed until your posted. Sorry. :*)

Comment by Charlie Hubbard — April 23, 2007

Hi,

I’m trying to find a JSON library for JDK 1.4. We’re migrating over to 1.5 slowly but are locked in due to our Oracle application server. Therefore I can’t use JDK 1.5 at the moment. Does anyone know of a solution that works with JDK 1.4?

Thanks.

Comment by Matt Newboult — July 16, 2007

Leave a comment

You must be logged in to post a comment.