AryaWu/sqlite
0
1# 2024-01-232#3# The author disclaims copyright to this source code. In place of4# 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# 12# Legacy JSON bug: If the input is a BLOB that when cast into TEXT looks13# like valid JSON, then treat it as valid JSON.14#15# The original intent of the JSON functions was to raise an error on any16# BLOB input. That intent was clearly documented, but the code failed to17# to implement it. Subsequently, many applications began to depend on the18# incorrect behavior, especially apps that used readfile() to read JSON19# content, since readfile() returns a BLOB. So we need to support the20# bug moving forward.21#22# The tests in this fail verify that the original buggy behavior is23# preserved.24#25 26set testdir [file dirname $argv0]27source $testdir/tester.tcl28set testprefix json10729 30if {[db one {PRAGMA encoding}]!="UTF-8"} {31 # These tests only work for a UTF-8 encoding.32 finish_test33 return34}35 36do_execsql_test 1.1 {37 SELECT json_valid( CAST('{"a":1}' AS BLOB) );38} 139do_execsql_test 1.1.1 {40 SELECT json_valid( CAST('{"a":1}' AS BLOB), 1);41} 142do_execsql_test 1.1.2 {43 SELECT json_valid( CAST('{"a":1}' AS BLOB), 2);44} 145do_execsql_test 1.1.4 {46 SELECT json_valid( CAST('{"a":1}' AS BLOB), 4);47} 048do_execsql_test 1.1.8 {49 SELECT json_valid( CAST('{"a":1}' AS BLOB), 8);50} 051 52do_execsql_test 1.2.1 {53 SELECT CAST('{"a":123}' AS blob) -> 'a';54} 12355do_execsql_test 1.2.2 {56 SELECT CAST('{"a":123}' AS blob) ->> 'a';57} 12358do_execsql_test 1.2.3 {59 SELECT json_extract(CAST('{"a":123}' AS blob), '$.a');60} 12361do_execsql_test 1.3 {62 SELECT json_insert(CAST('{"a":123}' AS blob),'$.b',456);63} {{{"a":123,"b":456}}}64do_execsql_test 1.4 {65 SELECT json_remove(CAST('{"a":123,"b":456}' AS blob),'$.a');66} {{{"b":456}}}67do_execsql_test 1.5 {68 SELECT json_set(CAST('{"a":123,"b":456}' AS blob),'$.a',789);69} {{{"a":789,"b":456}}}70do_execsql_test 1.6 {71 SELECT json_replace(CAST('{"a":123,"b":456}' AS blob),'$.a',789);72} {{{"a":789,"b":456}}}73do_execsql_test 1.7 {74 SELECT json_type(CAST('{"a":123,"b":456}' AS blob));75} object76do_execsql_test 1.8 {77 SELECT json(CAST('{"a":123,"b":456}' AS blob));78} {{{"a":123,"b":456}}}79 80ifcapable vtab {81 do_execsql_test 2.1 {82 SELECT key, value FROM json_tree( CAST('{"a":123,"b":456}' AS blob) )83 WHERE atom;84 } {a 123 b 456}85} 86finish_test87 