B4J Library [Class] PHPSerializator

This class makes it easy to serialize objects and primitives to php (serialize/unserialize)

My "inspiration" came from: https://github.com/kayahr/pherialize

Here is a complete list of the conversions performed when serializing B4J/B4A data types and unserializing them in PHP:

B4J/B4A class -> PHP type
Null -> null
Boolean -> boolean
Char -> string
Byte -> int
Short -> int
Int -> int
Long -> int or double (Depending on how large the value is)
Float -> double
Double -> double
String -> string
Array -> array
List -> array
Map -> array

And here is a list of performed conversion when you unserialize PHP data in B4J/B4A.

PHP type -> B4J/B4A class
null -> Null
boolean -> Boolean
int -> Int
double -> Double
string -> String
array -> Map

Simple Example:
B4X:
    Dim phpser As PHPSerializator
    phpser.Initialize
    Dim serialized As String = phpser.Serialize(Array As Int(1,2,3,4))
    Log(serialized)
    Dim i() As Int = phpser.Unserialize(serialized)
    Log(i)

PHPSerializer Class:
B4X:
'https://github.com/kayahr/pherialize
Sub Class_Globals
    Private cCharSet As String
    Private cPos As Int
    Private cData As String
End Sub
Public Sub Initialize()
    cCharSet = "UTF-8"
    cPos = 0
    cData = ""
End Sub
Public Sub getCharset() As String
    Return cCharSet
End Sub
Public Sub setCharset(Charset As String)
    cCharSet = Charset
End Sub
Public Sub Serialize(ObjectToSerialize As Object) As String
    Dim sb As StringBuilder
    sb.Initialize
    SerializeObject(ObjectToSerialize, sb)
    Return sb.ToString
