Okay I think I may have figured this out.
You shouldn't/can't use an attribute for object key reference. So this will not
work:
<units>
<unit name="maths-101" points="120"/>
</units>
<students>
<student>
<unit name="maths-101"/>
</student>
</students>
However, you should/can use the value of an element. So this does work:
<units>
<unit name="maths-101" points="120"/>
</units>
<students>
<student>
<unit>maths-101</unit>
</student>
</students>
Hopefully this is helpful to others!
________________________________
From: [email protected] <[email protected]> on behalf
of Rupert Madden-Abbott <[email protected]>
Sent: 29 April 2018 18:01:14
To: jackson-user
Subject: Re: [jackson-user] How can I deserialize XML object references?
Hi Tatu,
Here is an example<https://github.com/rupert654/xml-object-reference-example>.
Note that the Unit class is annotated with JsonIdentityInfo.
When I run the main method, I get this exception:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException:
Already had POJO for id (java.lang.String) [[ObjectId: key=maths-101,
type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator,
scope=Unit]] (through reference chain:
School["students"]->java.util.ArrayList[0]->Student["unit"]->java.util.ArrayList[0]->Unit["name"])
at
com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:391)
When I remove that annotation, I get no exception but the units that make up a
student only have their name populated, and not their points.
On Sunday, April 29, 2018 at 4:17:38 AM UTC+1, Tatu Saloranta wrote:
On Fri, Apr 27, 2018 at 1:53 AM, Rupert Madden-Abbott
<[email protected]> wrote:
> How can I turn this XML:
>
> <units>
> <unit name="maths-101" points="120"/>
> <unit name="english-101" points="60"/>
> </units>
>
> <students>
> <student>
> <unit name="maths-101"/>
> <unit name="english-101"/>
> </student>
> <student>
> <unit name="english-101"/>
> </student>
> </students>
>
> into something like this Java:
> class Student {
> private List<Unit> units;
>
> public List<Unit> getUnits() {
> return units;
> }
>
> public void setUnits(final List<Unit> units) {
> this.units = units;
> }
> }
>
> class Unit {
> private String name;
>
> private Integer points;
>
> public String getName() {
> return name;
> }
>
> public void setName(final String name) {
> this.name<http://this.name> = name;
> }
>
> public Integer getPoints() {
> return points;
> }
>
> public void setPoints(final Integer points) {
> this.points = points;
> }
> }
>
>
> After deserializing, I want to be able to retrieve the points for a Unit,
> directly from a student (e.g. student.getUnit().getPoints()) and do not want
> to have to look up the unit by name from a separate list of units.
>
> I have tried the solution suggested in this StackOverflow Answer but I get
> an error saying "Already had POJO for id (java.lang.String) [[ObjectId:
> key=foo,
> type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator,
> scope=Unit]]. I'm not sure if that only works for JSON or if I need a
> slightly different XML/Java structure to get this working?
Perhaps you could share code you are using, as it sounds like you are
half way there.
It is true that much of more advanced functionality has been mostly
tested on JSON side,
and that XML is unfortunately bit tricky to get to work due to model
differences.
But Object Identity support should work similarly and it does work at
least in some cases;
there are unit test for `jackson-dataformat-xml` that work.
-+ 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
[email protected]<mailto:[email protected]>.
To post to this group, send email to
[email protected]<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.