Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson: @JsonIdentityInfo Object instead of id

Is there a way to influence the serialization process with @JsonIdentityInfo so that it inserts the whole object instead of referencing the id?

@Entity
@JsonIdentityInfo(
        generator = ObjectIdGenerators.IntSequenceGenerator.class,
        property = "linkLabel")
public class LinkLabel implements Serializable {
   //...
}

So instead of referencing "otherObj" with id 1, Jackson should include the whole object.

{
    "objects": [{
            "id": 1,
            "otherObj": [{
                    "id": 1,
                    ...
                }, {
                    "id": 3,
                    ...
                }]
        },
            "id": 2,
            "otherObj": [1] <-- referencing otherObj with id 1
    ]
}

like here:

{
    "objects": [{
            "id": 1,
            "otherObj": [{
                    "id": 1,
                    ...
                }, {
                    "id": 3,
                    ...
                }]
        },
            "id": 2,
            "otherObj": [{
                    "id": 1,  <-- desired format, whole object
                    ...
                }]
    ]
}

We have bidirectional references, so @JsonManagedReference and @JsonBackReference doesn't work properly. This behavior is described here (http://wiki.fasterxml.com/JacksonFeatureObjectIdentity).

like image 923
Rooky Avatar asked Jan 02 '16 17:01

Rooky


1 Answers

Like said in the comments and from the links, @JsonIdentityInfo and the Jackson Generators doesn't seem to have an option to enable inserting objects instead of ids.

After more research we finally found this: deserialize Jackson object in JavaScript containing JsonIdentityInfo

This was exactly the scenario we had and we are now using JSOG, which is optimized for bidirectional references and it works like a charm on server and client (AngularJS) side.

like image 84
Rooky Avatar answered Oct 13 '22 00:10

Rooky