Show:

Q_Json Class

Module: Q

Converts to and from JSON format.

Brief example of use:

// create a new instance of Q_Json
$json = new Q_Json();

// convert a complexe value to JSON notation, and send it to the browser
$value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
$output = $json->encode($value);

print($output);
// prints: ["foo","bar",[1,2,"baz"],[3,[4]]]

// accept incoming POST data, assumed to be in JSON notation
$input = file_get_contents('php://input', 1000000);
$value = $json->decode($input);

Constructor

Q_Json

(
  • $use
)

Parameters:

  • $use Integer

    object behavior flags; combine with boolean-OR

                          possible values:<br/>
                          - SERVICES_JSON_LOOSE_TYPE:  loose typing.
                                  "&123;...&125;" syntax creates associative arrays
                                  instead of objects in decode().<br/>
                          - SERVICES_JSON_SUPPRESS_ERRORS:  error suppression.
                                  Values which can't be encoded (e.g. resources)
                                  appear as NULL instead of throwing errors.
                                  By default, a deeply-nested resource will
                                  bubble up with an error, so all return values
                                  from encode() should be checked with isError()<br/>

Methods

decode

(
  • $str
)
Mixed

decodes a JSON string into appropriate variable

Parameters:

  • $str String

    JSON-formatted string

Returns:

Mixed:

number, boolean, string, array, or object corresponding to given JSON input string. See argument 1 to Q_Json() above for object-output behavior. Note that decode() always returns strings in ASCII or UTF-8 format!

encode

(
  • $var
)
Mixed

encodes an arbitrary variable into JSON format

Parameters:

  • $var Mixed

    any number, boolean, string, array, or object to be encoded. see argument 1 to Q_Json() above for array-parsing behavior. if var is a strng, note that encode() always expects it to be in ASCII or UTF-8 format!

Returns:

Mixed:

JSON string representation of input var or an error if a problem occurs

name_value

(
  • $name
  • $value
)
String private

array-walking function for use in generating JSON-formatted name-value pairs

Parameters:

  • $name String

    name of key to use

  • $value Mixed

    reference to an array element to be encoded

Returns:

String:

JSON-formatted name-value pair, like '"name":value'

reduce_string

(
  • $str
)
String private

reduce a string by removing leading and trailing comments and whitespace

Parameters:

  • $str String

    string value to strip of comments and whitespace

Returns:

String:

string value stripped of comments and whitespace

utf162utf8

(
  • $utf16
)
String private

convert a string from one UTF-16 char to one UTF-8 char

Normally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.

Parameters:

  • $utf16 String

    UTF-16 character

Returns:

String:

UTF-8 character

utf82utf16

(
  • $utf8
)
String private

convert a string from one UTF-8 char to one UTF-16 char

Normally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.

Parameters:

  • $utf8 String

    UTF-8 character

Returns:

String:

UTF-16 character