matlab.serialization.ElementSerializationContent - Representation of serialized object - MATLAB (original) (raw)

Namespace: matlab.serialization
Superclasses: matlab.mixin.CustomDisplay, matlab.mixin.indexing.RedefinesDot, handle

Representation of serialized object

Since R2024b

Description

A matlab.serialization.ElementSerializationContent instance represents an object to be serialized or deserialized.matlab.serialization.ElementSerializationContent implements methods that add, remove, or change the properties that are saved or loaded.

The matlab.serialization.ElementSerializationContent class is a handle class.

Creation

matlab.serialization.ElementSerializationContent objects are created by themodifyOutgoingSerializationContent andmodifyIncomingSerializationContent methods of matlab.mixin.CustomElementSerialization. You cannot instantiatematlab.serialization.ElementSerializationContent directly.

Methods

expand all

addNameValue addNameValue(propName,value)Add a property name and its value to an object being serialized or deserialized.Input Arguments propName, specified as a string or character vector.value, specified as any valid data type for the property.If you want to add a private property, qualify the name with the owning class. In other words, the propName argument should be_className_.propName
addDynamicNameValue addDynamicNameValue(propName,propVal,Name=Value)Add a dynamic property name and its value to an object being serialized or deserialized.Input Arguments propName, specified as a string or character vector.propVal, specified as any valid data type for the property.Name=Value, specified as one or more name-value arguments used to set property attributes. For example, SetAccess = private restricts access to the dynamic property to members defined by the class only. For more information, see Dynamic Properties — Adding Properties to an Instance.
getValue getValue(propName)Retrieve the value of a property from an object being serialized or deserialized.Input Arguments propName, specified as a string or character vector.
hasNameValue tf = hasNameValue(propName)Returns a logical 1 (true) if the serialized object has a property with the specified name.Input Arguments propName, specified as a string or character vector.
remove remove(propName)Remove a property from an object being serialized or deserialized.Input Arguments propName, specified as a string or character vector.
rename rename(oldName,newName)Rename a property of the object being serialized or deserialized. The value of the property remains the same.Input Arguments oldName, specified as a string or character vector.newName, specified as a string or character vector.If you rename a property and also want that to change that property to private, qualify the name with the owning class. In other words, the newName argument should be_className_.newName
serializedPropertyNames propNames = serializedPropertyNamespropNames = serializedPropertyNames(opts)Returns a string array of property names defined in an object being serialized or deserialized. Useopts to restrict which property names are returned.Input Arguments opts, specified as a string: "all" — Names of all the properties of the object. Calling the method with no arguments is equivalent to"all"."class-defined" — Names of the properties defined by the class of the object, which excludes dynamic properties."dynamic" — Names of the dynamic properties of the object.
updateValue updateValue(propName,value)Update the value of a property of an object being serialized or deserialized.Input Arguments propName, specified as a string or character vector.value, specified as any valid data type for the property.

Examples

collapse all

Define a class Rectangle that represents a rectangle on the coordinate plane. The properties define the coordinates of the lower-left corner (X and Y) and the width and height of the rectangle.

classdef Rectangle properties X Y Width Height end

methods function obj = Rectangle(x,y,w,h) obj.X = x; obj.Y = y; obj.Width = w; obj.Height = h; end end end

Create an instance of Rectangle and save it. Replace_yourfilepath_ with your local path.

rOld = Rectangle(1,1,4,5); save("yourfilepath/oldRectangle.mat","rOld");

Revise the class definition of Rectangle so that the constructor takes the same values, but the properties now define the lower-left corner of the rectangle (X1 and Y1) and the upper-right corner (X2 and Y2). To be able to load objects saved under the old definition, make these additional changes to the class definition:

classdef Rectangle < matlab.mixin.CustomElementSerialization properties X1
Y1 X2 Y2 end

methods function obj = Rectangle(x,y,w,h) obj.X1 = x; obj.Y1 = y; obj.X2 = w + x; obj.Y2 = h + y; end end

methods (Static) function modifyIncomingSerializationContent(sObj) if sObj.hasNameValue("X") sObj.rename("X","X1");
sObj.rename("Y","Y1"); newX2 = sObj.getValue("Width") + sObj.getValue("X1"); sObj.addNameValue("X2",newX2); newY2 = sObj.getValue("Height") + sObj.getValue("Y1"); sObj.addNameValue("Y2",newY2);
sObj.remove("Width");
sObj.remove("Height");
end end end end

Clear rOld from memory.

Remove the old definition of Rectangle from your path. Add the revised definition of Rectangle to the path, and load therOld object. Display the object to confirm that the variable has been deserialized as an object of the revised definition ofRectangle.

load("yourfilepath/oldRectangle.mat","rOld"); rOld

rOld =

Rectangle with properties:

X1: 1
Y1: 1
X2: 5
Y2: 6

Note

This example shows how to revise a class definition for backward compatibility, so that objects serialized under the old definition can be deserialized under the new definition. For an example that shows backward and forward compatibility, see matlab.mixin.CustomElementSerialization.

The matlab.mixin.CustomElementSerialization class enables you to add dynamic properties to an instance ofmatlab.serialization.ElementSerializationContent. Define a classDynDemo that defines two properties, Prop1 andTempData.

classdef DynDemo properties Prop1 TempData end methods function obj = DynDemo(p,t) if nargin > 0 obj.Prop1 = p; obj.TempData = t; end end end end

Create an instance of DynDemo and save it. Replace_yourfilepath_ with your local path.

d = DynDemo([1:10],14); save("yourfilepath/oldDynDemo.mat","d");

Revise the class definition so that TempData is now a dynamic property.

classdef DynDemo < dynamicprops & matlab.mixin.CustomElementSerialization properties Prop1 end methods function obj = DynDemo(p) if nargin == 1 obj.Prop1 = p; end end end methods (Static) function modifyIncomingSerializationContent(sObj) if isempty(sObj.serializedPropertyNames("dynamic")) val = sObj.TempData; sObj.remove("TempData"); sObj.addDynamicNameValue("TempData",val); end end end end

Clear d from memory.

Remove the old definition of DynDemo from your path. Add the revised definition of DynDemo to the path, and load d.

load("yourfilepath/oldDynDemo.mat","d");

Use findprop to confirm that TempData is now a dynamic property.

ans =

DynamicProperty with properties:

                Name: 'TempData'
                
                ...
         

Tips

Version History

Introduced in R2024b