Bogdan Kobylynskyi
Bogdan Kobylynskyi

Reputation: 1220

XML deserialization results in JsonMappingException: Can not deserialize instance of class out of VALUE_STRING token

I have such xml:

<game quarter="2">
  <quarter number="1">
    ...
  </quarter>
  <quarter number="2">
    ...
  </quarter>
</game>

And my POJO class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Game {

    @JacksonXmlElementWrapper(localName = "quarter")
    public Quarter[] quarters;

When I am trying to deserialize xml using:

xmlMapper.readValue(getFileContent(PBP_XML_PATH), Game.class);

I get:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Game$Quarter[] out of VALUE_STRING token
  at [Source: java.io.StringReader@5f45632; line: 4, column: 1] (through reference chain: Game["quarter"])
  at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
  at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
  at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
  at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:275)
  at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:179)
  at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:20)
  at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499)
  at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:108)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)

It looks like Jackson can't deal with the attribute and property of the same name.

Upvotes: 1

Views: 514

Answers (1)

Bogdan Kobylynskyi
Bogdan Kobylynskyi

Reputation: 1220

As it appeared, it is a known issue in the jackson-dataformat-xml-2.9.0 version:

Attribute / element name collision cannot be configured during deserialization

If an element has an attribute and sub-element with the same name, the attribute seems to be ignored and the element value used

Currently there is no way to handle synonyms (either between attribute or element; or between properties with different namespace).

Upvotes: 1

Related Questions