Friday, April 20th, 2007
Flexjson: A DSL approach to JSON for Java
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:
- return new JSONSerializer().include("hobbies", "favoriteMovies").serialize(person);
- return new JSONSerializer().exclude("password").include("hobbies").serialize(person);





good job
Very nice. This addresses many problems that I have experienced when attempting to use JSON for large object trees.
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.
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.
Very nice Charlie! Json-lib also has filtering capabilities, but your DSL-like one-liner looks very appealing ;-)
Nice. But why is this a DSL? Those letters get thrown around a lot and I’m not sure everything qualifies ..
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.
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 ;-)
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?
Try out ‘JSONObject’
-JSONStringer
-JSONTokener
-JSONArray
you can get it from here
http://json.org/
sudhir
http://www.jyog.com
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. :*)
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.