Android Question JSON - create from list of objects

PHB2

Member
Licensed User
Longtime User
It appears as though I am struggling today with JSON...

I have created a test class, with the following class globals:

B4X:
Sub Class_Globals
    Dim GUID As String
    Dim listName As String
    Dim mainItemType As String
    Dim createdDate As String
    Dim ownerGUID As String
    Dim transactionGUID As String
    Dim transactionType As String
    Dim fromUserGUID As String
    Dim transactionDate As String
    Dim notes As String
    Dim summary As String
    Dim success As Boolean
    Dim errMsg As String
End Sub

I then create a couple of test instances of my class, and add them to a list:

B4X:
    Dim list As List
    list.Initialize
    Dim x As TestClass
    x.Initialize
    x.GUID = "1"
    list.Add( x )
    Dim xx As TestClass
    xx.Initialize
    xx.GUID = "2"
    list.Add( xx )
    Dim jg As JSONGenerator
    jg.Initialize2( list )
    json = jg.ToString

What I get in the resulting JSONis a mish-mash of the class variables, but also mention of other classes in my b4a project, which I have no idea why they are there( why for example is dateutils serialized?):


["[createddate=, crypt=null, dateutils=null\n, errmsg=, fromuserguid=, guid=1\n, httputils=null, httputils2service=null, httputilsservice=null\n, listname=, main=null, mainitemtype=\n, manageexercisesactivity=null, manageroutinesactivity=null, notes=\n, ownerguid=, profiledetailsactivity=null, profilehomeactivity=null\n, registeractivity=null, starter=null, statemanager=null\n, success=false, summary=, transactiondate=\n, transactionguid=, transactiontype=]","[createddate=, crypt=null, [B]dateutils[/B]=null\n, errmsg=, fromuserguid=, guid=2\n, httputils=null, httputils2service=null, httputilsservice=null\n, listname=, main=null, mainitemtype=\n, manageexercisesactivity=null, manageroutinesactivity......

3 questions:

Why are the non class variables serialized in the JSON?
Can I set a marker against each class variable that i want to be serialized?
Why does the debugger chop off the end of the variable data I am watching and put ..... at the end? - it makes it difficult to extract large text chunks from variables to see what is going on!
 

DonManfred

Expert
Licensed User
Longtime User
You can not serialize a Class.

You should create a method in your class which returns a map with all fields/properties needed by the class.
And your class needs a 2nd initiaize method to set all fields needed by the given map.

Serialize: myclass.GetMap and then add the map to the jsongenerator.

DeSerialize: myclass.initialize2(values as Map) ' Here you set all fields/properties by using the given Map) to initialize the class to have the "saved state"
 
Last edited:
Upvote 0
Top