3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.JSON", function() {
18 beforeEach(function() {
19 nativeJson = Ext.USE_NATIVE_JSON;
20 Ext.USE_NATIVE_JSON = false;
24 afterEach(function() {
25 Ext.USE_NATIVE_JSON = nativeJson;
28 describe("encode", function() {
29 var encode = Ext.JSON.encode;
31 describe("numbers encoding", function() {
32 it("should convert integer to string", function() {
33 expect(encode(15)).toEqual("15");
36 it("should convert float to string", function() {
37 expect(encode(14.7)).toEqual("14.7");
40 it("should convert Infinity to null string", function() {
41 expect(encode(Infinity)).toEqual("null");
44 it("should convert NaN to null string", function() {
45 expect(encode(NaN)).toEqual("null");
49 describe("encoding of not defined values", function() {
50 it("should convert undefined to null string", function() {
51 expect(encode(undefined)).toEqual("null");
54 it("should convert null to null string", function() {
55 expect(encode(null)).toEqual("null");
59 describe("encoding function", function() {
60 it("should convert function to null string", function() {
61 expect(encode(Ext.emptyFn)).toEqual("null");
65 describe("boolean encoding", function() {
66 it("should convert true to 'true'' string", function() {
67 expect(encode(true)).toEqual("true");
70 it("should convert null to 'false' string", function() {
71 expect(encode(false)).toEqual("false");
75 describe("array encoding", function() {
76 it("should convert empty array", function() {
77 expect(encode([])).toEqual("[]");
80 it("should convert array of numbers to string", function() {
81 expect(encode([1, 2, 3])).toEqual("[1,2,3]");
84 it("should convert array of strings to string", function() {
85 expect(encode(["a", "b", "c"])).toEqual("[\"a\",\"b\",\"c\"]");
88 it("should encode array including function member to string", function() {
89 expect(encode([1, Ext.emptyFn, 3])).toEqual("[1,null,3]");
92 it("should convert array including undefined member to string", function() {
93 expect(encode([1, undefined, 3])).toEqual("[1,null,3]");
96 it("should convert array including null member to string", function() {
97 expect(encode([1, null, 3])).toEqual("[1,null,3]");
101 describe("string encoding", function() {
102 it("should convert string", function() {
103 expect(encode("You're fired!")).toEqual("\"You're fired!\"");
106 it("should convert string with international character", function() {
107 expect(encode("You're fired!")).toEqual("\"You're fired!\"");
110 it("should convert string with tab character", function() {
111 expect(encode("a\tb")).toEqual("\"a\\tb\"");
114 it("should convert string with carriage return character", function() {
115 expect(encode("a\rb")).toEqual("\"a\\rb\"");
118 it("should convert string with form feed character", function() {
119 expect(encode("a\fb")).toEqual("\"a\\fb\"");
122 it("should convert string with new line character", function() {
123 expect(encode("a\nb")).toEqual("\"a\\nb\"");
126 it("should convert string with vertical tab character", function() {
127 expect(encode("a\x0bb")).toEqual("\"a\\u000bb\"");
130 it("should convert string with backslash character", function() {
131 expect(encode("a\\b")).toEqual("\"a\\\\b\"");
135 describe("object encoding", function() {
136 it("should convert empty object", function() {
137 expect(encode({})).toEqual("{}");
140 it("should convert empty object with undefined property", function() {
144 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
147 it("should convert empty object with null property", function() {
151 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
154 it("should convert empty object with function property", function() {
158 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
161 it("should not encode dom object", function() {
162 expect(encode(Ext.getBody().dom)).toBe('undefined');
165 it("should handle encoding unknown child objects", function(){
167 prop: Ext.getBody().dom
168 })).toBe('{"prop":undefined}');
172 describe('encodeDate', function() {
175 it("should encode a date object", function() {
176 date = new Date("October 13, 1983 04:04:00");
178 expect(encode(date)).toEqual("\"1983-10-13T04:04:00\"");
181 it("should format integers to have at least two digits", function() {
182 date = new Date("August 9, 1983 06:03:02");
184 expect(encode(date)).toEqual("\"1983-08-09T06:03:02\"");
188 describe("mix all possibilities", function() {
189 it("should encode data", function() {
191 arr: [1, Ext.emptyFn, undefined, 2, [1, 2, 3], {a: 1, b: null}],
196 })).toEqual("{\"arr\":[1,null,null,2,[1,2,3],{\"a\":1,\"b\":null}],\"foo\":\"bar\",\"woo\":{\"chu\":\"a\\tb\"}}");
201 describe("decode", function() {
202 it("should decode data", function() {
203 expect(Ext.decode("{\"arr\":[1,null,null,2,[1,2,3],{\"a\":1,\"b\":null}],\"foo\":\"bar\",\"woo\":{\"chu\":\"a\\tb\"}}")).toEqual({
204 arr: [1, null, null, 2, [1, 2, 3], {a: 1, b: null}],
212 it("should raise an Ext.Error with invalid data", function() {
214 Ext.decode('{foo:"bar", x}');
215 }).toRaiseExtError();
218 describe("with safe param", function(){
219 it("should decode valid data", function() {
220 expect(Ext.decode("{\"foo\":\"bar\"}", true)).toEqual({
225 it("should return null with invalid data", function() {
226 expect(Ext.decode('{foo+"bar"}', true)).toBeNull();
231 it('should encode and decode an object', function(){
234 s: "It's-me-Jacky!!",
235 ss: "!@#$%^&*()~=_-+][{};:?/.,<>'\"",
254 expect(Ext.JSON.decode(Ext.JSON.encode(object))).toEqual(object);
257 describe("aliases", function() {
258 it("should alias Ext.JSON.decode with Ext.decode", function() {
259 expect(Ext.decode).toBe(Ext.JSON.decode);
262 it("should alias Ext.JSON.encode with Ext.encode", function() {
263 expect(Ext.encode).toBe(Ext.JSON.encode);