ServicesJSON
in package
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);
Table of Contents
Properties
- $use : mixed
- constructs a new JSON instance
Methods
- decode() : mixed
- decodes a JSON string into appropriate variable
- encode() : mixed
- encodes an arbitrary variable into JSON format
- isError() : mixed
- name_value() : string
- array-walking function for use in generating JSON-formatted name-value pairs
- reduce_string() : string
- reduce a string by removing leading and trailing comments and whitespace
- Services_JSON() : mixed
- utf162utf8() : string
- convert a string from one UTF-16 char to one UTF-8 char
- utf82utf16() : string
- convert a string from one UTF-8 char to one UTF-16 char
Properties
$use
constructs a new JSON instance
private
mixed
$use
= 0
Methods
decode()
decodes a JSON string into appropriate variable
public
decode(string $str) : mixed
Parameters
- $str : string
-
JSON-formatted string
Tags
Return values
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!
encode()
encodes an arbitrary variable into JSON format
public
encode(mixed $var) : mixed
Parameters
- $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!
Tags
Return values
mixed —JSON string representation of input var or an error if a problem occurs
isError()
public
isError(mixed $data[, mixed $code = null ]) : mixed
Parameters
- $data : mixed
- $code : mixed = null
Tags
name_value()
array-walking function for use in generating JSON-formatted name-value pairs
public
name_value(string $name, mixed $value) : string
Parameters
- $name : string
-
name of key to use
- $value : mixed
-
reference to an array element to be encoded
Tags
Return values
string —JSON-formatted name-value pair, like '"name":value'
reduce_string()
reduce a string by removing leading and trailing comments and whitespace
public
reduce_string(mixed $str) : string
Parameters
- $str : mixed
-
string string value to strip of comments and whitespace
Tags
Return values
string —string value stripped of comments and whitespace
Services_JSON()
public
Services_JSON([mixed $use = 0 ]) : mixed
Parameters
- $use : mixed = 0
utf162utf8()
convert a string from one UTF-16 char to one UTF-8 char
public
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.
Parameters
- $utf16 : string
-
UTF-16 character
Tags
Return values
string —UTF-8 character
utf82utf16()
convert a string from one UTF-8 char to one UTF-16 char
public
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.
Parameters
- $utf8 : string
-
UTF-8 character
Tags
Return values
string —UTF-16 character