Main Content

When to Customize the Serialization Process

Supporting backward and forward compatibility of classes is the primary reason to customize the serialization (saving) and deserialization (loading) process for objects. Customize the serialization process when you change a class definition, including but not limited to:

  • Renaming properties

  • Adding or removing properties

  • Changing validation for properties

How to Modify the Serialization Process

MATLAB® includes two ways to customize the serialization process:

The recommended process for customizing serialization is to inherit from matlab.mixin.CustomElementSerialization. This mixin, combined with the functionality of matlab.serialization.ElementSerializationContent and matlab.serialization.SerializationContext, enables you to control what information is serialized and deserialized. This control supports robust backward and forward compatibility between different versions of the same class. matlab.serialization.SerializationContext can be useful when serializing to destinations other than MAT files. matlab.mixin.CustomElementSerialization also supports the addition of dynamic properties during serialization, which loadobj and saveobj do not.

Use loadobj and saveobj when:

  • You have an existing implementation of saveobj that serializes data in a type other than an object or a struct, and your loadobj implementation can only deserialize data from that particular type.

  • You have an existing implementation of loadobj that can only handle structure inputs. Revising your class to use matlab.mixin.CustomElementSerialization can break forward compatibility when the revised class saves data as an object because loadobj in the older class definition might not be able to load all properties. See Transitioning Serialization and Deserialization Processes to matlab.mixin.CustomElementSerialization for more information.

  • You are using a MATLAB release older than R2024b.

See Also

| | | |

Related Topics