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
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
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:
- Inherit from
matlab.mixin.CustomElementSerialization
. - Implement the static method
modifyIncomingSerializationContent
. This method is called whenever you attempt to load aRectangle
object. (For more information, see matlab.mixin.CustomElementSerialization.) - As the first step of
modifyIncomingSerializationContent
, callhasNameValue
on the serialized object,sObj
, which is an instance ofmatlab.serialization.ElementSerializationContent
. If the object has a property namedX
, the object was created using the old definition ofRectangle
, andmodifyIncomingSerializationContent
modifies the properties of the object to match the new definition. - To update an instance serialized under the old definition,
modifyIncomingSerializationContent
calls four more methods ofmatlab.serialization.ElementSerializationContent
to update the object:rename
— Rename theX
andY
properties of the old class definition toX1
andY1
.getValue
— Retrieve the values ofWidth
andX
from the old object and add them to find the value ofX2
. Do the same forHeight
andY
.addNameValue
— Add the new propertiesX2
andY2
and their values.remove
— Remove the old propertiesWidth
andHeight
because they are not part of the new 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.
- Inherit from
dynamicprops
andmatlab.mixin.CustomElementSerialization
. - Implement the static method
modifyIncomingSerializationContent
. This method is called whenever you attempt to load aDynDemo
object. (For more information, see matlab.mixin.CustomElementSerialization.) - As the first step of
modifyIncomingSerializationContent
, callserializedPropertyNames("dynamic")
on the serialized object,sObj
, which is an instance ofmatlab.serialization.ElementSerializationContent
. If the object has no dynamic properties, the object was created using the old definition of theDynDemo
class. Remove the nondynamic version ofTempData
and add it as a dynamic property. - If the object does have a dynamic property, the object was serialized under the new definition, and the object can be deserialized without any additional action.
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
- If you need to customize a superclass private property from the serialization methods of one of its subclasses, use the fully qualified_
className.propName
_ syntax. For example,subclassA
inherits fromsuperclassA
, andsuperclassA
defines a private property namedprivProp
. Use the fully qualified syntax to referenceprivProp
fromsubclasssA
serialization methods.
classdef subclassA < superclassA
properties
prop1
end
methods (Static)
function modifyOutgoingSerializationContent(sObj,obj)
% Access the superclass private property privProp
x = sObj.getValue("superclassA.privProp");
% Access the sublcass property prop1
y = sObj.getValue("prop1");
...
end
end
end - You can use dot notation in place of explicitly using the
getValue
andupdateValue
methods. For example, these two syntaxes are equivalent.
x = sObj.getValue("prop1")
x = sObj.prop1
You cannot use dot notation when accessing a private property of a superclass. Use the explicit methods instead.
Version History
Introduced in R2024b