drh | 5fa5c10 | 2015-08-12 16:49:40 +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 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | |
| 18 | load_static_extension db json |
| 19 | do_execsql_test json1-1.1 { |
| 20 | SELECT json_array(1,2.5,null,'hello'); |
| 21 | } {[1,2.5,null,"hello"]} |
| 22 | do_execsql_test json1-1.2 { |
| 23 | SELECT hex(json_array('String "\ Test')); |
| 24 | } {5B22537472696E67205C225C5C2054657374225D} |
| 25 | do_catchsql_test json1-1.3 { |
| 26 | SELECT json_array(1,2,x'abcd',3); |
| 27 | } {1 {JSON cannot hold BLOB values}} |
| 28 | do_execsql_test json1-1.4 { |
| 29 | SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1, |
| 30 | 0.0, 1.0, -1.0, -1e99, +2e100, |
| 31 | 'one','two','three', |
| 32 | 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 33 | 19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 34 | 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 35 | 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 36 | 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 37 | 99); |
| 38 | } {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]} |
| 39 | |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 40 | do_execsql_test json1-2.1 { |
| 41 | SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test'); |
| 42 | } {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}} |
| 43 | do_catchsql_test json1-2.2 { |
| 44 | SELECT json_object('a',1,2,2.5); |
| 45 | } {1 {json_object() labels must be TEXT}} |
| 46 | do_catchsql_test json1-2.3 { |
| 47 | SELECT json_object('a',1,'b'); |
| 48 | } {1 {json_object() requires an even number of arguments}} |
| 49 | do_catchsql_test json1-2.4 { |
| 50 | SELECT json_object('a',1,'b',x'abcd'); |
| 51 | } {1 {JSON cannot hold BLOB values}} |
| 52 | |
drh | ecb5fed | 2015-08-28 03:33:50 +0000 | [diff] [blame] | 53 | do_execsql_test json1-3.1 { |
| 54 | SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]'); |
| 55 | } {{{"a":"[3,4,5]","b":2}}} |
| 56 | do_execsql_test json1-3.2 { |
| 57 | SELECT json_replace('{"a":1,"b":2}','$$.a','[3,4,5]'); |
| 58 | } {{{"a":[3,4,5],"b":2}}} |
| 59 | do_execsql_test json1-3.3 { |
| 60 | SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b'); |
| 61 | } {text} |
| 62 | do_execsql_test json1-3.4 { |
| 63 | SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b'); |
| 64 | } {object} |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 65 | |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 66 | # Per rfc7159, any JSON value is allowed at the top level, and whitespace |
| 67 | # is permitting before and/or after that value. |
| 68 | # |
| 69 | do_execsql_test json1-4.1 { |
| 70 | CREATE TABLE j1(x); |
| 71 | INSERT INTO j1(x) |
| 72 | VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'), |
| 73 | ('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'), |
| 74 | ('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'), |
| 75 | ('{"a":true,"b":{"c":false}}'); |
| 76 | SELECT * FROM j1 WHERE NOT json_valid(x); |
| 77 | } {} |
| 78 | do_execsql_test json1-4.2 { |
| 79 | SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x); |
| 80 | } {} |
| 81 | do_execsql_test json1-4.3 { |
| 82 | SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d)); |
| 83 | } {} |
| 84 | |
| 85 | # But an empty string, or a string of pure whitespace is not valid JSON. |
| 86 | # |
| 87 | do_execsql_test json1-4.4 { |
| 88 | SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d)); |
| 89 | } {0 0} |
| 90 | |
| 91 | # json_remove() and similar functions with no edit operations return their |
| 92 | # input unchanged. |
| 93 | # |
| 94 | do_execsql_test json1-4.5 { |
| 95 | SELECT x FROM j1 WHERE json_remove(x)<>x; |
| 96 | } {} |
| 97 | do_execsql_test json1-4.6 { |
| 98 | SELECT x FROM j1 WHERE json_replace(x)<>x; |
| 99 | } {} |
| 100 | do_execsql_test json1-4.7 { |
| 101 | SELECT x FROM j1 WHERE json_set(x)<>x; |
| 102 | } {} |
| 103 | do_execsql_test json1-4.8 { |
| 104 | SELECT x FROM j1 WHERE json_insert(x)<>x; |
| 105 | } {} |
| 106 | |
| 107 | # json_extract(JSON,'$') will return objects and arrays without change. |
| 108 | # |
| 109 | do_execsql_test json-4.10 { |
| 110 | SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array'); |
| 111 | SELECT x FROM j1 |
| 112 | WHERE json_extract(x,'$')<>x |
| 113 | AND json_type(x) IN ('object','array'); |
| 114 | } {4} |
| 115 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 116 | finish_test |