jakartaee / jakartaee/jsonb-api

Support for cyclic references and object deduplication

Open
#72 7 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
95
Forks
41
Avg merge
1d 6h
Merged PRs (30d)
35

Description

This is a feature request to enhance JSON-B for 1.1 by supporting serialising objects with cycles using JSON-Pointers.

If you have a data structure

```
class Person {
String name;
Person marriedTo;
}
```

And the following 2 Person instances:
John, marriedTo Lisa; and
Lisa, marriedTo John;

Then serialising any of those via JSON-B will likely end up in a stack overflow.

The same problem appears if you have a data structure without cycles but the same Java instance to appear multiple times. When serialising this via JSON-B then the person 'John' might get converted to JSON multiple times.
And when receiving this JSON on the other side we will end up with having 3 different instances of 'John'.

The solution we have in Apache Johnzon is to provide a mode to use JsonPointers to reference any subsequent occurences while serialising.

```
Person john = new Person("John");
Person lisa = new Person("Lisa");
john.marriedTo = lisa;
lisa.marriedTo = john;
```

We use a Johnzon specific JsonbConfig `johnzon.deduplicateObjects`.
If you serialise with this config flag enabled then you'll end up with the following JSON:

```
{
"name":"John",
"marriedTo": {
"name":"Lisa",
"marriedTo":"/"
}
}
```

Note the `"marriedTo":"/"`. This value is actually a JsonPointer to the root object (John).
And you can also create many deeper nested structures with it.
Of course they are not that human readable friendly. But perfect for serialisation and deserialisation.

This is a feature we already implemented in Apache Johnzon. It works really well.
https://issues.apache.org/jira/browse/JOHNZON-135
https://issues.apache.org/jira/browse/JOHNZON-141
https://issues.apache.org/jira/browse/JOHNZON-143

Contributor guide

Open the contributing guide

Research direction

No repository files, tests, or entry points are named. Start by reviewing the JSON-B API and the linked Johnzon issues to understand the proposed JsonPointer and deduplication behavior. Done means cyclic and repeated object references can be serialized and deserialized without stack overflow or duplicate instances.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.