Serialization of object identity
Nir Lisker
nlisker at gmail.com
Wed Jun 12 19:03:20 UTC 2019
Hi Brian,
Thanks for the Towards Better Serialization document, I'm glad to see
movement in this area.
I take issue with your view on JSON and cycles representation. Jackson, for
example, has an @JsonIdentityInfo annotation [1] that allows to add an
ad-hoc property to the serialized objects to mark their identity within the
context of the serialized object graph. The annotation takes an ID
generator to specify the ID assignments. One such generator is JSOG [2];
you can see in the link what the JSON looks like for a cyclic graph.
I understand that you probably mean that the format can't "natively"
represent cycles, but the Java libraries that de/serialize JSON can and do
solve this limitation. And because of that, the argument of "JSON can't do
that and it's popular so it's OK if Java doesn't do that" doesn't work
anymore.
In fact, the cyclic graph issue is a result of an inability to represent
object identity in serialization, which is a much larger problem. Two
additional issues that the lack of identity info causes are:
1. Serializing and deserializing an object graph will result in a different
state. Reference equality will fail where is once succeeded. This is more
relevant for mutable objects, but still very relevant.
2. Deserializing heavy objects is very costly. If I have an
expensive-to-construct object which is referenced from many objects in a
list, and I serialize the list and then deserialize it, I will create many
heavy objects.
As a real world example, a library has many books, and each book has a
label that identifies the library that owns it. Serializing the (cyclic)
library is not a lot to ask for as it is a standard data representation,
and JSON solutions do it well. Moreover, deserializing the library should
not create 5001 difference library objects when a library has 5000 books,
and if I want to change the name of the library, I don't want to do it for
5001 different objects because it's actually the same one.
Hopefully I didn't spew nonsense,
Nir
[1]
https://static.javadoc.io/com.fasterxml.jackson.core/jackson-annotations/2.9.9/com/fasterxml/jackson/annotation/JsonIdentityInfo.html
[2] https://github.com/jsog/jsog#jsog---javascript-object-graph
More information about the amber-dev
mailing list