Android Question Help with B4XSerializator

Harris

Expert
Licensed User
Longtime User
I have a map object and want the value of the key to be another map... Do this all the time... no biggie..

But, I want to store this map in a SQLite table field (of type - TEXT), and later send record to my server and insert into MySQL table feild.
I think I understand it must be serialized to accomplish the feat.

I have studied Erels "new" KeyValueStore to try and learn how.

Questions:
Does one serialize each value - or does one serialize the entire map? (containing the value map with strings for their values) - very small in size....
Also, does the table field type for value have to be NONE, as with the CreateTable method in his example?




B4X:
Sub GetStopVeh() As ResumableSub
    
    Dim m, v As Map
    m.Initialize
    v.Initialize
    
    v.Put("etcon",etcon.Text)
    v.Put("etfreight",etfreight.Text)
    v.Put("etfuel",etfuel.Text)
    v.Put("etlight",etlight.Text)
    v.Put("etsurface",etsurface.Text)
' other side   
    v.Put("etothcon",etothcon.Text)
    v.Put("etothfreight",etothfreight.Text)
    v.Put("etothfuel",etothfuel.Text)
    v.Put("etothlight",etothlight.Text)
    v.Put("etothsurface",etothsurface.Text)


    Log("What key size: "&v.Size)

    For Each key As String In v.Keys
        Log("What key: "&key)
        Dim myser As B4XSerializator
        myser.ConvertObjectToBytes(  v.Get(key) )   ' ????
' this waited forever - no looping
'        Wait For (myser) myser_ObjectToBytes (Success As Boolean, Bytes() As Byte)
'        If Success Then
'            sql1.AddNonQueryToBatch("INSERT OR REPLACE INTO main VALUES(?, ?)", Array(key, Bytes))
'            Log("Each serialize object: " & v.Get(key))

'        Else
'            Log("Failed to serialize object: " & v.Get(key))
'        End If
        Sleep(100)
    Next

    
    m.Put("stopveh",v)  '???
    
    Return m
    
    
    
End Sub
B4X:
 

DonManfred

Expert
Licensed User
Longtime User
Does one serialize each value - or does one serialize the entire map?
It makes not sence to build a map and only serialize a Value in it.
B4X:
Dim myser As B4XSerializator
dim serialized() as Byte = myser.ConvertObjectToBytes(v)
' Store serialized in the Database after you Base64encode it (textfield) or in a blob (serialized bytes)

B4X:
    Dim v As Map
    v.Initialize
    
    v.Put("etcon","etcon.Text")
    v.Put("etfreight","etfreight.Text")
    v.Put("etfuel","etfuel.Text")
    v.Put("etlight","etlight.Text")
    v.Put("etsurface","etsurface.Text")
    ' other side
    v.Put("etothcon","etothcon.Text")
    v.Put("etothfreight","etothfreight.Text")
    v.Put("etothfuel","etothfuel.Text")
    v.Put("etothlight","etothlight.Text")
    v.Put("etothsurface","etothsurface.Text")

    Dim ser As B4XSerializator
    Dim serialized() As Byte = ser.ConvertObjectToBytes(v)
    Dim bc As ByteConverter
    Dim value2storeindb As String = bc.HexFromBytes(serialized)
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
When I:

Dim myser As B4XSerializator
Dim serialized As Byte() = myser.ConvertObjectToBytes( v )

When I do as above, I get (error, warning?) - "Array dimensions should follow the variable name, not the type"

B4X:
Error compiling program.
Error description: Array dimensions should follow the variable name, not the type.
Error occurred on line: 632
Dim serialized As Byte() = myser.ConvertObjectToBytes(  v )
Word: byte
B4X:



What the heck?

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Should be:
B4X:
Dim myser As B4XSerializator
Dim serialized() As Byte = myser.ConvertObjectToBytes( v )
Little wonder Don didn't pipe up... He already showed me the correct way - now you confirmed it, and I see it... da...
Often can't see the forest for the trees....

Now to carry on and see what I can corrupt!
Thx
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
With a lot of your help, I got it!
Rather trivial when pointed in the right direction - and understanding the difference between TEXT and BLOB requirements (Thanks Don).

Below is the main section of an entry form for collecting user data (when they encounter caribou crossing the haul road).
Many details are required so I find a map of maps the perfect solution - providing it can be save into a DB and later extracted for reporting.

