Main Content

matlab.serialization.SerializationContext Class

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 the modifyOutgoingSerialization 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:

  • Inherit from matlab.mixin.CustomElementSerialization.

  • Implement the static method modifyOutgoingSerializationContent. This method is called whenever you attempt to save an ObjectStorage object. See matlab.mixin.CustomElementSerialization for more information about this method.

  • Start the method with an if statement that checks the value of the CustomizeForReadability property of context, which is an instance of matlab.serialization.SerializationContext.

  • If CustomizeForReadability is true, use the methods of matlab.serialization.ElementSerializationContent on sObj to customize the serialization output. Rename the storedObj property to matlabObject, capture some basic class metadata in new properties, and remove the classMetadata property from the serialized instance.

If the object is not being stored in a human-readable context, the modifyOutgoingSerializationContent 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