Merge "PrefixSearch: Enforce including the exact match as first result"
[lhc/web/wiklou.git] / includes / json / FormatJson.php
index 5565644..f3e5c76 100644 (file)
@@ -61,7 +61,14 @@ class FormatJson {
         *
         * @since 1.24
         */
-       const FORCE_ASSOC = 1;
+       const FORCE_ASSOC = 0x100;
+
+       /**
+        * If set, attempts to fix invalid json.
+        *
+        * @since 1.24
+        */
+       const TRY_FIXING = 0x200;
 
        /**
         * Regex that matches whitespace inside empty arrays and objects.
@@ -123,14 +130,16 @@ class FormatJson {
        }
 
        /**
-        * Decodes a JSON string.
+        * Decodes a JSON string. It is recommended to use FormatJson::parse(), which returns more comprehensive
+        * result in case of an error, and has more parsing options.
         *
         * @param string $value The JSON string being decoded
         * @param bool $assoc When true, returned objects will be converted into associative arrays.
         *
         * @return mixed The value encoded in JSON in appropriate PHP type.
-        * `null` is returned if the JSON cannot be decoded or if the encoded data is deeper than
-        * the recursion limit.
+        * `null` is returned if $value represented `null`, if $value could not be decoded,
+        * or if the encoded data was deeper than the recursion limit.
+        * Use FormatJson::parse() to distinguish between types of `null` and to get proper error code.
         */
        public static function decode( $value, $assoc = false ) {
                return json_decode( $value, $assoc );
@@ -138,17 +147,39 @@ class FormatJson {
 
        /**
         * Decodes a JSON string.
+        * Unlike FormatJson::decode(), if $value represents null value, it will be properly decoded as valid.
         *
         * @param string $value The JSON string being decoded
-        * @param int $options A bit field that allows FORCE_ASSOC, TRY_FIXING, WRAP_RESULT
-        * For backward compatibility, FORCE_ASSOC is set to 1 to match the legacy 'true'
-        * @return Status If good, the value is available in $result->getValue()
+        * @param int $options A bit field that allows FORCE_ASSOC, TRY_FIXING
+        * @return Status If valid JSON, the value is available in $result->getValue()
         */
        public static function parse( $value, $options = 0 ) {
                $assoc = ( $options & self::FORCE_ASSOC ) !== 0;
                $result = json_decode( $value, $assoc );
                $code = json_last_error();
 
+               if ( $code === JSON_ERROR_SYNTAX && ( $options & self::TRY_FIXING ) !== 0 ) {
+                       // The most common error is the trailing comma in a list or an object.
+                       // We cannot simply replace /,\s*[}\]]/ because it could be inside a string value.
+                       // But we could use the fact that JSON does not allow multi-line string values,
+                       // And remove trailing commas if they are et the end of a line.
+                       // JSON only allows 4 control characters: [ \t\r\n].  So we must not use '\s' for matching.
+                       // Regex match   ,]<any non-quote chars>\n   or   ,\n]   with optional spaces/tabs.
+                       $count = 0;
+                       $value =
+                               preg_replace( '/,([ \t]*[}\]][^"\r\n]*([\r\n]|$)|[ \t]*[\r\n][ \t\r\n]*[}\]])/', '$1',
+                                       $value, - 1, $count );
+                       if ( $count > 0 ) {
+                               $result = json_decode( $value, $assoc );
+                               if ( JSON_ERROR_NONE === json_last_error() ) {
+                                       // Report warning
+                                       $st = Status::newGood( $result );
+                                       $st->warning( wfMessage( 'json-warn-trailing-comma' )->numParams( $count ) );
+                                       return $st;
+                               }
+                       }
+               }
+
                switch ( $code ) {
                        case JSON_ERROR_NONE:
                                return Status::newGood( $result );