matlab.serialization.SerializationContext - Context in which objects are serialized - MATLAB (original) (raw)

Main Content

Namespace: matlab.serialization

Context in which objects are serialized

Since R2024b

Description

Use an instance of matlab.serialization.SerializationContext to determine how to customize the serialization of an object in themodifyOutgoingSerialization method of matlab.mixin.CustomElementSerialization. MATLABĀ® sets the class property CustomizeForReadability to true when an object of the class is being serialized in a context that is human readable, for example through the C++ MATLAB Data API.

Creation

MATLAB creates an instance of matlab.serialization.SerializationContext when class authors pass a third argument to the modifyOutgoingSerialization method of matlab.mixin.CustomElementSerialization. The argument name becomes the name of the matlab.serialization.SerializationContext instance. You cannot create an instance of this class directly.

Properties

expand all

Indicates whether the serialized object is readable by external users, specified as a logical. For example, objects serialized through the C++ MATLAB Data API are human readable, whereas MAT files are not.

Examples

collapse all

Define a class ObjectStorage that stores a MATLAB object along with the class name of the object and class metadata.

classdef ObjectStorage properties storedObj
className classMetadata end

methods function obj = ObjectStorage(o) obj.storedObj = o; obj.className = class(o); obj.classMetadata = metaclass(o); end end end

If an object of this class is being serialized in a human-readable format, you can perform some cleanup on the data based on that fact. Make these changes to the class:

If the object is not being stored in a human-readable context, themodifyOutgoingSerializationContent method performs no action.

classdef ObjectStorage < matlab.mixin.CustomElementSerialization properties storedObj
className classMetadata end

methods function obj = ObjectStorage(o) obj.storedObj = o; obj.className = class(o); obj.classMetadata = metaclass(o); end end

methods (Static) function modifyOutgoingSerializationContent(sObj,obj,context) if context.CustomizeForReadability sObj.rename("storedObj","matlabObject"); sObj.namespace = obj.classMetadata.Namespace; sObj.description = obj.classMetadata.Description; sObj.remove("classMetadata"); end
end end end

Version History

Introduced in R2024b