From 87f5c6e5b716100e203ec59c5874c3e927f83fa0 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Tue, 27 Mar 2012 10:21:49 -0700 Subject: [PATCH] refactor(fromJson): always use native JSON.parse This breaks IE7 for which you can use polyfill: https://github.com/douglascrockford/JSON-js or http://bestiejs.github.com/json3/ --- src/JSON.js | 9 +-- src/ng/parse.js | 6 -- test/JsonSpec.js | 118 ++++------------------------------ test/ngScenario/FutureSpec.js | 2 +- 4 files changed, 16 insertions(+), 119 deletions(-) diff --git a/src/JSON.js b/src/JSON.js index cddfc52d018a..bcb5fabeca00 100644 --- a/src/JSON.js +++ b/src/JSON.js @@ -27,15 +27,12 @@ function toJson(obj, pretty) { * Deserializes a JSON string. * * @param {string} json JSON string to deserialize. - * @param {boolean} [useNative=false] Use native JSON parser, if available. * @returns {Object|Array|Date|string|number} Deserialized thingy. */ -function fromJson(json, useNative) { - if (!isString(json)) return json; - - return (useNative && window.JSON && window.JSON.parse) +function fromJson(json) { + return isString(json) ? JSON.parse(json) - : parseJson(json, true)(); + : json; } diff --git a/src/ng/parse.js b/src/ng/parse.js index 47c5188e2f44..cd60bc832a73 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -752,9 +752,3 @@ function $ParseProvider() { }; }]; } - - -// This is a special access for JSON parser which bypasses the injector -var parseJson = function(json) { - return parser(json, true); -}; diff --git a/test/JsonSpec.js b/test/JsonSpec.js index ad0cb415d970..e4c2745508d1 100644 --- a/test/JsonSpec.js +++ b/test/JsonSpec.js @@ -1,6 +1,18 @@ 'use strict'; describe('json', function() { + + describe('fromJson', function() { + + it('should delegate to native parser', function() { + var spy = spyOn(JSON, 'parse').andCallThrough(); + + expect(fromJson('{}')).toEqual({}); + expect(spy).toHaveBeenCalled(); + }); + }); + + it('should serialize primitives', function() { expect(toJson(0/0)).toEqual('null'); expect(toJson(null)).toEqual('null'); @@ -50,15 +62,6 @@ describe('json', function() { expect(toJson({a:function() {}})).toEqual('{}'); }); - it('should parse null', function() { - expect(fromJson('null')).toBeNull(); - }); - - it('should parse boolean', function() { - expect(fromJson('true')).toBeTruthy(); - expect(fromJson('false')).toBeFalsy(); - }); - it('should serialize array with empty items', function() { var a = []; a[1] = 'X'; @@ -111,103 +114,6 @@ describe('json', function() { expect(toJson(document)).toEqual('DOCUMENT'); }); - it('should parse floats', function() { - expect(fromJson("{value:2.55, name:'misko'}")).toEqual({value:2.55, name:'misko'}); - }); - - it('should parse negative / possitve numbers', function() { - expect(fromJson("{neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}")).toEqual({neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}); - }); - - it('should parse exponents', function() { - expect(fromJson("{exp:1.2E10}")).toEqual({exp:1.2E10}); - expect(fromJson("{exp:1.2E-10}")).toEqual({exp:1.2E-10}); - expect(fromJson("{exp:1.2e+10}")).toEqual({exp:1.2E10}); - expect(fromJson("{exp:1.2e-10}")).toEqual({exp:1.2E-10}); - }); - - it('should ignore non-strings', function() { - expect(fromJson([])).toEqual([]); - expect(fromJson({})).toEqual({}); - expect(fromJson(null)).toEqual(null); - expect(fromJson(undefined)).toEqual(undefined); - }); - - - //run these tests only in browsers that have native JSON parser - if (JSON && JSON.parse) { - - describe('native parser', function() { - - var nativeParser = JSON.parse; - - afterEach(function() { - JSON.parse = nativeParser; - }); - - - it('should delegate to native parser if available and boolean flag is passed', function() { - var spy = this.spyOn(JSON, 'parse').andCallThrough(); - - expect(fromJson('{}')).toEqual({}); - expect(spy).not.toHaveBeenCalled(); - - expect(fromJson('{}', true)).toEqual({}); - expect(spy).toHaveBeenCalled(); - }); - }); - } - - - describe('security', function() { - it('should not allow naked expressions', function() { - expect(function() {fromJson('1+2');}). - toThrow(new Error("Syntax Error: Token '+' is an unexpected token at column 2 of the expression [1+2] starting at [+2].")); - }); - - it('should not allow naked expressions group', function() { - expect(function() {fromJson('(1+2)');}). - toThrow(new Error("Syntax Error: Token '(' is not valid json at column 1 of the expression [(1+2)] starting at [(1+2)].")); - }); - - it('should not allow expressions in objects', function() { - expect(function() {fromJson('{a:abc()}');}). - toThrow(new Error("Syntax Error: Token 'abc' is not valid json at column 4 of the expression [{a:abc()}] starting at [abc()}].")); - }); - - it('should not allow expressions in arrays', function() { - expect(function() {fromJson('[1+2]');}). - toThrow(new Error("Syntax Error: Token '+' is not valid json at column 3 of the expression [[1+2]] starting at [+2]].")); - }); - - it('should not allow vars', function() { - expect(function() {fromJson('[1, x]');}). - toThrow(new Error("Syntax Error: Token 'x' is not valid json at column 5 of the expression [[1, x]] starting at [x]].")); - }); - - it('should not allow dereference', function() { - expect(function() {fromJson('["".constructor]');}). - toThrow(new Error("Syntax Error: Token '.' is not valid json at column 4 of the expression [[\"\".constructor]] starting at [.constructor]].")); - }); - - it('should not allow expressions ofter valid json', function() { - expect(function() {fromJson('[].constructor');}). - toThrow(new Error("Syntax Error: Token '.' is not valid json at column 3 of the expression [[].constructor] starting at [.constructor].")); - }); - - it('should not allow object dereference', function() { - expect(function() {fromJson('{a:1, b: $location, c:1}');}).toThrow(); - expect(function() {fromJson("{a:1, b:[1]['__parent__']['location'], c:1}");}).toThrow(); - }); - - it('should not allow assignments', function() { - expect(function() {fromJson('{a:1, b:[1]=1, c:1}');}).toThrow(); - expect(function() {fromJson('{a:1, b:=1, c:1}');}).toThrow(); - expect(function() {fromJson('{a:1, b:x=1, c:1}');}).toThrow(); - }); - - }); - describe('string', function() { it('should quote', function() { diff --git a/test/ngScenario/FutureSpec.js b/test/ngScenario/FutureSpec.js index 2a75f275d971..9ae284dc8e17 100644 --- a/test/ngScenario/FutureSpec.js +++ b/test/ngScenario/FutureSpec.js @@ -42,7 +42,7 @@ describe('angular.scenario.Future', function() { it('should parse json with fromJson', function() { var future = new angular.scenario.Future('test name', function(done) { - done(null, "{test: 'foo'}"); + done(null, '{"test": "foo"}'); }); future.fromJson().execute(angular.noop); expect(future.value).toEqual({test: 'foo'});