Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identical nested XML tag with Jackson

Tags:

java

xml

jackson

I am parsing the following external XML with Jackson.

<SomeRootObject>
  <Events>
    <Event>
      <EventID>248739296</EventID>
      ...
      <Event>1709</Event>
      ...

I defined a POJO for “Event”.

@JacksonXmlRootElement(localName = "Event")
public class MyEvent {
    @JsonProperty("EventID")
    public String eventID;

    ...

    @JsonProperty("Event")
    public int event;

    ...

As you can see one of the fields in this POJO is also mapped as “Event”. And so Jackson complains that it can’t create an int from an event:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
at [Source: java.io.StringReader@12417468; line: 1, column: 280] (through reference chain: be.parkd.api.tnt.ram.model.RamEvents[“Event”]->java.util.ArrayList[0]->be.parkd.api.tnt.ram.model.RamEvent[“Event”]).

Can this case be handled with Jackson?

One dirty fix I have in mind is to preprocess the XML to change the underlying Event but I would prefer a cleaner solution.

like image 363
Michiel Ariens Avatar asked Oct 16 '18 11:10

Michiel Ariens


1 Answers

Variant 1

The following example reads a list of <Event> elements wrapped in an <Events> element. The <Event> itself contains another nested <Event> element. This seems to be not a problem for Jackson.

Note: I used TypeReference<List<Event>>() {} as a serialization rule.

@Test
public void test1() throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new XmlMapper();
    List<Event> event=mapper.readValue("<Events><Event><EventID>248739296</EventID><Event>1709</Event></Event><Event><EventID>248739297</EventID><Event>1710</Event></Event></Events>", new TypeReference<List<Event>>() {
    });
    System.out.println(toString(event));
}

public String toString(Object obj) {
    try {
        StringWriter w = new StringWriter();
        new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, obj);
        return w.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

With Event.java

@JacksonXmlRootElement(localName = "Event")
public class Event {
    @JsonProperty("EventID")
    private String eventID;
    @JsonProperty("Event")
    private int event;
    public String getEventID() {
        return eventID;
    }
    public void setEventID(String eventID) {
        this.eventID = eventID;
    }
    public int getEvent() {
        return event;
    }
    public void setEvent(int event) {
        this.event = event;
    }
}

Prints

[ {
  "EventID" : "248739296",
  "Event" : 1709
}, {
  "EventID" : "248739297",
  "Event" : 1710
 } ]

So, it works!

Variant 2

@Test
public void test2() throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new XmlMapper();
    SomeRootObject object=mapper.readValue("<SomeRootObject><Events><Event><EventID>248739296</EventID><Event>1709</Event></Event><Event><EventID>248739297</EventID><Event>1710</Event></Event></Events></SomeRootObject>", SomeRootObject.class);
    System.out.println(toString(object));
}

With SomeRootObject.class

@JacksonXmlRootElement(localName = "SomeRootObject")
public class SomeRootObject {
    @JsonProperty("Events")
    List<Event> events;
    public SomeRootObject() {

    }
    public List<Event> getEvents() {
        return events;
    }
    public void setEvents(List<Event> events) {
        this.events = events;
    }
}

Prints

{
  "Events" : [ {
    "EventID" : "248739296",
    "Event" : 1709
  }, {
    "EventID" : "248739297",
    "Event" : 1710
  } ]
}

Works too!

like image 169
jschnasse Avatar answered Nov 08 '22 14:11

jschnasse