-1

I have an object from a class I cannot modify.

The object has a circular dependency.

I would like to serialize the object, but I don't have access to java source code, as it's in a library.

In C++ I could create a subclass, override the virtual methods, then cast down to get the desired behavior. In Java this is not possible.

What options do I have besides creating a new POJO class and copying over every field by hand?

Andrew Prock
  • 6,573
  • 5
  • 37
  • 59

2 Answers2

0

Serialization using Serializable is a bit smelly anyway. I'd prefer using an external serializer, using either JSON (e.g. Jackson, GSON) or a binary format (e.g. Kryo). Either way, you can write custom serializers in all of these that resolve your circular dependencies.

Sean Patrick Floyd
  • 284,665
  • 62
  • 456
  • 576
0

I'd opt for Jackson, even if you are going to be the only one reading/writing your objects. Many advantages, up to and including:

  1. Diagnosing/debugging the serialized JSON makes life easy.
  2. Staying on this well-trodden path means solutions to any questions are a Google away, e.g., http://www.baeldung.com/jackson-deserialization
Kenneth Sizer
  • 41
  • 1
  • 3