B4J Question Serializing/deserialize custom types with JSON

wl

Well-Known Member
Licensed User
Longtime User
Hello,


I'm trying to make a generic serialize/deserialize mechanism through JSON (to serialize/deserialize custom types). It seems this is not working. Am I doing something wrong or is this generally impossible ?

This is an example

B4X:
...
Sub Process_Globals
    Type CustomerType (FirstName As String, LastName As String)
End Sub
...

    Dim cst As CustomerType, cstList As List
    cst.Initialize
    cst.FirstName = "John"
    cst.LastName = "Doe"
  
    cstList.Initialize
    cstList.Add(cst)
  
    'Serialize list of customer (containing a single customer)  
    Dim gen As JSONGenerator
    gen.Initialize2(cstList)
    Log (gen.ToString)
  
    'Deserialize the JSON string
    Dim pars As JSONParser
    pars.Initialize(gen.ToString)
    Dim customers As List = pars.NextArray
    Dim customer As CustomerType = customers.Get(0)
      
    Log(customer.FirstName)

the last log causes:
java.lang.RuntimeException: Field: FirstName not found in: java.lang.String
 

wl

Well-Known Member
Licensed User
Longtime User
Thanks. That's whay I was doing meanwhile (hence my other question).

I wrap the entire object in a Base64Encoded string, so I can pass it to the webserver using a POST request.
 
Upvote 0
Top