Class Services_JSON


Converts to and from JSON format.

Brief example of use:

1 // create a new instance of Services_JSON
2 $json = new Services_JSON();
4 // convert a complexe value to JSON notation, and send it to the browser
5 $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
6 $output = $json->encode($value);
8 print($output);
9 // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
11 // accept incoming POST data, assumed to be in JSON notation
12 $input = file_get_contents('php://input', 1000000);
13 $value = $json->decode($input);

Located in /JSON.php (line 114)

Method Summary
Services_JSON Services_JSON (int $use)
mixed decode (string $str)
mixed encode (mixed $var)
void isError (mixed $data, [mixed $code = null])
Constructor Services_JSON (line 132)

constructs a new JSON instance

Services_JSON Services_JSON (int $use)
  • int $use:

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

decode (line 474)

decodes a JSON string into appropriate variable

  • return: 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!
  • access: public
mixed decode (string $str)
  • string $str: JSON-formatted string
encode (line 234)

encodes an arbitrary variable into JSON format

  • return: JSON string representation of input var or an error if a problem occurs
  • access: public
mixed encode (mixed $var)
  • mixed $var: 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!
isError (line 751)
  • todo: Ultimately, this should just call PEAR::isError()
void isError (mixed $data, [mixed $code = null])

Documentation generated on Sat, 03 Dec 2005 16:19:51 -0800 by phpDocumentor 1.2.3