End Sub
Private Sub SerializeObject(ObjectToSerialize As Object, Buffer As StringBuilder)
    If ObjectToSerialize = Null Then
        SerializeNull(Buffer)
    Else If ObjectToSerialize Is String Then
        SerializeString(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Int Then
        SerializeInt(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Short Then
        SerializeInt(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Byte Then
        SerializeInt(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Long Then
        SerializeLong(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Double Then
        SerializeDouble(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Float Then
        SerializeDouble(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Boolean Then
        SerializeBoolean(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is List Then
        SerializeList(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Map Then
        SerializeMap(ObjectToSerialize, Buffer)
    Else If ObjectToSerialize Is Char Then
        SerializeChar(ObjectToSerialize, Buffer)
    Else If IsArray(ObjectToSerialize) Then
        Select GetType(ObjectToSerialize).SubString2(1, 2)
            Case "B"
                SerializeByteArray(ObjectToSerialize, Buffer)
            Case "S"
                SerializeShortArray(ObjectToSerialize, Buffer)
            Case "I"
                SerializeIntArray(ObjectToSerialize, Buffer)
            Case "J"
                SerializeLongArray(ObjectToSerialize, Buffer)
            Case "F"
                SerializeFloatArray(ObjectToSerialize, Buffer)
            Case "D"
                SerializeDoubleArray(ObjectToSerialize, Buffer)
            Case "Z"
                SerializeBooleanArray(ObjectToSerialize, Buffer)
            Case "C"
                SerializeCharArray(ObjectToSerialize, Buffer)
            Case "L"
                SerializeObjectArray(ObjectToSerialize, Buffer)
        End Select
    End If
End Sub
Private Sub SerializeString(Value As String, Buffer As StringBuilder)
    Dim decoded As String = Decode(Value, cCharSet)
    Buffer.Append("s:")
    Buffer.Append(decoded.Length)
    Buffer.Append($":""$)
    Buffer.Append(Value)
    Buffer.Append($"";"$)
End Sub
Private Sub SerializeChar(Value As Char, Buffer As StringBuilder)
    Buffer.Append($"s:1:""$)
    Buffer.Append(Value)
    Buffer.Append($"";"$)
End Sub
Private Sub SerializeNull(Buffer As StringBuilder)
    Buffer.Append("N;")
End Sub
Private Sub SerializeInt(Value As Int, Buffer As StringBuilder)
    Buffer.Append("i:")
    Buffer.Append(Value)
    Buffer.Append(";")
End Sub
Private Sub SerializeLong(Value As Long, Buffer As StringBuilder)
    If Value > = -2147483648 And Value <= 2147483647 Then
        Buffer.Append("i:")
    Else
        Buffer.Append("d:")
    End If
    Buffer.Append(Value)
    Buffer.Append(";")
End Sub
Private Sub SerializeDouble(Value As Double, Buffer As StringBuilder)
    Buffer.Append("d:")
    Buffer.Append(Value)
    Buffer.Append(";")
End Sub
Private Sub SerializeBoolean(Value As Boolean, Buffer As StringBuilder)
    Buffer.Append("b:")
    If Value Then
        Buffer.Append(1)
    Else
        Buffer.Append(0)
    End If
    Buffer.Append(";")
End Sub
Private Sub SerializeByteArray(Value() As Byte, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeInt(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeShortArray(Value() As Short, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeInt(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeIntArray(Value() As Int, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeInt(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeLongArray(Value() As Long, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeLong(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeFloatArray(Value() As Float, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeDouble(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeDoubleArray(Value() As Double, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeDouble(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeBooleanArray(Value() As Boolean, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeBoolean(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeCharArray(Value() As Char, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeChar(Value(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeObjectArray(Value() As Object, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Length)
    Buffer.Append(":{")
    For k = 0 To Value.Length - 1
        SerializeInt(k, Buffer)
        SerializeObject(Value(k), Buffer)
    Next
    Buffer.Append("}")   
End Sub
Private Sub SerializeList(Value As List, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Size)
    Buffer.Append(":{")
    For k = 0 To Value.Size - 1
        SerializeInt(k, Buffer)
        SerializeObject(Value.Get(k), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub SerializeMap(Value As Map, Buffer As StringBuilder)
    Buffer.Append("a:")
    Buffer.Append(Value.Size)
    Buffer.Append(":{")
    For Each key As Object In Value.Keys
        SerializeObject(key, Buffer)
        SerializeObject(Value.Get(key), Buffer)
    Next
    Buffer.Append("}")
End Sub
Private Sub Decode(Encoded As String, Charset As String) As String
    Try
        Dim b() As Byte = Encoded.GetBytes(Charset)
        Return BytesToString(b, 0, b.Length, "ISO-8859-1")
    Catch
        Return Encoded
    End Try
End Sub
Private Sub Encode(Decoded As String, Charset As String) As String
    Try
        Dim b() As Byte = Decoded.GetBytes("ISO-8859-1")
        Return BytesToString(b, 0, b.Length, Charset)
    Catch
        Return Decoded
    End Try
End Sub
Public Sub Unserialize(Data As String) As Object
    cPos = 0
    cData = Decode(Data, cCharSet)
    Return UnserializeObject
End Sub
Private Sub UnserializeObject() As Object
    Dim sType As Char = cData.CharAt(cPos)
    Select sType
        Case "s"
            Return UnserializeString
        Case "i"
            Return UnserializeInt
        Case "d"
            Return UnserializeDouble
        Case "b"
            Return UnserializeBoolean
        Case "N"
            Return UnserializeNull
        Case "a"
            Return UnserializeArray
        Case Else
            Return Null
    End Select
End Sub
Private Sub UnserializeString() As Object
    Dim pos, length As Int
    pos = cData.IndexOf2(":", cPos + 2)
    length = cData.SubString2(cPos + 2, pos)
    cPos = pos + length + 4
    Dim unencoded As String = cData.SubString2(pos + 2, pos + 2 + length)
    Return Encode(unencoded, cCharSet)
End Sub
Private Sub UnserializeInt() As Object
    Dim result As Int
    Dim pos As Int
    pos = cData.IndexOf2(";", cPos + 2)
    result = cData.SubString2(cPos + 2, pos)
    cPos = pos + 1
    Return result
End Sub
Private Sub UnserializeDouble() As Object
    Dim result As Double
    Dim pos As Int
    pos = cData.IndexOf2(";", cPos + 2)
    result = cData.SubString2(cPos + 2, pos)
    cPos = pos + 1
    Return result
End Sub
Private Sub UnserializeBoolean() As Object
    Dim result As Boolean
    result = (cData.CharAt(cPos + 2) = "1")
    cPos = cPos + 4
    Return result
End Sub
Private Sub UnserializeNull() As Object
    cPos = cPos + 2
    Return Null
End Sub
Private Sub UnserializeArray() As Object
    Dim m As Map
    Dim pos, length As Int
    Dim key, value As Object
    pos = cData.IndexOf2(":", cPos + 2)
    length = cData.SubString2(cPos + 2, pos)
    cPos = pos + 2
    m.Initialize
    For k = 0 To length - 1
        key = UnserializeObject
        value = UnserializeObject
        m.Put(key, value)
    Next
    cPos = cPos + 1
    Return m
End Sub
Private Sub IsArray(Value As Object) As Boolean
    Return GetType(Value).StartsWith("[")
End Sub
Public Sub EncodeBase64(Value As String) As String
    Dim su As StringUtils
    Return su.EncodeBase64(Value.GetBytes(cCharSet))
End Sub
Public Sub DecodeBase64(Value As String) As String
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(Value)
    Return BytesToString(b, 0, b.Length, cCharSet)
End Sub
 
Top