B4A Library [B4X] .Net Framework implementation of B4XSerializator

Discussion in 'Additional libraries, classes and official updates' started by Erel, Nov 15, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    B4XSerializator is a great tool that makes it simple to share data between B4A, B4i and B4J. It takes care of converting objects, including complex objects, to bytes and vice versa. (It also has a smaller brother named B4RSerializator that is supported by B4A, B4i, B4J and B4R but is less powerful.)

    Example: https://www.b4x.com/android/forum/threads/network-asyncstreams-b4xserializator.72149/#content

    The attached .Net dll is a .Net implementation of B4XSerializator. You can use it if you need to communicate with a .Net application or server.

    Using it is simple. Create a B4XSerializator object and call ConvertBytesToObject or ConvertObjectToBytes.
    Custom B4X types will be converted to a class named B4XType. B4XType holds the class name and a dictionary (map) with the fields names and values.
    You can also create B4XType instances yourself.

    Tips & Notes

    - The following types are supported: primitives, strings, List<Object>, Dictionary<Object, Object>, arrays of objects, arrays of bytes and B4XType objects. Including combinations of these types (lists of maps with custom types for example).
    - It depends on SharpZipLib. It is included in the zip file. License (allows commercial usage): http://icsharpcode.github.io/SharpZipLib/
    - The c# code is included.

    Attached Files:

    Last edited: Nov 16, 2016
  2. mmieher

    mmieher Active Member Licensed User

    I am trying to convert Erel's .Net_B4XSerializator from C# to VB.NET.
    I know very little about C# and am having a problem with syntax of B4XType
    Private Function readType(o As Object)
    Dim cls As String = readObject(o)
    Dim data As VariantType = readMap(o)
    Return New B4XType(cls, data)    '<===== "Too many arguments to Public Sub New()"  cls is underlined
        End Function
    Public Class B4XType
    Public ClassName As String
    Public Fields As Dictionary(Of Object, Object)
    Public Sub B4XType(ClassName As String, data As Dictionary(Of Object, Object))
    Dim i As Integer = ClassName.LastIndexOf("$")
    If i > -1 Then
                    ClassName = ClassName.Substring(i + 
                ElseIf ClassName.StartsWith(
                    ClassName = ClassName.Substring(
                    ClassName = ClassName.ToLower(New CultureInfo(
                    Me.ClassName = ClassName
                    Me.Fields = data
    End If
    End Sub
    End Class
    Does anyone see what is wrong here?
  3. LucaMs

    LucaMs Expert Licensed User


    Imports System
    Imports System.Collections.Generic
    Imports System.Globalization
    Imports System.IO
    Imports System.Linq
    Imports System.Text
    Imports ICSharpCode.SharpZipLib.Zip.Compression.Streams

    Namespace WpfApplication1

    Public Class B4XSerializator

    Private Const T_NULL As Byte = 0, T_STRING As Byte = 1, T_SHORT As Byte = 2, T_INT As Byte = 3, T_LONG As Byte = 4, T_FLOAT As Byte = 5, T_DOUBLE As Byte = 6, T_BOOLEAN As Byte = 7, T_BYTE As Byte = 10, T_CHAR As Byte = 14, T_MAP As Byte = 20, T_LIST As Byte = 21, T_NSARRAY As Byte = 22, T_NSDATA As Byte = 23, T_TYPE As Byte = 24

    Private br As BinaryReader

    Private bw As BinaryWriter

    Private ReadOnly utf8 As UTF8Encoding

    Public Sub New()
                utf8 = New UTF8Encoding(
    End Sub

    Public Function ConvertBytesToObject(ByVal Bytes As Byte()) As Object
                Using inf 
    As InflaterInputStream = New InflaterInputStream(New MemoryStream(Bytes))
                    br = New BinaryReader(inf)
    Dim ret As Object = readObject()
    Return ret
    End Using
    End Function

    Public Function ConvertObjectToBytes(ByVal Object As Object) As Byte()
    Dim ms As MemoryStream = New MemoryStream()
                Using def 
    As DeflaterOutputStream = New DeflaterOutputStream(ms)
                    bw = New BinaryWriter(def)
    End Using

    Return ms.ToArray()
    End Function

    Private Sub writeObject(ByVal o As Object)
    If o Is Nothing Then
                ElseIf TypeOf o 
    Is Integer Then
                ElseIf TypeOf o 
    Is Double Then
                ElseIf TypeOf o 
    Is Single Then
                ElseIf TypeOf o 
    Is Long Then
                ElseIf TypeOf o 
    Is Byte Then
                ElseIf TypeOf o 
    Is Short Then
                ElseIf TypeOf o 
    Is Char Then
                ElseIf TypeOf o 
    Is Boolean Then
    If(CBool(o), 10))))
                ElseIf TypeOf o 
    Is String Then
    Dim temp As Byte() = utf8.GetBytes(CStr(o))
    0, temp.Length)
                ElseIf TypeOf o 
    Is List(Of Object) Then
    List(Of Object)))
                ElseIf TypeOf o 
    Is Dictionary(Of Object, Object) Then
                    writeMap(CType(o, Dictionary(Of Object, Object)))
                ElseIf o.[
    GetType]().IsArray Then
    If TypeOf o Is Byte() Then
    Dim b As Byte() = CType(o, Byte())
    0, b.Length)
                    ElseIf TypeOf o 
    Is Object() Then
    List(Of Object)(CType(o, Object())))
                        Throw New 
    Exception("Only arrays of bytes or objects are supported.")
    End If
                ElseIf TypeOf o 
    Is B4XType Then
                    writeType(CType(o, B4XType))
                    Throw New 
    Exception("Type not supported: " & o.[GetType]())
    End If
    End Sub

    Private Sub writeMap(ByVal m As Dictionary(Of Object, Object))
    For Each kvp As KeyValuePair(Of Object, Object) In m
    End Sub

    Private Sub writeList(ByVal list As List(Of Object))
    For Each o As Object In list
    End Sub

    Private Function readObject() As Object
    Dim t As Byte = br.ReadByte()
    Dim len As Integer
    Dim b As Byte()
    Select Case t
    Case T_NULL
    Return Nothing
    Case T_INT
    Return readInt()
    Case T_SHORT
    Return readShort()
    Case T_LONG
    Return br.ReadInt64()
    Case T_FLOAT
    Return br.ReadSingle()
    Case T_DOUBLE
    Return br.ReadDouble()
    Case T_BOOLEAN
    Return br.ReadByte() = 1
    Case T_BYTE
    Return br.ReadByte()
    Case T_STRING
                        len = readInt()
                        b = br.ReadBytes(len)
    Return utf8.GetString(b)
    Case T_CHAR
    Return CChar(readShort())
    Case T_LIST
    Return readList()
    Case T_MAP
    Return readMap()
    Case T_NSDATA
                        len = readInt()
    Return br.ReadBytes(len)
    Case T_NSARRAY
    Dim list As List(Of Object) = readList()
    Return list.ToArray()
    Case T_TYPE
    Return readType()
    Case Else
                        Throw New 
    Exception("Unsupported type: " & t)
    End Select
    End Function

    Private Sub writeByte(ByVal b As Byte)
    End Sub

    Private Sub writeInt(ByVal i As Integer)
    End Sub

    Private Function readList() As List(Of Object)
    Dim len As Integer = readInt()
    Dim arr As List(Of Object) = New List(Of Object)(len)
    For i As Integer = 0 To len - 1

    Return arr
    End Function

    Private Function readMap() As Dictionary(Of Object, Object)
    Dim len As Integer = readInt()
    Dim mm As Dictionary(Of Object, Object) = New Dictionary(Of Object, Object)()
    For i As Integer = 0 To len - 1
                    mm(readObject()) = readObject()

    Return mm
    End Function

    Private Function readInt() As Integer
    Return br.ReadInt32()
    End Function

    Private Function readShort() As Short
    Return br.ReadInt16()
    End Function

    Private Function readType() As Object
    Dim cls As String = CStr(readObject())
    Dim data = readMap()
    Return New B4XType(cls, data)
    End Function

    Private Sub writeType(ByVal t As B4XType)
    "_" & t.ClassName)
    End Sub
    End Class

    Public Class B4XType

    Public ReadOnly ClassName As String

    Public ReadOnly Fields As Dictionary(Of Object, Object)

    Public Sub New(ByVal className As String, ByVal data As Dictionary(Of Object, Object))
    Dim i As Integer = className.LastIndexOf("$")
    If i > -1 Then
                    className = className.Substring(i + 
                ElseIf className.StartsWith(
                    className = className.Substring(
                    className = className.ToLower(New CultureInfo(
    End If

                Me.ClassName = className
                Me.Fields = data
    End Sub
    End Class
    End Namespace

    'Service provided by Telerik (www.telerik.com)
    'Conversion powered by Refactoring Essentials.
    'Twitter: @telerik
    'Facebook: facebook.com/telerik
    Star-Dust, mmieher and mindful like this.
  4. mmieher

    mmieher Active Member Licensed User

    Wow! What a fantastic tool! Knowing about this years ago would have saved me thousands of hours. Thank you!
    LucaMs likes this.
  5. LucaMs

    LucaMs Expert Licensed User

    I saw the comment lines on the bottom of the source code converted:
    'Service provided by Telerik (www.telerik.com)
    'Conversion powered by Refactoring Essentials.
    'Twitter: @telerik
    'Facebook: facebook.com/telerik
    "powered by Refactoring Essentials"? Well, Luca AKA Mario :D, search for it, to know what is it ;)


    (I think I will install it soon).
  6. mmieher

    mmieher Active Member Licensed User

    "Using it is simple. Create a B4XSerializator object and call ConvertBytesToObject or ConvertObjectToBytes.
    Custom B4X types will be converted to a class named B4XType. B4XType holds the class name and a dictionary (map) with the fields names and values."

    Ok, I need a little more help here. What exactly is the VB.NET syntax to do the "simple" steps above? All my attempts have failed.
  7. Toky Olivier

    Toky Olivier Member Licensed User

    I ported also @Erel's source of B4XSerializator to Php. I'll post it here when I finish. Now it's working but only with basic types. I'll add Lists, Maps and custom Types.
    Last edited: Aug 15, 2019
    peacemaker likes this.
  8. Toky Olivier

    Toky Olivier Member Licensed User

    Last edited: Aug 20, 2019
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice