60 lines
1.6 KiB
Plaintext
60 lines
1.6 KiB
Plaintext
Class Serialize.Example Extends %SerialObject
|
|
{
|
|
|
|
ClassMethod Main()
|
|
{
|
|
Do ..Save("/temp/objects.dat")
|
|
Do ..Load("/temp/objects.dat")
|
|
Quit
|
|
}
|
|
|
|
ClassMethod Save(pFilename As %String)
|
|
{
|
|
// creating objects of base class
|
|
Set emp1 = ##class(Employee).%New(.id, "Maintenance", "Fritz Schmalstieg")
|
|
Set emp2 = ##class(Employee).%New(.id, "Maintenance", "John Berry")
|
|
Set emp3 = ##class(Employee).%New(.id, "Repair", "Pawel Lichatschow")
|
|
Set emp4 = ##class(Employee).%New(.id, "IT", "Marian Niculescu")
|
|
|
|
// creating objects of derived class
|
|
Set worker1 = ##class(Worker).%New(.id, "Maintenance", "Laurent Le Chef", 20)
|
|
Set worker2 = ##class(Worker).%New(.id, "IT", "Srinivan Taraman", 55.35)
|
|
|
|
// put objects into collections
|
|
Set example = ..%New()
|
|
Set sc = example.Employees.Insert(emp1)
|
|
Set sc = example.Employees.Insert(emp2)
|
|
Set sc = example.Employees.Insert(emp3)
|
|
Set sc = example.Employees.Insert(emp4)
|
|
Set sc = example.Workers.Insert(worker1)
|
|
Set sc = example.Workers.Insert(worker2)
|
|
|
|
// serialize the data and save to a file
|
|
Set sc=example.%GetSwizzleObject(,.oid)
|
|
Set fs=##class(%Stream.FileBinary).%New()
|
|
Set fs.Filename=pFilename
|
|
Set sc=fs.Write(oid)
|
|
Set sc=fs.%Save()
|
|
Quit
|
|
}
|
|
|
|
ClassMethod Load(pFilename As %String)
|
|
{
|
|
// read serialized data from file
|
|
Set fs=##class(%Stream.FileBinary).%New()
|
|
Set fs.Filename=pFilename
|
|
Set oid=fs.Read(.len, .sc)
|
|
|
|
// open the example object
|
|
Set example = ..%Open(oid,, .sc)
|
|
Do example.Employees.GetAt(1).Print()
|
|
Do example.Employees.GetAt(3).Print()
|
|
Do example.Workers.GetAt(2).Print()
|
|
Quit
|
|
}
|
|
|
|
Property Employees As list Of Employee;
|
|
Property Workers As list Of Worker;
|
|
|
|
}
|