New in version 6.1.0: See the new features.
Print JSON content to the output¶
print_json() |
Print a JSON content into stdout from a !record |
SYNTAX
print_json(recordId [, options])
This method is only available in nph-srep.
Print the record content in JSON syntax to stdout (if nph-srep is executed from a web server the response is returned to the client “browser” as the message content)
The output is converted from the system native one to utf8.
recordId |
Record where to write the JSON values into |
options |
JSONXML_MINIFY removes all unnecessary spaces and characters to reduce the transfered size. |
It returns the number of JSON keys/value combinations printed onto stdout (0 if none)
Note
New in version 6.2.0: See the new features.
Nested objects are supported since the nested record feature.
Array of objects are supported since the array record feature.
To address the problem of reading/writting a json/xml tag which happens to be a Sculptor reserved word (creating a field on a record with that name is not possible). It’s possible to define the reserved word with an underscore “_” in front of it. Not clashing with the reserved word any more. As a rule, when writting/reading the first “_” will be removed (if an underscore is needed just add one more). E.g:
!record Rec { _image,,a64 }
Now the function treats an ASCII or binary array with a “p” (packed flag) flag as one big field, so that large JSON fields can be written. E.g.:
!record Rec { large8kDataString,,a128[64],,p }
New in version 6.3.0: See the new features.
The character conversion honors the value set in sys.CharSetEncodeFrom and sys.CharSetEncodeTo, so that the string received is supposed to be sys.CharSetEncodeFrom encoded while the result is converted into the sys.CharSetEncodeTo encoding.
EXAMPLE
This nph-srep example returns a JSON or an XML response depending on the xml parameter received:
!record request {
xml,,i2
extra,,a8,,v
}
!record response {
valueStr,,a32,,v
valueInt,,i4
}
/* Get the input parameters */
read_web_values(request)
response.valueStr = "áéíóúget.ràèìòù" + extra
response.valueInt = 1234
/* Set the correct mime type and print */
if (request.xml == 1) {
/* Return an xml object */
web_content_type("application/xml")
print_xml(response, JSONXML_MINIFY)
} else {
/* Return a json object */
web_content_type("application/json")
print_json(response, JSONXML_MINIFY)
}
RELATED TOPICS |