json.Serializer.VariableToJSON
Serializes a Genero BDL variable into JSON using a JSONWriter object.
Syntax
json.Serializer.VariableToJSON(
var AnyType,
json json.JSONWriter )
- var can be of various kind of types.
- json is a
JSONWriter
object.
Usage
This method serializes a Genero BDL variable into JSON using a JSONWriter object.
In case of error, the method throws an exception and sets the
status
variable. Depending on the error, a human-readable description of the
problem is available in the sqlca.sqlerrm
register. See Error handling in GWS calls (status).
Example
IMPORT json
MAIN
DEFINE writer json.JSONWriter
DEFINE var RECORD
cust_num INTEGER,
cust_name VARCHAR(30),
order_ids DYNAMIC ARRAY OF INTEGER
END RECORD
LET var.cust_num = "2735"
LET var.cust_name = "McCarlson"
LET var.order_ids[1] = 234
LET var.order_ids[2] = 3456
LET var.order_ids[3] = 24656
LET var.order_ids[4] = 34561
TRY
DISPLAY "START test"
LET writer = json.JSONWriter.Create()
CALL writer.setOutputCharset("UTF-8")
CALL writer.writeTo("toto1.json")
CALL writer.startJson()
CALL json.Serializer.VariableToJSON(var, writer) # serialize var into the file
CALL writer.endJson()
CALL writer.close()
CATCH
DISPLAY "JSON Serializer ERROR: shouldn't raise error :",
status || " " || sqlca.sqlerrm
EXIT PROGRAM -1
END TRY
DISPLAY "END test ok"
END MAIN
Output:
{"cust_num": 2735,"cust_name": "McCarlson","order_ids": [234,3456,24656,34561]}