-1

I have a data structure (not designed by me, so no chance to change it!). In which one class (e.g. foo) contains other (e.g. bar), and bar contains an array foo. I solved this using @JsonIdentityInfo in this manner:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "object.id" )
public class Foo {
   @JsonProperty
   Object id;
   @JsonProperty
   Bar bar;
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "object.id" )
public class Bar{
   @JsonProperty
   Object id;
   @JsonProperty
   List<Foo> foos;
}

This deserializes properly but it serializes not completely correct. The issue is this generate an extra id object.id. I would like to use the ID as the object.id. How can I achieve this using Jackson annotations?

I want to parse an serialize this:

Object Foo:

{
   "id":1,
   "bar":
      {
        "id":2,
        "foos":[1]
      } 
}

and not this:

{
   "id":1,
   "object.id":2,
   "bar":
      {
        "id":2,
        "object.id":2,
        "foos":[1]
      } 
}
ender.an27
  • 703
  • 1
  • 13
  • 35

1 Answers1

0

The solution is the use of the of ObjectIdGenerators.PropertyGenerator.class instead of ObjectIdGenerators.IntSequenceGenerator.class in the annotation configuration @JsonIdentityInfo. Addtionally, the scope must be configured as is shown below:

 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Foo.class)
    public class Foo {
       @JsonProperty
       Object id;
       @JsonProperty
       Bar bar;
    }

   @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Bar.class)
    public class Bar{
       @JsonProperty
       Object id;
       @JsonProperty
       List<Foo> foos;
    }

I got the answer from Deserialize with Jackson with reference to an existing object

ender.an27
  • 703
  • 1
  • 13
  • 35