0

I'm having trouble getting this bit of JSON into a POJO. I'm using Jackson configured like this:

protected ThreadLocal<ObjectMapper> jparser = new ThreadLocal<ObjectMapper>();

    public void receive(Object object) {
    try { 
        if (object instanceof String && ((String)object).length() != 0) {
            ObjectDefinition t = null ; 
            if (parserChoice==0) { 
                if (jparser.get()==null) {
                    jparser.set(new ObjectMapper());
                }
                t = jparser.get().readValue((String)object, ObjectDefinition.class);
            }

            Object key = t.getKey();
            if (key == null)
                return;
            transaction.put(key,t); 
        } 
    } catch (Exception e) { 
        e.printStackTrace();
    }
}

Here's the JSON that needs to be turned into a POJO:

{
    "id":"exampleID1",
    "entities":{
      "tags":[
         {
            "text":"textexample1",
            "indices":[
               2,
               14
            ]
         },
         {
            "text":"textexample2",
            "indices":[
               31,
               36
            ]
         },
         {
            "text":"textexample3",
            "indices":[
               37,
               43
            ]
         }
      ]
}

And lastly, here's what I currently have for the java class:

    protected Entities entities;
@JsonIgnoreProperties(ignoreUnknown = true)
protected class Entities {
    public Entities() {}
    protected Tags tags;
    @JsonIgnoreProperties(ignoreUnknown = true)
    protected class Tags {
        public Tags() {}

        protected String text;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    };

    public Tags getTags() {
        return tags;
    }
    public void setTags(Tags tags) {
        this.tags = tags;
    }
};
//Getters & Setters ...

I've been able to translate the more simple objects into a POJO, but the list has me stumped.

Any help is appreciated. Thanks!

Josh Lambert
  • 115
  • 1
  • 5
  • 1
    http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson – mre Aug 30 '12 at 15:46

1 Answers1

4

I think your issue is with your class definition. It seems that you wish that the Tags class contains the raw text from Json, which is an array. What I would do instead:

protected Entities entities;
@JsonIgnoreProperties(ignoreUnknown = true)
protected class Entities {
    public Entities() {}
    @JsonDeserialize(contentAs=Tag.class)
    protected List<Tag> tags;

    @JsonIgnoreProperties(ignoreUnknown = true)
    protected class Tag {
        public Tag() {}

        protected String text;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    };

    public Tags getTags() {
        return tags;
    }
    public void setTags(Tags tags) {
        this.tags = tags;
    }
};

Here on the field tags I use a List to represent the Json array, and I tell Jackson to deserialize the content of that list as the Tag class. This is required because Jackson doesn't have the runtime information of the generic declaration. You'd do the same thing for the indices, namely have a field List<Integer> indices with the JsonDeserialize annotation.

Pascal Gélinas
  • 2,744
  • 19
  • 20
  • In addition I had to make both of these classes static to get it to work. Here's what led me to that: http://stackoverflow.com/questions/8526333/jackson-error-no-suitable-constructor – Josh Lambert Aug 30 '12 at 18:50
  • 2
    Looks good. Note that one of annotations (`@JsonDeserialize(contentAs=)`) is redundant: does not hurt, but is not needed (since field signature has all the info). – StaxMan Aug 30 '12 at 19:06
  • 1
    Really? I was under the impression that I needed those definition because of Runtime type erasure... well then, I'll get my axe real quick! – Pascal Gélinas Aug 30 '12 at 19:27