B4J Question Unable to serialize my class Object ?

aregan

Member
Licensed User
Hi,

I'm new to B4J so this is probably a really simple question.

I'm writing my first B4J Server process to with handlers to accept requests from by B4A app and send back lists of records which are available in a list of class objects. The class with its public members are shared in both my client and server apps.

I'm hoping to simply serialize it in the server and send it back to the app to deserialize.

So in my server handler class handle function, I've created an instance of the B4XSerializator and called the ConvertObjectToBytes function.

If I simply pass a string type to the ConvertObjectToBytes if works.
If I pass my class object instance to the it I get an error "Cannot serialize object"
Both are declared in the Class Globals
The class only has some basic public members of primitive types declared in it.

Any ideas why?




B4X:
Sub Class_Globals
    Private mreq As ServletRequest 'ignore
    Private mresp As ServletResponse 'ignore

    Public lstTasks As List
    
    Public strSample As String
    Public obj As clsIncident

End Sub

Public Sub Initialize

End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    
    
    
    Private serializer As B4XSerializator
    
    
    mreq = req
    mresp = resp
    
    
    lstTasks = mDatabase.GetTaskList()
    lstTasks.Initialize
    
    Dim bBytes() As Byte
    obj.Initialize(1,"123","123")
    
    strSample = "TEST"
    
    'This works
    bBytes = serializer.ConvertObjectToBytes(a)
    
    'This throws the error
    bBytes = serializer.ConvertObjectToBytes(obj)


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

B4X:
Sub Class_Globals

    
    Public ID As Long
    Public OpenedTime As Long 
    Public IncidentStatusID As Long
    
    
    Public AcceptedTime As Long
    Public ExpectedOnSiteTime As Long
    Public OnSiteTime As Long
    Public ExpectedOffSiteTime As Long
    Public OffSiteTime As Long
    
    Public IncidentTypeID As Long
    Public Post As String
    Public PostAddress As String
    
    Public ClosedTime As Long
    Public DocketNumber As String
    Public KeypointsClocked As Boolean
    Public IncidentDescription As String
    Public Observations As String
    Public ActionTaken As String
    Public ZonesReportedByMonitoringStation As String
    Public ReasonForActivation As String
    Public FurtherActionRequired As Boolean
    Public FurtherAction As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (parID As Long, parPostName As String, parPostAddress As String)
    
    ID = parID
    Post = parPostName
    PostAddress = parPostAddress
    
End Sub
 

aregan

Member
Licensed User
Create a type instead of that class and declare it in Main - Process_Globals.


Thanks LucaMs

Are classes just not supported for serializing ? I looked in the spec and it mentioned the various types that could be serialized. It mentioned OBJECTS could be serialized. I assumed (but maybe incorrectly) that my own defines class objects could be serialized.

Thanks
Alan
 
Upvote 0
Top