UPDATE 4.4.0.0
[phpmyadmin.git] / js / jquery / src / jquery / ajax / parseJSON.js
blob69b5c837d81643aae128308ca9d6c696d77000b8
1 define([
2         "../core"
3 ], function( jQuery ) {
5 var rvalidtokens = /(,)|(\[|{)|(}|])|"(?:[^"\\\r\n]|\\["\\\/bfnrt]|\\u[\da-fA-F]{4})*"\s*:?|true|false|null|-?(?!0\d)\d+(?:\.\d+|)(?:[eE][+-]?\d+|)/g;
7 jQuery.parseJSON = function( data ) {
8         // Attempt to parse using the native JSON parser first
9         if ( window.JSON && window.JSON.parse ) {
10                 // Support: Android 2.3
11                 // Workaround failure to string-cast null input
12                 return window.JSON.parse( data + "" );
13         }
15         var requireNonComma,
16                 depth = null,
17                 str = jQuery.trim( data + "" );
19         // Guard against invalid (and possibly dangerous) input by ensuring that nothing remains
20         // after removing valid tokens
21         return str && !jQuery.trim( str.replace( rvalidtokens, function( token, comma, open, close ) {
23                 // Force termination if we see a misplaced comma
24                 if ( requireNonComma && comma ) {
25                         depth = 0;
26                 }
28                 // Perform no more replacements after returning to outermost depth
29                 if ( depth === 0 ) {
30                         return token;
31                 }
33                 // Commas must not follow "[", "{", or ","
34                 requireNonComma = open || comma;
36                 // Determine new depth
37                 // array/object open ("[" or "{"): depth += true - false (increment)
38                 // array/object close ("]" or "}"): depth += false - true (decrement)
39                 // other cases ("," or primitive): depth += true - true (numeric cast)
40                 depth += !close - !open;
42                 // Remove this token
43                 return "";
44         }) ) ?
45                 ( Function( "return " + str ) )() :
46                 jQuery.error( "Invalid JSON: " + data );
49 return jQuery.parseJSON;
51 });