WRITE-JSON( ) method
Writes a JSON string from a ProDataSet, a temp-table, or a temp-table buffer object. In the case of the temp-table buffer object, all the records of the temp-table associated with the buffer object are written to the JSON string—not just the one in the buffer.
Return type: LOGICAL
Applies to: Buffer object handle, ProDataSet object handle, Temp-table object handle
mode
file
A CHARACTER expression that specifies the name of a file to which the AVM writes the JSON string. You can specify an absolute pathname or a pathname relative to the current working directory. If a file with the specified name already exists, the AVM verifies that the file is writeable and overwrites the file.stream
A CHARACTER expression that specifies the name of a stream. If you specify the empty string (""), the AVM writes the JSON string to the default unnamed output stream. For WebSpeed, write the JSON string to the WebSpeed-defined output stream (WEBSTREAM).For more information about using ABL unnamed output streams, see the DEFINE STREAM statement reference entry and the chapter on alternate I/O sources in OpenEdge Development: Programming Interfaces. For more information about using WebSpeed-defined output streams, see OpenEdge Application Server: Developing WebSpeed Applications.stream-handle
memptr
A MEMPTR variable to contain the JSON string in memory. If you do not specify the encoding parameter, the AVM encodes the text written to the MEMPTR as UTF-8. This method allocates the required amount of memory for the JSON string and sets the size of the variable. When you are finished using the MEMPTR, you must free the associated memory, by executing SET-SIZE(memptr
) = 0 on the MEMPTR.longchar
A LONGCHAR variable to contain the JSON string in memory.The AVM saves the JSON string to the LONGCHAR variable in the code page that corresponds to the character encoding you specify in theencoding
option. If you do not specify a character encoding for the JSON string, the AVM saves the LONGCHAR variable in UTF-8.If the LONGCHAR variable's code page is fixed (that is, set using the FIX-CODEPAGE statement) and the fixed code page is not equivalent to the character encoding you specify in theencoding
option, the WRITE-JSON( ) method generates an error and returns FALSE. The JSON string is not saved to the LONGCHAR.formatted
encoding
An optional CHARACTER expression that specifies the name of the character encoding the AVM uses to write the JSON string. The default encoding is "UTF-8".The encoding name must specify a Unicode transformation format. Valid values are "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", and "UTF-32LE".Note: If you specify the empty string ("") or the Unknown value (?
), the AVM uses the default encoding of UTF-8.omit-initial-values
An optional LOGICAL expression where TRUE directs the AVM to exclude temp-table fields containing their initial values from the JSON string, and FALSE directs the AVM to include all temp-table field data in the JSON. The default value is FALSE. If you specify the Unknown value (?
), the method uses the default value of FALSE.When working with large ProDataSets, omitting fields containing their initial values can yield smaller JSON values, more efficient network transfers, and performance gains with the READ-JSON( ) and WRITE-JSON( ) methods.This behavior applies both to temp-table fields that have the default initial value for its data type, and for fields that have an initial value set with the ABL INITIAL option.Although using the omit-initial-values option can give your application performance and resource use improvements, you must be sure that the consumers of the generated JSON string will correctly handle the JSON. The ABL READ-JSON( ) method always populates created records with initial values from the temp-table or ProDataSet definition. Other applications might not do this.When writing data from a ProDataSet object that contains data-relations, you can nest child rows of a ProDataSet buffer within their parent rows in the resulting JSON string by:
- Setting the NESTED attribute on the Data-relation object handle to TRUE
- Specifying the NESTED option for the data-relation on the DEFINE DATASET statement
- Specifying the NESTED option in the ADD-RELATION( ) method
- Specifying the FOREIGN-KEY-HIDDEN option in addition to the NESTED option to prevent duplicating the key fields in the child records that are nested within the parent record.
Note: If you do not nest child tables, the JSON loses the data relation information. The serialization process also loses any information about key columns.You cannot write a JSON string from a database buffer.
The following code example defines a static ProDataSet object, attaches its data sources, fills the ProDataSet object, and writes the ProDataSet object to a JSON string in a nested manner:
The following code example defines a static temp-table object, populates the temp-table object (code not shown), and writes the temp-table object to a JSON string:
See also: ENCODING attribute, FIX-CODEPAGE statement, FOREIGN-KEY-HIDDEN attribute, NESTED attribute, READ-JSON( ) method
OpenEdge Release 10.2B
|