Revert r70874: <RoanKattouw> Dude that just looks kinda ugly
[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
22 public static function decode( $value, $assoc = self::AS_ARRAY ) {
23 if ( !function_exists( 'json_decode' ) ) {
24 $json = new Services_JSON();
25 $jsonDec = $json->decode( $value );
26 if( $assoc ) {
27 $jsonDec = wfObjectToArray( $jsonDec );
28 }
29 return $jsonDec;
30 } else {
31 return json_decode( $value, $assoc );
32 }
33 }
34 }