Missing array order information

14 views
Skip to first unread message

Juan Rada

unread,
Jan 16, 2022, 8:23:04 PM1/16/22
to jackson-user
Hello folks

I am using jackson xml  to deserialize an array of elements of diferent types like.

<array>
     <type1>...content-1a...<type1/>
     <type2>...content-2...<type2/>
     <type1>...content-1b...<type1/>
<array>

The problem is that when using tree model (JsonParser#readValueAsTree<TreeNode>) in my custom deserializer, this is transformed into json and it becomes

{
    "array" :  {
        "type1": [{content-1a}, {content-1b}]
        "type2": {content-2}
    }
}

As you notice order information is lost.  I am aware (as it said in documentation) that tree modeling has some limitation but I am wondering if there is any suggested workaround. Note that I am using custom serialization register using JacksonXmlModule.addDeserializer and not annotation base approach. 


Tatu Saloranta

unread,
Jan 16, 2022, 9:08:33 PM1/16/22
to jackso...@googlegroups.com
There is no way to represent such content as JsonNode without losing
some of the ordering information, unfortunately, as duplicate keys are
not allowed.
This is indeed a limitation that cannot be overcome with that model.

One thing that you could do is to instead define a POJO with setters
to match "type1" and "type2": setters will get called
in document order and you can retain ordering using whatever data
structure you want.

Or, alternatively, you could possibly sub-class one of `Map`
implementations (or define a completely custom one), override its
`put()` method which should also be
called for properties, in order. Within that method you could do
something different than call "super.put()".

I hope this helps,

-+ 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/5b129782-c3c2-4bb3-b9c8-8222d18339c0n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages