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. 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).

Prototype

makeFormattedJSONRPC11ErrorString(softint $code, string $error, any $id, any $error_response) returns string

Example
$json = makeFormattedJSONRPC11ErrorString(200, $msg, $id, $error);

Table 3.482. Arguments and Return Values for makeFormattedJSONRPC11ErrorString()

Argument Type

Return Type

Description

softint $code, string $error, [any $id, any $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.483. 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.2. 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).

Prototype

makeFormattedJSONRPCErrorString(any $json_version, any $id, any $response_msg) returns string

Example
$json = makeFormattedJSONRPCErrorString("1.1", $id, $response);

Table 3.484. Arguments and Return Values for makeFormattedJSONRPCErrorString()

Argument Type

Return Type

Description

any $json_version, any $id, any $response_msg

string

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


Table 3.485. Exceptions Thrown by makeFormattedJSONRPCErrorString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.3. 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).

Prototype

makeFormattedJSONRPCRequestString(string $method_name, any $json_version, any $id, any $request_msg) returns string

Example
$json = makeFormattedJSONRPCRequestString("method_name", "1.1", $id, $request_data);

Table 3.486. Arguments and Return Values for makeFormattedJSONRPCRequestString()

Argument Type

Return Type

Description

string $method_name, any $json_version, any $id, any $request_msg

string

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


Table 3.487. 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.4. 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).

Prototype

makeFormattedJSONRPCResponseString(any $json_version, any $id, any $response_msg) returns string

Example
$json = makeFormattedJSONRPCResponseString("1.1", $id, $response);

Table 3.488. Arguments and Return Values for makeFormattedJSONRPCResponseString()

Argument Type

Return Type

Description

any $json_version, any $id, any $response_msg

string

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


Table 3.489. Exceptions Thrown by makeFormattedJSONRPCResponseString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.5. 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.

Prototype

makeFormattedJSONString(any $data, string $encoding) returns string

makeFormattedJSONString(any $data) returns string

Example
$json = makeFormattedJSONString($value);

Table 3.490. Arguments and Return Values for makeFormattedJSONString()

Argument Type

Return Type

Description

any $data, [string $encoding]

string

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


Table 3.491. Exceptions Thrown by makeFormattedJSONString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.6. 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).

Prototype

makeJSONRPC11ErrorString(softint $code, string $error, any $id, any $error_response) returns string

Example
$json = makeJSONRPC11ErrorString(200, $msg, $id, $error);

Table 3.492. Arguments and Return Values for makeJSONRPC11ErrorString()

Argument Type

Return Type

Description

softint $code, string $error, any $id, any $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.493. 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.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).

Prototype

makeJSONRPCErrorString(any $json_version, any $id, any $response_msg) returns string

Example
$json = makeJSONRPCErrorString("1.1", $id, $response);

Table 3.494. Arguments and Return Values for makeJSONRPCErrorString()

Argument Type

Return Type

Description

any $json_version, any $id, any $response_msg

string

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


Table 3.495. Exceptions Thrown by makeJSONRPCErrorString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.8. 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).

Prototype

makeJSONRPCRequestString(string $method_name, any $json_version, any $id, any $request_msg) returns string

Example
$json = makeJSONRPCRequestString("method_name", "1.1", $id, $request_data);

Table 3.496. Arguments and Return Values for makeJSONRPCRequestString()

Argument Type

Return Type

Description

string $method_name, any $json_version, any $id, any $request_msg

string

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


Table 3.497. 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.9. 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).

Prototype

makeJSONRPCResponseString(any $json_version, any $id, any $response_msg) returns string

Example
$json = makeJSONRPCResponseString("1.1", $id, $response);

Table 3.498. Arguments and Return Values for makeJSONRPCResponseString()

Argument Type

Return Type

Description

any $json_version, any $id, any $response_msg

string

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


Table 3.499. Exceptions Thrown by makeJSONRPCResponseString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.10. 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.

Prototype

makeJSONString(any $data, string $encoding) returns string

makeJSONString(any $data) returns string

Example
$json = makeJSONString($value);

Table 3.500. Arguments and Return Values for makeJSONString()

Argument Type

Return Type

Description

any $data, [string $encoding]

string

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


Table 3.501. Exceptions Thrown by makeJSONString()

err

desc

JSON-SERIALIZATION-ERROR

Error serializing to JSON string.


3.15.11. parseJSON()

Synopsis

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

Prototype

parseJSON(string $json_string) returns any

parseJSON() returns nothing (RT_NOOP)

Example
$data = parseJSON($json_string);

Table 3.502. Arguments and Return Values for parseJSON()

Argument Type

Return Type

Description

string $json_string

any

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


Table 3.503. Exceptions Thrown by parseJSON()

err

desc

JSON-PARSE-ERROR

error parsing JSON string.