Showing posts with label jackson. Show all posts
Showing posts with label jackson. Show all posts

Monday, July 14, 2014

Low-Level JSON Parsing With Jackson

Jackson is primary used for mapping Java classes to JSON objects. However, there are times where you need to retrieve the raw text content of a specific JSON node rather than to map the entire document to a class.

This can be done with the following code:

ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory();
JsonParser jp = factory.createJsonParser(jsonString);
JsonNode node = mapper.readTree(jp);
JsonNode node1 = node.get(“key1”);
JsonNode node2 = node2.get("key2");
String node2Content = node2.asText();

References:

Saturday, July 12, 2014

Jackson Uses Getter Instead of Variable

Here's a tricky gotcha in Jackson. You have defined an object with private variables and created their respective getters and setters, and that all works well, then you add a convenience method named something like "getXXXX()" and then suddenly there is an extra property "XXXX" in your JSON document. This is because Jackson uses getters rather than variable to decide what properties to write to JSON.

This is demonstrated by the output of this class:
public class JacsksonTest {
private String str1 = "str1";
private String str2 = "str2";
private String str3 = "str3";
public String getStr1(){return str1;}
public String getStr2(){return str2;}
public String getStr4(){return "str4";}
public static void main(String[] args) throws Exception {
JacsksonTest testObj = new JacsksonTest();
ObjectWriter jsonWriter = (new ObjectMapper()).writerWithDefaultPrettyPrinter();
System.out.println(jsonWriter.writeValueAsString(testObj));
}
}

Output:
{
 "str1" : "str1",
 "str2" : "str2",
 "str4" : "str4"
}

Notice that "str3", which is defined as a member variable but doesn't have a getter, is missing from the document. Instead, "str4", which has a getter but not a member variable, that is added to the JSON output.

When adding convenience methods to these objects, it is therefore important to not start them with "get".

Wednesday, November 13, 2013

Jackson Date Handling

The default is behavior is epoch, but lots of way to customize, as explained here: http://wiki.fasterxml.com/JacksonFAQDateHandling

Tuesday, December 4, 2012

Jackson Pretty Print

Some of the solutions posted online is deprecated.

Here's the way to do it with a recent version of Jackson.


ObjectWriter jsonWriter = (new ObjectMapper()).writerWithDefaultPrettyPrinter();
String result = jsonWriter.writeValueAsString(obj);

Thursday, July 19, 2012

Jackson Polymorphism

You can handle polymorphism with Jackson using the following annotations at the class level:
j@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "accountType")
@JsonSubTypes({
   @Type(value = AdminAccount.class, name =”ADM”),
   @Type(value = NormalAccount.class, name =”NML”)
})

This will tell Jackson to map the object to the "AdminAccount" class when the "accountType" property is "ADM", and to the "NormalAccount" class when the "accountType" property is "NML".

Other options are available, such as directly using the class name.

One "gotcha" I discovered is that the "accountType" property must not be implemented in the POJO class. If there is an "accountType" property in the class, it will be null when it's time to work with it in Java. In Java-land, you'll have to use "instanceof" to know what object it is.

A article with more detailed examples: http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-into.html

Sunday, July 15, 2012

Jackson: Ignore Unrecognized Fields

By default, Jackson will throw an exception when a field in the JSON is not declared as a member variable in the class it is mapped to. To ignore these fields, add the following annotation to the POJO class:
@JsonIgnoreProperties(ignoreUnknown=true)

Monday, July 9, 2012

Jackson: From Object to JSON String and Back

This is how it's done:
ObjectMapper jsonMapper = new ObjectMapper();
String jsonStr = jsonMapper.writeValueAsString(jsonObj);

And the other way round:
ObjectMapper jsonMapper = new ObjectMapper();
UserAccount result = jsonMapper.readValue(userAccountJson,  UserAccount.class);