blob: 22bbb4d5e43709039748cf43c9e9beaa139a68f8 [file] [log] [blame]
drh5fa5c102015-08-12 16:49:40 +00001# 2015-08-12
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements tests for JSON SQL functions extension to the
12# SQLite library.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18load_static_extension db json
drhf5ddb9c2015-09-11 00:06:41 +000019do_execsql_test json1-1.1.00 {
drh5fa5c102015-08-12 16:49:40 +000020 SELECT json_array(1,2.5,null,'hello');
21} {[1,2.5,null,"hello"]}
drhf5ddb9c2015-09-11 00:06:41 +000022do_execsql_test json1-1.1.01 {
23 SELECT json_array(1,'{"abc":2.5,"def":null,"ghi":hello}',99);
24 -- the second term goes in as a string:
25} {[1,"{\\"abc\\":2.5,\\"def\\":null,\\"ghi\\":hello}",99]}
26do_execsql_test json1-1.1.02 {
27 SELECT json_array(1,json('{"abc":2.5,"def":null,"ghi":"hello"}'),99);
28 -- the second term goes in as JSON
29} {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
30do_execsql_test json1-1.1.03 {
31 SELECT json_array(1,json_object('abc',2.5,'def',null,'ghi','hello'),99);
32 -- the second term goes in as JSON
33} {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
drh5fa5c102015-08-12 16:49:40 +000034do_execsql_test json1-1.2 {
35 SELECT hex(json_array('String "\ Test'));
36} {5B22537472696E67205C225C5C2054657374225D}
37do_catchsql_test json1-1.3 {
drhdc384952015-09-19 18:54:39 +000038 SELECT json_array(1,printf('%.1000c','x'),x'abcd',3);
drh5fa5c102015-08-12 16:49:40 +000039} {1 {JSON cannot hold BLOB values}}
40do_execsql_test json1-1.4 {
41 SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1,
42 0.0, 1.0, -1.0, -1e99, +2e100,
43 'one','two','three',
44 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
45 19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
46 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
47 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
48 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
49 99);
50} {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]}
51
drh2032d602015-08-12 17:23:34 +000052do_execsql_test json1-2.1 {
53 SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
54} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
55do_catchsql_test json1-2.2 {
drhdc384952015-09-19 18:54:39 +000056 SELECT json_object('a',printf('%.1000c','x'),2,2.5);
drh2032d602015-08-12 17:23:34 +000057} {1 {json_object() labels must be TEXT}}
58do_catchsql_test json1-2.3 {
59 SELECT json_object('a',1,'b');
60} {1 {json_object() requires an even number of arguments}}
61do_catchsql_test json1-2.4 {
drhdc384952015-09-19 18:54:39 +000062 SELECT json_object('a',printf('%.1000c','x'),'b',x'abcd');
drh2032d602015-08-12 17:23:34 +000063} {1 {JSON cannot hold BLOB values}}
64
drhecb5fed2015-08-28 03:33:50 +000065do_execsql_test json1-3.1 {
66 SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]');
67} {{{"a":"[3,4,5]","b":2}}}
68do_execsql_test json1-3.2 {
drhf5ddb9c2015-09-11 00:06:41 +000069 SELECT json_replace('{"a":1,"b":2}','$.a',json('[3,4,5]'));
drhecb5fed2015-08-28 03:33:50 +000070} {{{"a":[3,4,5],"b":2}}}
71do_execsql_test json1-3.3 {
72 SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
73} {text}
74do_execsql_test json1-3.4 {
drhf5ddb9c2015-09-11 00:06:41 +000075 SELECT json_type(json_set('{"a":1,"b":2}','$.b',json('{"x":3,"y":4}')),'$.b');
drhecb5fed2015-08-28 03:33:50 +000076} {object}
drh8cb0c832015-09-22 00:21:03 +000077do_execsql_test json1-3.5 {
78 SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456));
79} {{$} {} | {$.x} 456 |}
drh2032d602015-08-12 17:23:34 +000080
drhd1f00682015-08-29 16:02:37 +000081# Per rfc7159, any JSON value is allowed at the top level, and whitespace
82# is permitting before and/or after that value.
83#
84do_execsql_test json1-4.1 {
85 CREATE TABLE j1(x);
86 INSERT INTO j1(x)
87 VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'),
88 ('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'),
89 ('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'),
90 ('{"a":true,"b":{"c":false}}');
91 SELECT * FROM j1 WHERE NOT json_valid(x);
92} {}
93do_execsql_test json1-4.2 {
94 SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x);
95} {}
96do_execsql_test json1-4.3 {
97 SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d));
98} {}
99
100# But an empty string, or a string of pure whitespace is not valid JSON.
101#
102do_execsql_test json1-4.4 {
103 SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d));
104} {0 0}
105
106# json_remove() and similar functions with no edit operations return their
107# input unchanged.
108#
109do_execsql_test json1-4.5 {
110 SELECT x FROM j1 WHERE json_remove(x)<>x;
111} {}
112do_execsql_test json1-4.6 {
113 SELECT x FROM j1 WHERE json_replace(x)<>x;
114} {}
115do_execsql_test json1-4.7 {
116 SELECT x FROM j1 WHERE json_set(x)<>x;
117} {}
118do_execsql_test json1-4.8 {
119 SELECT x FROM j1 WHERE json_insert(x)<>x;
120} {}
121
122# json_extract(JSON,'$') will return objects and arrays without change.
123#
124do_execsql_test json-4.10 {
125 SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array');
126 SELECT x FROM j1
127 WHERE json_extract(x,'$')<>x
128 AND json_type(x) IN ('object','array');
129} {4}
130
drh20b3b612015-08-29 18:30:30 +0000131do_execsql_test json-5.1 {
132 CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src);
133 INSERT INTO j2(id,json,src)
134 VALUES(1,'{
135 "firstName": "John",
136 "lastName": "Smith",
137 "isAlive": true,
138 "age": 25,
139 "address": {
140 "streetAddress": "21 2nd Street",
141 "city": "New York",
142 "state": "NY",
143 "postalCode": "10021-3100"
144 },
145 "phoneNumbers": [
146 {
147 "type": "home",
148 "number": "212 555-1234"
149 },
150 {
151 "type": "office",
152 "number": "646 555-4567"
153 }
154 ],
155 "children": [],
156 "spouse": null
157 }','https://en.wikipedia.org/wiki/JSON');
158 INSERT INTO j2(id,json,src)
159 VALUES(2, '{
160 "id": "0001",
161 "type": "donut",
162 "name": "Cake",
163 "ppu": 0.55,
164 "batters":
165 {
166 "batter":
167 [
168 { "id": "1001", "type": "Regular" },
169 { "id": "1002", "type": "Chocolate" },
170 { "id": "1003", "type": "Blueberry" },
171 { "id": "1004", "type": "Devil''s Food" }
172 ]
173 },
174 "topping":
175 [
176 { "id": "5001", "type": "None" },
177 { "id": "5002", "type": "Glazed" },
178 { "id": "5005", "type": "Sugar" },
179 { "id": "5007", "type": "Powdered Sugar" },
180 { "id": "5006", "type": "Chocolate with Sprinkles" },
181 { "id": "5003", "type": "Chocolate" },
182 { "id": "5004", "type": "Maple" }
183 ]
184 }','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
185 INSERT INTO j2(id,json,src)
186 VALUES(3,'[
187 {
188 "id": "0001",
189 "type": "donut",
190 "name": "Cake",
191 "ppu": 0.55,
192 "batters":
193 {
194 "batter":
195 [
196 { "id": "1001", "type": "Regular" },
197 { "id": "1002", "type": "Chocolate" },
198 { "id": "1003", "type": "Blueberry" },
199 { "id": "1004", "type": "Devil''s Food" }
200 ]
201 },
202 "topping":
203 [
204 { "id": "5001", "type": "None" },
205 { "id": "5002", "type": "Glazed" },
206 { "id": "5005", "type": "Sugar" },
207 { "id": "5007", "type": "Powdered Sugar" },
208 { "id": "5006", "type": "Chocolate with Sprinkles" },
209 { "id": "5003", "type": "Chocolate" },
210 { "id": "5004", "type": "Maple" }
211 ]
212 },
213 {
214 "id": "0002",
215 "type": "donut",
216 "name": "Raised",
217 "ppu": 0.55,
218 "batters":
219 {
220 "batter":
221 [
222 { "id": "1001", "type": "Regular" }
223 ]
224 },
225 "topping":
226 [
227 { "id": "5001", "type": "None" },
228 { "id": "5002", "type": "Glazed" },
229 { "id": "5005", "type": "Sugar" },
230 { "id": "5003", "type": "Chocolate" },
231 { "id": "5004", "type": "Maple" }
232 ]
233 },
234 {
235 "id": "0003",
236 "type": "donut",
237 "name": "Old Fashioned",
238 "ppu": 0.55,
239 "batters":
240 {
241 "batter":
242 [
243 { "id": "1001", "type": "Regular" },
244 { "id": "1002", "type": "Chocolate" }
245 ]
246 },
247 "topping":
248 [
249 { "id": "5001", "type": "None" },
250 { "id": "5002", "type": "Glazed" },
251 { "id": "5003", "type": "Chocolate" },
252 { "id": "5004", "type": "Maple" }
253 ]
254 }
255 ]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
256 SELECT count(*) FROM j2;
257} {3}
258
259do_execsql_test json-5.2 {
260 SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id;
261} {1 1 object | 2 1 object | 3 1 array |}
262
263ifcapable !vtab {
264 finish_test
265 return
266}
267
268# fullkey is always the same as path+key (with appropriate formatting)
269#
270do_execsql_test json-5.3 {
271 SELECT j2.rowid, jx.rowid, fullkey, path, key
272 FROM j2, json_tree(j2.json) AS jx
273 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
274 ELSE '.'||key END);
275} {}
276do_execsql_test json-5.4 {
277 SELECT j2.rowid, jx.rowid, fullkey, path, key
278 FROM j2, json_each(j2.json) AS jx
279 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
280 ELSE '.'||key END);
281} {}
282
283
284# Verify that the json_each.json and json_tree.json output is always the
285# same as input.
286#
287do_execsql_test json-5.5 {
288 SELECT j2.rowid, jx.rowid, fullkey, path, key
289 FROM j2, json_each(j2.json) AS jx
290 WHERE jx.json<>j2.json;
291} {}
292do_execsql_test json-5.6 {
293 SELECT j2.rowid, jx.rowid, fullkey, path, key
294 FROM j2, json_tree(j2.json) AS jx
295 WHERE jx.json<>j2.json;
296} {}
297do_execsql_test json-5.7 {
298 SELECT j2.rowid, jx.rowid, fullkey, path, key
299 FROM j2, json_each(j2.json) AS jx
300 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
301} {}
302do_execsql_test json-5.8 {
303 SELECT j2.rowid, jx.rowid, fullkey, path, key
304 FROM j2, json_tree(j2.json) AS jx
305 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
306} {}
307
drhf27cd1f2015-09-23 01:10:29 +0000308do_execsql_test json-6.1 {
309 SELECT json_valid('{"a":55,"b":72,}');
310} {0}
311do_execsql_test json-6.2 {
312 SELECT json_valid('{"a":55,"b":72}');
313} {1}
314do_execsql_test json-6.3 {
315 SELECT json_valid('["a",55,"b",72,]');
316} {0}
317do_execsql_test json-6.4 {
318 SELECT json_valid('["a",55,"b",72]');
319} {1}
drh20b3b612015-08-29 18:30:30 +0000320
321
drh5fa5c102015-08-12 16:49:40 +0000322finish_test