Jackson: @JsonIdentityInfo Object instead of id

后端 未结 3 1969
温柔的废话
温柔的废话 2021-01-12 20:36

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

@Entity
@Json         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-12 20:59

    The issue precisely is with how Jackson work serializing the java object to JSON. When we use @JsonIdentityInfo to resolve the issue of cyclic dependency in object graph, we are forcing the serialization mechanism to serialize the first occurrence of Java object in JSON and the subsequent occurrence to be substituted by the id generated.

    The determining factor when to create JSON for Java object is based on access made to the memory location where the JAVA Objects are created.

    So, one good way to trick the serialization mechanism would be to create a copy of the child object and then set it. This way the same child object belonging to different parent in collection are treated as different child object instances.

    One great way to create an exact copy of the object is using SerializationUtils.serialize/desrialize method.

        byte[] bs= SerializationUtils.serialize(classObject);
        ClassObject cloneEntity = (ClassObject ) SerializationUtils.deserialize(bs);
    

    This creates an exact copy of the an object in different memory relocation.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题