Make `JsonNode` implicitly create `ArrayNode`s for repeated XML Elements
cowtowncoder opened this issue · 6 comments
(note: requires FasterXML/jackson-databind#2732 to be implemented first)
One of long-standing issues with this module is that JsonNode does not work well with relatively common XML case where there are sequences of elements with same name: this is typically used to present Arrays, for example.
By default JsonNode assumes uniqueness of property names, and this leads to either loss of all but one element (first or last, depending), or exception for duplicate entries.
It should be easy enough to make JsonNodeDeserializer have logic of "auto-converting" value nodes into ArrayNodes, in case of "duplicates", however; and as long as this is guarded with something (for example, new StreamReadCapability.DUPLICATE_PROPERTIES), it should be completely safe and not change regular JSON processing accidentally.
@cowtowncoder I'm facing this issue. Is there any work around for this before 2.12? Please help.
@cowtowncoder at present it is only recognising last found element if there are not inorder. Do we have any work around for this ? Otherwise I'm unsure what else to do other than switching other parser.
Appreciate your help.
Nope, I don't think this can really be worked around with earlier versions.
You could perhaps implement your own POJOs to get something similar by overriding set methods, but that's about it.
Nope, I don't think this can really be worked around with earlier versions.
You could perhaps implement your own POJOs to get something similar by overriding set methods, but that's about it.
Okay. Thanks for the respinse.for now, I went ahead with JAXB. May be will switch back to Jackson once new version released. 👍
I've found a solution for grouping repeated, non-consecutive XML fields into one list using Jackson, but it also uses Lombok.
- Create a Lombok for the class with '@builder': https://projectlombok.org/features/Builder
- Add the '@Jacksonized' annotation to the class, which makes Jackson use that builder to deserialize: https://projectlombok.org/features/experimental/Jacksonized
- Add the '@Singular' annotation to the List field, which makes Lombok create a method for adding a single element instead: https://projectlombok.org/features/Builder#singular
- Put the field name in the @Singular annotation, e.g. '@Singular("item")', which makes that method that takes one element have the field name
Together, these make Jackson pass each field as it is reached to the Lombok Builder, which has a single-item builder method that accumulates a list. Interleaving is fine.
Note: as per this issue, non-consequtive fields ARE supported for special cases of JsonNode, java.lang.Object and Map.