blob: 943292640d9cb192ed17f98da7b46ff49ec55692 [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
drhb7601462015-09-24 11:06:26 +000019do_execsql_test json101-1.1.00 {
drh5fa5c102015-08-12 16:49:40 +000020 SELECT json_array(1,2.5,null,'hello');
21} {[1,2.5,null,"hello"]}
drhb7601462015-09-24 11:06:26 +000022do_execsql_test json101-1.1.01 {
drhf5ddb9c2015-09-11 00:06:41 +000023 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]}
drhb7601462015-09-24 11:06:26 +000026do_execsql_test json101-1.1.02 {
drhf5ddb9c2015-09-11 00:06:41 +000027 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]}
drhb7601462015-09-24 11:06:26 +000030do_execsql_test json101-1.1.03 {
drhf5ddb9c2015-09-11 00:06:41 +000031 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]}
drhb7601462015-09-24 11:06:26 +000034do_execsql_test json101-1.2 {
drh5fa5c102015-08-12 16:49:40 +000035 SELECT hex(json_array('String "\ Test'));
36} {5B22537472696E67205C225C5C2054657374225D}
drhb7601462015-09-24 11:06:26 +000037do_catchsql_test json101-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}}
drhb7601462015-09-24 11:06:26 +000040do_execsql_test json101-1.4 {
drh5fa5c102015-08-12 16:49:40 +000041 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
drhb7601462015-09-24 11:06:26 +000052do_execsql_test json101-2.1 {
drh2032d602015-08-12 17:23:34 +000053 SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
54} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
drhb7601462015-09-24 11:06:26 +000055do_catchsql_test json101-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}}
drhb7601462015-09-24 11:06:26 +000058do_catchsql_test json101-2.3 {
drh2032d602015-08-12 17:23:34 +000059 SELECT json_object('a',1,'b');
60} {1 {json_object() requires an even number of arguments}}
drhb7601462015-09-24 11:06:26 +000061do_catchsql_test json101-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
drhb7601462015-09-24 11:06:26 +000065do_execsql_test json101-3.1 {
drhecb5fed2015-08-28 03:33:50 +000066 SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]');
67} {{{"a":"[3,4,5]","b":2}}}
drhb7601462015-09-24 11:06:26 +000068do_execsql_test json101-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}}}
drhb7601462015-09-24 11:06:26 +000071do_execsql_test json101-3.3 {
drhecb5fed2015-08-28 03:33:50 +000072 SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
73} {text}
drhb7601462015-09-24 11:06:26 +000074do_execsql_test json101-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}
drhb7601462015-09-24 11:06:26 +000077ifcapable vtab {
78do_execsql_test json101-3.5 {
drh8cb0c832015-09-22 00:21:03 +000079 SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456));
80} {{$} {} | {$.x} 456 |}
drhb7601462015-09-24 11:06:26 +000081}
drh2032d602015-08-12 17:23:34 +000082
drhd1f00682015-08-29 16:02:37 +000083# Per rfc7159, any JSON value is allowed at the top level, and whitespace
84# is permitting before and/or after that value.
85#
drhb7601462015-09-24 11:06:26 +000086do_execsql_test json101-4.1 {
drhd1f00682015-08-29 16:02:37 +000087 CREATE TABLE j1(x);
88 INSERT INTO j1(x)
89 VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'),
90 ('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'),
91 ('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'),
92 ('{"a":true,"b":{"c":false}}');
93 SELECT * FROM j1 WHERE NOT json_valid(x);
94} {}
drhb7601462015-09-24 11:06:26 +000095do_execsql_test json101-4.2 {
drhd1f00682015-08-29 16:02:37 +000096 SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x);
97} {}
drhb7601462015-09-24 11:06:26 +000098do_execsql_test json101-4.3 {
drhd1f00682015-08-29 16:02:37 +000099 SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d));
100} {}
101
102# But an empty string, or a string of pure whitespace is not valid JSON.
103#
drhb7601462015-09-24 11:06:26 +0000104do_execsql_test json101-4.4 {
drhd1f00682015-08-29 16:02:37 +0000105 SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d));
106} {0 0}
107
108# json_remove() and similar functions with no edit operations return their
109# input unchanged.
110#
drhb7601462015-09-24 11:06:26 +0000111do_execsql_test json101-4.5 {
drhd1f00682015-08-29 16:02:37 +0000112 SELECT x FROM j1 WHERE json_remove(x)<>x;
113} {}
drhb7601462015-09-24 11:06:26 +0000114do_execsql_test json101-4.6 {
drhd1f00682015-08-29 16:02:37 +0000115 SELECT x FROM j1 WHERE json_replace(x)<>x;
116} {}
drhb7601462015-09-24 11:06:26 +0000117do_execsql_test json101-4.7 {
drhd1f00682015-08-29 16:02:37 +0000118 SELECT x FROM j1 WHERE json_set(x)<>x;
119} {}
drhb7601462015-09-24 11:06:26 +0000120do_execsql_test json101-4.8 {
drhd1f00682015-08-29 16:02:37 +0000121 SELECT x FROM j1 WHERE json_insert(x)<>x;
122} {}
123
124# json_extract(JSON,'$') will return objects and arrays without change.
125#
126do_execsql_test json-4.10 {
127 SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array');
128 SELECT x FROM j1
129 WHERE json_extract(x,'$')<>x
130 AND json_type(x) IN ('object','array');
131} {4}
132
drh20b3b612015-08-29 18:30:30 +0000133do_execsql_test json-5.1 {
134 CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src);
135 INSERT INTO j2(id,json,src)
136 VALUES(1,'{
137 "firstName": "John",
138 "lastName": "Smith",
139 "isAlive": true,
140 "age": 25,
141 "address": {
142 "streetAddress": "21 2nd Street",
143 "city": "New York",
144 "state": "NY",
145 "postalCode": "10021-3100"
146 },
147 "phoneNumbers": [
148 {
149 "type": "home",
150 "number": "212 555-1234"
151 },
152 {
153 "type": "office",
154 "number": "646 555-4567"
155 }
156 ],
157 "children": [],
158 "spouse": null
159 }','https://en.wikipedia.org/wiki/JSON');
160 INSERT INTO j2(id,json,src)
161 VALUES(2, '{
162 "id": "0001",
163 "type": "donut",
164 "name": "Cake",
165 "ppu": 0.55,
166 "batters":
167 {
168 "batter":
169 [
170 { "id": "1001", "type": "Regular" },
171 { "id": "1002", "type": "Chocolate" },
172 { "id": "1003", "type": "Blueberry" },
173 { "id": "1004", "type": "Devil''s Food" }
174 ]
175 },
176 "topping":
177 [
178 { "id": "5001", "type": "None" },
179 { "id": "5002", "type": "Glazed" },
180 { "id": "5005", "type": "Sugar" },
181 { "id": "5007", "type": "Powdered Sugar" },
182 { "id": "5006", "type": "Chocolate with Sprinkles" },
183 { "id": "5003", "type": "Chocolate" },
184 { "id": "5004", "type": "Maple" }
185 ]
186 }','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
187 INSERT INTO j2(id,json,src)
188 VALUES(3,'[
189 {
190 "id": "0001",
191 "type": "donut",
192 "name": "Cake",
193 "ppu": 0.55,
194 "batters":
195 {
196 "batter":
197 [
198 { "id": "1001", "type": "Regular" },
199 { "id": "1002", "type": "Chocolate" },
200 { "id": "1003", "type": "Blueberry" },
201 { "id": "1004", "type": "Devil''s Food" }
202 ]
203 },
204 "topping":
205 [
206 { "id": "5001", "type": "None" },
207 { "id": "5002", "type": "Glazed" },
208 { "id": "5005", "type": "Sugar" },
209 { "id": "5007", "type": "Powdered Sugar" },
210 { "id": "5006", "type": "Chocolate with Sprinkles" },
211 { "id": "5003", "type": "Chocolate" },
212 { "id": "5004", "type": "Maple" }
213 ]
214 },
215 {
216 "id": "0002",
217 "type": "donut",
218 "name": "Raised",
219 "ppu": 0.55,
220 "batters":
221 {
222 "batter":
223 [
224 { "id": "1001", "type": "Regular" }
225 ]
226 },
227 "topping":
228 [
229 { "id": "5001", "type": "None" },
230 { "id": "5002", "type": "Glazed" },
231 { "id": "5005", "type": "Sugar" },
232 { "id": "5003", "type": "Chocolate" },
233 { "id": "5004", "type": "Maple" }
234 ]
235 },
236 {
237 "id": "0003",
238 "type": "donut",
239 "name": "Old Fashioned",
240 "ppu": 0.55,
241 "batters":
242 {
243 "batter":
244 [
245 { "id": "1001", "type": "Regular" },
246 { "id": "1002", "type": "Chocolate" }
247 ]
248 },
249 "topping":
250 [
251 { "id": "5001", "type": "None" },
252 { "id": "5002", "type": "Glazed" },
253 { "id": "5003", "type": "Chocolate" },
254 { "id": "5004", "type": "Maple" }
255 ]
256 }
257 ]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
258 SELECT count(*) FROM j2;
259} {3}
260
261do_execsql_test json-5.2 {
262 SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id;
263} {1 1 object | 2 1 object | 3 1 array |}
264
265ifcapable !vtab {
266 finish_test
267 return
268}
269
270# fullkey is always the same as path+key (with appropriate formatting)
271#
272do_execsql_test json-5.3 {
273 SELECT j2.rowid, jx.rowid, fullkey, path, key
274 FROM j2, json_tree(j2.json) AS jx
275 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
276 ELSE '.'||key END);
277} {}
278do_execsql_test json-5.4 {
279 SELECT j2.rowid, jx.rowid, fullkey, path, key
280 FROM j2, json_each(j2.json) AS jx
281 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
282 ELSE '.'||key END);
283} {}
284
285
286# Verify that the json_each.json and json_tree.json output is always the
287# same as input.
288#
289do_execsql_test json-5.5 {
290 SELECT j2.rowid, jx.rowid, fullkey, path, key
291 FROM j2, json_each(j2.json) AS jx
292 WHERE jx.json<>j2.json;
293} {}
294do_execsql_test json-5.6 {
295 SELECT j2.rowid, jx.rowid, fullkey, path, key
296 FROM j2, json_tree(j2.json) AS jx
297 WHERE jx.json<>j2.json;
298} {}
299do_execsql_test json-5.7 {
300 SELECT j2.rowid, jx.rowid, fullkey, path, key
301 FROM j2, json_each(j2.json) AS jx
302 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
303} {}
304do_execsql_test json-5.8 {
305 SELECT j2.rowid, jx.rowid, fullkey, path, key
306 FROM j2, json_tree(j2.json) AS jx
307 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
308} {}
309
drhf27cd1f2015-09-23 01:10:29 +0000310do_execsql_test json-6.1 {
311 SELECT json_valid('{"a":55,"b":72,}');
312} {0}
313do_execsql_test json-6.2 {
314 SELECT json_valid('{"a":55,"b":72}');
315} {1}
316do_execsql_test json-6.3 {
317 SELECT json_valid('["a",55,"b",72,]');
318} {0}
319do_execsql_test json-6.4 {
320 SELECT json_valid('["a",55,"b",72]');
321} {1}
drh20b3b612015-08-29 18:30:30 +0000322
323
drh5fa5c102015-08-12 16:49:40 +0000324finish_test