Monday, September 17, 2007

Jackson JSON-processor, v0.7

After some bugfixes, added unit testing, and one significant new feature, it is good time to release the next pre-1.0 version of Jackson JSON-processor.

So what's new? In addition to the basic stream parser (reader) and generator (writer) implementations, there is now support for simple data binding, implemented by mapper class This mapper allows for mapping from JSON content into corresponding basic JDK data types (Lists, Maps, Strings, Numbers, Booleans and null) and back. When mapping from Java objects to JSON, a few more types are recognized (like primitive arrays, various basic Collections and so on), but no attempt is done to handle Java beans. Such support may be added via other mappers, but for now it is more important to cover the simplest cases.

Simple examples should show-case how easy it is to use this mapper. Let's start by mapping JSON to Java objects:

  JsonFactory jf = new JsonFactory();
  Object result = new JavaTypeMapper().read(jf.createJsonParser(new StringReader("[ 1, 15, true ]")));

So what would 'result' look like? It would be equivalent to:

  List result = new ArrayList();

And the other direction (outputting JSON given basic Java wrapper or collection instances is about as simple:

  JsonFactory jf = new JsonFactory();
  StringWriter sw = new StringWriter();
  JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
  Map m = new LinkedHashMap();
  m.put("key", Integer.valueOf(29));
  m.put("value", "something");
  m.put("enabled", Boolean.TRUE);
  new JavaTypeMapper().writeAny(gen, m);

And the output would look something like:

{"key":29, "value":"something", "enabled":true}

Rather simple? I think so. No extra metadata needed, no messing with annotations or (heaven forbid!) xml configuration files. Nor new java classes, only purpose of which is to act as C struct equivalents. Simple tool for simple (but common!) use cases.

So what's next? Jackson 0.8, obviously, but what will that contain? There is another kind of straightforward mapper that will probably added: one that uses basic node structure, somewhat similar to XML tree model (XOM, JDOM, DOM, DOM4J) nodes: ones that can be both conveniently traveled, and accessed in dynamic type-safe manner (a la "duck typing"). Stay tuned!

blog comments powered by Disqus

Sponsored By

Related Blogs

(by Author (topics))

Powered By

About me

  • I am known as Cowtowncoder
  • Contact me
Check my profile to learn more.