Bidirectional Relationship Support in JSON
![Bidirectional Relationship Support in JSON - Image 1](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112414ScreenShot20170213at5.png)
@JsonManagedReference
and @JsonBackReference
(which simply omits the back reference from serialization) or using @JsonIgnore
to simply ignore one of the sides of the relationship. Alternatively, one can develop custom serialization code that ignore any such bidirectional relationship or circular dependency in the data. ![Bidirectional Relationship Support in JSON - Image 2](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112538ScreenShot20170213at5.png)
parent
object to JSON (for example, by using the stringify
method, as in var parentJson = JSON.stringify(parent);
), the exception Uncaught TypeError: Converting circular structure to JSON will be thrown. @JsonIgnore
), or we could simply remove the above references to the parent from the children, these are ways of avoiding rather than solving the problem. What we really want is a resulting JSON structure that maintains each bidirectional relationship and that we can convert to JSON without throwing any exceptions. ![Bidirectional Relationship Support in JSON - Image 3](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112619ScreenShot20170213at5.png)
id
). That will work just fine in the above example where the only property that has the value 100 is parent
![Bidirectional Relationship Support in JSON - Image 4](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112645ScreenShot20170213at5.png)
parent
references the value 100, that IS referencing the parent object’s id
, but when priority
references the value 100, that is NOT referencing the parent object’s id
(and since it will think that priority
is also referencing the parent id
![Bidirectional Relationship Support in JSON - Image 5](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112734ScreenShot20170213at5.png)
So that should work, right?
Yes.
But…Our approach is to have our serializer automatically create and add a unique ID (using a GUID) to each object. It then replaces any object reference with that object’s GUID. (Note that the serializer will need to use some unique property name for these IDs as well; in our example, we @id
![Bidirectional Relationship Support in JSON - Image 6](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112822ScreenShot20170213at5.png)
![Bidirectional Relationship Support in JSON - Image 7](https://www.mytechlogy.com/upload/by_users/ipapuc/131702112843ScreenShot20170213at5.png)
Then feeding the above JSON to the deserializer would generate the original set of objects (i.e., the parent object and its two children, referencing one another properly).
So now that we know what we want to do and how we want to do it, let’s implement it.![Bidirectional Relationship Support in JSON - Image 8](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113000ScreenShot20170213at5.png)
![Bidirectional Relationship Support in JSON - Image 9](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113017ScreenShot20170213at5.png)
![Bidirectional Relationship Support in JSON - Image 10](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113051ScreenShot20170213at5.png)
![Bidirectional Relationship Support in JSON - Image 11](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113122ScreenShot20170213at5.png)
convertToObject(convertToJson(obj)) === obj
evaluates to true. ![Bidirectional Relationship Support in JSON - Image 12](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113207ScreenShot20170213at5.png)
These two java classes Parent and Child represent the same structure as in JavaScript example in the beginning of this article. The main point here is in @JsonIdentityInfo
![Bidirectional Relationship Support in JSON - Image 13](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113227ScreenShot20170213at5.png)
![Bidirectional Relationship Support in JSON - Image 14](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113251ScreenShot20170213at5.png)
The described approach to handling a bidirectional relationship in JSON can also be leveraged to help reduce the size of a JSON file, since it enables you to reference objects simply by their unique ID, rather than needing to include redundant copies of the same object.
Consider the following example:![Bidirectional Relationship Support in JSON - Image 15](https://www.mytechlogy.com/upload/by_users/ipapuc/131702113319ScreenShot20170213at5.png)
filteredChildren
![Bidirectional Relationship Support in JSON - Image 16](https://uploads.toptal.io/blog/post_image/90379/hyam-b42e8abb80e91984a0f9dc9c7a107262.jpg)
JavaScript
Javascript Object Notation (JSON)
![](https://www.mytechlogy.com/view/images/edit_blog.png)