2011-07-17 08:05:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
describe('json', function() {
|
2012-03-27 17:21:49 +00:00
|
|
|
|
|
|
|
|
describe('fromJson', function() {
|
|
|
|
|
|
|
|
|
|
it('should delegate to native parser', function() {
|
|
|
|
|
var spy = spyOn(JSON, 'parse').andCallThrough();
|
|
|
|
|
|
|
|
|
|
expect(fromJson('{}')).toEqual({});
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize primitives', function() {
|
|
|
|
|
expect(toJson(0/0)).toEqual('null');
|
|
|
|
|
expect(toJson(null)).toEqual('null');
|
|
|
|
|
expect(toJson(true)).toEqual('true');
|
|
|
|
|
expect(toJson(false)).toEqual('false');
|
2012-01-03 22:49:41 +00:00
|
|
|
expect(toJson(123.45)).toEqual('123.45');
|
|
|
|
|
expect(toJson('abc')).toEqual('"abc"');
|
|
|
|
|
expect(toJson('a \t \n \r b \\')).toEqual('"a \\t \\n \\r b \\\\"');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should not serialize $$properties', function() {
|
2011-08-17 19:21:43 +00:00
|
|
|
expect(toJson({$$some:'value', 'this':1, '$parent':1}, false)).toEqual('{}');
|
|
|
|
|
});
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should not serialize this or $parent', function() {
|
2011-09-08 20:56:29 +00:00
|
|
|
expect(toJson({'this':'value', $parent:'abc'}, false)).toEqual('{}');
|
|
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize strings with escaped characters', function() {
|
|
|
|
|
expect(toJson("7\\\"7")).toEqual("\"7\\\\\\\"7\"");
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize objects', function() {
|
2012-01-03 22:49:41 +00:00
|
|
|
expect(toJson({a: 1, b: 2})).toEqual('{"a":1,"b":2}');
|
|
|
|
|
expect(toJson({a: {b: 2}})).toEqual('{"a":{"b":2}}');
|
|
|
|
|
expect(toJson({a: {b: {c: 0}}})).toEqual('{"a":{"b":{"c":0}}}');
|
|
|
|
|
expect(toJson({a: {b: 0/0}})).toEqual('{"a":{"b":null}}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should format objects pretty', function() {
|
2012-01-03 22:49:41 +00:00
|
|
|
expect(toJson({a: 1, b: 2}, true)).toEqual('{\n "a":1,\n "b":2}');
|
|
|
|
|
expect(toJson({a: {b: 2}}, true)).toEqual('{\n "a":{\n "b":2}}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize array', function() {
|
|
|
|
|
expect(toJson([])).toEqual('[]');
|
2012-01-03 22:49:41 +00:00
|
|
|
expect(toJson([1, 'b'])).toEqual('[1,"b"]');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize RegExp', function() {
|
|
|
|
|
expect(toJson(/foo/)).toEqual('"/foo/"');
|
2012-01-03 22:49:41 +00:00
|
|
|
expect(toJson([1, new RegExp('foo')])).toEqual('[1,"/foo/"]');
|
2010-11-03 17:13:03 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should ignore functions', function() {
|
2011-10-07 18:27:49 +00:00
|
|
|
expect(toJson([function() {},1])).toEqual('[null,1]');
|
|
|
|
|
expect(toJson({a:function() {}})).toEqual('{}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize array with empty items', function() {
|
2010-10-15 22:28:58 +00:00
|
|
|
var a = [];
|
2012-01-03 22:49:41 +00:00
|
|
|
a[1] = 'X';
|
2010-10-26 22:59:10 +00:00
|
|
|
expect(toJson(a)).toEqual('[null,"X"]');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should escape unicode', function() {
|
2012-01-03 22:49:41 +00:00
|
|
|
expect('\u00a0'.length).toEqual(1);
|
|
|
|
|
expect(toJson('\u00a0').length).toEqual(8);
|
|
|
|
|
expect(fromJson(toJson('\u00a0')).length).toEqual(1);
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize UTC dates', function() {
|
2012-03-27 06:38:20 +00:00
|
|
|
var date = new angular.mock.TzDate(-1, '2009-10-09T01:02:03.027Z');
|
2010-11-08 17:05:48 +00:00
|
|
|
expect(toJson(date)).toEqual('"2009-10-09T01:02:03.027Z"');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should prevent recursion', function() {
|
2012-01-03 22:49:41 +00:00
|
|
|
var obj = {a: 'b'};
|
2010-10-15 22:28:58 +00:00
|
|
|
obj.recursion = obj;
|
2010-10-26 22:59:10 +00:00
|
|
|
expect(angular.toJson(obj)).toEqual('{"a":"b","recursion":RECURSION}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-11-05 22:37:55 +00:00
|
|
|
it('should serialize $ properties', function() {
|
2010-11-16 21:57:41 +00:00
|
|
|
var obj = {$a: 'a'};
|
2010-11-05 22:37:55 +00:00
|
|
|
expect(angular.toJson(obj)).toEqual('{"$a":"a"}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2011-03-30 23:28:28 +00:00
|
|
|
it('should NOT serialize inherited properties', function() {
|
|
|
|
|
// This is what native Browser does
|
2010-11-05 22:37:55 +00:00
|
|
|
var obj = inherit({p:'p'});
|
|
|
|
|
obj.a = 'a';
|
2011-03-30 23:28:28 +00:00
|
|
|
expect(angular.toJson(obj)).toEqual('{"a":"a"}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should serialize same objects multiple times', function() {
|
2010-10-15 22:28:58 +00:00
|
|
|
var obj = {a:'b'};
|
2010-10-26 22:59:10 +00:00
|
|
|
expect(angular.toJson({A:obj, B:obj})).toEqual('{"A":{"a":"b"},"B":{"a":"b"}}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-26 22:59:10 +00:00
|
|
|
it('should not serialize undefined values', function() {
|
|
|
|
|
expect(angular.toJson({A:undefined})).toEqual('{}');
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|
2011-01-13 18:35:26 +00:00
|
|
|
|
2010-11-05 23:04:13 +00:00
|
|
|
it('should not serialize $window object', function() {
|
|
|
|
|
expect(toJson(window)).toEqual('WINDOW');
|
|
|
|
|
});
|
2011-01-13 18:35:26 +00:00
|
|
|
|
2010-11-05 23:04:13 +00:00
|
|
|
it('should not serialize $document object', function() {
|
|
|
|
|
expect(toJson(document)).toEqual('DOCUMENT');
|
|
|
|
|
});
|
2010-10-15 22:28:58 +00:00
|
|
|
|
2012-01-03 04:50:17 +00:00
|
|
|
|
2011-11-04 03:26:13 +00:00
|
|
|
describe('string', function() {
|
|
|
|
|
it('should quote', function() {
|
2012-01-03 04:50:17 +00:00
|
|
|
expect(quoteUnicode('a')).toBe('"a"');
|
|
|
|
|
expect(quoteUnicode('\\')).toBe('"\\\\"');
|
|
|
|
|
expect(quoteUnicode("'a'")).toBe('"\'a\'"');
|
|
|
|
|
expect(quoteUnicode('"a"')).toBe('"\\"a\\""');
|
|
|
|
|
expect(quoteUnicode('\n\f\r\t')).toBe('"\\n\\f\\r\\t"');
|
2011-11-04 03:26:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should quote slashes', function() {
|
2012-01-03 04:50:17 +00:00
|
|
|
expect(quoteUnicode("7\\\"7")).toBe('"7\\\\\\\"7"');
|
2011-11-04 03:26:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should quote unicode', function() {
|
2012-01-03 04:50:17 +00:00
|
|
|
expect(quoteUnicode('abc\u00A0def')).toBe('"abc\\u00a0def"');
|
2011-11-04 03:26:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
2012-01-03 04:50:17 +00:00
|
|
|
|
2010-10-15 22:28:58 +00:00
|
|
|
});
|