jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom serialising of objects with inheritance

williamclocksin opened this issue · comments

In my application, the object graph to be serialised contains objects that are subclassed about 5 or so deep. I have created custom readers and writers for one or more of the intermediate subclasses. However, when the objects are serialised, the entire inheritance structure of the object is not serialised. Only the subclasses with the custom readers/writers are serialised. What am I missing? In my custom reader/writer, do I need to do something analogous to super()? All the examples and tests do not use objects with inheritance, so I can't see how to do it. Thanks.

And, only the "lowest" subclass (that is, the final child of the inheritance farthest away from Object) that has a custom serialiser, is serialised. It is as though .toJson() and .toObjects() starts at the custom serialiser of the "lowest" subclass that has a custom serialiser, does that read/write, and then doesn't serialise the rest of the object.
So therefore, does the customer serialiser for the lowest subclass need to be responsible for serialising the entire object?

The custom reader/writer for a class, is as you asked at the end, it is expected to handle the whole class, not a "slice."