drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 1 | # 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 | # This file contains tests automatically generated from the json1 |
| 15 | # documentation. |
| 16 | # |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
drh | c306e08 | 2015-10-08 23:37:00 +0000 | [diff] [blame] | 21 | ifcapable !json1 { |
| 22 | finish_test |
| 23 | return |
| 24 | } |
| 25 | |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 26 | do_execsql_test json102-100 { |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 27 | SELECT json_object('ex','[52,3.14159]'); |
| 28 | } {{{"ex":"[52,3.14159]"}}} |
| 29 | do_execsql_test json102-110 { |
| 30 | SELECT json_object('ex',json('[52,3.14159]')); |
| 31 | } {{{"ex":[52,3.14159]}}} |
| 32 | do_execsql_test json102-120 { |
| 33 | SELECT json_object('ex',json_array(52,3.14159)); |
| 34 | } {{{"ex":[52,3.14159]}}} |
| 35 | do_execsql_test json102-130 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 36 | SELECT json(' { "this" : "is", "a": [ "test" ] } '); |
| 37 | } {{{"this":"is","a":["test"]}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 38 | do_execsql_test json102-140 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 39 | SELECT json_array(1,2,'3',4); |
| 40 | } {{[1,2,"3",4]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 41 | do_execsql_test json102-150 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 42 | SELECT json_array('[1,2]'); |
| 43 | } {{["[1,2]"]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 44 | do_execsql_test json102-160 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 45 | SELECT json_array(json_array(1,2)); |
| 46 | } {{[[1,2]]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 47 | do_execsql_test json102-170 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 48 | SELECT json_array(1,null,'3','[4,5]','{"six":7.7}'); |
| 49 | } {{[1,null,"3","[4,5]","{\"six\":7.7}"]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 50 | do_execsql_test json102-180 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 51 | SELECT json_array(1,null,'3',json('[4,5]'),json('{"six":7.7}')); |
| 52 | } {{[1,null,"3",[4,5],{"six":7.7}]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 53 | do_execsql_test json102-190 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 54 | SELECT json_array_length('[1,2,3,4]'); |
| 55 | } {{4}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 56 | do_execsql_test json102-200 { |
| 57 | SELECT json_array_length('[1,2,3,4]', '$'); |
| 58 | } {{4}} |
| 59 | do_execsql_test json102-210 { |
| 60 | SELECT json_array_length('[1,2,3,4]', '$[2]'); |
| 61 | } {{0}} |
| 62 | do_execsql_test json102-220 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 63 | SELECT json_array_length('{"one":[1,2,3]}'); |
| 64 | } {{0}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 65 | do_execsql_test json102-230 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 66 | SELECT json_array_length('{"one":[1,2,3]}', '$.one'); |
| 67 | } {{3}} |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 68 | do_execsql_test json102-240 { |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 69 | SELECT json_array_length('{"one":[1,2,3]}', '$.two'); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 70 | } {{}} |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 71 | do_execsql_test json102-250 { |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 72 | SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$'); |
| 73 | } {{{"a":2,"c":[4,5,{"f":7}]}}} |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 74 | do_execsql_test json102-251 { |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 75 | SELECT json_nextract('{"a":2,"c":[4,5,{"f":7}]}', '$'); |
| 76 | } {{{"a":2,"c":[4,5,{"f":7}]}}} |
| 77 | do_execsql_test json102-252 { |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 78 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$'; |
| 79 | } {{{"a":2,"c":[4,5,{"f":7}]}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 80 | do_execsql_test json102-260 { |
| 81 | SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c'); |
| 82 | } {{[4,5,{"f":7}]}} |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 83 | do_execsql_test json102-261 { |
| 84 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c'; |
| 85 | } {{[4,5,{"f":7}]}} |
| 86 | do_execsql_test json102-262 { |
| 87 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c'; |
| 88 | } {{[4,5,{"f":7}]}} |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 89 | do_catchsql_test json102-265 { |
| 90 | SELECT json_extract('[1,2,3', '$[2]'); |
| 91 | } {1 {malformed JSON}} |
| 92 | do_catchsql_test json102-266 { |
| 93 | SELECT json_nextract('[1,2,3', '$[2]'); |
| 94 | } {0 {{}}} |
| 95 | do_catchsql_test json102-267 { |
| 96 | SELECT json_extract('[1,2,3', '$'); |
| 97 | } {1 {malformed JSON}} |
| 98 | do_catchsql_test json102-268 { |
| 99 | SELECT json_nextract('[1,2,3', '$'); |
| 100 | } {0 {{"[1,2,3"}}} |
drh | 9956c18 | 2022-01-07 17:14:35 +0000 | [diff] [blame] | 101 | do_catchsql_test json102-269a { |
drh | a3f51d7 | 2022-01-07 17:26:40 +0000 | [diff] [blame] | 102 | SELECT '[1,2,3' ->> '$'; |
drh | 9956c18 | 2022-01-07 17:14:35 +0000 | [diff] [blame] | 103 | } {1 {malformed JSON}} |
| 104 | do_catchsql_test json102-269b { |
drh | a3f51d7 | 2022-01-07 17:26:40 +0000 | [diff] [blame] | 105 | SELECT '[1,2,3' -> '$'; |
drh | 338b1fd | 2022-01-07 17:08:48 +0000 | [diff] [blame] | 106 | } {0 {{"[1,2,3"}}} |
| 107 | |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 108 | do_execsql_test json102-270 { |
| 109 | SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]'); |
| 110 | } {{{"f":7}}} |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 111 | do_execsql_test json102-271 { |
| 112 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2]'; |
| 113 | } {{{"f":7}}} |
| 114 | do_execsql_test json102-272 { |
| 115 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2; |
| 116 | } {{{"f":7}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 117 | do_execsql_test json102-280 { |
| 118 | SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f'); |
| 119 | } {{7}} |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 120 | do_execsql_test json102-281 { |
| 121 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2 -> 'f'; |
| 122 | } {{7}} |
| 123 | do_execsql_test json102-282 { |
| 124 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> '[2]' -> 'f'; |
| 125 | } {{7}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 126 | do_execsql_test json102-290 { |
| 127 | SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a'); |
| 128 | } {{[[4,5],2]}} |
| 129 | do_execsql_test json102-300 { |
| 130 | SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x'); |
| 131 | } {{}} |
drh | 12b9fa9 | 2022-01-07 15:47:12 +0000 | [diff] [blame] | 132 | do_execsql_test json102-301 { |
| 133 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> 'x'; |
| 134 | } {{}} |
| 135 | do_execsql_test json102-302 { |
| 136 | SELECT '{"a":2,"c":[4,5,{"f":7}]}' -> NULL; |
| 137 | } {{}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 138 | do_execsql_test json102-310 { |
| 139 | SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a'); |
| 140 | } {{[null,2]}} |
| 141 | do_execsql_test json102-320 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 142 | SELECT json_insert('{"a":2,"c":4}', '$.a', 99); |
| 143 | } {{{"a":2,"c":4}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 144 | do_execsql_test json102-330 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 145 | SELECT json_insert('{"a":2,"c":4}', '$.e', 99); |
| 146 | } {{{"a":2,"c":4,"e":99}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 147 | do_execsql_test json102-340 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 148 | SELECT json_replace('{"a":2,"c":4}', '$.a', 99); |
| 149 | } {{{"a":99,"c":4}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 150 | do_execsql_test json102-350 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 151 | SELECT json_replace('{"a":2,"c":4}', '$.e', 99); |
| 152 | } {{{"a":2,"c":4}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 153 | do_execsql_test json102-360 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 154 | SELECT json_set('{"a":2,"c":4}', '$.a', 99); |
| 155 | } {{{"a":99,"c":4}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 156 | do_execsql_test json102-370 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 157 | SELECT json_set('{"a":2,"c":4}', '$.e', 99); |
| 158 | } {{{"a":2,"c":4,"e":99}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 159 | do_execsql_test json102-380 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 160 | SELECT json_set('{"a":2,"c":4}', '$.c', '[97,96]'); |
| 161 | } {{{"a":2,"c":"[97,96]"}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 162 | do_execsql_test json102-390 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 163 | SELECT json_set('{"a":2,"c":4}', '$.c', json('[97,96]')); |
| 164 | } {{{"a":2,"c":[97,96]}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 165 | do_execsql_test json102-400 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 166 | SELECT json_set('{"a":2,"c":4}', '$.c', json_array(97,96)); |
| 167 | } {{{"a":2,"c":[97,96]}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 168 | do_execsql_test json102-410 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 169 | SELECT json_object('a',2,'c',4); |
| 170 | } {{{"a":2,"c":4}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 171 | do_execsql_test json102-420 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 172 | SELECT json_object('a',2,'c','{e:5}'); |
| 173 | } {{{"a":2,"c":"{e:5}"}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 174 | do_execsql_test json102-430 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 175 | SELECT json_object('a',2,'c',json_object('e',5)); |
| 176 | } {{{"a":2,"c":{"e":5}}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 177 | do_execsql_test json102-440 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 178 | SELECT json_remove('[0,1,2,3,4]','$[2]'); |
| 179 | } {{[0,1,3,4]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 180 | do_execsql_test json102-450 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 181 | SELECT json_remove('[0,1,2,3,4]','$[2]','$[0]'); |
| 182 | } {{[1,3,4]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 183 | do_execsql_test json102-460 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 184 | SELECT json_remove('[0,1,2,3,4]','$[0]','$[2]'); |
| 185 | } {{[1,2,4]}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 186 | do_execsql_test json102-470 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 187 | SELECT json_remove('{"x":25,"y":42}'); |
| 188 | } {{{"x":25,"y":42}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 189 | do_execsql_test json102-480 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 190 | SELECT json_remove('{"x":25,"y":42}','$.z'); |
| 191 | } {{{"x":25,"y":42}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 192 | do_execsql_test json102-490 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 193 | SELECT json_remove('{"x":25,"y":42}','$.y'); |
| 194 | } {{{"x":25}}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 195 | do_execsql_test json102-500 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 196 | SELECT json_remove('{"x":25,"y":42}','$'); |
| 197 | } {{}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 198 | do_execsql_test json102-510 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 199 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}'); |
| 200 | } {{object}} |
drh | a4e4e18 | 2022-01-07 16:03:00 +0000 | [diff] [blame] | 201 | do_execsql_test json102-511 { |
| 202 | SELECT json_ntype('{"a":[2,3.5,true,false,null,"x"]}'); |
| 203 | } {{object}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 204 | do_execsql_test json102-520 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 205 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$'); |
| 206 | } {{object}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 207 | do_execsql_test json102-530 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 208 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a'); |
| 209 | } {{array}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 210 | do_execsql_test json102-540 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 211 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[0]'); |
| 212 | } {{integer}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 213 | do_execsql_test json102-550 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 214 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[1]'); |
| 215 | } {{real}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 216 | do_execsql_test json102-560 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 217 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[2]'); |
| 218 | } {{true}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 219 | do_execsql_test json102-570 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 220 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[3]'); |
| 221 | } {{false}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 222 | do_execsql_test json102-580 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 223 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[4]'); |
| 224 | } {{null}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 225 | do_execsql_test json102-590 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 226 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[5]'); |
| 227 | } {{text}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 228 | do_execsql_test json102-600 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 229 | SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[6]'); |
| 230 | } {{}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 231 | do_execsql_test json102-610 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 232 | SELECT json_valid(char(123)||'"x":35'||char(125)); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 233 | } {{1}} |
drh | e98b6fa | 2015-09-11 15:32:33 +0000 | [diff] [blame] | 234 | do_execsql_test json102-620 { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 235 | SELECT json_valid(char(123)||'"x":35'); |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 236 | } {{0}} |
drh | a4e4e18 | 2022-01-07 16:03:00 +0000 | [diff] [blame] | 237 | do_catchsql_test json102-630 { |
| 238 | SELECT json_type('["a",'); |
| 239 | } {1 {malformed JSON}} |
| 240 | do_catchsql_test json102-631 { |
| 241 | SELECT json_ntype('["a",'); |
| 242 | } {0 {{}}} |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 243 | |
drh | e6f5278 | 2015-09-10 15:22:35 +0000 | [diff] [blame] | 244 | ifcapable vtab { |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 245 | do_execsql_test json102-1000 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 246 | CREATE TABLE user(name,phone); |
| 247 | INSERT INTO user(name,phone) VALUES |
| 248 | ('Alice','["919-555-2345","804-555-3621"]'), |
| 249 | ('Bob','["201-555-8872"]'), |
| 250 | ('Cindy','["704-555-9983"]'), |
| 251 | ('Dave','["336-555-8421","704-555-4321","803-911-4421"]'); |
| 252 | SELECT DISTINCT user.name |
| 253 | FROM user, json_each(user.phone) |
| 254 | WHERE json_each.value LIKE '704-%' |
| 255 | ORDER BY 1; |
| 256 | } {Cindy Dave} |
| 257 | |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 258 | do_execsql_test json102-1010 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 259 | UPDATE user |
| 260 | SET phone=json_extract(phone,'$[0]') |
| 261 | WHERE json_array_length(phone)<2; |
| 262 | SELECT name, substr(phone,1,5) FROM user ORDER BY name; |
| 263 | } {Alice {["919} Bob 201-5 Cindy 704-5 Dave {["336}} |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 264 | do_execsql_test json102-1011 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 265 | SELECT name FROM user WHERE phone LIKE '704-%' |
| 266 | UNION |
| 267 | SELECT user.name |
| 268 | FROM user, json_each(user.phone) |
| 269 | WHERE json_valid(user.phone) |
| 270 | AND json_each.value LIKE '704-%'; |
| 271 | } {Cindy Dave} |
| 272 | |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 273 | do_execsql_test json102-1100 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 274 | CREATE TABLE big(json JSON); |
| 275 | INSERT INTO big(json) VALUES('{ |
| 276 | "id":123, |
| 277 | "stuff":[1,2,3,4], |
| 278 | "partlist":[ |
| 279 | {"uuid":"bb108722-572e-11e5-9320-7f3b63a4ca74"}, |
| 280 | {"uuid":"c690dc14-572e-11e5-95f9-dfc8861fd535"}, |
| 281 | {"subassembly":[ |
| 282 | {"uuid":"6fa5181e-5721-11e5-a04e-57f3d7b32808"} |
| 283 | ]} |
| 284 | ] |
| 285 | }'); |
| 286 | INSERT INTO big(json) VALUES('{ |
| 287 | "id":456, |
| 288 | "stuff":["hello","world","xyzzy"], |
| 289 | "partlist":[ |
| 290 | {"uuid":false}, |
| 291 | {"uuid":"c690dc14-572e-11e5-95f9-dfc8861fd535"} |
| 292 | ] |
| 293 | }'); |
| 294 | } {} |
| 295 | set correct_answer [list \ |
| 296 | 1 {$.id} 123 \ |
| 297 | 1 {$.stuff[0]} 1 \ |
| 298 | 1 {$.stuff[1]} 2 \ |
| 299 | 1 {$.stuff[2]} 3 \ |
| 300 | 1 {$.stuff[3]} 4 \ |
| 301 | 1 {$.partlist[0].uuid} bb108722-572e-11e5-9320-7f3b63a4ca74 \ |
| 302 | 1 {$.partlist[1].uuid} c690dc14-572e-11e5-95f9-dfc8861fd535 \ |
| 303 | 1 {$.partlist[2].subassembly[0].uuid} 6fa5181e-5721-11e5-a04e-57f3d7b32808 \ |
| 304 | 2 {$.id} 456 \ |
| 305 | 2 {$.stuff[0]} hello \ |
| 306 | 2 {$.stuff[1]} world \ |
| 307 | 2 {$.stuff[2]} xyzzy \ |
| 308 | 2 {$.partlist[0].uuid} 0 \ |
| 309 | 2 {$.partlist[1].uuid} c690dc14-572e-11e5-95f9-dfc8861fd535] |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 310 | do_execsql_test json102-1110 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 311 | SELECT big.rowid, fullkey, value |
| 312 | FROM big, json_tree(big.json) |
| 313 | WHERE json_tree.type NOT IN ('object','array') |
| 314 | ORDER BY +big.rowid, +json_tree.id |
| 315 | } $correct_answer |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 316 | do_execsql_test json102-1120 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 317 | SELECT big.rowid, fullkey, atom |
| 318 | FROM big, json_tree(big.json) |
| 319 | WHERE atom IS NOT NULL |
| 320 | ORDER BY +big.rowid, +json_tree.id |
| 321 | } $correct_answer |
| 322 | |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 323 | do_execsql_test json102-1130 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 324 | SELECT DISTINCT json_extract(big.json,'$.id') |
| 325 | FROM big, json_tree(big.json,'$.partlist') |
| 326 | WHERE json_tree.key='uuid' |
| 327 | AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808'; |
| 328 | } {123} |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 329 | do_execsql_test json102-1131 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 330 | SELECT DISTINCT json_extract(big.json,'$.id') |
| 331 | FROM big, json_tree(big.json,'$') |
| 332 | WHERE json_tree.key='uuid' |
| 333 | AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808'; |
| 334 | } {123} |
drh | 12b3b89 | 2015-09-11 01:22:41 +0000 | [diff] [blame] | 335 | do_execsql_test json102-1132 { |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 336 | SELECT DISTINCT json_extract(big.json,'$.id') |
| 337 | FROM big, json_tree(big.json) |
| 338 | WHERE json_tree.key='uuid' |
| 339 | AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808'; |
| 340 | } {123} |
drh | e6f5278 | 2015-09-10 15:22:35 +0000 | [diff] [blame] | 341 | } ;# end ifcapable vtab |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 342 | |
dan | 2e8f551 | 2015-09-17 17:21:09 +0000 | [diff] [blame] | 343 | #------------------------------------------------------------------------- |
| 344 | # Test that json_valid() correctly identifies non-ascii range |
| 345 | # characters as non-whitespace. |
| 346 | # |
| 347 | do_execsql_test json102-1201 { SELECT json_valid(char(32) || '"xyz"') } 1 |
| 348 | do_execsql_test json102-1202 { SELECT json_valid(char(200) || '"xyz"') } 0 |
| 349 | |
drh | 4977ccf | 2015-09-19 11:57:26 +0000 | [diff] [blame] | 350 | # Off-by-one error in jsonAppendString() |
| 351 | # |
| 352 | for {set i 0} {$i<100} {incr i} { |
| 353 | set str abcdef[string repeat \" [expr {$i+50}]]uvwxyz |
| 354 | do_test json102-[format %d [expr {$i+1300}]] { |
| 355 | db eval {SELECT json_extract(json_array($::str),'$[0]')==$::str} |
| 356 | } {1} |
| 357 | } |
| 358 | |
drh | 9fa866a | 2017-04-08 18:18:22 +0000 | [diff] [blame] | 359 | #------------------------------------------------------------------------- |
| 360 | # 2017-04-08 ticket b93be8729a895a528e2849fca99f7 |
| 361 | # JSON extension accepts invalid numeric values |
| 362 | # |
| 363 | # JSON does not allow leading zeros. But the JSON extension was |
| 364 | # allowing them. The following tests verify that the problem is now |
| 365 | # fixed. |
| 366 | # |
| 367 | do_execsql_test json102-1401 { SELECT json_valid('{"x":01}') } 0 |
| 368 | do_execsql_test json102-1402 { SELECT json_valid('{"x":-01}') } 0 |
| 369 | do_execsql_test json102-1403 { SELECT json_valid('{"x":0}') } 1 |
| 370 | do_execsql_test json102-1404 { SELECT json_valid('{"x":-0}') } 1 |
| 371 | do_execsql_test json102-1405 { SELECT json_valid('{"x":0.1}') } 1 |
| 372 | do_execsql_test json102-1406 { SELECT json_valid('{"x":-0.1}') } 1 |
| 373 | do_execsql_test json102-1407 { SELECT json_valid('{"x":0.0000}') } 1 |
| 374 | do_execsql_test json102-1408 { SELECT json_valid('{"x":-0.0000}') } 1 |
| 375 | do_execsql_test json102-1409 { SELECT json_valid('{"x":01.5}') } 0 |
| 376 | do_execsql_test json102-1410 { SELECT json_valid('{"x":-01.5}') } 0 |
| 377 | do_execsql_test json102-1411 { SELECT json_valid('{"x":00}') } 0 |
| 378 | do_execsql_test json102-1412 { SELECT json_valid('{"x":-00}') } 0 |
| 379 | |
drh | e12e24d | 2017-04-10 12:25:05 +0000 | [diff] [blame] | 380 | #------------------------------------------------------------------------ |
| 381 | # 2017-04-10 ticket 6c9b5514077fed34551f98e64c09a10dc2fc8e16 |
| 382 | # JSON extension accepts strings containing control characters. |
| 383 | # |
| 384 | # The JSON spec requires that all control characters be escaped. |
| 385 | # |
| 386 | do_execsql_test json102-1500 { |
| 387 | WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<0x20) |
| 388 | SELECT x FROM c WHERE json_valid(printf('{"a":"x%sz"}', char(x))) ORDER BY x; |
| 389 | } {32} |
| 390 | |
drh | 2ac702d | 2017-04-10 12:31:15 +0000 | [diff] [blame] | 391 | # All control characters are escaped |
| 392 | # |
| 393 | do_execsql_test json102-1501 { |
| 394 | WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<0x1f) |
| 395 | SELECT sum(json_valid(json_quote('a'||char(x)||'z'))) FROM c ORDER BY x; |
| 396 | } {31} |
| 397 | |
drh | 852944e | 2015-09-10 03:29:11 +0000 | [diff] [blame] | 398 | finish_test |