Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson xml deserialize inline array

How to deserialize such strange XML. In my opinion, the props-entity is missing (around the props), but I can't change the source of this XML (a web service).

<parents>
  <parent name="first">
    <description><![CDATA[Description for the first-Entity]]></description>
    <prop name="level">
      <value><![CDATA[1]]></value>
    </prop>
    <prop name="enabled">
      <value><![CDATA[true]]></value>
    </prop>
    <prop name="version">
      <value><![CDATA[1.0-beta3]]></value>
    </prop>
  </parent>
  <parent name="second">...</parent>
  ...
</parents>

My entities are

public class Test {
    @Test
    public void deserializerTest() throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper om = new XmlMapper();
        List<Parent> xml = om.readValue(new File("./test.xml"),
            new TypeReference<List<Parent>>() {});
    }
}

public class Prop {
    @JacksonXmlProperty(isAttribute = true)
    public String name;

    @JacksonXmlText
    public String value;
}

@JacksonXmlRootElement
public class Parent {
    @JacksonXmlProperty(isAttribute = true)
    public String name;

    public String description;

    // 1. alternative with List
    public List<Prop> prop;

    // 2. alternative with Map
    @JsonDeserialize(using = PropDeser.class)
    public Map<String, String> prop;
} 


public static class PropDeser extends JsonDeserializer<Map<String, String>> {

    @Override
    public Map<String, String> deserialize(JsonParser jp,
            DeserializationContext ctxt) throws IOException,
            JsonProcessingException {
        Map<String, String> ret = new HashMap<String, String>();
        boolean eof = false;
        while (jp.hasCurrentToken()) {
            JsonToken t = jp.getCurrentToken();
            switch (t) {
            case END_OBJECT:
                if (eof) {
                    return ret;
                }
                eof = true;
                break;
            case VALUE_STRING:
                ret.put(jp.getCurrentName(), jp.getText());
                break;
            default:
                eof = false;
                break;
            }
            jp.nextValue();
        }
        return null;
    }

}

1. Alternative

creates an exception 'Can not instantiate value of type [simple type, class my.test.Prop] from JSON String; no single-String constructor/factory method (through reference chain: my.test.Parent["prop"])'

I don't want a simple-String list. I need both: name and value. So I came to the idea of using a Map<String, String> by creating my own deserializer...

2. Alternative

The error seems to be method PropDeser.deserialize() consumes the closing-tag of the parent.

java.lang.NullPointerException
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.find(BeanPropertyMap.java:160)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2575)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1766)
at my.test.Test.deserializerTest(Test.java:57)

Is there a possibility to iterate backward in the XML-stream? How can the method know when to stop? I have no clue.

like image 847
mailwurf Avatar asked Nov 01 '12 15:11

mailwurf


1 Answers

It should be possible to handle "unwrapped" style of list elements with Jackson XML module 2.1, with @JacksonXmlElementWrapper(useWrapping=false).

Structure should be something like this:

@JacksonXmlRootElement(localName="parents")
public class Parents {
  @JacksonXmlElementWrapper(useWrapping=false)
  public List<Parent> parent;
}

public class Parent {
  @JacksonXmlProperty(isAttribute=true)
  public String name;

  public String description;

  @JacksonXmlElementWrapper(useWrapping=false)
  public List<Prop> prop;
}

public class Prop {
  @JacksonXmlProperty(isAttribute=true)
  public String name;

  public String value;
}

so your solution was quite close.

Note that if inner classes are used, they need to have 'static' in declaration. I tested this with 2.1.4, and it works for me.

like image 93
StaxMan Avatar answered Nov 14 '22 07:11

StaxMan