Skip to content

Commit 93a8fa6

Browse files
committedMar 2, 2016
Core: Deprecate jQuery.parseJSON
Fixes gh-2800 Closes gh-2948
1 parent 8a91f84 commit 93a8fa6

File tree

9 files changed

+80
-107
lines changed

9 files changed

+80
-107
lines changed
 

‎src/ajax.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ define( [
77
"./ajax/var/rquery",
88

99
"./core/init",
10-
"./ajax/parseJSON",
1110
"./ajax/parseXML",
1211
"./event/trigger",
1312
"./deferred",
@@ -348,7 +347,7 @@ jQuery.extend( {
348347
"text html": true,
349348

350349
// Evaluate text as a json expression
351-
"text json": jQuery.parseJSON,
350+
"text json": JSON.parse,
352351

353352
// Parse text as xml
354353
"text xml": jQuery.parseXML

‎src/ajax/parseJSON.js

-9
This file was deleted.

‎src/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function dataAttr( elem, key, data ) {
3535

3636
// Only convert to a number if it doesn't change the string
3737
+data + "" === data ? +data :
38-
rbrace.test( data ) ? jQuery.parseJSON( data ) :
38+
rbrace.test( data ) ? JSON.parse( data ) :
3939
data;
4040
} catch ( e ) {}
4141

‎src/deprecated.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ jQuery.fn.extend( {
2323
}
2424
} );
2525

26+
jQuery.parseJSON = JSON.parse;
27+
2628
} );

‎test/unit/ajax.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ QUnit.module( "ajax", {
11201120
},
11211121
success: function( text ) {
11221122
assert.strictEqual( typeof text, "string", "json wasn't auto-determined" );
1123-
var json = jQuery.parseJSON( text );
1123+
var json = JSON.parse( text );
11241124
assert.ok( json.length >= 2, "Check length" );
11251125
assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" );
11261126
assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" );

‎test/unit/basic.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ QUnit.test( "show/hide", function( assert ) {
7676
}
7777

7878
QUnit.test( "core", function( assert ) {
79-
assert.expect( 28 );
79+
assert.expect( 27 );
8080

8181
var elem = jQuery( "<div></div><span></span>" );
8282

@@ -135,8 +135,6 @@ QUnit.test( "core", function( assert ) {
135135

136136
assert.strictEqual( jQuery.parseHTML( "<div></div><span></span>" ).length,
137137
2, "jQuery.parseHTML" );
138-
139-
assert.deepEqual( jQuery.parseJSON( "{\"a\": 2}" ), { a: 2 }, "jQuery.parseJON" );
140138
} );
141139

142140
QUnit.test( "data", function( assert ) {

‎test/unit/core.js

-87
Original file line numberDiff line numberDiff line change
@@ -1548,93 +1548,6 @@ if ( jQuery.support.createHTMLDocument ) {
15481548
} );
15491549
}
15501550

1551-
QUnit.test( "jQuery.parseJSON", function( assert ) {
1552-
assert.expect( 20 );
1553-
1554-
assert.strictEqual( jQuery.parseJSON( null ), null, "primitive null" );
1555-
assert.strictEqual( jQuery.parseJSON( "0.88" ), 0.88, "Number" );
1556-
assert.strictEqual(
1557-
jQuery.parseJSON( "\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u007E \\u263a \"" ),
1558-
" \" \\ / \b \f \n \r \t ~ \u263A ",
1559-
"String escapes"
1560-
);
1561-
assert.deepEqual( jQuery.parseJSON( "{}" ), {}, "Empty object" );
1562-
assert.deepEqual( jQuery.parseJSON( "{\"test\":1}" ), { "test": 1 }, "Plain object" );
1563-
assert.deepEqual( jQuery.parseJSON( "[0]" ), [ 0 ], "Simple array" );
1564-
1565-
assert.deepEqual(
1566-
jQuery.parseJSON( "[ \"string\", -4.2, 2.7180e0, 3.14E-1, {}, [], true, false, null ]" ),
1567-
[ "string", -4.2, 2.718, 0.314, {}, [], true, false, null ],
1568-
"Array of all data types"
1569-
);
1570-
assert.deepEqual(
1571-
jQuery.parseJSON( "{ \"string\": \"\", \"number\": 4.2e+1, \"object\": {}," +
1572-
"\"array\": [[]], \"boolean\": [ true, false ], \"null\": null }" ),
1573-
{ string: "", number: 42, object: {}, array: [ [] ], "boolean": [ true, false ], "null": null },
1574-
"Dictionary of all data types"
1575-
);
1576-
1577-
assert.deepEqual( jQuery.parseJSON( "\n{\"test\":1}\t" ), { "test": 1 },
1578-
"Leading and trailing whitespace are ignored" );
1579-
1580-
assert.throws( function() {
1581-
jQuery.parseJSON();
1582-
}, null, "Undefined raises an error" );
1583-
assert.throws( function() {
1584-
jQuery.parseJSON( "" );
1585-
}, null, "Empty string raises an error" );
1586-
assert.throws( function() {
1587-
jQuery.parseJSON( "''" );
1588-
}, null, "Single-quoted string raises an error" );
1589-
/*
1590-
1591-
// Broken on IE8
1592-
assert.throws(function() {
1593-
jQuery.parseJSON("\" \\a \"");
1594-
}, null, "Invalid string escape raises an error" );
1595-
1596-
// Broken on IE8, Safari 5.1 Windows
1597-
assert.throws(function() {
1598-
jQuery.parseJSON("\"\t\"");
1599-
}, null, "Unescaped control character raises an error" );
1600-
1601-
// Broken on IE8
1602-
assert.throws(function() {
1603-
jQuery.parseJSON(".123");
1604-
}, null, "Number with no integer component raises an error" );
1605-
1606-
*/
1607-
assert.throws( function() {
1608-
var result = jQuery.parseJSON( "0101" );
1609-
1610-
// Support: IE9+
1611-
// Ensure base-10 interpretation on browsers that erroneously accept leading-zero numbers
1612-
if ( result === 101 ) {
1613-
throw new Error( "close enough" );
1614-
}
1615-
}, null, "Leading-zero number raises an error or is parsed as decimal" );
1616-
assert.throws( function() {
1617-
jQuery.parseJSON( "{a:1}" );
1618-
}, null, "Unquoted property raises an error" );
1619-
assert.throws( function() {
1620-
jQuery.parseJSON( "{'a':1}" );
1621-
}, null, "Single-quoted property raises an error" );
1622-
assert.throws( function() {
1623-
jQuery.parseJSON( "[,]" );
1624-
}, null, "Array element elision raises an error" );
1625-
assert.throws( function() {
1626-
jQuery.parseJSON( "{},[]" );
1627-
}, null, "Comma expression raises an error" );
1628-
assert.throws( function() {
1629-
jQuery.parseJSON( "[]\n,{}" );
1630-
}, null, "Newline-containing comma expression raises an error" );
1631-
assert.throws( function() {
1632-
jQuery.parseJSON( "\"\"\n\"\"" );
1633-
}, null, "Automatic semicolon insertion raises an error" );
1634-
1635-
assert.strictEqual( jQuery.parseJSON( [ 0 ] ), 0, "Input cast to string" );
1636-
} );
1637-
16381551
QUnit.test( "jQuery.parseXML", function( assert ) {
16391552
assert.expect( 8 );
16401553

‎test/unit/data.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ QUnit.test( "data-* attributes", function( assert ) {
277277

278278
var prop, i, l, metadata, elem,
279279
obj, obj2, check, num, num2,
280-
parseJSON = jQuery.parseJSON,
280+
parseJSON = JSON.parse,
281281
div = jQuery( "<div>" ),
282282
child = jQuery( "<div data-myobj='old data' data-ignored=\"DOM\" data-other='test' data-foo-42='boosh'></div>" ),
283283
dummy = jQuery( "<div data-myobj='old data' data-ignored=\"DOM\" data-other='test' data-foo-42='boosh'></div>" );
@@ -336,7 +336,7 @@ QUnit.test( "data-* attributes", function( assert ) {
336336

337337
// attribute parsing
338338
i = 0;
339-
jQuery.parseJSON = function() {
339+
JSON.parse = function() {
340340
i++;
341341
return parseJSON.apply( this, arguments );
342342
};
@@ -389,7 +389,7 @@ QUnit.test( "data-* attributes", function( assert ) {
389389
assert.strictEqual( child.data( "string" ), "test", "Typical string read from attribute" );
390390
assert.equal( i, 2, "Correct number of JSON parse attempts when reading from attributes" );
391391

392-
jQuery.parseJSON = parseJSON;
392+
JSON.parse = parseJSON;
393393
child.remove();
394394

395395
// tests from metadata plugin

‎test/unit/deprecated.js

+71-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,74 @@ QUnit.test( "delegate/undelegate", function( assert ) {
3939
.end()
4040
.undelegate( "b", "click" )
4141
.remove();
42-
} );
42+
} );
43+
44+
QUnit.test( "jQuery.parseJSON", function( assert ) {
45+
assert.expect( 20 );
46+
47+
assert.strictEqual( jQuery.parseJSON( null ), null, "primitive null" );
48+
assert.strictEqual( jQuery.parseJSON( "0.88" ), 0.88, "Number" );
49+
assert.strictEqual(
50+
jQuery.parseJSON( "\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u007E \\u263a \"" ),
51+
" \" \\ / \b \f \n \r \t ~ \u263A ",
52+
"String escapes"
53+
);
54+
assert.deepEqual( jQuery.parseJSON( "{}" ), {}, "Empty object" );
55+
assert.deepEqual( jQuery.parseJSON( "{\"test\":1}" ), { "test": 1 }, "Plain object" );
56+
assert.deepEqual( jQuery.parseJSON( "[0]" ), [ 0 ], "Simple array" );
57+
58+
assert.deepEqual(
59+
jQuery.parseJSON( "[ \"string\", -4.2, 2.7180e0, 3.14E-1, {}, [], true, false, null ]" ),
60+
[ "string", -4.2, 2.718, 0.314, {}, [], true, false, null ],
61+
"Array of all data types"
62+
);
63+
assert.deepEqual(
64+
jQuery.parseJSON( "{ \"string\": \"\", \"number\": 4.2e+1, \"object\": {}," +
65+
"\"array\": [[]], \"boolean\": [ true, false ], \"null\": null }" ),
66+
{ string: "", number: 42, object: {}, array: [ [] ], "boolean": [ true, false ], "null": null },
67+
"Dictionary of all data types"
68+
);
69+
70+
assert.deepEqual( jQuery.parseJSON( "\n{\"test\":1}\t" ), { "test": 1 },
71+
"Leading and trailing whitespace are ignored" );
72+
73+
assert.throws( function() {
74+
jQuery.parseJSON();
75+
}, null, "Undefined raises an error" );
76+
assert.throws( function() {
77+
jQuery.parseJSON( "" );
78+
}, null, "Empty string raises an error" );
79+
assert.throws( function() {
80+
jQuery.parseJSON( "''" );
81+
}, null, "Single-quoted string raises an error" );
82+
83+
assert.throws( function() {
84+
var result = jQuery.parseJSON( "0101" );
85+
86+
// Support: IE9+
87+
// Ensure base-10 interpretation on browsers that erroneously accept leading-zero numbers
88+
if ( result === 101 ) {
89+
throw new Error( "close enough" );
90+
}
91+
}, null, "Leading-zero number raises an error or is parsed as decimal" );
92+
assert.throws( function() {
93+
jQuery.parseJSON( "{a:1}" );
94+
}, null, "Unquoted property raises an error" );
95+
assert.throws( function() {
96+
jQuery.parseJSON( "{'a':1}" );
97+
}, null, "Single-quoted property raises an error" );
98+
assert.throws( function() {
99+
jQuery.parseJSON( "[,]" );
100+
}, null, "Array element elision raises an error" );
101+
assert.throws( function() {
102+
jQuery.parseJSON( "{},[]" );
103+
}, null, "Comma expression raises an error" );
104+
assert.throws( function() {
105+
jQuery.parseJSON( "[]\n,{}" );
106+
}, null, "Newline-containing comma expression raises an error" );
107+
assert.throws( function() {
108+
jQuery.parseJSON( "\"\"\n\"\"" );
109+
}, null, "Automatic semicolon insertion raises an error" );
110+
111+
assert.strictEqual( jQuery.parseJSON( [ 0 ] ), 0, "Input cast to string" );
112+
} );

0 commit comments

Comments
 (0)