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

Commit 87f5c6e

Browse files
committed
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]-->
1 parent a8a750a commit 87f5c6e

File tree

4 files changed

+16
-119
lines changed

4 files changed

+16
-119
lines changed

src/JSON.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ function toJson(obj, pretty) {
2727
* Deserializes a JSON string.
2828
*
2929
* @param {string} json JSON string to deserialize.
30-
* @param {boolean} [useNative=false] Use native JSON parser, if available.
3130
* @returns {Object|Array|Date|string|number} Deserialized thingy.
3231
*/
33-
function fromJson(json, useNative) {
34-
if (!isString(json)) return json;
35-
36-
return (useNative && window.JSON && window.JSON.parse)
32+
function fromJson(json) {
33+
return isString(json)
3734
? JSON.parse(json)
38-
: parseJson(json, true)();
35+
: json;
3936
}
4037

4138

src/ng/parse.js

-6
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,3 @@ function $ParseProvider() {
752752
};
753753
}];
754754
}
755-
756-
757-
// This is a special access for JSON parser which bypasses the injector
758-
var parseJson = function(json) {
759-
return parser(json, true);
760-
};

test/JsonSpec.js

+12-106
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
'use strict';
22

33
describe('json', function() {
4+
5+
describe('fromJson', function() {
6+
7+
it('should delegate to native parser', function() {
8+
var spy = spyOn(JSON, 'parse').andCallThrough();
9+
10+
expect(fromJson('{}')).toEqual({});
11+
expect(spy).toHaveBeenCalled();
12+
});
13+
});
14+
15+
416
it('should serialize primitives', function() {
517
expect(toJson(0/0)).toEqual('null');
618
expect(toJson(null)).toEqual('null');
@@ -50,15 +62,6 @@ describe('json', function() {
5062
expect(toJson({a:function() {}})).toEqual('{}');
5163
});
5264

53-
it('should parse null', function() {
54-
expect(fromJson('null')).toBeNull();
55-
});
56-
57-
it('should parse boolean', function() {
58-
expect(fromJson('true')).toBeTruthy();
59-
expect(fromJson('false')).toBeFalsy();
60-
});
61-
6265
it('should serialize array with empty items', function() {
6366
var a = [];
6467
a[1] = 'X';
@@ -111,103 +114,6 @@ describe('json', function() {
111114
expect(toJson(document)).toEqual('DOCUMENT');
112115
});
113116

114-
it('should parse floats', function() {
115-
expect(fromJson("{value:2.55, name:'misko'}")).toEqual({value:2.55, name:'misko'});
116-
});
117-
118-
it('should parse negative / possitve numbers', function() {
119-
expect(fromJson("{neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}")).toEqual({neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]});
120-
});
121-
122-
it('should parse exponents', function() {
123-
expect(fromJson("{exp:1.2E10}")).toEqual({exp:1.2E10});
124-
expect(fromJson("{exp:1.2E-10}")).toEqual({exp:1.2E-10});
125-
expect(fromJson("{exp:1.2e+10}")).toEqual({exp:1.2E10});
126-
expect(fromJson("{exp:1.2e-10}")).toEqual({exp:1.2E-10});
127-
});
128-
129-
it('should ignore non-strings', function() {
130-
expect(fromJson([])).toEqual([]);
131-
expect(fromJson({})).toEqual({});
132-
expect(fromJson(null)).toEqual(null);
133-
expect(fromJson(undefined)).toEqual(undefined);
134-
});
135-
136-
137-
//run these tests only in browsers that have native JSON parser
138-
if (JSON && JSON.parse) {
139-
140-
describe('native parser', function() {
141-
142-
var nativeParser = JSON.parse;
143-
144-
afterEach(function() {
145-
JSON.parse = nativeParser;
146-
});
147-
148-
149-
it('should delegate to native parser if available and boolean flag is passed', function() {
150-
var spy = this.spyOn(JSON, 'parse').andCallThrough();
151-
152-
expect(fromJson('{}')).toEqual({});
153-
expect(spy).not.toHaveBeenCalled();
154-
155-
expect(fromJson('{}', true)).toEqual({});
156-
expect(spy).toHaveBeenCalled();
157-
});
158-
});
159-
}
160-
161-
162-
describe('security', function() {
163-
it('should not allow naked expressions', function() {
164-
expect(function() {fromJson('1+2');}).
165-
toThrow(new Error("Syntax Error: Token '+' is an unexpected token at column 2 of the expression [1+2] starting at [+2]."));
166-
});
167-
168-
it('should not allow naked expressions group', function() {
169-
expect(function() {fromJson('(1+2)');}).
170-
toThrow(new Error("Syntax Error: Token '(' is not valid json at column 1 of the expression [(1+2)] starting at [(1+2)]."));
171-
});
172-
173-
it('should not allow expressions in objects', function() {
174-
expect(function() {fromJson('{a:abc()}');}).
175-
toThrow(new Error("Syntax Error: Token 'abc' is not valid json at column 4 of the expression [{a:abc()}] starting at [abc()}]."));
176-
});
177-
178-
it('should not allow expressions in arrays', function() {
179-
expect(function() {fromJson('[1+2]');}).
180-
toThrow(new Error("Syntax Error: Token '+' is not valid json at column 3 of the expression [[1+2]] starting at [+2]]."));
181-
});
182-
183-
it('should not allow vars', function() {
184-
expect(function() {fromJson('[1, x]');}).
185-
toThrow(new Error("Syntax Error: Token 'x' is not valid json at column 5 of the expression [[1, x]] starting at [x]]."));
186-
});
187-
188-
it('should not allow dereference', function() {
189-
expect(function() {fromJson('["".constructor]');}).
190-
toThrow(new Error("Syntax Error: Token '.' is not valid json at column 4 of the expression [[\"\".constructor]] starting at [.constructor]]."));
191-
});
192-
193-
it('should not allow expressions ofter valid json', function() {
194-
expect(function() {fromJson('[].constructor');}).
195-
toThrow(new Error("Syntax Error: Token '.' is not valid json at column 3 of the expression [[].constructor] starting at [.constructor]."));
196-
});
197-
198-
it('should not allow object dereference', function() {
199-
expect(function() {fromJson('{a:1, b: $location, c:1}');}).toThrow();
200-
expect(function() {fromJson("{a:1, b:[1]['__parent__']['location'], c:1}");}).toThrow();
201-
});
202-
203-
it('should not allow assignments', function() {
204-
expect(function() {fromJson('{a:1, b:[1]=1, c:1}');}).toThrow();
205-
expect(function() {fromJson('{a:1, b:=1, c:1}');}).toThrow();
206-
expect(function() {fromJson('{a:1, b:x=1, c:1}');}).toThrow();
207-
});
208-
209-
});
210-
211117

212118
describe('string', function() {
213119
it('should quote', function() {

test/ngScenario/FutureSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('angular.scenario.Future', function() {
4242

4343
it('should parse json with fromJson', function() {
4444
var future = new angular.scenario.Future('test name', function(done) {
45-
done(null, "{test: 'foo'}");
45+
done(null, '{"test": "foo"}');
4646
});
4747
future.fromJson().execute(angular.noop);
4848
expect(future.value).toEqual({test: 'foo'});

0 commit comments

Comments
 (0)