Skip to content
Advertisement

@JsonIdentityInfo serialization of items

I’m trying to serialize relations using @JsonItentityInfo to avoid circular references. I’ve created a test to try test the result of the serialization, and I’ve found that jackson is not behaving as I expected. The serialization in not what I though it would be and, in fact, when I try to desarialize the serializated object, an exception is thrown. The code I’m using is:

JavaScript

To my understanding, the output should be

JavaScript

but the test returns

JavaScript

In fact, when trying to read the serialized string jackson throws an exception. What am I doing wrong?

Thank you all for your help

Edit: The example was not complete. The object should be enveloped in another object, so the two of them are serialized.

JavaScript

When implemented this way, the result is the expected: {“a”:{“id”:”1″,”name”:”a”,”b”:”2″},”b”:{“id”:”2″,”name”:”b”}}

Thank you all for you help!

Advertisement

Answer

Under default circumstances, you get exactly what you you should get.

What you can do is change you class A like follows.

Note that I have changed getB() method. It no longer return instance of class B. It returns id attribute of that class B instance.

JavaScript

You can also create a custom serializer for class B as well.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement