PHP Class FastJSON, wordpress-console

FastJSON, simple and fast Pear Service_JSON encoder/decoder alternative [http://pear.php.net/pepr/pepr-proposal-show.php?id=198] --------------------------------------- This class is about two time faster than Pear Service_JSON class. This class is probably not powerful as Service_JSON but it has no dependencies and converts correctly ASCII range 0x00 - 0x1F too. There's any string convertion, just regular RFC specific characters are converted into \u00XX string. To don't have problems with other chars try to use utf8_encode($json_encoded_string). To recieve correctly JSON strings from JavaScript use encodeURIComponent then use, if is necessary, utef8_decode before JS to PHP convertion. decode method doesn't returns a standard object class but You can create the corret class directly with FastJSON::convert method and with them You can manage JS Date objects too. --------------------------------------- Summary of static public methods convert extra, special method decode converts a valid JSON string into a native PHP variable encode converts a native php variable into a valid JSON string --------------------------------------- Special FastJSON::convert method Informations _______________________________________ --------------------------------------- This method is used by FastJSON::encode method but should be used to do these convertions too: - JSON string to time() integer: FastJSON::convert(decodedDate:String):time() If You recieve a date string rappresentation You could convert into respective time() integer. Example: FastJSON::convert(FastJSON::decode($clienttime)); i.e. $clienttime = 2006-11-09T14:42:30 returned time will be an integer useful with gmdate or date to create, for example, this string Thu Nov 09 2006 14:42:30 GMT+0100 (Rome, Europe) - time() to JSON string: FastJSON::convert(time():Int32, true:Boolean):JSON Date String format You could send server time() informations and send them to clients. Example: FastJSON::convert(time(), true); i.e. 2006-11-09T14:42:30 - associative array to generic class: FastJSON::convert(array(params=>values), new GenericClass):new Instance of GenericClass With a decoded JSON object You could convert them into a new instance of your Generic Class. Example: class MyClass { var $param = "somevalue"; function MyClass($somevar) { $this->somevar = $somevar; }; function getVar = function(){ return $this->somevar; }; }; $instance = new MyClass("example"); $encoded = FastJSON::encode($instance); {"param":"somevalue"} $decoded = FastJSON::decode($encoded); $decoded instanceof Object => true $decoded instanceof MyClass => false $decoded = FastJSON::convert($decoded, new MyClass("example")); $decoded instanceof Object => true $decoded instanceof MyClass => true $decoded->getVar(); // example ---------------------------------------
Author: Andrea Giammarchi
ファイルを表示 Open project: jerodsanto/wordpress-console Class Usage Examples

Public Methods

Method Description
__decode ( &$encode, &$pos, &$slen, &$stdClass )
__decodeString ( &$encode, &$pos )
__endString ( &$encode, $position, &$pos )
__exit ( $str, $a, $b )
__getStaticReplacement ( ) private methods, uncommented, sorry
__slashedChar ( &$encode, $position )
convert ( $params, $result = null ) public static method
decode ( $encode, $stdClass = false ) public method
encode ( $decode ) : String public method

Method Details

__decode() public method

public __decode ( &$encode, &$pos, &$slen, &$stdClass )

__decodeString() public method

public __decodeString ( &$encode, &$pos )

__endString() public method

public __endString ( &$encode, $position, &$pos )

__exit() public method

public __exit ( $str, $a, $b )

__getStaticReplacement() public method

private methods, uncommented, sorry

__slashedChar() public method

public __slashedChar ( &$encode, $position )

convert() public method

FastJSON::convert(params:* [, result:Instance]):*
public convert ( $params, $result = null )

decode() public method

FastJSON::decode(params:String[, useStdClass:Boolean]):*
public decode ( $encode, $stdClass = false )

encode() public method

FastJSON::encode(params:*):String
public encode ( $decode ) : String
return String JSON genric object rappresentation or empty string if param is not compatible.