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.
[CODE lang="python" title="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)[/CODE]
[CODE lang="b4x" title="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[/CODE]