3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # Legacy JSON bug: If the input is a BLOB that when cast into TEXT looks
13 # like valid JSON, then treat it as valid JSON.
15 # The original intent of the JSON functions was to raise an error on any
16 # BLOB input. That intent was clearly documented, but the code failed to
17 # to implement it. Subsequently, many applications began to depend on the
18 # incorrect behavior, especially apps that used readfile() to read JSON
19 # content, since readfile() returns a BLOB. So we need to support the
22 # The tests in this fail verify that the original buggy behavior is
26 set testdir [file dirname $argv0]
27 source $testdir/tester.tcl
28 set testprefix json107
30 if {[db one {PRAGMA encoding}]!="UTF-8"} {
31 # These tests only work for a UTF-8 encoding.
37 SELECT json_valid( CAST('{"a":1}' AS BLOB) );
39 do_execsql_test 1.1.1 {
40 SELECT json_valid( CAST('{"a":1}' AS BLOB), 1);
42 do_execsql_test 1.1.2 {
43 SELECT json_valid( CAST('{"a":1}' AS BLOB), 2);
45 do_execsql_test 1.1.4 {
46 SELECT json_valid( CAST('{"a":1}' AS BLOB), 4);
48 do_execsql_test 1.1.8 {
49 SELECT json_valid( CAST('{"a":1}' AS BLOB), 8);
52 do_execsql_test 1.2.1 {
53 SELECT CAST('{"a":123}' AS blob) -> 'a';
55 do_execsql_test 1.2.2 {
56 SELECT CAST('{"a":123}' AS blob) ->> 'a';
58 do_execsql_test 1.2.3 {
59 SELECT json_extract(CAST('{"a":123}' AS blob), '$.a');
62 SELECT json_insert(CAST('{"a":123}' AS blob),'$.b',456);
63 } {{{"a":123,"b":456}}}
65 SELECT json_remove(CAST('{"a":123,"b":456}' AS blob),'$.a');
68 SELECT json_set(CAST('{"a":123,"b":456}' AS blob),'$.a',789);
69 } {{{"a":789,"b":456}}}
71 SELECT json_replace(CAST('{"a":123,"b":456}' AS blob),'$.a',789);
72 } {{{"a":789,"b":456}}}
74 SELECT json_type(CAST('{"a":123,"b":456}' AS blob));
77 SELECT json(CAST('{"a":123,"b":456}' AS blob));
78 } {{{"a":123,"b":456}}}
82 SELECT key, value FROM json_tree( CAST('{"a":123,"b":456}' AS blob) )