B4J Code Snippet Python B4XSerializator

If you ever need to send B4X objects from Python service to a B4X app/service or vice versa, this might be helpful. By using the opensource code of B4J and B4A on GitHub, along with some assistance from ChatGPT o1 model, I created a Python class to make it possible.

Yes, you should be using JSON or keep concerns separated, B4X objects for B4X clients or python with existing industry standards. But since it is possible, why not, at least for testing ideas.

Short example:
python - B4XSerializator:
from B4XSerializator import B4XSerializator
serializer = B4XSerializator()
my_dict = {...}
bytes_data = serializer.convert_object_to_bytes(my_dict)


Full example:
The following is a Flask server that serves a B4X map at the /test endpoint, along with a B4J client that requests this object by calling the API.
Flask app.py:
from flask import Flask, Response
from B4XSerializator import B4XSerializator

app = Flask(__name__)


@app.route('/test', methods=['GET'])
def test_b4x_serializator():
    serializer = B4XSerializator()
    my_dict = {
        "key1": "value1",
        "key2": False,
        "testKey": 123,
        "nestedList": [1, 2, 3],
        "nestedDict": {"innerKey": "innerValue"},
        "byteData": b'\x00\x01\x02',
        "floatVal": 3.14,
        "longInt": 1234567890123456789,
        "shortInt": 12345,
        "byteInt": -128,
        "nullValue": None
    }
    bytes_data = serializer.convert_object_to_bytes(my_dict)

    # Return the bytes as a response
    return Response(bytes_data, content_type='application/octet-stream')


if __name__ == '__main__':
    app.run(debug=True)

B4J Test Client:
Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("http://localhost:5000/test")
    StartMessageLoop
End Sub

Sub JobDone(j As HttpJob)
    Log("Job succes: "&j.Success)
    Dim ret As Map
    ret.Initialize
    If j.Success Then
        Try
            Dim ser As B4XSerializator
            Dim respMap As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(j.GetInputStream))
            Log(respMap)

            For Each k As String In respMap.Keys
                If respMap.Get(k) = Null Then
                    Log(k & ": is null")
                Else
                    Log(k & ": " & GetType(respMap.Get(k)))
                End If
              
            Next
          
        Catch
            Log(LastException)
        End Try
    End If
    StopMessageLoop
End Sub

DISCLAIMER: This has only been tested briefly in a local environment and has not yet been tested in production.
 

Attachments

  • b4j_client.zip
    1 KB · Views: 27
  • B4XSerializator.zip
    2 KB · Views: 26
Cookies are required to use this site. You must accept them to continue using the site. Learn more…