Try OpenEdge Now
skip to main content
Working with JSON
Reading and Serializing JSON to/from ProDataSets and Temp-tables : Writing JSON from a temp-table buffer's current row
 

Writing JSON from a temp-table buffer's current row

The SERIALIZE-ROW( ) method serializes the current row of the temp-table buffer handle on which you call it either as a JSON string or as a Progress.Json.ObjectModel.JsonObject object.
Note that you cannot call SERIALIZE-ROW( ) on a database buffer handle.
For string outputs only, SERIALIZE-ROW( ) provides the option to format the output in a hierarchical manner using extra white space and line breaks.
The syntax for SERIALIZE-ROW( ) is shown below. The syntax is similar to that for the WRITE-JSON( ) method, with the addition of the target-format parameter. The method returns TRUE or FALSE to indicate whether the operation was successful or not.

Syntax

SERIALIZE-ROW ( target-format , target-type ,
{ file | stream | stream-handle | memptr | longchar | JsonObject }
[ , formatted [ , encoding [ , omit-initial-values
[ , omit-outer-object ] ] ] ] )
target-format
A CHARACTER expression that specifies the target format used to serialize the current row. Valid values are "JSON" and "XML". However, this method description focuses on serializing to JSON.
For information on using this method to serialize the current row of a temp-table buffer to XML, see OpenEdge Development: Working with XML.
target-type
A CHARACTER expression that specifies the target type for the JSON output. Valid values are "FILE", "STREAM", "STREAM-HANDLE", "MEMPTR", "LONGCHAR", and "JsonObject".
All values except "JsonObject" specify a target for a JSON string.
file
A CHARACTER expression that specifies the name of a file to which the AVM writes the JSON string. You can specify an absolute path or a path relative to the current working directory. If a file with the specified name already exists, the AVM verifies if 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 output stream defined by WebSpeed (WEBSTREAM).
stream-handle
A HANDLE variable that specifies a stream object 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 finish using the MEMPTR, you must free the associated memory by setting the MEMTER to 0 bytes with the SET-SIZE statement.
longchar
A LONGCHAR variable which contains the JSON string in memory.
The AVM saves the JSON string to the LONGCHAR variable in the code page which corresponds to the character encoding you specify in the encoding 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 the encoding option, SERIALIZE-ROW( ) generates an error and returns FALSE. The JSON string is not saved to the LONGCHAR.
JsonObject
A reference to a Progress.Json.ObjectModel.JsonObject object to which the AVM writes the buffer's current row. Any prior contents of the specified JsonObject are removed.
The JsonObject must be a valid instance of type Progress.Json.ObjectModel.JsonObject. If not, SERIALIZE-ROW( ) generates an error message and returns FALSE.
formatted
An optional LOGICAL expression where TRUE directs the AVM to format a JSON string in a hierarchical manner using white space, carriage returns, and line feeds. The default value is FALSE. If you specify the Unknown value (?), the method uses the default value FALSE. The formatted option is ignored for JsonObject targets.
encoding
An optional CHARACTER expression that specifies the name of the character encoding the AVM uses to write the JSON output. 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 "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 output, and FALSE directs the AVM to include all temp-table field data in the output. The default value is FALSE. If you specify the Unknown value (?), the method uses the default value of FALSE.
For more information about this option, see the Minimizingthe size of JSON data.
omit-outer-object
An optional LOGICAL expression indicating whether the outer-most object in JSON output is included. TRUE directs the AVM to remove the outer-most object on output. FALSE directs the AVM to include the outer-most object in the output.
For more information about this option, see the Omitting the outer object in JSON data.
The following example demonstrates the use of SERIALIZE-ROW( ):
DEFINE TEMP-TABLE ttCust NO-UNDO
  FIELD custNum AS INTEGER
  FIELD Name AS CHARACTER
  FIELD Address AS CHARACTER.

DEFINE VARIABLE bcust AS HANDLE.

FIND FIRST sports2000.Customer.
CREATE ttCust.
BUFFER-COPY sports2000.Customer TO ttCust.
bcust = TEMP-TABLE ttCust:DEFAULT-BUFFER-HANDLE.
bcust:SERIALIZE-ROW("JSON", "file", "bcust.json", TRUE).
bcust:SERIALIZE-ROW("JSON", "file", "bcust2.json", TRUE, ?, ?, TRUE).
The SERIALIZE-ROW( ) method produces the following JSON string outputs. The JSON output below is bcust.json with omit-outer-object set to FALSE.
{"ttCust":
{
"custNum": 1,
"Name": "Lift Tours",
"Address": "276 North Drive"
}
}
The JSON output below is bcust2.json with omit-outer-object set to TRUE.
{
"custNum": 1,
"Name": "Lift Tours",
"Address": "276 North Drive"
}