Using JSON bindings with iBeans

December 15 2009


JSON, short for JavaScript Object Notation, is a lightweight data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects).

Many REST services use JSON as the preferred data format (including Tcat Server’s REST API). JSON provides a much simpler model for data representation and is widely used in AJAX programing.

iBeans support binding JSON data to objects and marshaling Java objects to JSON using the excellent Jackson Framework.

Jackson uses annotations to describe how data is mapped to a Java object model. For example, lets say we have a JSON file that describes a person. When we receive that JSON data, we want to convert it into a Person object. The JSON looks like this:

We use annotations to describe how to perform the mapping between the JSON data and the Person object. We use @JsonAutoDetect to say that field member names map directly to JSON field names:

The EmailAddress object that is used in the emailAddresses property is just another JavaBean with the @JsonAutoDetect annotation.

At this point, can figure out whether to perform a JSON transform based on the parameters of the method being called. For example:

Here we would receive the contents of the people. file above on a JMS queue, and iBeans would see that Person.class is an annotated JSON object and that we had received JSON data from the JMS queue and perform the conversion.

Binding transformers can also be used with iBean interfaces. For example, we could parse the response from Twitter into a status object:

Global JSON Mapper

Jackson performs mappings through the ObjectMapper. You can configure this object to control how data is serialized and define mixins that add annotations to objects that you cannot change directly. It is possible to define a global ObjectMapper, a single mapper that will be used for all JSON transforms in your application. This is not required, since iBeans will automatically create a mapper for a transformer, but using a global mapper can be useful if you need to configure specific properties on the mapper or use mixins. To create a shared ObjectMapper, create a module class that creates the ObjectMapper instance. This example uses Guice:

See Using Guice for more information about modules and dependency injection in iBeans.

Intercepting JSON Transforms

So far we have discussed how iBeans will perform automatic JSON transforms. If you want to intercept the transform, just create a transformer with a method return or parameter type of your JSON class:

The ObjectMapper instance will either be created for you, or the global context for your application will be used. One reason for doing this would be to strip out some JSON elements and create objects from a subset of the JSON received. For more information about transforms see the Working with Transformers page.

iBeans provides a way to work with JSON data with automatic Java object binding thanks to Jackson. For most scenarios the developers needs to do nothing more than define their binding objects, and iBeans will figure out the rest. However, it is very easy to customize and control how the JSON data is processed using a custom transformer.

We'd love to hear your opinion on this post

2 Responses to “Using JSON bindings with iBeans”

  1. I should have mentioned in this post that, Mule 3.0 will have exactly the same Json capabilities

  2. Nice blog ..use ful