Serialize / Deserialize Object

Hi, i have a SDK that return me a Variant as Object Type.
I need to save this in a database

Any example code to create function to serialize / deserialize its apreciated

Thank You for your help.

Xojo has no built-in way to serialize arbitrary objects. You’ll need to write your own routines to serialize and deserialize all Xojo objects.

Any example please?

You’ll need to have a way of reading the properties of the object, storing them in a serial structure such as a string or a memory block, and then a routine that instantiates a new instance of the object, reads the properties out of the serial structure and places them into the corresponding properties of the object.

An example would be fairly involved. Because each object’s structure is different - i.e., they all have different property definitions - you can’t write a generic routine that handles all objects. Each serialize/deserialize pair is specific to the type of object it is designed for.

Although this is a common Xojo task and I’ve done it innumerable times, I don’t have a convenient example project for you to look at. Perhaps another forum member will have such a beast at the ready.

this is the serialize class that come with the SDK and its working, my problem is with the deserialize class that return empty.

Serialize class

If mThis = Nil Then Raise New NilObjectException
Dim func As New Serialize_Func1(mThis.Ptr( 0 ).Ptr(28 ))
Dim resultCode As Integer
Dim Return_pRawData_Param As Ptr
Dim pRawData_Param_MB As New MemoryBlock(16)
Return_pRawData_Param = pRawData_Param_MB
resultCode = func.Invoke(mThis, Return_pRawData_Param)
If resultCode = 0 Then
Dim retVal As Variant = COM.VARIANTToRBVariant(Return_pRawData_Param)
COM.FreeVARIANT(Return_pRawData_Param)
Return retVal
Else // Throw Exception
MsgBox "Failed on Serialize error code "+ str(resultCode)
//Raise New COM.COMException(“Failed on Serialize”, resultCode)
End If

Deserialize class

Deserialize(RawData_Param As Variant)
If mThis = Nil Then Raise New NilObjectException
Dim func As New Deserialize_Func1(mThis.Ptr( 0 ).Ptr(32 ))
Dim resultCode As Integer
Dim Local_RawData_Param As MemoryBlock = COM.RBVariantToVARIANT(RawData_Param)
resultCode = func.Invoke(mThis, Local_RawData_Param)
COM.FreeVARIANT(Local_RawData_Param)
If resultCode = 0 Then
Else // Throw Exception
MsgBox "Failed on Deserialize error code "+ str(resultCode)
//Raise New COM.COMException(“Failed on Deserialize”, resultCode)
End If

“this is the serialize class that come with the SDK and its working, my problem is with the deserialize class that return empty.”

Doesn’t sound like it’s working. :slight_smile:

Anyway, it’s impossible to tell from this code what’s going on. It calls out to external functions, which we can’t see. Offhand, though, I would point out that the deserialize code has no Return statement, which is required to have a function return a value (otherwise, you get the default value for the return type, which is nil for objects, “” for strings, etc.).