Peta
 All Classes Namespaces Functions Variables Pages
Public Member Functions | List of all members
Services_JSON Class Reference

Public Member Functions

 Services_JSON ($use=SERVICES_JSON_STRICT_TYPE)
 
 utf162utf8 ($utf16)
 
 utf82utf16 ($utf8)
 
 encode ($var)
 
 name_value ($name, $value)
 
 reduce_string ($str)
 
 decode ($str)
 

Detailed Description

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

Member Function Documentation

Services_JSON::decode (   $str)

decodes a JSON string into appropriate variable

Parameters
string$strJSON-formatted string
Returns
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! public
Services_JSON::encode (   $var)

encodes an arbitrary variable into JSON format

Parameters
mixed$varany 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!
Returns
string JSON string representation of input var public
Services_JSON::name_value (   $name,
  $value 
)

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

Parameters
string$namename of key to use
mixed$valuereference to an array element to be encoded
Returns
string JSON-formatted name-value pair, like '"name":value' private
Services_JSON::reduce_string (   $str)

reduce a string by removing leading and trailing comments and whitespace

Parameters
$strstring string value to strip of comments and whitespace
Returns
string string value stripped of comments and whitespace private
Services_JSON::Services_JSON (   $use = SERVICES_JSON_STRICT_TYPE)

constructs a new JSON instance

Parameters
int$useobject behavior: when encoding or decoding, be loose or strict about object/array usage

possible values:

  • SERVICES_JSON_STRICT_TYPE: strict typing, default. "{...}" syntax creates objects in decode().
  • SERVICES_JSON_LOOSE_TYPE: loose typing. "{...}" syntax creates associative arrays in decode().
Services_JSON::utf162utf8 (   $utf16)

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
string$utf16UTF-16 character
Returns
string UTF-8 character private
Services_JSON::utf82utf16 (   $utf8)

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
string$utf8UTF-8 character
Returns
string UTF-16 character private

The documentation for this class was generated from the following file: