Services_JSON

Converts to and from JSON format.

Brief example of use:

// create a new instance of Services_JSON $json = new Services_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);

Methods

constructs a new JSON instance

__construct(int $use) 

Arguments

$use

int

object behavior flags; combine with boolean-OR possible values: - SERVICES_JSON_LOOSE_TYPE: loose typing. "{...}" syntax creates associative arrays instead of objects in decode(). - 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()

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

utf162utf8(string $utf16) : string

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

access

private

Arguments

$utf16

string

UTF-16 character

Response

string

UTF-8 character

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

utf82utf16(string $utf8) : string

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

access

private

Arguments

$utf8

string

UTF-8 character

Response

string

UTF-16 character

encodes an arbitrary variable into JSON format

encode(mixed $var) : mixed
access

public

Arguments

$var

mixed

any number, boolean, string, array, or object to be encoded. see argument 1 to Services_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!

Response

mixed

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

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

name_value(string $name, mixed $value) : string
access

private

Arguments

$name

string

name of key to use

$value

mixed

reference to an array element to be encoded

Response

string

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

reduce a string by removing leading and trailing comments and whitespace

reduce_string(string $str) : string
access

private

Arguments

$str

string

string string value to strip of comments and whitespace

Response

string

string value stripped of comments and whitespace

decodes a JSON string into appropriate variable

decode(string $str) : mixed
access

public

Arguments

$str

string

JSON-formatted string

Response

mixed

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

isError

isError($data, $code = null) 
todo

Ultimately, this should just call PEAR::isError()

Arguments

$data

$code