json.Serializer.JSONToVariable

Serializes entries contained in a JSONReader object into a Genero BDL variable.

Syntax

json.Serializer.JSONToVariable(
  json json.JSONReader, 
  var AnyType )
  1. json is a JSONReader object.
  2. var can be of various kind of types.

Usage

The JSONToVariable() method fills the variable passed as parameter with the corresponding values defined in the JSON object.

The destination record must have the same structure as the JSON source data. For more details see JSON support.

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 cust_rec RECORD
               cust_num INTEGER,
               cust_name VARCHAR(30),
               order_ids DYNAMIC ARRAY OF INTEGER
           END RECORD
    DEFINE i INTEGER
    DEFINE reader json.JSONReader
    TRY
        DISPLAY "START test"
        LET reader = json.JSONReader.Create()
        CALL reader.setInputCharset("UTF-8")
        CALL reader.readFrom("toto1.json")
        CALL reader.next()
        CALL json.Serializer.JSONToVariable(reader, cust_rec)
        CALL reader.close()
        DISPLAY "Cust name:", cust_rec.cust_name
        FOR i = 1 TO cust_rec.order_ids.getLength()
            DISPLAY "Order id:", cust_rec.order_ids[i]
        END FOR
    CATCH
        DISPLAY "JSON Serializer ERROR: shouldn't raise error :",
            status || " " || sqlca.sqlerrm
        EXIT PROGRAM -1
    END TRY
    DISPLAY "END test ok"
END MAIN
The toto1.json file :
{
    "cust_num": 2735,
    "cust_name": "McCarlson",
    "order_ids": [234, 3456, 24656, 34561]
}