interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / json501.test

373 lines · 11405 bytes Raw Blame History
1# 2023-04-27
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 the JSON5 enhancements to the
12# JSON SQL functions extension to the SQLite library.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix json501
18
19# From https://spec.json5.org/#introduction
20#
21#-----------------------------------------------------------------------------
22# Summary of Features
23#
24# The following ECMAScript 5.1 features, which are not supported in JSON, have
25# been extended to JSON5.
26#
27# Objects
28#
29# 1) Object keys may be an ECMAScript 5.1 IdentifierName.
30# 2) Objects may have a single trailing comma.
31#
32# Arrays
33#
34# 3) Arrays may have a single trailing comma.
35#
36# Strings
37#
38# 4) Strings may be single quoted.
39# 5) Strings may span multiple lines by escaping new line characters.
40# 6) Strings may include character escapes.
41#
42# Numbers
43#
44# 7) Numbers may be hexadecimal.
45# 8) Numbers may have a leading or trailing decimal point.
46# 9) Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.
47# 10) Numbers may begin with an explicit plus sign.
48#
49# Comments
50#
51# 11) Single and multi-line comments are allowed.
52#
53# White Space
54#
55# 12) Additional white space characters are allowed.
56#-----------------------------------------------------------------------------
57#
58# Test number in this file are of the form X.Y where X is one of the item
59# numbers in the feature list above and Y is the test sequence number.
60#
61
62###############################################################################
63# 1) Object keys may be an ECMAScript 5.1 IdentifierName.
64do_execsql_test 1.1 {
65 WITH c(x) AS (VALUES('{a:5,b:6}'))
66 SELECT x->>'a', json(x), json_valid(x), NOT json_error_position(x) FROM c;
67} {5 {{"a":5,"b":6}} 0 1}
68do_execsql_test 1.2 {
69 SELECT '[7,null,{a:5,b:6},[8,9]]'->>'$[2].b';
70} {6}
71do_execsql_test 1.3 {
72 SELECT '{ $123 : 789 }'->>'$."$123"';
73} 789
74do_execsql_test 1.4 {
75 SELECT '{ _123$xyz : 789 }'->>'$."_123$xyz"';
76} 789
77do_execsql_test 1.5 {
78 SELECT '{ MNO_123$xyz : 789 }'->>'$."MNO_123$xyz"';
79} 789
80
81do_execsql_test 1.6 {
82 SELECT json('{ MNO_123$xyz : 789 }');
83} [list {{"MNO_123$xyz":789}}]
84
85do_catchsql_test 1.10 {
86 SELECT json('{ MNO_123/xyz : 789 }');
87} {1 {malformed JSON}}
88
89do_execsql_test 1.11 {
90 SELECT '{ MNO_123æxyz : 789 }'->>'MNO_123æxyz';
91} {789}
92
93###############################################################################
94# 2) Objects may have a single trailing comma.
95
96do_execsql_test 2.1 {
97 WITH c(x) AS (VALUES('{"a":5, "b":6, }'))
98 SELECT x->>'b', json(x), json_valid(x), NOT json_error_position(x) FROM c;
99} {6 {{"a":5,"b":6}} 0 1}
100do_execsql_test 2.2 {
101 SELECT '{a:5, b:6 , }'->>'b';
102} 6
103do_catchsql_test 2.3 {
104 SELECT '{a:5, b:6 ,, }'->>'b';
105} {1 {malformed JSON}}
106do_catchsql_test 2.4 {
107 SELECT '{a:5, b:6, ,}'->>'b';
108} {1 {malformed JSON}}
109
110###############################################################################
111# 3) Arrays may have a single trailing comma.
112
113do_execsql_test 3.1 {
114 WITH c(x) AS (VALUES('[5, 6,]'))
115 SELECT x->>1, json(x), json_valid(x), NOT json_error_position(x) FROM c;
116} {6 {[5,6]} 0 1}
117do_execsql_test 3.2 {
118 SELECT '[5, 6 , ]'->>1;
119} 6
120do_catchsql_test 3.3 {
121 SELECT '[5, 6,,]'->>1;
122} {1 {malformed JSON}}
123do_catchsql_test 3.4 {
124 SELECT '[5, 6 , , ]'->>1;
125} {1 {malformed JSON}}
126
127###############################################################################
128# 4) Strings may be single quoted.
129
130do_execsql_test 4.1 {
131 WITH c(x) AS (VALUES('{"a": ''abcd''}'))
132 SELECT x->>'a', json(x), json_valid(x), NOT json_error_position(x) FROM c;
133} {abcd {{"a":"abcd"}} 0 1}
134do_execsql_test 4.2 {
135 SELECT '{b: 123, ''a'': ''ab\''cd''}'->>'a';
136} {ab'cd}
137
138###############################################################################
139# 5) Strings may span multiple lines by escaping new line characters.
140
141do_execsql_test 5.1 {
142 WITH c(x) AS (VALUES('{a: "abc'||char(0x5c,0x0a)||'xyz"}'))
143 SELECT x->>'a', json(x), json_valid(x), NOT json_error_position(x) FROM c;
144} {abcxyz {{"a":"abcxyz"}} 0 1}
145do_execsql_test 5.2 {
146 SELECT ('{a: "abc'||char(0x5c,0x0d)||'xyz"}')->>'a';
147} {abcxyz}
148do_execsql_test 5.3 {
149 SELECT ('{a: "abc'||char(0x5c,0x0d,0x0a)||'xyz"}')->>'a';
150} {abcxyz}
151do_execsql_test 5.4 {
152 SELECT ('{a: "abc'||char(0x5c,0x2028)||'xyz"}')->>'a';
153} {abcxyz}
154do_execsql_test 5.5 {
155 SELECT ('{a: "abc'||char(0x5c,0x2029)||'xyz"}')->>'a';
156} {abcxyz}
157
158
159###############################################################################
160# 6) Strings may include character escapes.
161
162do_execsql_test 6.1 {
163 SELECT ('{a: "abc'||char(0x5c,0x27)||'xyz"}')->>'a';
164} {abc'xyz}
165do_execsql_test 6.2 {
166 SELECT ('{a: "abc'||char(0x5c,0x22)||'xyz"}')->>'a';
167} {abc"xyz}
168do_execsql_test 6.3 {
169 SELECT ('{a: "abc'||char(0x5c,0x5c)||'xyz"}')->>'a';
170} {{abc\xyz}}
171do_execsql_test 6.4 {
172 SELECT hex(('{a: "abc\bxyz"}')->>'a');
173} {6162630878797A}
174do_execsql_test 6.5 {
175 SELECT hex(('{a: "abc\f\n\r\t\vxyz"}')->>'a');
176} {6162630C0A0D090B78797A}
177do_execsql_test 6.6 {
178 SELECT hex(('{a: "abc\0xyz"}')->>'a');
179} {6162630078797A}
180do_execsql_test 6.7 {
181 SELECT '{a: "abc\x35\x4f\x6Exyz"}'->>'a';
182} {abc5Onxyz}
183do_execsql_test 6.8 {
184 SELECT '{a: "\x6a\x6A\x6b\x6B\x6c\x6C\x6d\x6D\x6e\x6E\x6f\x6F"}'->>'a';
185} {jjkkllmmnnoo}
186
187###############################################################################
188# 7) Numbers may be hexadecimal.
189
190do_execsql_test 7.1 {
191 SELECT '{a: 0x0}'->>'a';
192} 0
193do_execsql_test 7.2 {
194 SELECT '{a: -0x0}'->>'a';
195} 0
196do_execsql_test 7.3 {
197 SELECT '{a: +0x0}'->>'a';
198} 0
199do_execsql_test 7.4 {
200 SELECT '{a: 0xabcdef}'->>'a';
201} 11259375
202do_execsql_test 7.5 {
203 SELECT '{a: -0xaBcDeF}'->>'a';
204} -11259375
205do_execsql_test 7.6 {
206 SELECT '{a: +0xABCDEF}'->>'a';
207} 11259375
208
209###############################################################################
210# 8) Numbers may have a leading or trailing decimal point.
211
212do_execsql_test 8.1 {
213 WITH c(x) AS (VALUES('{x: 4.}')) SELECT x->>'x', json(x) FROM c;
214} {4.0 {{"x":4.0}}}
215do_execsql_test 8.2 {
216 WITH c(x) AS (VALUES('{x: +4.}')) SELECT x->>'x', json(x) FROM c;
217} {4.0 {{"x":4.0}}}
218do_execsql_test 8.3 {
219 WITH c(x) AS (VALUES('{x: -4.}')) SELECT x->>'x', json(x) FROM c;
220} {-4.0 {{"x":-4.0}}}
221do_execsql_test 8.3 {
222 WITH c(x) AS (VALUES('{x: .5}')) SELECT x->>'x', json(x) FROM c;
223} {0.5 {{"x":0.5}}}
224do_execsql_test 8.4 {
225 WITH c(x) AS (VALUES('{x: -.5}')) SELECT x->>'x', json(x) FROM c;
226} {-0.5 {{"x":-0.5}}}
227do_execsql_test 8.5 {
228 WITH c(x) AS (VALUES('{x: +.5}')) SELECT x->>'x', json(x) FROM c;
229} {0.5 {{"x":0.5}}}
230do_execsql_test 8.6 {
231 WITH c(x) AS (VALUES('{x: 4.e0}')) SELECT x->>'x', json(x) FROM c;
232} {4.0 {{"x":4.0e0}}}
233do_execsql_test 8.7 {
234 WITH c(x) AS (VALUES('{x: +4.e1}')) SELECT x->>'x', json(x) FROM c;
235} {40.0 {{"x":4.0e1}}}
236do_execsql_test 8.8 {
237 WITH c(x) AS (VALUES('{x: -4.e2}')) SELECT x->>'x', json(x) FROM c;
238} {-400.0 {{"x":-4.0e2}}}
239do_execsql_test 8.9 {
240 WITH c(x) AS (VALUES('{x: .5e3}')) SELECT x->>'x', json(x) FROM c;
241} {500.0 {{"x":0.5e3}}}
242do_execsql_test 8.10 {
243 WITH c(x) AS (VALUES('{x: -.5e-1}')) SELECT x->>'x', json(x) FROM c;
244} {-0.05 {{"x":-0.5e-1}}}
245do_execsql_test 8.11 {
246 WITH c(x) AS (VALUES('{x: +.5e-2}')) SELECT x->>'x', json(x) FROM c;
247} {0.005 {{"x":0.5e-2}}}
248
249
250###############################################################################
251# 9) Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.
252
253do_execsql_test 9.1 {
254 WITH c(x) AS (VALUES('{x: +Infinity}')) SELECT x->>'x', json(x) FROM c;
255} {Inf {{"x":9e999}}}
256do_execsql_test 9.2 {
257 WITH c(x) AS (VALUES('{x: -Infinity}')) SELECT x->>'x', json(x) FROM c;
258} {-Inf {{"x":-9e999}}}
259do_execsql_test 9.3 {
260 WITH c(x) AS (VALUES('{x: Infinity}')) SELECT x->>'x', json(x) FROM c;
261} {Inf {{"x":9e999}}}
262do_execsql_test 9.4 {
263 WITH c(x) AS (VALUES('{x: NaN}')) SELECT x->>'x', json(x) FROM c;
264} {{} {{"x":null}}}
265
266###############################################################################
267# 10) Numbers may begin with an explicit plus sign.
268
269do_execsql_test 10.1 {
270 SELECT '{a: +123}'->'a';
271} 123
272
273###############################################################################
274# 11) Single and multi-line comments are allowed.
275
276do_execsql_test 11.1 {
277 SELECT ' /* abc */ { /*def*/ aaa /* xyz */ : // to the end of line
278 123 /* xyz */ , /* 123 */ }'->>'aaa';
279} 123
280
281###############################################################################
282# 12) Additional white space characters are allowed.
283
284do_execsql_test 12.1 {
285 SELECT (char(0x09,0x0a,0x0b,0x0c,0x0d,0x20,0xa0,0x2028,0x2029)
286 || '{a: "xyz"}')->>'a';
287} xyz
288do_execsql_test 12.2 {
289 SELECT ('{a:' || char(0x09,0x0a,0x0b,0x0c,0x0d,0x20,0xa0,0x2028,0x2029)
290 || '"xyz"}')->>'a';
291} xyz
292do_execsql_test 12.3 {
293 SELECT (char(0x1680,0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,
294 0x2006,0x2007,0x2008,0x2009,0x200a,0x3000,0xfeff)
295 || '{a: "xyz"}')->>'a';
296} xyz
297do_execsql_test 12.4 {
298 SELECT ('{a: ' ||char(0x1680,0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,
299 0x2006,0x2007,0x2008,0x2009,0x200a,0x3000,0xfeff)
300 || ' "xyz"}')->>'a';
301} xyz
302
303# 2023-11-08 forum/forumpost/ddcad3e884
304#
305do_execsql_test 13.1 {
306 SELECT json('{x:''a "b" c''}');
307} {{{"x":"a \"b\" c"}}}
308
309# 2024-01-31
310# Allow control characters within JSON5 string literals.
311#
312for {set c 1} {$c<=0x1f} {incr c} {
313 do_execsql_test 14.$c.1 {
314 SELECT json_valid('"abc' || char($c) || 'xyz"');
315 } {0}
316 do_execsql_test 14.$c.2 {
317 SELECT json_valid('"abc' || char($c) || 'xyz"', 2);
318 } {1}
319 switch $c {
320 8 {set e "\\b"}
321 9 {set e "\\t"}
322 10 {set e "\\n"}
323 12 {set e "\\f"}
324 13 {set e "\\r"}
325 default {set e [format "\\u00%02x" $c]}
326 }
327 do_execsql_test 14.$c.3 {
328 SELECT json('{label:"abc' || char($c) || 'xyz"}');
329 } "{{\"label\":\"abc${e}xyz\"}}"
330 do_execsql_test 14.$c.4 {
331 SELECT jsonb('{label:"abc' || char($c) || 'xyz"}') -> '$';
332 } "{{\"label\":\"abc${e}xyz\"}}"
333}
334
335# Forum post 2026-10-14T08:42:26Z
336#
337ifcapable vtab {
338 do_execsql_test 15.0 {
339 WITH json(jid,jx) AS (VALUES
340 (1,'{"a":"text","b":123,"c":[1,2,3,"t",{"cc":99,"dd":100}],"d":null}'),
341 (2,'[1,"a",{"aa":11,"bb":[9,8,7]},null]')
342 ), paths(pid,px) AS (VALUES(1,'$.c[4]'),(2,'$.c'),(3,'$[2].bb'),(4,'$[2]'))
343 SELECT jj.key, typeof(jj.key) FROM json, paths, json_tree(jx,px) AS jj
344 WHERE parent IS NULL
345 ORDER BY jid, pid;
346 } {4 integer c text bb text 2 integer}
347}
348
349# Bug 2026-10-18T04:42:37Z
350#
351do_execsql_test 16.1 {
352 SELECT hex(('"ab\' || char(10) || '"') ->> '$');
353} {6162}
354do_execsql_test 16.2 {
355 SELECT hex(('"ab\' || char(13,10) || '"') ->> '$');
356} {6162}
357do_execsql_test 16.3 {
358 SELECT hex(('"ab\' || char(8232) || '"') ->> '$');
359} {6162}
360do_execsql_test 16.4 {
361 SELECT hex(('"\' || char(10) || '"') ->> '$');
362} {{}}
363do_execsql_test 16.5 {
364 SELECT hex(jsonb('"ab\' || char(10) || '"') ->> '$');
365} {6162}
366do_execsql_test 16.6 {
367 SELECT hex(('"ab\' || char(10) || 'c"') ->> '$');
368} {616263}
369do_execsql_test 16.7 {
370 SELECT hex('"ab\u0000"' ->> '$');
371} {616200}
372
373finish_test