mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
refactor(fromJson): always use native JSON.parse
This breaks IE7 for which you can use polyfill: https://github.com/douglascrockford/JSON-js <!--[if lt IE 8]> <script src="json2.min.js"></script> <![endif]--> or http://bestiejs.github.com/json3/ <!--[if lt IE 8]> <script src="json3.min.js"></script> <![endif]-->
This commit is contained in:
parent
a8a750ab05
commit
87f5c6e5b7
4 changed files with 16 additions and 119 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
|
|
|
|||
118
test/JsonSpec.js
118
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() {
|
||||
|
|
|
|||
|
|
@ -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'});
|
||||
|
|
|
|||
Loading…
Reference in a new issue