3 * Simple wrapper for json_econde and json_decode that falls back on Services_JSON class
8 if ( !defined( 'MEDIAWIKI' ) ) {
15 * Returns the JSON representation of a value.
17 * @param $value Mixed: the value being encoded. Can be any type except a resource.
18 * @param $isHtml Boolean
20 * @fixme "$isHtml" parameter's purpose is not documented. It appears to
21 * map to a parameter labeled "pretty-print output with indents and
22 * newlines" in Services_JSON::encode(), which has no string relation
27 public static function encode( $value, $isHtml = false ) {
28 // Some versions of PHP have a broken json_encode, see PHP bug
29 // 46944. Test encoding an affected character (U+20000) to
31 if ( !function_exists( 'json_encode' ) ||
$isHtml ||
strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '\ud840\udc00' ) {
32 $json = new Services_JSON();
33 return $json->encode( $value, $isHtml );
35 return json_encode( $value );
40 * Decodes a JSON string.
42 * @param $value String: the json string being decoded.
43 * @param $assoc Boolean: when true, returned objects will be converted into associative arrays.
45 * @return Mixed: the value encoded in json in appropriate PHP type.
46 * Values true, false and null (case-insensitive) are returned as true, false
47 * and &null; respectively. &null; is returned if the json cannot be
48 * decoded or if the encoded data is deeper than the recursion limit.
50 public static function decode( $value, $assoc = false ) {
51 if ( !function_exists( 'json_decode' ) ) {
53 $json = new Services_JSON( SERVICES_JSON_LOOSE_TYPE
);
55 $json = new Services_JSON();
56 $jsonDec = $json->decode( $value );
59 return json_decode( $value, $assoc );