1 describe("Ext.JSON", function() {
4 beforeEach(function() {
5 nativeJson = Ext.USE_NATIVE_JSON;
6 Ext.USE_NATIVE_JSON = false;
10 afterEach(function() {
11 Ext.USE_NATIVE_JSON = nativeJson;
14 describe("encode", function() {
15 var encode = Ext.JSON.encode;
17 describe("numbers encoding", function() {
18 it("should convert integer to string", function() {
19 expect(encode(15)).toEqual("15");
22 it("should convert float to string", function() {
23 expect(encode(14.7)).toEqual("14.7");
26 it("should convert Infinity to null string", function() {
27 expect(encode(Infinity)).toEqual("null");
30 it("should convert NaN to null string", function() {
31 expect(encode(NaN)).toEqual("null");
35 describe("encoding of not defined values", function() {
36 it("should convert undefined to null string", function() {
37 expect(encode(undefined)).toEqual("null");
40 it("should convert null to null string", function() {
41 expect(encode(null)).toEqual("null");
45 describe("encoding function", function() {
46 it("should convert function to null string", function() {
47 expect(encode(Ext.emptyFn)).toEqual("null");
51 describe("boolean encoding", function() {
52 it("should convert true to 'true'' string", function() {
53 expect(encode(true)).toEqual("true");
56 it("should convert null to 'false' string", function() {
57 expect(encode(false)).toEqual("false");
61 describe("array encoding", function() {
62 it("should convert empty array", function() {
63 expect(encode([])).toEqual("[]");
66 it("should convert array of numbers to string", function() {
67 expect(encode([1, 2, 3])).toEqual("[1,2,3]");
70 it("should convert array of strings to string", function() {
71 expect(encode(["a", "b", "c"])).toEqual("[\"a\",\"b\",\"c\"]");
74 it("should encode array including function member to string", function() {
75 expect(encode([1, Ext.emptyFn, 3])).toEqual("[1,null,3]");
78 it("should convert array including undefined member to string", function() {
79 expect(encode([1, undefined, 3])).toEqual("[1,null,3]");
82 it("should convert array including null member to string", function() {
83 expect(encode([1, null, 3])).toEqual("[1,null,3]");
87 describe("string encoding", function() {
88 it("should convert string", function() {
89 expect(encode("You're fired!")).toEqual("\"You're fired!\"");
92 it("should convert string with international character", function() {
93 expect(encode("You're fired!")).toEqual("\"You're fired!\"");
96 it("should convert string with tab character", function() {
97 expect(encode("a\tb")).toEqual("\"a\\tb\"");
100 it("should convert string with carriage return character", function() {
101 expect(encode("a\rb")).toEqual("\"a\\rb\"");
104 it("should convert string with form feed character", function() {
105 expect(encode("a\fb")).toEqual("\"a\\fb\"");
108 it("should convert string with new line character", function() {
109 expect(encode("a\nb")).toEqual("\"a\\nb\"");
112 it("should convert string with vertical tab character", function() {
113 expect(encode("a\x0bb")).toEqual("\"a\\u000bb\"");
116 it("should convert string with backslash character", function() {
117 expect(encode("a\\b")).toEqual("\"a\\\\b\"");
121 describe("object encoding", function() {
122 it("should convert empty object", function() {
123 expect(encode({})).toEqual("{}");
126 it("should convert empty object with undefined property", function() {
130 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
133 it("should convert empty object with null property", function() {
137 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
140 it("should convert empty object with function property", function() {
144 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
147 it("should not encode dom object", function() {
148 expect(encode(Ext.getBody().dom)).toBe('undefined');
151 it("should handle encoding unknown child objects", function(){
153 prop: Ext.getBody().dom
154 })).toBe('{"prop":undefined}');
158 describe('encodeDate', function() {
161 it("should encode a date object", function() {
162 date = new Date("October 13, 1983 04:04:00");
164 expect(encode(date)).toEqual("\"1983-10-13T04:04:00\"");
167 it("should format integers to have at least two digits", function() {
168 date = new Date("August 9, 1983 06:03:02");
170 expect(encode(date)).toEqual("\"1983-08-09T06:03:02\"");
174 describe("mix all possibilities", function() {
175 it("should encode data", function() {
177 arr: [1, Ext.emptyFn, undefined, 2, [1, 2, 3], {a: 1, b: null}],
182 })).toEqual("{\"arr\":[1,null,null,2,[1,2,3],{\"a\":1,\"b\":null}],\"foo\":\"bar\",\"woo\":{\"chu\":\"a\\tb\"}}");
187 describe("decode", function() {
188 it("should decode data", function() {
189 expect(Ext.decode("{\"arr\":[1,null,null,2,[1,2,3],{\"a\":1,\"b\":null}],\"foo\":\"bar\",\"woo\":{\"chu\":\"a\\tb\"}}")).toEqual({
190 arr: [1, null, null, 2, [1, 2, 3], {a: 1, b: null}],
198 it("should raise an Ext.Error with invalid data", function() {
200 Ext.decode('{foo:"bar", x}');
201 }).toRaiseExtError();
204 describe("with safe param", function(){
205 it("should decode valid data", function() {
206 expect(Ext.decode("{\"foo\":\"bar\"}", true)).toEqual({
211 it("should return null with invalid data", function() {
212 expect(Ext.decode('{foo+"bar"}', true)).toBeNull();
217 it('should encode and decode an object', function(){
220 s: "It's-me-Jacky!!",
221 ss: "!@#$%^&*()~=_-+][{};:?/.,<>'\"",
240 expect(Ext.JSON.decode(Ext.JSON.encode(object))).toEqual(object);
243 describe("aliases", function() {
244 it("should alias Ext.JSON.decode with Ext.decode", function() {
245 expect(Ext.decode).toBe(Ext.JSON.decode);
248 it("should alias Ext.JSON.encode with Ext.encode", function() {
249 expect(Ext.encode).toBe(Ext.JSON.encode);