Skip to content

Commit befbed9

Browse files
committed
Core: Warn against using jQuery.parseJSON, remove old hacks around this method
Fixes #150 Refs jquery/jquery#2800 Refs #152
1 parent 1a7c6ea commit befbed9

File tree

7 files changed

+23
-136
lines changed

7 files changed

+23
-136
lines changed

Gruntfile.js

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module.exports = function( grunt ) {
2424
"src/attributes.js",
2525
"src/core.js",
2626
"src/css.js",
27-
"src/ajax.js",
2827
"src/data.js",
2928
"src/manipulation.js",
3029
"src/effects.js",

src/ajax.js

-7
This file was deleted.

src/core.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
var matched, browser,
33
oldInit = jQuery.fn.init,
4-
oldParseJSON = jQuery.parseJSON,
54
rspaceAngle = /^\s*</,
65
rattrHash = /\[\s*\w+\s*[~|^$*]?=\s*(?![\s'"])[^#\]]*#/,
76

@@ -76,15 +75,6 @@ jQuery.fn.init = function( selector, context, rootjQuery ) {
7675
};
7776
jQuery.fn.init.prototype = jQuery.fn;
7877

79-
// Let $.parseJSON(falsy_value) return null
80-
jQuery.parseJSON = function( json ) {
81-
if ( !json ) {
82-
migrateWarn( "jQuery.parseJSON requires a valid JSON string" );
83-
return null;
84-
}
85-
return oldParseJSON.apply( this, arguments );
86-
};
87-
8878
jQuery.uaMatch = function( ua ) {
8979
ua = ua.toLowerCase();
9080

@@ -159,3 +149,8 @@ jQuery.fn.size = function() {
159149
migrateWarn( "jQuery.fn.size() is deprecated; use the .length property" );
160150
return this.length;
161151
};
152+
153+
jQuery.parseJSON = function() {
154+
migrateWarn( "jQuery.parseJSON is deprecated; use JSON.parse" );
155+
return JSON.parse.apply( null, arguments );
156+
};

test/ajax.js

-74
This file was deleted.

test/core.js

+12-23
Original file line numberDiff line numberDiff line change
@@ -212,29 +212,6 @@ test( "jQuery( '<element>' ) usable on detached elements (#128)", function() {
212212
ok( true, "No crash when operating on detached elements with window" );
213213
} );
214214

215-
test( "jQuery.parseJSON() falsy values", function() {
216-
expect( 6 );
217-
218-
expectNoWarning( "valid JSON", function() {
219-
jQuery.parseJSON( "{\"a\":1}" );
220-
} );
221-
expectWarning( "actual null", function() {
222-
jQuery.parseJSON( null );
223-
} );
224-
expectNoWarning( "string null", function() {
225-
jQuery.parseJSON( "null" );
226-
} );
227-
expectWarning( "empty string", function() {
228-
jQuery.parseJSON( "" );
229-
} );
230-
expectWarning( "Boolean false", function() {
231-
jQuery.parseJSON( false );
232-
} );
233-
expectWarning( "undefined", function() {
234-
jQuery.parseJSON( undefined );
235-
} );
236-
} );
237-
238215
test( "jQuery.browser", function() {
239216
expect( 3 );
240217

@@ -386,3 +363,15 @@ test( ".size", function() {
386363
jQuery( "<div />" ).size();
387364
} );
388365
} );
366+
367+
test( "jQuery.parseJSON", function() {
368+
expect( 2 );
369+
370+
expectWarning( "jQuery.parseJSON", function() {
371+
deepEqual(
372+
jQuery.parseJSON( "{\"a\":1}" ),
373+
{ a: 1 },
374+
"jQuery.parseJSON output"
375+
);
376+
} );
377+
} );

test/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
<script src="data.js"></script>
4040
<script src="attributes.js"></script>
4141
<script src="manipulation.js"></script>
42-
<script src="ajax.js"></script>
4342
<script src="event.js"></script>
4443
<script src="traversing.js"></script>
4544
<script src="deferred.js"></script>

warnings.md

+6-20
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,6 @@ This is _not_ a warning, but a console log message the plugin shows when it firs
5656

5757
**Solution:** Do not use jQuery in Quirks mode, it has never been supported. See the previous item for solutions.
5858

59-
### JQMIGRATE: jQuery.parseJSON requires a valid JSON string
60-
61-
**Cause**: Before jQuery 1.9.0, the `$.parseJSON()` method allowed some invalid JSON strings and returned `null` as a result without throwing an error. This put it at odds with the `JSON.parse()` method. The two methods are aligned as of 1.9.0 and values such as an empty string are properly not considered valid by `$.parseJSON()`.
62-
63-
**Solution:** If you want to consider values such as `""` or `false` successful and treat them as `null`, check for them before calling `$.parseJSON()`. Since falsy values such as an empty string were previously returned as a `null` without complaint, this code will suffice in most cases:
64-
```js
65-
var json = $.parseJSON(jsonString || "null");
66-
```
67-
If your own code is not calling `$.parseJSON()` directly, it is probably using AJAX to retrieve a JSON value from a server that is returning an empty string in the content body rather than a valid JSON response such as `null` or `{}`. If it isn't possible to correct the invalid JSON in the server response, you can retrieve the response as text:
68-
```js
69-
$.ajax({
70-
url: "...",
71-
dataType: "text",
72-
success: function( text ) {
73-
var json = text? $.parseJSON(text) : null;
74-
...
75-
}
76-
});
77-
```
78-
7959
### JQMIGRATE: jQuery.browser is deprecated
8060

8161
**Cause:** `jQuery.browser` was deprecated in version 1.3, and finally removed in 1.9. Browser sniffing is notoriously unreliable as means of detecting whether to implement particular features.
@@ -240,3 +220,9 @@ jQuery.easing.easeInCubic = function ( p ) {
240220
```
241221

242222
See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58b6ad122ab425c4cc1a4da4a520#diff-9cd789a170c765edcf0f4854db386e1a) for other possible cases.
223+
224+
### JQMIGRATE: jQuery.parseJSON is deprecated; use JSON.parse
225+
226+
**Cause**: The `jQuery.parseJSON` method in recent jQuery is identical to the native `JSON.parse`. As of jQuery 3.0 `jQuery.parseJSON` is deprecated.
227+
228+
**Solution**: Replace any use of `jQuery.parseJSON` with `JSON.parse`.

0 commit comments

Comments
 (0)