Open peregin55 opened 8 years ago
I think this would belong under jackson-dataformat-xml
, unless there is something Scala specific.
In Java this code works if I use the JavaBean pattern to create the domain model. It sounds like JacksonXmlElementWrapper doesn't really work very well with immutable Java objects [1].
In Scala I'd like to avoid JavaBean since case-classes use immutability. Is there a way to control the element names for Seq/immutable lists? Either with JacksonXmlElementWrapper or something else?
Thanks!
[1] https://github.com/FasterXML/jackson-dataformat-xml/issues/149
@peregin55 Perhaps this has more to do with problems with case classes, related to handling of @JsonCreator
and constructors -- this is what exception also suggests. So I don't think it is XML-specific after all, but more likely Scala issue. So scratch the suggestion to move this.
However what would be useful would be equivalent json-only example, I think.
+1 have this issue with scala List
Come to think of it, there's a good chance this would be due to XML-specific extra handling that is required. But if anyone can double-check to ensure this can not be reproduced with json, that would be great. Building a combined test is problematic from module perspective; neither module should (ideally) depend on each either wrt releases.
+1 have this issue with scala List
I had the same issue. I fixed it with adding new constructor with a different signature than the built-in one, and marking it as @JsonCreator. I also had to change annotations so they don't get applied on the constructor parameters.
This is how the code should look like (I didn't verify it with the case from this issue, so there may be typos):
case class Header(
@(JacksonXmlElementWrapper @field @getter)(localName = "authors")
@(JacksonXmlElementWrapper @field @getter)(localName = "author")
authors: Seq[Author]) {
@JsonCreator
def this() = {
this(null)
}
}
See also this one: https://blog.adamfurmanek.pl/2024/05/20/serializing-collections-with-jackson-in-scala-and-renaming-the-nested-element/
I'm having trouble getting a case class with a collection field to serialize the way I want. I have a data model that looks like this:
And I'm using Jackson to produce an XML:
The document that looks like this:
But
<authors>
is duplicated with its nested elements. I want the<authors>
tag to contain a list of<author>
elements. I can get this output by adding the following annotations to the Header case class:This serializes the way I want, but when deserializing I get the following exception:
Is there something I can do to get this to deserialize properly?
Thanks for your help!