Smutty


Smutty.Smutty_JSON

Class Smutty_JSON

Smutty_Object
|
+--Smutty_JSON

public class Smutty_JSON
extends Smutty_Object

Converts to and from JSON format.

Brief example of use:

// 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 = Smutty_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); /


Method Summary
static mixed

encode(mixed var, mixed use)

encodes an arbitrary variable into JSON format

private static string

name_value(string name, mixed value)

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

private static string

reduce_string(mixed str, $str string)

reduce a string by removing leading and trailing comments and whitespace

private static string

utf162utf8(string utf16)

convert a string from one UTF-16 char to one UTF-8 charNormally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.

private static string

utf82utf16(string utf8)

convert a string from one UTF-8 char to one UTF-16 charNormally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.

Method Detail

encode

public static mixed encode(mixed var, mixed use)

encodes an arbitrary variable into JSON format

Parameters:
var - any number, boolean, string, array, or object to be encoded. * see argument 1 to Smutty_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:
JSON string representation of input var or an error if a problem occurs
Go to code

name_value

private static string name_value(string name, mixed value)

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

Parameters:
name - name of key to use
value - reference to an array element to be encoded
Returns:
JSON-formatted name-value pair, like '"name":value'
Go to code

reduce_string

private static string reduce_string(mixed str, $str string)

reduce a string by removing leading and trailing comments and whitespace

Parameters:
string - string value to strip of comments and whitespace
Returns:
string value stripped of comments and whitespace
Go to code

utf162utf8

private static string utf162utf8(string 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:
utf16 - UTF-16 character
Returns:
UTF-8 character
Go to code

utf82utf16

private static string utf82utf16(string 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:
utf8 - UTF-8 character
Returns:
UTF-16 character
Go to code

Smutty