B4J Question This logical conversion won't work. Why?

LWGShane

Well-Known Member
Licensed User
Longtime User
After thinking things through, I'm having to change my thinking of how I want to connect to MySQL databases in my upcoming library. PHP would be the logical & user friendly way to go, but I'm having problems with trying to convert a string to bytes, those bytes to an object and then the other way around. The following code is completely logical however it fails. I'm doing this so the MySQL class will be nearly identical to the local DB class. Any ideas as to why it fails?

B4X:
Public Sub Testing As String
    Dim Hello As String = "Hello 12345"
    Dim Input() As Byte = Conv.StringToBytes(Hello, "UTF-8")
    Dim MainObject As Object = Ser.ConvertBytesToObject(Input)
    Dim Output() As Byte = Ser.ConvertObjectToBytes(MainObject)
    Return Conv.StringFromBytes(Output, "UTF-8")
End Sub
 

fixit30

Active Member
Licensed User
Longtime User
B4XSerializator will only work with Byte Arrays created with it.

This code works as expected.

B4X:
Public Sub Testing As String
    Dim Hello As String = "Hello 12345"
    Dim Input() As Byte = ser.ConvertObjectToBytes(Hello)
    Dim MainObject As Object = ser.ConvertBytesToObject(Input)
    Dim Output() As Byte = ser.ConvertObjectToBytes(MainObject)
    Return ser.ConvertBytesToObject(Output)
End Sub
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
Code doesn't work when used in a HTTPJob like way, all it does is output the Object without any serialization. Object serialization is required to maintain feature polarity between a local and remote database.

B4X:
Public Sub Write
    Dim WO As Object = "ColdBlueLava"
    Dim M As Map
    M.Initialize
    M.Put("1", Ser.ConvertObjectToBytes(WO))
    File.WriteMap(File.DirTemp, "123.txt", M)
End Sub

Public Sub Read As Object
    Dim RM As Map = File.ReadMap(File.DirTemp, "123.txt")
    Dim RMO As Object = RM.Get("1")
    Dim Input() As Byte = Ser.ConvertObjectToBytes(RMO)
    Dim MainObject As Object = Ser.ConvertBytesToObject(Input)
    Dim Output() As Byte = Ser.ConvertObjectToBytes(MainObject)
    Return Ser.ConvertBytesToObject(Output)
End Sub
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
@Erel - Awesome! Tested it and now I'll be able to use the PHP method for MySQL without sacrificing compatibility between local & remote; KVS v3 is now a go.
 
Last edited:
Upvote 0
Top