blob: 8c62c8b16c6410eed41dd24ae901230b1c3e7f8f [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
drhc306e082015-10-08 23:37:00 +000018ifcapable !json1 {
19 finish_test
20 return
21}
22
drhb7601462015-09-24 11:06:26 +000023do_execsql_test json101-1.1.00 {
drh5fa5c102015-08-12 16:49:40 +000024 SELECT json_array(1,2.5,null,'hello');
25} {[1,2.5,null,"hello"]}
drhb7601462015-09-24 11:06:26 +000026do_execsql_test json101-1.1.01 {
drhf5ddb9c2015-09-11 00:06:41 +000027 SELECT json_array(1,'{"abc":2.5,"def":null,"ghi":hello}',99);
28 -- the second term goes in as a string:
29} {[1,"{\\"abc\\":2.5,\\"def\\":null,\\"ghi\\":hello}",99]}
drhb7601462015-09-24 11:06:26 +000030do_execsql_test json101-1.1.02 {
drhf5ddb9c2015-09-11 00:06:41 +000031 SELECT json_array(1,json('{"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]}
drhb7601462015-09-24 11:06:26 +000034do_execsql_test json101-1.1.03 {
drhf5ddb9c2015-09-11 00:06:41 +000035 SELECT json_array(1,json_object('abc',2.5,'def',null,'ghi','hello'),99);
36 -- the second term goes in as JSON
37} {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
drhb7601462015-09-24 11:06:26 +000038do_execsql_test json101-1.2 {
drh5fa5c102015-08-12 16:49:40 +000039 SELECT hex(json_array('String "\ Test'));
40} {5B22537472696E67205C225C5C2054657374225D}
drhb7601462015-09-24 11:06:26 +000041do_catchsql_test json101-1.3 {
drhdc384952015-09-19 18:54:39 +000042 SELECT json_array(1,printf('%.1000c','x'),x'abcd',3);
drh5fa5c102015-08-12 16:49:40 +000043} {1 {JSON cannot hold BLOB values}}
drhb7601462015-09-24 11:06:26 +000044do_execsql_test json101-1.4 {
drh5fa5c102015-08-12 16:49:40 +000045 SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1,
46 0.0, 1.0, -1.0, -1e99, +2e100,
47 'one','two','three',
48 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
49 19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
50 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
51 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
52 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
53 99);
54} {[-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]}
55
drhb7601462015-09-24 11:06:26 +000056do_execsql_test json101-2.1 {
drh2032d602015-08-12 17:23:34 +000057 SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
58} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
drhb7601462015-09-24 11:06:26 +000059do_catchsql_test json101-2.2 {
drhdc384952015-09-19 18:54:39 +000060 SELECT json_object('a',printf('%.1000c','x'),2,2.5);
drh2032d602015-08-12 17:23:34 +000061} {1 {json_object() labels must be TEXT}}
drhb7601462015-09-24 11:06:26 +000062do_catchsql_test json101-2.3 {
drh2032d602015-08-12 17:23:34 +000063 SELECT json_object('a',1,'b');
64} {1 {json_object() requires an even number of arguments}}
drhb7601462015-09-24 11:06:26 +000065do_catchsql_test json101-2.4 {
drhdc384952015-09-19 18:54:39 +000066 SELECT json_object('a',printf('%.1000c','x'),'b',x'abcd');
drh2032d602015-08-12 17:23:34 +000067} {1 {JSON cannot hold BLOB values}}
68
drhb7601462015-09-24 11:06:26 +000069do_execsql_test json101-3.1 {
drhecb5fed2015-08-28 03:33:50 +000070 SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]');
71} {{{"a":"[3,4,5]","b":2}}}
drhb7601462015-09-24 11:06:26 +000072do_execsql_test json101-3.2 {
drhf5ddb9c2015-09-11 00:06:41 +000073 SELECT json_replace('{"a":1,"b":2}','$.a',json('[3,4,5]'));
drhecb5fed2015-08-28 03:33:50 +000074} {{{"a":[3,4,5],"b":2}}}
drhb7601462015-09-24 11:06:26 +000075do_execsql_test json101-3.3 {
drhecb5fed2015-08-28 03:33:50 +000076 SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
77} {text}
drhb7601462015-09-24 11:06:26 +000078do_execsql_test json101-3.4 {
drhf5ddb9c2015-09-11 00:06:41 +000079 SELECT json_type(json_set('{"a":1,"b":2}','$.b',json('{"x":3,"y":4}')),'$.b');
drhecb5fed2015-08-28 03:33:50 +000080} {object}
drhb7601462015-09-24 11:06:26 +000081ifcapable vtab {
82do_execsql_test json101-3.5 {
drh8cb0c832015-09-22 00:21:03 +000083 SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456));
84} {{$} {} | {$.x} 456 |}
drhb7601462015-09-24 11:06:26 +000085}
drh2032d602015-08-12 17:23:34 +000086
drhd1f00682015-08-29 16:02:37 +000087# Per rfc7159, any JSON value is allowed at the top level, and whitespace
88# is permitting before and/or after that value.
89#
drhb7601462015-09-24 11:06:26 +000090do_execsql_test json101-4.1 {
drhd1f00682015-08-29 16:02:37 +000091 CREATE TABLE j1(x);
92 INSERT INTO j1(x)
93 VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'),
94 ('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'),
95 ('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'),
96 ('{"a":true,"b":{"c":false}}');
97 SELECT * FROM j1 WHERE NOT json_valid(x);
98} {}
drhb7601462015-09-24 11:06:26 +000099do_execsql_test json101-4.2 {
drhd1f00682015-08-29 16:02:37 +0000100 SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x);
101} {}
drhb7601462015-09-24 11:06:26 +0000102do_execsql_test json101-4.3 {
drhd1f00682015-08-29 16:02:37 +0000103 SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d));
104} {}
105
106# But an empty string, or a string of pure whitespace is not valid JSON.
107#
drhb7601462015-09-24 11:06:26 +0000108do_execsql_test json101-4.4 {
drhd1f00682015-08-29 16:02:37 +0000109 SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d));
110} {0 0}
111
112# json_remove() and similar functions with no edit operations return their
113# input unchanged.
114#
drhb7601462015-09-24 11:06:26 +0000115do_execsql_test json101-4.5 {
drhd1f00682015-08-29 16:02:37 +0000116 SELECT x FROM j1 WHERE json_remove(x)<>x;
117} {}
drhb7601462015-09-24 11:06:26 +0000118do_execsql_test json101-4.6 {
drhd1f00682015-08-29 16:02:37 +0000119 SELECT x FROM j1 WHERE json_replace(x)<>x;
120} {}
drhb7601462015-09-24 11:06:26 +0000121do_execsql_test json101-4.7 {
drhd1f00682015-08-29 16:02:37 +0000122 SELECT x FROM j1 WHERE json_set(x)<>x;
123} {}
drhb7601462015-09-24 11:06:26 +0000124do_execsql_test json101-4.8 {
drhd1f00682015-08-29 16:02:37 +0000125 SELECT x FROM j1 WHERE json_insert(x)<>x;
126} {}
127
128# json_extract(JSON,'$') will return objects and arrays without change.
129#
130do_execsql_test json-4.10 {
131 SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array');
132 SELECT x FROM j1
133 WHERE json_extract(x,'$')<>x
134 AND json_type(x) IN ('object','array');
135} {4}
136
drh20b3b612015-08-29 18:30:30 +0000137do_execsql_test json-5.1 {
138 CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src);
139 INSERT INTO j2(id,json,src)
140 VALUES(1,'{
141 "firstName": "John",
142 "lastName": "Smith",
143 "isAlive": true,
144 "age": 25,
145 "address": {
146 "streetAddress": "21 2nd Street",
147 "city": "New York",
148 "state": "NY",
149 "postalCode": "10021-3100"
150 },
151 "phoneNumbers": [
152 {
153 "type": "home",
154 "number": "212 555-1234"
155 },
156 {
157 "type": "office",
158 "number": "646 555-4567"
159 }
160 ],
161 "children": [],
162 "spouse": null
163 }','https://en.wikipedia.org/wiki/JSON');
164 INSERT INTO j2(id,json,src)
165 VALUES(2, '{
166 "id": "0001",
167 "type": "donut",
168 "name": "Cake",
169 "ppu": 0.55,
170 "batters":
171 {
172 "batter":
173 [
174 { "id": "1001", "type": "Regular" },
175 { "id": "1002", "type": "Chocolate" },
176 { "id": "1003", "type": "Blueberry" },
177 { "id": "1004", "type": "Devil''s Food" }
178 ]
179 },
180 "topping":
181 [
182 { "id": "5001", "type": "None" },
183 { "id": "5002", "type": "Glazed" },
184 { "id": "5005", "type": "Sugar" },
185 { "id": "5007", "type": "Powdered Sugar" },
186 { "id": "5006", "type": "Chocolate with Sprinkles" },
187 { "id": "5003", "type": "Chocolate" },
188 { "id": "5004", "type": "Maple" }
189 ]
190 }','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
191 INSERT INTO j2(id,json,src)
192 VALUES(3,'[
193 {
194 "id": "0001",
195 "type": "donut",
196 "name": "Cake",
197 "ppu": 0.55,
198 "batters":
199 {
200 "batter":
201 [
202 { "id": "1001", "type": "Regular" },
203 { "id": "1002", "type": "Chocolate" },
204 { "id": "1003", "type": "Blueberry" },
205 { "id": "1004", "type": "Devil''s Food" }
206 ]
207 },
208 "topping":
209 [
210 { "id": "5001", "type": "None" },
211 { "id": "5002", "type": "Glazed" },
212 { "id": "5005", "type": "Sugar" },
213 { "id": "5007", "type": "Powdered Sugar" },
214 { "id": "5006", "type": "Chocolate with Sprinkles" },
215 { "id": "5003", "type": "Chocolate" },
216 { "id": "5004", "type": "Maple" }
217 ]
218 },
219 {
220 "id": "0002",
221 "type": "donut",
222 "name": "Raised",
223 "ppu": 0.55,
224 "batters":
225 {
226 "batter":
227 [
228 { "id": "1001", "type": "Regular" }
229 ]
230 },
231 "topping":
232 [
233 { "id": "5001", "type": "None" },
234 { "id": "5002", "type": "Glazed" },
235 { "id": "5005", "type": "Sugar" },
236 { "id": "5003", "type": "Chocolate" },
237 { "id": "5004", "type": "Maple" }
238 ]
239 },
240 {
241 "id": "0003",
242 "type": "donut",
243 "name": "Old Fashioned",
244 "ppu": 0.55,
245 "batters":
246 {
247 "batter":
248 [
249 { "id": "1001", "type": "Regular" },
250 { "id": "1002", "type": "Chocolate" }
251 ]
252 },
253 "topping":
254 [
255 { "id": "5001", "type": "None" },
256 { "id": "5002", "type": "Glazed" },
257 { "id": "5003", "type": "Chocolate" },
258 { "id": "5004", "type": "Maple" }
259 ]
260 }
261 ]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
262 SELECT count(*) FROM j2;
263} {3}
264
265do_execsql_test json-5.2 {
266 SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id;
267} {1 1 object | 2 1 object | 3 1 array |}
268
269ifcapable !vtab {
270 finish_test
271 return
272}
273
274# fullkey is always the same as path+key (with appropriate formatting)
275#
276do_execsql_test json-5.3 {
277 SELECT j2.rowid, jx.rowid, fullkey, path, key
278 FROM j2, json_tree(j2.json) AS jx
279 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
280 ELSE '.'||key END);
281} {}
282do_execsql_test json-5.4 {
283 SELECT j2.rowid, jx.rowid, fullkey, path, key
284 FROM j2, json_each(j2.json) AS jx
285 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
286 ELSE '.'||key END);
287} {}
288
289
290# Verify that the json_each.json and json_tree.json output is always the
291# same as input.
292#
293do_execsql_test json-5.5 {
294 SELECT j2.rowid, jx.rowid, fullkey, path, key
295 FROM j2, json_each(j2.json) AS jx
296 WHERE jx.json<>j2.json;
297} {}
298do_execsql_test json-5.6 {
299 SELECT j2.rowid, jx.rowid, fullkey, path, key
300 FROM j2, json_tree(j2.json) AS jx
301 WHERE jx.json<>j2.json;
302} {}
303do_execsql_test json-5.7 {
304 SELECT j2.rowid, jx.rowid, fullkey, path, key
305 FROM j2, json_each(j2.json) AS jx
306 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
307} {}
308do_execsql_test json-5.8 {
309 SELECT j2.rowid, jx.rowid, fullkey, path, key
310 FROM j2, json_tree(j2.json) AS jx
311 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
312} {}
313
drhf27cd1f2015-09-23 01:10:29 +0000314do_execsql_test json-6.1 {
315 SELECT json_valid('{"a":55,"b":72,}');
316} {0}
317do_execsql_test json-6.2 {
318 SELECT json_valid('{"a":55,"b":72}');
319} {1}
320do_execsql_test json-6.3 {
321 SELECT json_valid('["a",55,"b",72,]');
322} {0}
323do_execsql_test json-6.4 {
324 SELECT json_valid('["a",55,"b",72]');
325} {1}
drh20b3b612015-08-29 18:30:30 +0000326
drhb9e8f592015-10-16 15:16:06 +0000327# White-space tests. Note that form-feed is not white-space in JSON.
328# ticket [57eec374ae1d0a1d4a23077a95f4e173fe269113]
329#
330foreach {tn isvalid ws} {
331 7.1 1 char(0x20)
332 7.2 1 char(0x09)
333 7.3 1 char(0x0A)
334 7.4 1 char(0x0D)
335 7.5 0 char(0x0C)
336 7.6 1 char(0x20,0x09,0x0a,0x0d,0x20)
337 7.7 0 char(0x20,0x09,0x0a,0x0c,0x0d,0x20)
338} {
339 do_execsql_test json-$tn.1 \
340 "SELECT json_valid(printf('%s{%s\"x\"%s:%s9%s}%s',
341 $::ws,$::ws,$::ws,$::ws,$::ws,$::ws));" \
342 $isvalid
343}
drh20b3b612015-08-29 18:30:30 +0000344
drh5fa5c102015-08-12 16:49:40 +0000345finish_test