MediaWiki  REL1_19
Services_JSON Class Reference

Converts to and from JSON format. More...

List of all members.

Public Member Functions

 __construct ($use=0)
 constructs a new JSON instance
 decode ($str)
 decodes a JSON string into appropriate variable
 encode ($var, $pretty=false)
 encodes an arbitrary variable into JSON format
 encode2 ($var)
 encodes an arbitrary variable into JSON format
 isError ($data, $code=null)
 name_value ($name, $value)
 array-walking function for use in generating JSON-formatted name-value pairs
 reduce_string ($str)
 reduce a string by removing leading and trailing comments and whitespace
 utf162utf8 ($utf16)
 convert a string from one UTF-16 char to one UTF-8 char
 utf82utf16 ($utf8)
 convert a string from one UTF-8 char to one UTF-16 char

Static Private Member Functions

static pearInstalled ()
 Returns cached result of class_exists('pear'), to avoid calling AutoLoader numerous times in cases when PEAR is not present.

Static Private Attributes

static $mHavePear = null

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

Definition at line 117 of file Services_JSON.php.


Constructor & Destructor Documentation

constructs a new JSON instance

Parameters:
$useInteger: 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()

Definition at line 135 of file Services_JSON.php.


Member Function Documentation

decodes a JSON string into appropriate variable

Parameters:
$strString: JSON-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!
Access:
public

Definition at line 539 of file Services_JSON.php.

References reduce_string(), and utf162utf8().

Here is the call graph for this function:

Services_JSON::encode ( var,
pretty = false 
)

encodes an arbitrary variable into JSON format

Parameters:
$varMixed: 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!
$prettyBoolean: pretty-print output with indents and newlines
Returns:
mixed JSON string representation of input var or an error if a problem occurs
Access:
public

Definition at line 278 of file Services_JSON.php.

References encode2().

Referenced by MakeLanguageSpec\writeJavascriptFile().

Here is the call graph for this function:

Here is the caller graph for this function:

encodes an arbitrary variable into JSON format

Parameters:
$varMixed: 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!
Returns:
mixed JSON string representation of input var or an error if a problem occurs
Access:
private

Definition at line 297 of file Services_JSON.php.

References isError(), and utf82utf16().

Referenced by encode(), and name_value().

Here is the call graph for this function:

Here is the caller graph for this function:

Services_JSON::isError ( data,
code = null 
)
Todo:
Ultimately, this should just call PEAR::isError()

Definition at line 830 of file Services_JSON.php.

Referenced by encode2(), and name_value().

Here is the caller graph for this function:

Services_JSON::name_value ( name,
value 
)

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

Parameters:
$nameString: name of key to use
$valueMixed: reference to an array element to be encoded
Returns:
String: JSON-formatted name-value pair, like '"name":value'
Access:
private

Definition at line 489 of file Services_JSON.php.

References encode2(), and isError().

Here is the call graph for this function:

static Services_JSON::pearInstalled ( ) [static, private]

Returns cached result of class_exists('pear'), to avoid calling AutoLoader numerous times in cases when PEAR is not present.

Returns:
boolean

Definition at line 146 of file Services_JSON.php.

References $mHavePear.

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
Access:
private

Definition at line 508 of file Services_JSON.php.

Referenced by decode().

Here is the caller graph for this function:

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:
$utf16String: UTF-16 character
Returns:
String: UTF-8 character
Access:
private

Definition at line 164 of file Services_JSON.php.

Referenced by decode().

Here is the caller graph for this function:

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:
$utf8String: UTF-8 character
Returns:
String: UTF-16 character
Access:
private

Definition at line 219 of file Services_JSON.php.

Referenced by encode2().

Here is the caller graph for this function:


Member Data Documentation

Services_JSON::$mHavePear = null [static, private]

Definition at line 140 of file Services_JSON.php.

Referenced by pearInstalled().


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