(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / json / FormatJson.php
blobf7373e450af3d5bae42420f6e8c3b144191f1960
1 <?php
2 /**
3 * Simple wrapper for json_econde and json_decode that falls back on Services_JSON class
5 * @file
6 */
8 if ( !defined( 'MEDIAWIKI' ) ) {
9 die( 1 );
12 require_once dirname( __FILE__ ) . '/Services_JSON.php';
14 /**
15 * JSON formatter wrapper class
17 class FormatJson {
19 /**
20 * Returns the JSON representation of a value.
22 * @param $value Mixed: the value being encoded. Can be any type except a resource.
23 * @param $isHtml Boolean
25 * @todo FIXME: "$isHtml" parameter's purpose is not documented. It appears to
26 * map to a parameter labeled "pretty-print output with indents and
27 * newlines" in Services_JSON::encode(), which has no string relation
28 * to HTML output.
30 * @return string
32 public static function encode( $value, $isHtml = false ) {
33 // Some versions of PHP have a broken json_encode, see PHP bug
34 // 46944. Test encoding an affected character (U+20000) to
35 // avoid this.
36 if ( !function_exists( 'json_encode' ) || $isHtml || strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '"\ud840\udc00"' ) {
37 $json = new Services_JSON();
38 return $json->encode( $value, $isHtml );
39 } else {
40 return json_encode( $value );
44 /**
45 * Decodes a JSON string.
47 * @param $value String: the json string being decoded.
48 * @param $assoc Boolean: when true, returned objects will be converted into associative arrays.
50 * @return Mixed: the value encoded in json in appropriate PHP type.
51 * Values true, false and null (case-insensitive) are returned as true, false
52 * and &null; respectively. &null; is returned if the json cannot be
53 * decoded or if the encoded data is deeper than the recursion limit.
55 public static function decode( $value, $assoc = false ) {
56 if ( !function_exists( 'json_decode' ) ) {
57 $json = $assoc ? new Services_JSON( SERVICES_JSON_LOOSE_TYPE ) :
58 new Services_JSON();
59 $jsonDec = $json->decode( $value );
60 return $jsonDec;
61 } else {
62 return json_decode( $value, $assoc );