| 1 |
# 2023-04-28 |
| 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 |
|
| 15 |
set testdir [file dirname $argv0] |
| 16 |
source $testdir/tester.tcl |
| 17 |
set testprefix json502 |
| 18 |
|
| 19 |
ifcapable vtab { |
| 20 |
|
| 21 |
do_execsql_test 1.1 { |
| 22 |
CREATE TABLE t1(x JSON); |
| 23 |
INSERT INTO t1(x) VALUES('{a:{b:{c:"hello",},},}'); |
| 24 |
SELECT fullkey FROM t1, json_tree(x); |
| 25 |
} {{$} {$.a} {$.a.b} {$.a.b.c}} |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
do_execsql_test 2.1 { |
| 30 |
SELECT json_error_position('{a:null,{"h":[1,[1,2,3]],"j":"abc"}:true}'); |
| 31 |
} 9 |
| 32 |
do_catchsql_test 2.2 { |
| 33 |
SELECT json('{a:null,{"h":[1,[1,2,3]],"j":"abc"}:true}'); |
| 34 |
} {1 {malformed JSON}} |
| 35 |
do_catchsql_test 2.3 { |
| 36 |
SELECT '{a:null,{"h":[1,[1,2,3]],"j":"abc"}:true}'->'$h[#-1]'; |
| 37 |
} {1 {malformed JSON}} |
| 38 |
|
| 39 |
# Verify that escaped label names are compared correctly. |
| 40 |
# |
| 41 |
do_execsql_test 3.1 { |
| 42 |
SELECT '{"a\x62c":123}' ->> 'abc'; |
| 43 |
} 123 |
| 44 |
do_execsql_test 3.2 { |
| 45 |
SELECT '{"abc":123}' ->> 'a\x62c'; |
| 46 |
} 123 |
| 47 |
|
| 48 |
db null null |
| 49 |
do_execsql_test 3.3 { |
| 50 |
DROP TABLE IF EXISTS t1; |
| 51 |
CREATE TABLE t1(x); |
| 52 |
INSERT INTO t1 VALUES(json_insert('{}','$.a\',111,'$."b\\"',222)); |
| 53 |
INSERT INTO t1 VALUES(jsonb_insert('{}','$.a\',111,'$."b\\"',222)); |
| 54 |
SELECT x->'$.a\', x->'$.a\\', x->'$."a\\"', x->'$."b\\"' FROM t1; |
| 55 |
} {111 null 111 222 111 null 111 222} |
| 56 |
|
| 57 |
do_execsql_test 3.4 { |
| 58 |
SELECT json_patch('{"a\x62c":123}','{"ab\x63":456}') ->> 'abc'; |
| 59 |
} 456 |
| 60 |
|
| 61 |
ifcapable vtab { |
| 62 |
do_execsql_test 4.1 { |
| 63 |
SELECT * FROM json_tree('{"\u0017":1}','$."\x17"'); |
| 64 |
} {{\x17} 1 integer 1 1 null {$."\x17"} {$}} |
| 65 |
} |
| 66 |
|
| 67 |
# JSON PATH parsing bug involving backslash escapes, reported via |
| 68 |
# private email from Florent De'Neve on 2024-09-04. |
| 69 |
# |
| 70 |
do_execsql_test 5.1 { |
| 71 |
SELECT json_extract('{"A\"Key":1}', '$.A"Key'); |
| 72 |
} 1 |
| 73 |
do_execsql_test 5.2 { |
| 74 |
SELECT json_extract('{"A\"Key":1}', '$."A\"Key"'); |
| 75 |
} 1 |
| 76 |
do_execsql_test 5.3 { |
| 77 |
SELECT JSON_SET('{}', '$."\"Key"', 1); |
| 78 |
} {{{"\"Key":1}}} |
| 79 |
|
| 80 |
finish_test |