From 93a8fa6bfc1c8a469e188630b61e736dfb69e128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski?= Date: Wed, 24 Feb 2016 23:47:19 +0100 Subject: [PATCH] Core: Deprecate jQuery.parseJSON Fixes gh-2800 Closes gh-2948 --- src/ajax.js | 3 +- src/ajax/parseJSON.js | 9 ----- src/data.js | 2 +- src/deprecated.js | 2 + test/unit/ajax.js | 2 +- test/unit/basic.js | 4 +- test/unit/core.js | 87 ----------------------------------------- test/unit/data.js | 6 +-- test/unit/deprecated.js | 72 +++++++++++++++++++++++++++++++++- 9 files changed, 80 insertions(+), 107 deletions(-) delete mode 100644 src/ajax/parseJSON.js diff --git a/src/ajax.js b/src/ajax.js index 35b29bab4..a71428580 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -7,7 +7,6 @@ define( [ "./ajax/var/rquery", "./core/init", - "./ajax/parseJSON", "./ajax/parseXML", "./event/trigger", "./deferred", @@ -348,7 +347,7 @@ jQuery.extend( { "text html": true, // Evaluate text as a json expression - "text json": jQuery.parseJSON, + "text json": JSON.parse, // Parse text as xml "text xml": jQuery.parseXML diff --git a/src/ajax/parseJSON.js b/src/ajax/parseJSON.js deleted file mode 100644 index c2aeb6aae..000000000 --- a/src/ajax/parseJSON.js +++ /dev/null @@ -1,9 +0,0 @@ -define( [ - "../core" -], function( jQuery ) { - -jQuery.parseJSON = JSON.parse; - -return jQuery.parseJSON; - -} ); diff --git a/src/data.js b/src/data.js index d2855dde1..9c296ef62 100644 --- a/src/data.js +++ b/src/data.js @@ -35,7 +35,7 @@ function dataAttr( elem, key, data ) { // Only convert to a number if it doesn't change the string +data + "" === data ? +data : - rbrace.test( data ) ? jQuery.parseJSON( data ) : + rbrace.test( data ) ? JSON.parse( data ) : data; } catch ( e ) {} diff --git a/src/deprecated.js b/src/deprecated.js index 78885266d..79522e9b1 100644 --- a/src/deprecated.js +++ b/src/deprecated.js @@ -23,4 +23,6 @@ jQuery.fn.extend( { } } ); +jQuery.parseJSON = JSON.parse; + } ); diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 3e00afd4e..63f0edd9f 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -1120,7 +1120,7 @@ QUnit.module( "ajax", { }, success: function( text ) { assert.strictEqual( typeof text, "string", "json wasn't auto-determined" ); - var json = jQuery.parseJSON( text ); + var json = JSON.parse( text ); assert.ok( json.length >= 2, "Check length" ); assert.strictEqual( json[ 0 ][ "name" ], "John", "Check JSON: first, name" ); assert.strictEqual( json[ 0 ][ "age" ], 21, "Check JSON: first, age" ); diff --git a/test/unit/basic.js b/test/unit/basic.js index 49b518d3f..8a3d19ae6 100644 --- a/test/unit/basic.js +++ b/test/unit/basic.js @@ -76,7 +76,7 @@ QUnit.test( "show/hide", function( assert ) { } QUnit.test( "core", function( assert ) { - assert.expect( 28 ); + assert.expect( 27 ); var elem = jQuery( "
" ); @@ -135,8 +135,6 @@ QUnit.test( "core", function( assert ) { assert.strictEqual( jQuery.parseHTML( "
" ).length, 2, "jQuery.parseHTML" ); - - assert.deepEqual( jQuery.parseJSON( "{\"a\": 2}" ), { a: 2 }, "jQuery.parseJON" ); } ); QUnit.test( "data", function( assert ) { diff --git a/test/unit/core.js b/test/unit/core.js index accba6bd6..99b2acc45 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -1548,93 +1548,6 @@ if ( jQuery.support.createHTMLDocument ) { } ); } -QUnit.test( "jQuery.parseJSON", function( assert ) { - assert.expect( 20 ); - - assert.strictEqual( jQuery.parseJSON( null ), null, "primitive null" ); - assert.strictEqual( jQuery.parseJSON( "0.88" ), 0.88, "Number" ); - assert.strictEqual( - jQuery.parseJSON( "\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u007E \\u263a \"" ), - " \" \\ / \b \f \n \r \t ~ \u263A ", - "String escapes" - ); - assert.deepEqual( jQuery.parseJSON( "{}" ), {}, "Empty object" ); - assert.deepEqual( jQuery.parseJSON( "{\"test\":1}" ), { "test": 1 }, "Plain object" ); - assert.deepEqual( jQuery.parseJSON( "[0]" ), [ 0 ], "Simple array" ); - - assert.deepEqual( - jQuery.parseJSON( "[ \"string\", -4.2, 2.7180e0, 3.14E-1, {}, [], true, false, null ]" ), - [ "string", -4.2, 2.718, 0.314, {}, [], true, false, null ], - "Array of all data types" - ); - assert.deepEqual( - jQuery.parseJSON( "{ \"string\": \"\", \"number\": 4.2e+1, \"object\": {}," + - "\"array\": [[]], \"boolean\": [ true, false ], \"null\": null }" ), - { string: "", number: 42, object: {}, array: [ [] ], "boolean": [ true, false ], "null": null }, - "Dictionary of all data types" - ); - - assert.deepEqual( jQuery.parseJSON( "\n{\"test\":1}\t" ), { "test": 1 }, - "Leading and trailing whitespace are ignored" ); - - assert.throws( function() { - jQuery.parseJSON(); - }, null, "Undefined raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "" ); - }, null, "Empty string raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "''" ); - }, null, "Single-quoted string raises an error" ); - /* - - // Broken on IE8 - assert.throws(function() { - jQuery.parseJSON("\" \\a \""); - }, null, "Invalid string escape raises an error" ); - - // Broken on IE8, Safari 5.1 Windows - assert.throws(function() { - jQuery.parseJSON("\"\t\""); - }, null, "Unescaped control character raises an error" ); - - // Broken on IE8 - assert.throws(function() { - jQuery.parseJSON(".123"); - }, null, "Number with no integer component raises an error" ); - - */ - assert.throws( function() { - var result = jQuery.parseJSON( "0101" ); - - // Support: IE9+ - // Ensure base-10 interpretation on browsers that erroneously accept leading-zero numbers - if ( result === 101 ) { - throw new Error( "close enough" ); - } - }, null, "Leading-zero number raises an error or is parsed as decimal" ); - assert.throws( function() { - jQuery.parseJSON( "{a:1}" ); - }, null, "Unquoted property raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "{'a':1}" ); - }, null, "Single-quoted property raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "[,]" ); - }, null, "Array element elision raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "{},[]" ); - }, null, "Comma expression raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "[]\n,{}" ); - }, null, "Newline-containing comma expression raises an error" ); - assert.throws( function() { - jQuery.parseJSON( "\"\"\n\"\"" ); - }, null, "Automatic semicolon insertion raises an error" ); - - assert.strictEqual( jQuery.parseJSON( [ 0 ] ), 0, "Input cast to string" ); -} ); - QUnit.test( "jQuery.parseXML", function( assert ) { assert.expect( 8 ); diff --git a/test/unit/data.js b/test/unit/data.js index 31c6a6174..19f32504b 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -277,7 +277,7 @@ QUnit.test( "data-* attributes", function( assert ) { var prop, i, l, metadata, elem, obj, obj2, check, num, num2, - parseJSON = jQuery.parseJSON, + parseJSON = JSON.parse, div = jQuery( "
" ), child = jQuery( "
" ), dummy = jQuery( "
" ); @@ -336,7 +336,7 @@ QUnit.test( "data-* attributes", function( assert ) { // attribute parsing i = 0; - jQuery.parseJSON = function() { + JSON.parse = function() { i++; return parseJSON.apply( this, arguments ); }; @@ -389,7 +389,7 @@ QUnit.test( "data-* attributes", function( assert ) { assert.strictEqual( child.data( "string" ), "test", "Typical string read from attribute" ); assert.equal( i, 2, "Correct number of JSON parse attempts when reading from attributes" ); - jQuery.parseJSON = parseJSON; + JSON.parse = parseJSON; child.remove(); // tests from metadata plugin diff --git a/test/unit/deprecated.js b/test/unit/deprecated.js index 797290f3b..baf4562b1 100644 --- a/test/unit/deprecated.js +++ b/test/unit/deprecated.js @@ -39,4 +39,74 @@ QUnit.test( "delegate/undelegate", function( assert ) { .end() .undelegate( "b", "click" ) .remove(); -} ); \ No newline at end of file +} ); + +QUnit.test( "jQuery.parseJSON", function( assert ) { + assert.expect( 20 ); + + assert.strictEqual( jQuery.parseJSON( null ), null, "primitive null" ); + assert.strictEqual( jQuery.parseJSON( "0.88" ), 0.88, "Number" ); + assert.strictEqual( + jQuery.parseJSON( "\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u007E \\u263a \"" ), + " \" \\ / \b \f \n \r \t ~ \u263A ", + "String escapes" + ); + assert.deepEqual( jQuery.parseJSON( "{}" ), {}, "Empty object" ); + assert.deepEqual( jQuery.parseJSON( "{\"test\":1}" ), { "test": 1 }, "Plain object" ); + assert.deepEqual( jQuery.parseJSON( "[0]" ), [ 0 ], "Simple array" ); + + assert.deepEqual( + jQuery.parseJSON( "[ \"string\", -4.2, 2.7180e0, 3.14E-1, {}, [], true, false, null ]" ), + [ "string", -4.2, 2.718, 0.314, {}, [], true, false, null ], + "Array of all data types" + ); + assert.deepEqual( + jQuery.parseJSON( "{ \"string\": \"\", \"number\": 4.2e+1, \"object\": {}," + + "\"array\": [[]], \"boolean\": [ true, false ], \"null\": null }" ), + { string: "", number: 42, object: {}, array: [ [] ], "boolean": [ true, false ], "null": null }, + "Dictionary of all data types" + ); + + assert.deepEqual( jQuery.parseJSON( "\n{\"test\":1}\t" ), { "test": 1 }, + "Leading and trailing whitespace are ignored" ); + + assert.throws( function() { + jQuery.parseJSON(); + }, null, "Undefined raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "" ); + }, null, "Empty string raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "''" ); + }, null, "Single-quoted string raises an error" ); + + assert.throws( function() { + var result = jQuery.parseJSON( "0101" ); + + // Support: IE9+ + // Ensure base-10 interpretation on browsers that erroneously accept leading-zero numbers + if ( result === 101 ) { + throw new Error( "close enough" ); + } + }, null, "Leading-zero number raises an error or is parsed as decimal" ); + assert.throws( function() { + jQuery.parseJSON( "{a:1}" ); + }, null, "Unquoted property raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "{'a':1}" ); + }, null, "Single-quoted property raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "[,]" ); + }, null, "Array element elision raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "{},[]" ); + }, null, "Comma expression raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "[]\n,{}" ); + }, null, "Newline-containing comma expression raises an error" ); + assert.throws( function() { + jQuery.parseJSON( "\"\"\n\"\"" ); + }, null, "Automatic semicolon insertion raises an error" ); + + assert.strictEqual( jQuery.parseJSON( [ 0 ] ), 0, "Input cast to string" ); +} );