mapping - Map a JSON field that can have different types with Jackson? -



mapping - Map a JSON field that can have different types with Jackson? -

i json web service , can not influence json format. json code below illustration illustrate problem. field cars can either object containing car objects or can empty string. if alter web service, i'd alter empty string empty object "cars" : {} instead of "cars" : "".

when trying map json java object:

public class person { public int id; public string name; public map<string, car> cars; }

this works:

{ "id" : "1234", "name" : "john doe", "cars" : { "tesla model s" : { "color" : "silver", "buying_date" : "2012-06-01" }, "toyota yaris" : { "color" : "blue", "buying_date" : "2005-01-01" } } }

and fails:

{ "id" : "1", "name" : "the dude", "cars" : "" }

what best way handle case in jackson? if there's empty string, i'd null field cars. tried using accept_empty_string_as_null_object, didn't help.

the field cars can either contain list of auto objects ... works:

{ "id" : "1234", "name" : "john doe", "cars" : { "tesla model s" : { "color" : "silver", "buying_date" : "2012-06-01" }, "toyota yaris" : { "color" : "blue", "buying_date" : "2005-01-01" } } }

the "cars" element value not list (aka array). it's json object, can considered map-type collection, not list.

so, rephrase issue, goal deserialize json object , empty string java map.

to solve this, i'm surprised accept_empty_string_as_null_object didn't work. recommend logging issue @ http://jira.codehaus.org/browse/jackson.

you implement custom deserialization. next illustration solution. if target info construction has other map references, solution need accordingly changed.

import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; import org.codehaus.jackson.jsonnode; import org.codehaus.jackson.jsonparser; import org.codehaus.jackson.jsonprocessingexception; import org.codehaus.jackson.objectcodec; import org.codehaus.jackson.version; import org.codehaus.jackson.map.deserializationcontext; import org.codehaus.jackson.map.jsondeserializer; import org.codehaus.jackson.map.objectmapper; import org.codehaus.jackson.map.module.simplemodule; import org.codehaus.jackson.type.typereference; public class foo { public static void main(string[] args) throws exception { simplemodule module = new simplemodule("carsdeserializer", version.unknownversion()); module.adddeserializer(map.class, new carsdeserializer()); objectmapper mapper = new objectmapper().withmodule(module); person person1 = mapper.readvalue(new file("input1.json"), person.class); system.out.println(mapper.writevalueasstring(person1)); // {"id":1234,"name":"john doe","cars":{"tesla model s":{"color":"silver","buying_date":"2012-06-01"},"toyota yaris":{"color":"blue","buying_date":"2005-01-01"}}} person person2 = mapper.readvalue(new file("input2.json"), person.class); system.out.println(mapper.writevalueasstring(person2)); // {"id":1,"name":"the dude","cars":{}} } } class person { public int id; public string name; public map<string, car> cars; } class auto { public string color; public string buying_date; } class carsdeserializer extends jsondeserializer<map<string, car>> { @override public map<string, car> deserialize(jsonparser jp, deserializationcontext ctxt) throws ioexception, jsonprocessingexception { objectcodec codec = jp.getcodec(); jsonnode node = codec.readtree(jp); if (!"".equals(node.gettextvalue())) { objectmapper mapper = new objectmapper(); homecoming mapper.readvalue(node, new typereference<map<string, car>>() {}); } homecoming new hashmap<string, car>(); // or homecoming null, if preferred } }

json mapping jackson

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -