Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Jackson and how to deserialize JSON objects using a constructor

1. Introduction

This is a tutorial where we will see how to deserialize a JSON Object with the Jackson library and using a constructor of our Java class.

2. Environment

The tutorial is written using the following environment:

  • Hardware: Portable MacBook Pro 17 ‘(2.8 GHz Intel i7, 8GB DDR3 SDRAM, 256GB Solid State Drive).
  • NVIDIA GeForce GT 330M with 512MB
  • Operating System: Mac OS X Snow Leopard 10.6.6
  • JDK 1.6.0_22
  • Jackson 1.6.4

3. Jackson and how to deserialize JSON objects using a constructor of our Java class

First of all let’s go a little bit into context and we’ll tell you what those title words are:

  • JSON (http://www.json.org/js.html) is a representation of objects in the form of a string. It is widely used in JavaScript for its convenience, but it can also be used outside of this scope. An example of a Person representation:

{ “name” : “Alejandro”, “lastName” : “Pérez García”, “company” : “Autentia
Real Business Solutions” }

You can see that it is a very simple format where the objects go in braces {}, and the attributes are represented by key / value pairs.

  • Deserialize is the process by which we are going to convert this text representation into a real object. In this case in a Java object.
  • Jackson (http://jackson.codehaus.org/) is a Java utility library that simplifies the work of serializing (converting a Java object into a text string with
    its JSON representation), and deserializing (converting a string of text with a JSON representation of an object in a real Java object) JSON objects.

Jackson is quite “intelligent” and without saying anything he is able to serialize and deserialize the objects quite well. To do this, it basically uses introspection so that if we have a “name” attribute in the JSON object, it will look for a “getName ()” method for serialization and search for a “setName (String s)” method for deserialization. So the code to unselect a JSON object would be as simple as:

Person person = new ObjectMapper().readValue(jsonText, Person.class);

But what if I do not want to have setters in my implementation. That is, if I want my Person objects to be immutable and can not be changed, I should not put in a setter because Jackson needs it! Always remember that the model is really important, we should not let a useful library influence our design, and less on something as important as turning an immutable object into mutable.

So what options do we have? Well, there’s one that I like a lot and that’s what we’re going to see here, it’s simply having a constructor to correctly build our objects. I like this option more than, for example, injecting the values
directly into the attributes, because having a constructor allows us to do tests in a simpler way and with fewer “artifices”. This system also I like for the injection of dependencies with Spring, make the injection through the contructor (not with the setters or with the attributes directly), and keep the collaborators that you inject in attributesfinal, to make clear that they are constant and that they will not be modified during the life of the object.

We are fortunate that Jackson allows us to use annotations to indicate which method he has to use to do the deserialization. It would be something as simple as putting inside the class Person, a constructor of the style:

private final String name;
private final String lastName;
private final String company;
b>@JsonCreator/b>
public Person(b>@JsonProperty/b>(“name”) String name, b>@JsonProperty/b>(“lastName”) String lastName,
        b>@JsonProperty/b>(“company”) String company) {
    this.name = name;
    this.lastName = lastName;
    this.company = company;
}

Another option that Jackson also allows us is to use a Factory Method, that is, to use static method in the Person class that is responsible for building the object. This option is also very attractive, and it would simply be a style method:

This solution requires a bit more code because we use the ObjectMapper directly to do the deserialization, but sometimes it can be very interesting because it gives us more flexibility when building the object. I leave you as duties to see how we configure Jackson to use this static method

4. Conclusions

Read the documentation and learn to use the libraries that you have at your disposal. Sometimes the documentation is not clear at all and it is convenient to search online and above all to do a few tests to see how the library behaves. As the book “The Pragmatic Programmer” says, do not program by coincidence, you have to work really knowing the behavior of the things you use, and for that many times it is best to try it to be certain.

The post Jackson and how to Deserialize Json Objects using a constructor appeared first on Target Veb.



This post first appeared on Targetveb, please read the originial post: here

Share the post

Jackson and how to deserialize JSON objects using a constructor

×

Subscribe to Targetveb

Get updates delivered right to your inbox!

Thank you for your subscription

×