Skip to content

Demonstrates how Optionals can be (de)serialized.

License

Notifications You must be signed in to change notification settings

nipafx/demo-serialize-optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serialize Optional

This project provides the code samples for the CodeFX post about serializing Optional. It demonstrates how an Optional instance can be (de)serialized using different approaches.

For fields it shows how it fails and how it is done correctly. It provides a class SerializableOptional, which wraps an Optional for serialization (e.g. for RMI) and some classes using it. Finally, it provides a demo which ties it all together.

Code

Classes Using Optional

The classes ClassUsingOptional and ClassUsingOptionalCorrectly each contain a field with type Optional. While the first fails at serialization (using the default serialization mechanism), the second does not (because it properly implements the serialization proxy pattern).

SerializableOptional

The SerializableOptional is a simple wrapper for an Optional which implements Serializable.

Classes Using SerializableOptional

The comment on SerializableOptional describes different approaches on how to use it to serialize a class with an optional field. The classes TransformFor... show examples for these approaches.

Disclaimer

That Optional is not serializable is no mere oversight. The reasons are described in all detail in a blog post from CodeFX. The gist is:

  • Optional was only meant for return values, so there is no need to serialize it.
  • Making a class serializable heavily reduces the flexibility to change it later.
  • Optional is a value-based class and those should not be serialized.

About

Demonstrates how Optionals can be (de)serialized.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages