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