1 dojo.provide('opensrf.tests.testJSON_v1');
3 dojo.require('DojoSRF');
5 doh.register("JSONTests", [
6 function test_version() {
7 doh.assertTrue(JSON_version() == 'wrapper');
9 function test_decodeJS() {
10 doh.assertTrue(decodeJS(0) === 0);
11 doh.assertTrue(decodeJS(null) === null);
12 doh.assertTrue(decodeJS("") === "");
14 function test_encodeJS() {
15 doh.assertTrue(encodeJS(0) === 0);
16 doh.assertTrue(encodeJS(null) === null);
17 doh.assertTrue(encodeJS("") === "");
19 function test_js2JSON_strict() {
20 // Solo nulls and booleans are stringified XXX
21 doh.assertTrue(js2JSON(null) === "null");
22 doh.assertTrue(js2JSON(true) === "true");
23 doh.assertTrue(js2JSON(false) === "false");
25 function test_js2JSON_numbers() {
26 doh.assertTrue(js2JSON(0) === 0);
27 doh.assertTrue(js2JSON(1.5) === 1.5);
28 doh.assertTrue(js2JSON(.7) === .7);
30 function test_js2JSON_strings() {
31 doh.assertTrue(js2JSON("") == '""');
32 doh.assertTrue(js2JSON("foo") == '"foo"');
34 doh.assertTrue(js2JSON("foo\n\t\n") == '"foo\\n\\t\\n"');
36 function test_js2JSON_arrays() {
37 doh.assertTrue(js2JSON([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
39 function test_js2JSON_objects() {
40 doh.assertTrue(js2JSON({"foo":"bar"}) == '{"foo":"bar"}');
41 doh.assertTrue(js2JSON({"foo":true}) == '{"foo":true}');
42 doh.assertTrue(js2JSON({"foo":0}) == '{"foo":0}');
44 function test_js2JSON_objects_ordered() {
45 // Order of object attributes is not guaranteed
46 doh.assertTrue(js2JSON({"foo":{"one":[null,"two",2]}}) == '{"foo":{"one":[null,"two",2]}}');
48 function test_js2JSONRaw_strict() {
49 // Solo nulls and booleans are stringified XXX
50 doh.assertTrue(js2JSONRaw(null) === "null");
51 doh.assertTrue(js2JSONRaw(true) === "true");
52 doh.assertTrue(js2JSONRaw(false) === "false");
54 function test_js2JSONRaw_numbers() {
55 doh.assertTrue(js2JSONRaw(0) === 0);
56 doh.assertTrue(js2JSONRaw(1.5) === 1.5);
57 doh.assertTrue(js2JSONRaw(.7) === .7);
59 function test_js2JSONRaw_strings() {
60 doh.assertTrue(js2JSONRaw("") == '""');
61 doh.assertTrue(js2JSONRaw("foo") == '"foo"');
63 doh.assertTrue(js2JSONRaw("foo\n\t\n") == '"foo\\n\\t\\n"');
65 function test_js2JSONRaw_arrays() {
66 doh.assertTrue(js2JSONRaw([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
68 function test_js2JSONRaw_objects() {
69 doh.assertTrue(js2JSONRaw({"foo":"bar"}) == '{"foo":"bar"}');
70 doh.assertTrue(js2JSONRaw({"foo":true}) == '{"foo":true}');
71 doh.assertTrue(js2JSONRaw({"foo":0}) == '{"foo":0}');
73 function test_js2JSONRaw_objects_ordered() {
74 // Order of object attributes is not guaranteed
75 doh.assertTrue(js2JSONRaw({"foo":{"one":[null,"two",2]}}) == '{"foo":{"one":[null,"two",2]}}');
77 function test_JSON2jsRaw_strict() {
78 // Standalone quoted nulls and booleans are converted to primitives
79 doh.assertTrue(JSON2jsRaw(null) === null);
80 doh.assertTrue(JSON2jsRaw("null") === null);
81 doh.assertTrue(JSON2jsRaw(true) === true);
82 doh.assertTrue(JSON2jsRaw("true") === true);
83 doh.assertTrue(JSON2jsRaw(false) === false);
84 doh.assertTrue(JSON2jsRaw("false") === false);
86 function test_JSON2jsRaw_numbers() {
87 // Zero is zero and only zero
88 doh.assertTrue(JSON2jsRaw(0) === 0);
89 doh.assertTrue(JSON2jsRaw(1.5) === 1.5);
90 doh.assertTrue(JSON2jsRaw(.5) === .5);
92 function test_JSON2jsRaw_strings() {
94 doh.assertTrue(JSON2jsRaw('""') === "");
96 doh.assertTrue(JSON2jsRaw('"foo"') == "foo");
98 function test_JSON2jsRaw_arrays() {
99 // Array; access an index
100 doh.assertTrue(JSON2jsRaw('[0,1,2,3,4,5]')[1] == 1);
102 function test_JSON2jsRaw_objects() {
103 // Object; access a key
104 doh.assertTrue(JSON2jsRaw('{"foo":"bar"}').foo == "bar");
105 doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":null}}').foo.one === null);
106 doh.assertTrue(JSON2jsRaw('{"foo":{"two":2,"one":"null"}}').foo.one === "null");
108 function test_JSON2js_strict() {
109 // Standalone quoted nulls and booleans are converted to primitives
110 doh.assertTrue(JSON2js(null) === null);
111 doh.assertTrue(JSON2js("null") === null);
112 doh.assertTrue(JSON2js(true) === true);
113 doh.assertTrue(JSON2js("true") === true);
114 doh.assertTrue(JSON2js(false) === false);
115 doh.assertTrue(JSON2js("false") === false);
117 function test_JSON2js_numbers() {
118 // Zero is zero and only zero
119 doh.assertTrue(JSON2js(0) === 0);
120 doh.assertTrue(JSON2js(1.5) === 1.5);
121 doh.assertTrue(JSON2js(.5) === .5);
123 function test_JSON2js_strings() {
125 doh.assertTrue(JSON2js('""') === "");
127 doh.assertTrue(JSON2js('"foo"') == "foo");
129 function test_JSON2js_arrays() {
130 // Array; access an index
131 doh.assertTrue(JSON2js('[0,1,2,3,4,5]')[1] == 1);
133 function test_JSON2js_objects() {
134 // Object; access a key
135 doh.assertTrue(JSON2js('{"foo":"bar"}').foo == "bar");
136 doh.assertTrue(JSON2js('{"foo":{"two":2,"one":null}}').foo.one === null);
137 doh.assertTrue(JSON2js('{"foo":{"two":2,"one":"null"}}').foo.one === "null");