Serializer options
Options of the json.Serializer
class.
Option | Description | Default | Example use |
---|---|---|---|
allowNullAsDefault | Allow NULL values to be accepted during deserialization
(JSON/BDL) when the json_null="null" attribute is not explicitly specified. Applies to
deserialization only. |
0 ( |
CALL json.Serializer.setOption("allowNullAsDefault",1) For
examples, go to Example 1: JSON to BDL deserialization options to allow nulls |
serializeNullAsDefault | Allow NULL values to be accepted during serialization
(BDL/JSON), even if constraints are set (for example, JSONRequired defined or
json_null="null" not defined). Applies to serialization only. |
0 ( |
CALL json.Serializer.setOption("serializeNullAsDefault",1) For
examples, go to Example 2: BDL to JSON serialization options to allow nulls |
Examples
Options of the json.Serializer
class usage examples.
Example 1: JSON to BDL deserialization options to allow nulls
To relax deserialization, you can set the allowNullAsDefault
option to
allow nulls.
By default, Genero Web Services JSON to BDL deserialization will raise errors if the JSON content does not match the BDL variable receiving the data.
TYPE tAddress RECORD
street STRING,
city STRING,
state STRING,
zip STRING
END RECORD
TYPE tCustomer RECORD
id STRING,
name STRING,
address tAddress
END RECORD
{
"id": "1",
"name": "John Doe",
"address": null
}
CALL json.Serializer.setOption( "allowNullAsDefault", 1 )
For more details, go to json.Serializer.setOption.
json_null="null"
to handle JSON serialization of null values.
TYPE tAddress RECORD ATTRIBUTES(json_null="null")
street STRING,
city STRING,
state STRING,
zip STRING
END RECORD
For more information, go to Using the json_null="null" attribute.Example 2: BDL to JSON serialization options to allow nulls
To relax serialization, you can set the serializeNullAsDefault
option to
allow nulls.
NULL
values are sent as in this example:IMPORT JSON
CONSTANT TNAME = "one_value_withoutNull()"
DEFINE
writer json.JSONWriter,
t TEXT,
str STRING
MAIN
LOCATE t in MEMORY
DISPLAY "\nSTART ", TNAME
TRY
LET writer = json.JSONWriter.Create()
CALL writer.setOutputCharset("UTF-8")
CALL writer.writeToText(t)
CALL writer.startJSON()
CALL json.Serializer.variableToJSON(str, writer)
CALL writer.endJSON()
CALL writer.close()
DISPLAY "Should raise a serialization error"
DISPLAY "json: ", t
EXIT PROGRAM 1
CATCH
DISPLAY status || ": " || sqlca.sqlerrm
DISPLAY "END " || TNAME || " ok"
END TRY
END MAIN
str
" is
NULL
:START one_value_withoutNull()
-15807: Primitive value cannot be serialized to 'null'
END one_value_withoutNull() ok
Serializing data with nulls to JSON using serializeNullAsDefault
JSONRequired
constraint is set:IMPORT JSON
CONSTANT TNAME = "object_withOption()"
DEFINE rec RECORD
a STRING ATTRIBUTE(JSONRequired),
b INTEGER
END RECORD
DEFINE
writer json.JSONWriter,
t TEXT
MAIN
LOCATE t in MEMORY
DISPLAY "\nSTART ", TNAME
TRY
LET writer = json.JSONWriter.Create()
CALL writer.setOutputCharset("UTF-8")
CALL writer.writeToText(t)
CALL writer.startJSON()
CALL json.Serializer.setOption("serializeNullAsDefault",1)
CALL json.Serializer.variableToJSON(rec, writer)
CALL writer.endJSON()
CALL writer.close()
DISPLAY "json: ", t
CATCH
DISPLAY status || ": " || sqlca.sqlerrm
EXIT PROGRAM 1
END TRY
DISPLAY "END " || TNAME || " ok"
# reset serializeNullAsDefault to default
CALL json.Serializer.setOption("serializeNullAsDefault",0)
END MAIN
No serialization error is raised and the following output is produced with null values:
START object_withOption() json: {"a":null,"b":0} END object_withOption() ok
For more details about serializeNullAsDefault
, go to json.Serializer.setOption.
Serialization error with nulls in an array
NULL
values are sent in an array as in this example:IMPORT JSON
CONSTANT TNAME = "array_withoutNull()"
DEFINE arr DYNAMIC ARRAY OF STRING
DEFINE
writer json.JSONWriter,
t TEXT
MAIN
LOCATE t in MEMORY
DISPLAY "\nSTART ", TNAME
LET arr[1] = "foo"
LET arr[2] = NULL
TRY
LET writer = json.JSONWriter.Create()
CALL writer.setOutputCharset("UTF-8")
CALL writer.writeToText(t)
CALL writer.startJSON()
CALL json.Serializer.variableToJSON(arr, writer)
CALL writer.endJSON()
CALL writer.close()
DISPLAY "Should generate serialization error"
DISPLAY "json: ", t
EXIT PROGRAM 1
CATCH
DISPLAY status || ": " || sqlca.sqlerrm
DISPLAY "END " || TNAME || " ok"
END TRY
END MAIN
error-15807 is raised and the following output is returned because "arr[2]
"
is NULL
:START array_withoutNull() -15807: Array cannot serialize 'null' elements. It requires json_null="null" END array_withoutNull() ok
Serializing array with nulls to JSON using serializeNullAsDefault
IMPORT JSON
CONSTANT TNAME = "array_withOption()"
DEFINE arr DYNAMIC ARRAY OF STRING
DEFINE
writer json.JSONWriter,
t TEXT
MAIN
LOCATE t in MEMORY
DISPLAY "\nSTART ", TNAME
LET arr[1] = "foo"
LET arr[2] = NULL
TRY
LET writer = json.JSONWriter.Create()
CALL writer.setOutputCharset("UTF-8")
CALL writer.writeToText(t)
CALL writer.startJSON()
CALL json.Serializer.setOption("serializeNullAsDefault",1)
CALL json.Serializer.variableToJSON(arr, writer)
CALL writer.endJSON()
CALL writer.close()
DISPLAY "json: ", t
CATCH
DISPLAY status || ": " || sqlca.sqlerrm
EXIT PROGRAM 1
END TRY
DISPLAY "END " || TNAME || " ok"
# reset serializeNullAsDefault to default
CALL json.Serializer.setOption("serializeNullAsDefault",0)
END MAIN
No serialization error is raised and the following output is produced with null values:
START array_withOption() json: ["foo",null] END array_withOption() ok
For more details about serializeNullAsDefault
, go to json.Serializer.setOption.
Using json_null="null"
to manage JSON serialization of null values
json_null="null"
to allow JSON serialization of null values.
DEFINE str STRING ATTRIBUTE(json_null="null")
You can define an array with the
json_null="null"
attribute to handle a null index or an attempt to serialize it
without elements. For examples, go to Using the json_null="null" attribute.