Some considerations when creating objects with >1 level of serial objects

Primary tabs

Let's say we have two serial classes, one as a property of another:

Class test.Serial Extends %SerialObject
{
Property Serial2 As test.Serial2;
}

Class test.Serial2 Extends %SerialObject
{
Property Property As %String;
}

And a persistent class, that has a property of test.Serial type:

Class test.Persistent Extends %Persistent
{

Property Datatype As %String;

Property Serial As test.Serial;

}

So it's a serial, inside a serial, inside a persistent object.

When you have an object structure with more than one level of serial objects, it's important to remember, that if you modify a parent object via SQL, serial objects with serial objects would become initialized.

Consider this method of test.Persistent class:

/// Do ##class(test.Persistent).Test()
ClassMethod Test()
{
    Do ..%KillExtent()
    Set Obj = ..%New()
    Set Obj.Datatype = 1
    Write $System.Status.GetErrorText(Obj.%Save())
    Kill
    
    Set Obj = ..%OpenId(1)
    Do Obj.Serial.%GetSwizzleObject(,.SerialBefore)
    Kill (SerialBefore)
    
    &sql(UPDATE test.Persistent SET Datatype = 2)
    
    Set Obj = ..%OpenId(1)
    Do Obj.Serial.%GetSwizzleObject(,.SerialAfter)
    
    Zw SerialBefore,SerialAfter
}

In this method we compare serialization of serial object with a serial object  before and after the main persistent object gets updated via SQL. Here's the output:

>Do ##class(test.Persistent).Test()
SerialBefore=""
SerialAfter=$lb($lb($lb("")),"test.Serial")

Note, that while they are the same objects (all properties are empty), the serialization changes.

The code on GitHub.

I would like to thank Dmitry Zasypkin and Alexander Koblov for their help in figuring this out.

Replies

@Eduard Lebedyuk 
I have classes with the similar structure in your test.Serial class I have Two properties  Serial1 and Serial2 as test.Serial1 and 2 respectively and i would like to print out XML maybe populate  Serial 1 and have Serial2 as an empty object and Leave it out when printing.How do I controll that ?,as it stands it prints out the empty elements even when ignore null is set.Please not all is the same as above so I am printing from a persistent Level.The difference being I have two serial objects properties In my test.Serial equavalent

Please consider posting your question with sample code to reproduce your issue as a separate question.