9a1f57fd8824088e6f8c5dba1de4ed94847be5bb
[lhc/web/wiklou.git] / includes / json / FormatJson.php
1 <?php
2 /*
3 * simple wrapper for json_econde and json_decode that falls back on Services_JSON class
4 */
5 if( !(defined( 'MEDIAWIKI' ) ) ) {
6 die( 1 );
7 }
8
9 class FormatJson {
10 public static function encode($value, $isHtml=false){
11 // Some versions of PHP have a broken json_encode, see PHP bug
12 // 46944. Test encoding an affected character (U+20000) to
13 // avoid this.
14 if (!function_exists('json_encode') || $isHtml || strtolower(json_encode("\xf0\xa0\x80\x80")) != '\ud840\udc00') {
15 $json = new Services_JSON();
16 return $json->encode($value, $isHtml) ;
17 } else {
18 return json_encode($value);
19 }
20 }
21 public static function decode( $value, $assoc=false ){
22 if (!function_exists('json_decode') ) {
23 $json = new Services_JSON();
24 $jsonDec = $json->decode( $value );
25 if( $assoc )
26 $jsonDec = wfObjectToArray( $jsonDec );
27 return $jsonDec;
28 } else {
29 return json_decode( $value, $assoc );
30 }
31 }
32 }