diff options
Diffstat (limited to 'includes/json')
-rw-r--r-- | includes/json/FormatJson.php | 54 | ||||
-rw-r--r-- | includes/json/Services_JSON.php | 48 |
2 files changed, 68 insertions, 34 deletions
diff --git a/includes/json/FormatJson.php b/includes/json/FormatJson.php index 6db4a23f..b7049aeb 100644 --- a/includes/json/FormatJson.php +++ b/includes/json/FormatJson.php @@ -1,32 +1,60 @@ <?php -/* - * simple wrapper for json_econde and json_decode that falls back on Services_JSON class +/** + * Simple wrapper for json_econde and json_decode that falls back on Services_JSON class + * + * @file */ -if( !(defined( 'MEDIAWIKI' ) ) ) { + +if ( !defined( 'MEDIAWIKI' ) ) { die( 1 ); } -class FormatJson{ - public static function encode($value, $isHtml=false){ +require_once dirname( __FILE__ ) . '/Services_JSON.php'; + +class FormatJson { + + /** + * Returns the JSON representation of a value. + * + * @param $value Mixed: the value being encoded. Can be any type except a resource. + * @param $isHtml Boolean + * + * @return string + */ + public static function encode( $value, $isHtml = false ) { // Some versions of PHP have a broken json_encode, see PHP bug // 46944. Test encoding an affected character (U+20000) to // avoid this. - if (!function_exists('json_encode') || $isHtml || strtolower(json_encode("\xf0\xa0\x80\x80")) != '\ud840\udc00') { + if ( !function_exists( 'json_encode' ) || $isHtml || strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '\ud840\udc00' ) { $json = new Services_JSON(); - return $json->encode($value, $isHtml) ; + return $json->encode( $value, $isHtml ); } else { - return json_encode($value); + return json_encode( $value ); } } - public static function decode( $value, $assoc=false ){ - if (!function_exists('json_decode') ) { - $json = new Services_JSON(); - $jsonDec = $json->decode( $value ); + + /** + * Decodes a JSON string. + * + * @param $value String: the json string being decoded. + * @param $assoc Boolean: when true, returned objects will be converted into associative arrays. + * + * @return Mixed: the value encoded in json in appropriate PHP type. + * Values true, false and null (case-insensitive) are returned as true, false + * and &null; respectively. &null; is returned if the json cannot be + * decoded or if the encoded data is deeper than the recursion limit. + */ + public static function decode( $value, $assoc = false ) { + if ( !function_exists( 'json_decode' ) ) { if( $assoc ) - $jsonDec = wfObjectToArray( $jsonDec ); + $json = new Services_JSON( SERVICES_JSON_LOOSE_TYPE ); + else + $json = new Services_JSON(); + $jsonDec = $json->decode( $value ); return $jsonDec; } else { return json_decode( $value, $assoc ); } } + } diff --git a/includes/json/Services_JSON.php b/includes/json/Services_JSON.php index 588ece9c..5b4e0503 100644 --- a/includes/json/Services_JSON.php +++ b/includes/json/Services_JSON.php @@ -45,12 +45,13 @@ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * +* @file * @ingroup API * @author Michal Migurski <mike-json@teczno.com> * @author Matt Knapp <mdknapp[at]gmail[dot]com> * @author Brett Stimmerman <brettstimmerman[at]gmail[dot]com> * @copyright 2005 Michal Migurski -* @version CVS: $Id: Services_JSON.php 79562 2011-01-04 06:15:54Z tstarling $ +* @version CVS: $Id: Services_JSON.php 90492 2011-06-20 22:39:10Z reedy $ * @license http://www.opensource.org/licenses/bsd-license.php * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198 */ @@ -118,7 +119,7 @@ class Services_JSON /** * constructs a new JSON instance * - * @param int $use object behavior flags; combine with boolean-OR + * @param $use Integer: object behavior flags; combine with boolean-OR * * possible values: * - SERVICES_JSON_LOOSE_TYPE: loose typing. @@ -131,7 +132,7 @@ class Services_JSON * bubble up with an error, so all return values * from encode() should be checked with isError() */ - function Services_JSON($use = 0) + function __construct($use = 0) { $this->use = $use; } @@ -156,8 +157,8 @@ class Services_JSON * provides a slower PHP-only method for installations * that lack the multibye string extension. * - * @param string $utf16 UTF-16 character - * @return string UTF-8 character + * @param $utf16 String: UTF-16 character + * @return String: UTF-8 character * @access private */ function utf162utf8($utf16) @@ -211,8 +212,8 @@ class Services_JSON * provides a slower PHP-only method for installations * that lack the multibye string extension. * - * @param string $utf8 UTF-8 character - * @return string UTF-16 character + * @param $utf8 String: UTF-8 character + * @return String: UTF-16 character * @access private */ function utf82utf16($utf8) @@ -265,11 +266,11 @@ class Services_JSON /** * encodes an arbitrary variable into JSON format * - * @param mixed $var any number, boolean, string, array, or object to be encoded. + * @param $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! - * @param bool $pretty pretty-print output with indents and newlines + * @param $pretty Boolean: pretty-print output with indents and newlines * * @return mixed JSON string representation of input var or an error if a problem occurs * @access public @@ -285,7 +286,7 @@ class Services_JSON /** * encodes an arbitrary variable into JSON format * - * @param mixed $var any number, boolean, string, array, or object to be encoded. + * @param $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! @@ -431,7 +432,7 @@ class Services_JSON $this->indent--; foreach($properties as $property) { - if(Services_JSON::isError($property)) { + if($this->isError($property)) { return $property; } } @@ -445,7 +446,7 @@ class Services_JSON $this->indent--; foreach($elements as $element) { - if(Services_JSON::isError($element)) { + if($this->isError($element)) { return $element; } } @@ -462,7 +463,7 @@ class Services_JSON $this->indent--; foreach($properties as $property) { - if(Services_JSON::isError($property)) { + if($this->isError($property)) { return $property; } } @@ -479,17 +480,17 @@ class Services_JSON /** * array-walking function for use in generating JSON-formatted name-value pairs * - * @param string $name name of key to use - * @param mixed $value reference to an array element to be encoded + * @param $name String: name of key to use + * @param $value Mixed: reference to an array element to be encoded * - * @return string JSON-formatted name-value pair, like '"name":value' + * @return String: JSON-formatted name-value pair, like '"name":value' * @access private */ function name_value($name, $value) { $encoded_value = $this->encode2($value); - if(Services_JSON::isError($encoded_value)) { + if($this->isError($encoded_value)) { return $encoded_value; } @@ -499,9 +500,9 @@ class Services_JSON /** * reduce a string by removing leading and trailing comments and whitespace * - * @param $str string string value to strip of comments and whitespace + * @param $str String: string value to strip of comments and whitespace * - * @return string string value stripped of comments and whitespace + * @return String: string value stripped of comments and whitespace * @access private */ function reduce_string($str) @@ -526,7 +527,7 @@ class Services_JSON /** * decodes a JSON string into appropriate variable * - * @param string $str JSON-formatted string + * @param $str String: JSON-formatted string * * @return mixed number, boolean, string, array, or object * corresponding to given JSON input string. @@ -869,7 +870,12 @@ if (class_exists('PEAR_Error')) { function Services_JSON_Error($message = 'unknown error', $code = null, $mode = null, $options = null, $userinfo = null) { - + $this->message = $message; + } + + function __toString() + { + return $this->message; } } } |