Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Json Type Mapping Inner Class [duplicate]

I am trying to create the inner class type for an object being passed in as JSON but while I have read tons on here as well as Jackson's site I don't seem to be able to get the right combination so if anyone else has any pointers they would be much appreciated. I've posted some snippets below and removed all getters and setters, I didn't figure they needed posting. I'm using Jackson 2.2.

The classes I'm attempting to deserialize:

public class Settings {   private int offset;   private int limit;   private String type;   private Map<String, Criteria> criteria;    public class Criteria {     private String restriction;     private Object value;   } } 

The code I'm using to deserialize:

ObjectMapper om = new ObjectMapper(); TypeFactory tf = om.getTypeFactory(); JavaType map = tf.constructMapLikeType( Map.class, String.class, Criteria.class ); JavaType type = typeFactory.constructType( Settings.class, map ); Settings settings = om.readValue( entity, type ); 

My JSON testing data:

{ "type": "org.json.Car", "criteria": { "restriction": "eq", "value": "bmw" } } 
like image 710
ars265 Avatar asked Jun 25 '13 05:06

ars265


2 Answers

The correct answer is that you are missing the static keyword on the inner class.

Just make sure that the "static" keyword is there.

Read http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html

it takes you 3 minutes but make you happy for the rest of the day.

like image 187
redochka Avatar answered Sep 23 '22 03:09

redochka


If you can, then make your life simple and move the inner class to a normal class with a reference in the Settings class. And then do the marshalling using jackson, here is how you can have your classes:

public class Settings {   private int offset;   private int limit;   private String type;   private Map<String, Criteria> criteria;   private Criteria criteria; }    class Criteria {     private String restriction;     private Object value;   } 
like image 33
Juned Ahsan Avatar answered Sep 21 '22 03:09

Juned Ahsan