Object serialization: Difference between revisions
Content added Content deleted
(added Caché ObjectScript) |
|||
Line 422: | Line 422: | ||
Wanda, id=7 is alive |
Wanda, id=7 is alive |
||
Kiki, id=3 is dead</pre> |
Kiki, id=3 is dead</pre> |
||
=={{header|Caché ObjectScript}}== |
|||
<lang cos>Class Serialize.Employee Extends %SerialObject |
|||
{ |
|||
Method %OnNew(ByRef pId As %Integer = 0, pDepartment As %String, pName As %String) As %Status |
|||
{ |
|||
Do ..IDSet(pId) |
|||
Set pId=pId+1 |
|||
Do ..DepartmentSet(pDepartment) |
|||
Do ..NameSet(pName) |
|||
Quit $$$OK |
|||
} |
|||
Method Print() |
|||
{ |
|||
Write "[", ..%ClassName(), "]", ! |
|||
Write "- ID: "_..IDGet(), ! |
|||
Write "- Name: "_..NameGet(), ! |
|||
Write "- Department: "_..DepartmentGet(), ! |
|||
Quit |
|||
} |
|||
Property ID As %Integer [ Private ]; |
|||
Property Name As %String [ Private ]; |
|||
Property Department As %String [ Private ]; |
|||
}</lang> |
|||
<lang cos>Class Serialize.Worker Extends Employee |
|||
{ |
|||
Method HourlyPaySet(pHourlyPay As %Numeric) As %Status [ ServerOnly = 1 ] |
|||
{ |
|||
Set i%HourlyPay=$Select(pHourlyPay<0: 0, 1: pHourlyPay) |
|||
Quit $$$OK |
|||
} |
|||
Method %OnNew(ByRef pId As %Integer = 0, pDepartment As %String, pName As %String, pHourlyPay As %Numeric) As %Status |
|||
{ |
|||
Do ..HourlyPaySet(pHourlyPay) |
|||
Quit ##super(.pId, pDepartment, pName) |
|||
} |
|||
Method Print() |
|||
{ |
|||
Do ##super() |
|||
Write "- Hourly Pay: ", $FNumber(..HourlyPayGet(), ",", 2), ! |
|||
Quit |
|||
} |
|||
Property HourlyPay As %Numeric [ Private ]; |
|||
}</lang> |
|||
<lang cos>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; |
|||
}</lang> |
|||
{{out|Examples}} |
|||
<pre> |
|||
USER>Do ##class(Serialize.Example).Main() |
|||
[Employee] |
|||
- ID: 0 |
|||
- Name: Fritz Schmalstieg |
|||
- Department: Maintenance |
|||
[Employee] |
|||
- ID: 2 |
|||
- Name: Pawel Lichatschow |
|||
- Department: Repair |
|||
[Worker] |
|||
- ID: 5 |
|||
- Name: Srinivan Taraman |
|||
- Department: IT |
|||
- Hourly Pay: 55.35 |
|||
</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |