Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JsonNode implicitly create ArrayNodes for repeated XML Elements #403

Closed
cowtowncoder opened this issue May 21, 2020 · 6 comments
Closed
Milestone

Comments

@cowtowncoder
Copy link
Member

(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.

@Jearkrishna
Copy link

@cowtowncoder I'm facing this issue. Is there any work around for this before 2.12? Please help.

@Jearkrishna
Copy link

@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.

@cowtowncoder
Copy link
Member Author

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.

@Jearkrishna
Copy link

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. 👍

@son0fether
Copy link

son0fether commented Nov 2, 2022

I've found a solution for grouping repeated, non-consecutive XML fields into one list using Jackson, but it also uses Lombok.

  1. Create a Lombok for the class with '@builder': https://projectlombok.org/features/Builder
  2. Add the '@Jacksonized' annotation to the class, which makes Jackson use that builder to deserialize: https://projectlombok.org/features/experimental/Jacksonized
  3. 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
  4. 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.

@cowtowncoder
Copy link
Member Author

Note: as per this issue, non-consequtive fields ARE supported for special cases of JsonNode, java.lang.Object and Map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants