Uncategorized

Serializing JSON with Jackson ObjectMapper and Groovy

Hello

I will show you how do you serialize JSON Objects with Jackson Serializer.

Problem 1: The json object starts with a []. In other words a Collection.

Problem 2: All JSON values (except String ones) are without “”, that means they are not following the default for JSON objects (have “” for every json object, example {“number”:”0″}, in our case is {“number”:0})

Problem 3: We also have normal json (not a Collection) and we want to serialize it to object

To solve this 3 problems I’ve created a simple Groovy script to parse the json object correctly, see the code bellow:

/**
 *
 * @author thomasm
 *
 */
 class JSonParser {

@Test
 public void testProblem1() throws JsonParseException, JsonMappingException, IOException{

def json = "[{ \"string\":\"soujava\", \"enabled\":\"false\", \"nullValue\":\"null\", \"zeroNumber\":\"0\" }]";

def mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 def obj = mapper.readValue(json, new TypeReference<Collection>(){
 });
 println obj

assert obj.string[0] != null
 assert obj.enabled[0] != null
 assert obj.nullValue[0] == "null"
 assert obj.zeroNumber[0] != null
 }

@Test
 public void testProblem2(){

def json = "[{ \"string\":\"soujava\", \"enabled\":false, \"nullValue\":null, \"zeroNumber\":0 }]";

def mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 def obj = mapper.readValue(json, new TypeReference<Collection>(){
 });
 println obj

assert obj.string[0] !=} null
 assert obj.enabled[0] != null
 assert obj.nullValue[0] == null
 assert obj.zeroNumber[0] != null
 }

@Test
 public void testProblem3(){
 def json = "{ \"string\":\"soujava\", \"enabled\":false, \"nullValue\":null, \"zeroNumber\":0 }";

def mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 def obj = mapper.readValue(json, new TypeReference(){
 });
 println obj

assert obj.string != null
 assert obj.enabled != null
 assert obj.nullValue == null
 assert obj.zeroNumber != null
 }
 }

/**
 *
 * @author thomasm
 *
 */
 def class ObjectSerialized {

def string
 def enabled
 def nullValue
 def zeroNumber

def String toString(){
 String result = "$string $enabled $nullValue $zeroNumber"
}
}

 

Advertisements

2 thoughts on “Serializing JSON with Jackson ObjectMapper and Groovy”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s