[Gambas-user] oibject serialization, how to do?

Christof Thalhofer chrisml at deganius.de
Mon Jul 16 21:16:41 CEST 2018


Am 16.07.2018 um 16:30 schrieb PICCORO McKAY Lenz:

> any that can help in serialization on gambas?

I am doing serialization of classes that I call "Models". These classes
inherit a class called CModel, which defines the interfaces for
serialization.

A Model contains only read/write properties of types like string,
integer, float and boolean, which can be serialized easily via JSON.

The serialization goes like that:

In CModel.class there are these two functions:

----------------------------------------------------------------

' Gambas class file

'' Creates a JSon string containing the properties and content
'' of the model.
'' Symbol names beginning with "_" are excluded.

Public Function GetModelAsJson() As String

    Dim symname As String
    Dim col As New JSONCollection
    Dim Value As Variant

    For Each symname In Object.Class(Me).Symbols
        If Not $NotSyms.Exist(symname) Then
            If Left(symname, 1) <> "_" Then
                Try Value = Object.GetProperty(Me, symname)
                If IsNull(Value) Then
                    col.Add(JSON.Null, symname)
                Else
                    col.Add(Value, symname)
                Endif
            Endif
        Endif
    Next

    Return JSON.Encode(col)

End

' The reverse function. Fill the model from a JSON string:

Public Function FillModelFromJson(Value As String)

    Dim col As JSONCollection
    Dim Element As Variant

    col = JSON.Decode(value, True)
    For Each Element In col
        Object.SetProperty(Me, col.Key, Element)
    Next

End

----------------------------------------------------------------
A model looks like that (ModelTest.class):
----------------------------------------------------------------

' Gambas class file

Inherits CModel

' -------------------------- Strings
Property MyName As String
Private $MyName As String

' -------------------------- Integers
Property MyInt As Integer
Private $MyInt As Integer

Private Function MyName_Read() As String

    Return $MyName

End

Private Sub MyName_Write(Value As String)

     $MyName = Value

End

Private Function MyInt_Read() As Integer

    Return $MyInt

End

Private Sub MyInt_Write(Value As Integer)

     $MyInt = Value

End

----------------------------------------------------------------
And can be used like so:
----------------------------------------------------------------

Dim Model1 as New ModelTest
Dim Model2 as ModelTest
Dim sJson as String

With Model1
	.MyName = "Piccoro"
	.MyInt = 888
End With

'serialize
sJson = Model1.GetModelAsJson()

Model2 = New ModelTest
Model2.FillModelFromJson(sJson)

'Now Model2 is a identical copy of Model1
----------------------------------------------------------------


Alles Gute

Christof Thalhofer

-- 
Dies ist keine Signatur

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20180716/58b73aed/attachment.sig>


More information about the User mailing list