main1.jpg





Here, people (truck drivers) enter the values encountered.

input1.jpg







B4X:
' start with a base map - Put( 6, GetStopVeh ) accepts the hex / serialized (string) data
        tmap.Put(1,pk+i) ' pk
        tmap.Put(2,pk)  'mastid
        tmap.Put(3,i+1)
        tmap.Put(4,0) '       Use 6,7,8 for panel values in a serialized map...
        tmap.Put(5,0)
        tmap.Put(6,GetStopVeh)   ' stopped vehicles map
        tmap.Put(7,"")           ' herd size map
        tmap.Put(8,"")           ' dir_dist of herd
        tmap.Put(9,0)
        tmap.Put(10,0)
        tmap.Put(11,DefCM.Company_id)
      
        Log(" Get Key 6 values as Hex: "&tmap.Get( 6))
  
        Dim bc As ByteConverter
        Dim val() As Byte  = bc.HexToBytes(tmap.Get(6))
          
        Dim myser As B4XSerializator
        Dim serr As Object = myser.ConvertBytesToObject( val )
      
        Dim mm As Map
        mm.Initialize
        mm = serr
      
        Log(" Show MyMap of maps...: "&mm)


Sub GetStopVeh() As Object
  
    Dim m, v As Map
    m.Initialize
    v.Initialize
  
    v.Put("etcon",etcon.Text)
    v.Put("etfreight",etfreight.Text)
    v.Put("etfuel",etfuel.Text)
    v.Put("etlight",etlight.Text)
    v.Put("etsurface",etsurface.Text)
' other side 
    v.Put("etothcon",etothcon.Text)
    v.Put("etothfreight",etothfreight.Text)
    v.Put("etothfuel",etothfuel.Text)
    v.Put("etothlight",etothlight.Text)
    v.Put("etothsurface",etothsurface.Text)
    v.Put("rbstop1",rbstop1.Checked)
    v.Put("rbstop2",rbstop2.Checked)

    For Each vals As String In v.Values
        Log(" Each Value: "&vals)
    Next
    m.Put("stopveh",v)
  
    Dim myser As B4XSerializator
    Dim serialized() As Byte = myser.ConvertObjectToBytes( m )
    Dim bc As ByteConverter
    Dim val As String = bc.HexFromBytes(serialized)
    Log(" What is val obj: "&val)
    Return val
  
End Sub
B4X:

' the logs....
Each Value: 123
Each Value:
Each Value: 9
Each Value: 2
Each Value: 2
Each Value: 456
Each Value:
Each Value: 13
Each Value: 1
Each Value: 0
Each Value: true
Each Value: false

What is val obj: 789C55CF510E82300C06E056A313C3230771434C3C8E92E24C8833A3787ED6CA045F9AEDCBDABFAB1000D0A43270787FC857A5C02E15E236BC709B4ED6D558A874919E0FCFD204B8FFD2483D0A5C750C71AF2F44DCDC358CB1BBB534DB412DB0CFE3CFCD05CB8C7F09C54F256423ABD478CCB804D9A57F9D75D28DE25DBE660DAE6ECEC0045E6030A9


Get Key 6 values as Hex: 789C55CF510E82300C06E056A313C3230771434C3C8E92E24C8833A3787ED6CA045F9AEDCBDABFAB1000D0A43270787FC857A5C02E15E236BC709B4ED6D558A874919E0FCFD204B8FFD2483D0A5C750C71AF2F44DCDC358CB1BBB534DB412DB0CFE3CFCD05CB8C7F09C54F256423ABD478CCB804D9A57F9D75D28DE25DBE660DAE6ECEC0045E6030A9


' show what was converted back - the map...
Show MyMap of maps...: (MyMap) {stopveh={etcon=123, etfreight=, etfuel=9, etlight=2, etsurface=2, etothcon=456, etothfreight=, etothfuel=13, etothlight=1, etothsurface=0, rbstop1=true, rbstop2=false}}

Now when it gets to my server, B4J can accomplish the extraction (back into map) and reporting will be easy.
The alternative (123,456,33,44,0,0,9) method is ugly and very cumbersome.

Thanks
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
Ok, I shall try it.
I need to create a final app to put on all trucks while I am here.
However, with appupdater working (again for OS 9, sdk 28) - I am not worried cause I can apply an update anytime...

Thanks
 
Upvote 0
Top