3.15. JSON Functions

JSON (JavaScript Object Notation) functions allow serializing and deserializing between JSON strings and qore data structures. There are also functions for JSONRPC support for easier integration with JavaScript clients; see below for details.

3.15.1. makeJSONString()

Synopsis

Serializes qore data into a JSON string, without any line breaks. By default the string produced will be in UTF-8 encoding, but this can be overridden by the second argument.

Usage
makeJSONString(Any Type, [Encoding])
Example
$json = makeJSONString($value);

Table 3.445. Arguments and Return Values for makeJSONString()

Argument Type

Return Type

Description

Any Type, [Encoding]

String

Serializes qore data into a JSON string, without any line breaks.


Table 3.446. Exceptions Thrown by makeJSONString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.2. makeFormattedJSONString()

Synopsis

Serializes qore data into a JSON string, formatted with line breaks for easier readability. By default the string produced will be in UTF-8 encoding, but this can be overridden by the second argument.

Usage
makeFormattedJSONString(Any Type, [Encoding])
Example
$json = makeFormattedJSONString($value);

Table 3.447. Arguments and Return Values for makeFormattedJSONString()

Argument Type

Return Type

Description

Any Type, [Encoding]

String

Serializes qore data into a JSON string, formatted with line breaks for easier readability.


Table 3.448. Exceptions Thrown by makeFormattedJSONString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.3. makeJSONRPCRequestString()

Synopsis

Creates a JSON-RPC request string from the parameters passed, without any line breaks. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeJSONRPCRequestString(method_name, [json_version], [id], [arguments])
Example
$json = makeJSONRPCRequestString("method_name", "1.1", $id, $request_data);

Table 3.449. Arguments and Return Values for makeJSONRPCRequestString()

Argument Type

Return Type

Description

Method_name, [json_version], [id], [arguments]

String

Creates a JSON-RPC request string from the parameters passed, without any line breaks.


Table 3.450. Exceptions Thrown by makeJSONRPCRequestString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.

MAKE-JSONRPC-REQUEST-STRING-ERROR

missing method name as first parameter.


3.15.4. makeFormattedJSONRPCRequestString()

Synopsis

Creates a JSON-RPC request string from the parameters passed, formatted with line breaks for easier readability. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeFormattedJSONRPCRequestString(method_name, [json_version], [id], [arguments])
Example
$json = makeFormattedJSONRPCRequestString("method_name", "1.1", $id, $request_data);

Table 3.451. Arguments and Return Values for makeFormattedJSONRPCRequestString()

Argument Type

Return Type

Description

Method_name, [json_version], [id], [arguments]

String

Creates a JSON-RPC request string from the parameters passed, formatted with line breaks for easier readability.


Table 3.452. Exceptions Thrown by makeFormattedJSONRPCRequestString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.

MAKE-JSONRPC-REQUEST-STRING-ERROR

missing method name as first parameter.


3.15.5. makeJSONRPCResponseString()

Synopsis

Creates a JSON-RPC response string from the parameters passed, without any line breaks. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeJSONRPCResponseString([json_version], [id], [response])
Example
$json = makeJSONRPCResponseString("1.1", $id, $response);

Table 3.453. Arguments and Return Values for makeJSONRPCResponseString()

Argument Type

Return Type

Description

[json_version], [id], [response]

String

Creates a JSON-RPC response string from the parameters passed, without any line breaks.


Table 3.454. Exceptions Thrown by makeJSONRPCResponseString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.6. makeFormattedJSONRPCResponseString()

Synopsis

Creates a JSON-RPC response string from the parameters passed, formatted with line breaks for easier readability. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeFormattedJSONRPCResponseString([json_version], [id], [response])
Example
$json = makeFormattedJSONRPCResponseString("1.1", $id, $response);

Table 3.455. Arguments and Return Values for makeFormattedJSONRPCResponseString()

Argument Type

Return Type

Description

[json_version], [id], [response]

String

Creates a JSON-RPC response string from the parameters passed, formatted with line breaks for easier readability.


Table 3.456. Exceptions Thrown by makeFormattedJSONRPCResponseString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.7. makeJSONRPCErrorString()

Synopsis

Creates a generic JSON-RPC error response string from the parameters passed, without any line breaks. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeJSONRPCErrorString([json_version], [id], [response])
Example
$json = makeJSONRPCErrorString("1.1", $id, $response);

Table 3.457. Arguments and Return Values for makeJSONRPCErrorString()

Argument Type

Return Type

Description

[json_version], [id], [response]

String

Creates a generic JSON-RPC error response string from the parameters passed, without any line breaks.


Table 3.458. Exceptions Thrown by makeJSONRPCErrorString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.8. makeFormattedJSONRPCErrorString()

Synopsis

Creates a generic JSON-RPC error response string from the parameters passed, formatted with line breaks for easier readability. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeFormattedJSONRPCErrorString([json_version], [id], [response])
Example
$json = makeFormattedJSONRPCErrorString("1.1", $id, $response);

Table 3.459. Arguments and Return Values for makeFormattedJSONRPCErrorString()

Argument Type

Return Type

Description

[json_version], [id], [response]

String

Creates a generic JSON-RPC error response string from the parameters passed, formatted with line breaks for easier readability.


Table 3.460. Exceptions Thrown by makeFormattedJSONRPCErrorString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.9. makeJSONRPC11ErrorString()

Synopsis

Creates a JSON-RPC 1.1 error response string from the parameters passed, without any line breaks. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The last argument can be of any qore type (or complex data structure).

Usage
makeJSONRPC11ErrorString(code, message, [[id], [error])
Example
$json = makeJSONRPC11ErrorString(200, $msg, $id, $error);

Table 3.461. Arguments and Return Values for makeJSONRPC11ErrorString()

Argument Type

Return Type

Description

Code, Error_message, [id], [error_response]

String

Creates a JSON-RPC 1.1 error response string from the parameters passed, formatted with line breaks for easier readability.


Table 3.462. Exceptions Thrown by makeJSONRPC11ErrorString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.

MAKE-JSONRPC11-ERROR-STRING-ERROR

Invalid argument to method.


3.15.10. makeFormattedJSONRPC11ErrorString()

Synopsis

Creates a JSON-RPC 1.1 error response string from the parameters passed, formatted with line breaks for easier readability. To follow JSON-RPC specifications, the generated string will always be in UTF-8 encoding. The first code argument must be an integer between 100 and 999 (inclusive). The last argument can be of any Qore type (or complex data structure).

Usage
makeFormattedJSONRPC11ErrorString(code, error_message, [id], [error_response])
Example
$json = makeFormattedJSONRPC11ErrorString(200, $msg, $id, $error);

Table 3.463. Arguments and Return Values for makeFormattedJSONRPC11ErrorString()

Argument Type

Return Type

Description

Code, Error_message, [id], [error_response]

String

Creates a JSON-RPC 1.1 error response string from the parameters passed, formatted with line breaks for easier readability.


Table 3.464. Exceptions Thrown by makeFormattedJSONRPC11ErrorString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.

MAKE-JSONRPC11-ERROR-STRING-ERROR

Invalid argument to method.


3.15.11. parseJSON()

Synopsis

Parses a JSON string and returns the corresponding qore data structure.

Usage
parseJSON(json_string)
Example
$data = parseJSON($json_string);

Table 3.465. Arguments and Return Values for parseJSON()

Argument Type

Return Type

Description

Json_string

Qore Data

Parses a JSON string and returns the corresponding qore data structure.


Table 3.466. Exceptions Thrown by parseJSON()

err

desc

JSON-PARSE-ERROR

error parsing JSON string.