fast-serialization icon indicating copy to clipboard operation
fast-serialization copied to clipboard

"SerializedForm" pattern and Json format

Open nedtwigg opened this issue 6 years ago • 0 comments

I use the Object writeReplace() { return new SerializedForm(this) } mechanism a fair bit, here's an example from guava:

https://github.com/google/guava/blob/2278123479357836b9090b0e14bca8222c5b0459/guava/src/com/google/common/collect/ImmutableMap.java#L887-L924

If I do this:

FSTConfiguration config = FSTConfiguration.createJsonConfiguration();
String json = FSTConfiguration.createJsonConfiguration().asJsonString(test);

then I get cannot support legacy JDK serialization methods in crossplatform mode. Define a serializer.... If I disable crossplatform mode, then I get this error:

https://github.com/RuedigerMoeller/fast-serialization/blob/59f8f800b79479d0b9c80901cf55903d9b027f26/src/main/java/org/nustaq/serialization/coders/FSTJsonEncoder.java#L247-L249

It seems pretty straightforward to call the writeReplace method, serialize that instead, and then do something similar for readResolve when reading. Are you open to a PR for this? I haven't thought a ton about it, maybe I'm missing an important detail?

Also, what is the semantic intent of "crossPlatform" mode? It seems to mean "ignore FST annotations":

https://github.com/RuedigerMoeller/fast-serialization/blob/59f8f800b79479d0b9c80901cf55903d9b027f26/src/main/java/org/nustaq/serialization/FSTClazzInfo.java#L513

but also "no writeReplace allowed":

https://github.com/RuedigerMoeller/fast-serialization/blob/59f8f800b79479d0b9c80901cf55903d9b027f26/src/main/java/org/nustaq/serialization/FSTClazzInfo.java#L175-L177

So it seems to be a mix of "no new stuff like annotations, and also no old stuff like writeReplace". What is the design intent?

Very cool library, thanks!

nedtwigg avatar Oct 02 '19 06:10 nedtwigg