How do I deserialize this XML array?

629 views
Skip to first unread message

ndo...@mindbridge.ai

unread,
Apr 14, 2017, 12:09:47 AM4/14/17
to jackson-user
Hi,

How would I go about deserializing this array of mixed elements?

<result>
<animals>
<cat><name>foo</name></cat>
<dog><name>pop</name></dog>
<cat><name>fluff</name></cat>
<giraffe><name>dodo</name></giraffe>
</animals>
</result>

I haven't discovered a way yet :(

Tatu Saloranta

unread,
Apr 14, 2017, 1:55:08 AM4/14/17
to jackson-user
Please note that Jackson does not guarantee that it can support all kinds of XML.
It does aim to be able to deserialize content it serializes: so if you can find a way to serialize content in certain XML structure, it should be readable. But there are other structures that are not.

In this case we would have POJO with `animals` property which is a collection type, and would have to be "unwrapped" (configure to use unwrapped list, or annotate).
Elements are polymorphic, so need `@JsonTypeInfo`, and type id inclusion looks like `As.WRAPPER_OBJECT`.
I am not sure if that would exactly as presented, but it'd be something like:

public class Result {
  @JacksonXmlElementWrapper(useWrapping=false)
  public List<Animal> animals;
}

@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
public abstract class Animal {
}

@JsonTypeName("cat")
public class Cat extends Animal {
  public String name;
}

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscribe@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ndo...@mindbridge.ai

unread,
Apr 14, 2017, 10:04:17 AM4/14/17
to jackson-user
Hi Tatu,

Thanks for responding. I think I have a similar setup here: https://groups.google.com/forum/#!topic/jackson-user/QN3bVAgFluU. But you'll notice that the object gets wrapped twice in serialization and expects the same for deserialization. Instead of getting <result><lion></lion></result>, I get <result><result><lion></lion></result</result>. This is with latest stable release (2.8.6).
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.

ndo...@mindbridge.ai

unread,
Apr 14, 2017, 10:11:46 AM4/14/17
to jackson-user
hi Tatu,

I just tried your recommendation and it gave me similar results as before:

<Zoo>
 
<name>Samba Wild Park</name>
 
<city>Paz</city>
 
<animals>
   
<lion>
     
<name>Simba</name>
     
<sound>Roar</sound>
     
<type>carnivorous</type>
     
<endangered>true</endangered>
   
</lion>
 
</animals>
 
<animals>
   
<elephant>
     
<name>Manny</name>
     
<sound>trumpet</sound>
     
<type>herbivorous</type>
     
<endangered>false</endangered>
   
</elephant>
 
</animals>
</Zoo>

Notice the <animals> tag doesnt wrap around both elements but around each, instead.

Tatu Saloranta

unread,
Apr 14, 2017, 6:55:59 PM4/14/17
to jackson-user
I think this is then a structure that is not supported by Jackson at this point.

I don't know if it would be possible to try combine elements; the
problem is that polymorphic type being written results in one element,
and then POJO itself results in the other.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages