On this page:
serializable
9.9.1 Serialization and Deserialization
Serializable
serialize
deserialize
deserializer
8.15.0.12

9.9 Serializables🔗ℹ

A serializable value is one of the predefined serializable datatypes (numbers, strings, lists, etc.; see Serializable) or an instance of a class that is declared with a serializable clause. The rhombus/serialize module provides functions for serializing and deserializing values.

class clause

serializable

 

class clause

serializable:

  option

  ...

 

option

 = 

~version nonneg_int

 | 

~version: nonneg_int

 | 

~serialize: entry_point

 | 

~deserialize: entry_point

 | 

~deserialize_shell: entry_point

 | 

~deserialize_fill: entry_point

A class clause recognized by class that makes instances of the class work with serialize. When a class has a serializable, it must be declared at the top level of a module or in an interactive context.

When serializable is declared without any options, the class must declare no uninitialized private fields. An instance is serialized by extracting and serializing all of its public fields, and it is deserialized by calling the class’s constructor. The constructor is called using the convention of a default constructor, but the class can have a custom constructor that accepts the same arguments.

When serializable is declared with options, each option must use a distinct keyword to customize serialization or deserialization:

9.9.1 Serialization and Deserialization🔗ℹ

 import: rhombus/serialize package: rhombus-lib

annotation

Serializable

An annotation (not an interface) that is satisfied by value that may be serializable. The Serializable annotation does not check that elements within a compound value (such as a list) are also serializable, so it is not a complete check for whether serialize will succeed.

The following pre-defined datatypes are serializable:

function

fun serialize(v :: Serializable,

              ~out: out :: maybe(Port.Output) = #false)

  :: Bytes || Void

Serializes v to a byte string. If out is an output port, then the serialized data is written to out and #void is returned. Otherwise, the result is a byte string containing the serialized data.

The byte-string encoding produced by serialize is independent of the Racket and Rhombus version, except as future versions introduce extensions that are not currently recognized.

function

fun deserialize(in :: Bytes || Port.Input) :: Any

Deserializes value from in, reversing the encoding of serialize.

declaration

deserializer:

  option

  ...

 

option

 = 

~deserialize: entry_point

 | 

~deserialize_shell: entry_point

 | 

~deserialize_fill: entry_point

For use within a submodule, defines and exports deserialize in the enclosing module.

The enclosing submodule normally should have a name of the form deserialize_Name_version where Name is the name of a serializable class declared in the submodule’s parent, and version is the class’s old version.

The intent of a deserializer is that the ~deserialize entry point accepts the content of an old-version object and returns a current-version object corresponding the the old object. For example, default values may be used for fields added in a newer version of the class.