Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
refactor(fromJson): always use native JSON.parse
Browse files Browse the repository at this point in the history
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]-->
  • Loading branch information
IgorMinar committed Mar 28, 2012
1 parent a8a750a commit 87f5c6e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 119 deletions.
9 changes: 3 additions & 6 deletions src/JSON.js
Expand Up @@ -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;
}


Expand Down
6 changes: 0 additions & 6 deletions src/ng/parse.js
Expand Up @@ -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 changes: 12 additions & 106 deletions 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');
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion test/ngScenario/FutureSpec.js
Expand Up @@ -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'});
Expand Down

0 comments on commit 87f5c6e

Please sign in to comment.