3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /**_______________________________________ * * 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 * @site http://www.devpro.it/ * @version 0.4 [fixed string convertion problems, add stdClass optional convertion instead of associative array (used by default)] * @requires anything * @compatibility PHP >= 4 * @license * --------------------------------------- * * Copyright (c) 2006 - 2007 Andrea Giammarchi * * Permission is hereby granted, free of charge, * to any person obtaining a copy of this software and associated * documentation files (the "Software"), * to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * _______________________________________ */ class FastJSON { // public methods /** * public static method * * FastJSON::convert(params:* [, result:Instance]):* * * @param * String or Object * @param Instance optional new generic class instance if first * parameter is an object. * @return * time() value or new Instance with object parameters. * * @note please read Special FastJSON::convert method Informations */ } ?>
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0