SQLite

Check-in [ab78e694]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improved handling of NULL arguments to json_valid() and json_error_position(). Forum post 06c6334412.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ab78e6946ba8125a83ff155561adf9c8df4d1c09a645fce08c8245ddb08bfa3b
User & Date: drh 2023-05-02 11:12:01
Context
2023-05-02
16:34
Interpret negative arguments to sqlite3_sleep() as zero. (check-in: 2b542326 user: drh tags: trunk)
11:12
Improved handling of NULL arguments to json_valid() and json_error_position(). Forum post 06c6334412. (check-in: ab78e694 user: drh tags: trunk)
10:22
Fix typo in comment. Forum post 3da7d9c445. (check-in: 41a8a15c user: drh tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

2552
2553
2554
2555
2556
2557
2558

2559
2560
2561
2562
2563
2564
2565
static void jsonValidFunc(
  sqlite3_context *ctx,
  int argc,
  sqlite3_value **argv
){
  JsonParse *p;          /* The parse */
  UNUSED_PARAMETER(argc);

  p = jsonParseCached(ctx, argv, 0);
  if( p==0 || p->oom ){
    sqlite3_result_error_nomem(ctx);
    sqlite3_free(p);
  }else{
    sqlite3_result_int(ctx, p->nErr==0 && p->hasNonstd==0);
    if( p->nErr ) jsonParseFree(p);







>







2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
static void jsonValidFunc(
  sqlite3_context *ctx,
  int argc,
  sqlite3_value **argv
){
  JsonParse *p;          /* The parse */
  UNUSED_PARAMETER(argc);
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  p = jsonParseCached(ctx, argv, 0);
  if( p==0 || p->oom ){
    sqlite3_result_error_nomem(ctx);
    sqlite3_free(p);
  }else{
    sqlite3_result_int(ctx, p->nErr==0 && p->hasNonstd==0);
    if( p->nErr ) jsonParseFree(p);
2597
2598
2599
2600
2601
2602
2603

2604
2605
2606
2607
2608
2609
2610
static void jsonErrorFunc(
  sqlite3_context *ctx,
  int argc,
  sqlite3_value **argv
){
  JsonParse *p;          /* The parse */
  UNUSED_PARAMETER(argc);

  p = jsonParseCached(ctx, argv, 0);
  if( p==0 || p->oom ){
    sqlite3_result_error_nomem(ctx);
    sqlite3_free(p);
  }else if( p->nErr==0 ){
    sqlite3_result_int(ctx, 0);
  }else{







>







2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
static void jsonErrorFunc(
  sqlite3_context *ctx,
  int argc,
  sqlite3_value **argv
){
  JsonParse *p;          /* The parse */
  UNUSED_PARAMETER(argc);
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  p = jsonParseCached(ctx, argv, 0);
  if( p==0 || p->oom ){
    sqlite3_result_error_nomem(ctx);
    sqlite3_free(p);
  }else if( p->nErr==0 ){
    sqlite3_result_int(ctx, 0);
  }else{

Changes to test/json101.test.

913
914
915
916
917
918
919





























































































920
921
} {{{"a":9.0e+999,"b":-9.0e+999}}}
do_execsql_test json-20.2 {
  SELECT json_object('a',2e370,'b',-3e380)->>'a';
} Inf
do_execsql_test json-20.3 {
  SELECT json_object('a',2e370,'b',-3e380)->>'b';
} {-Inf}






























































































finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
} {{{"a":9.0e+999,"b":-9.0e+999}}}
do_execsql_test json-20.2 {
  SELECT json_object('a',2e370,'b',-3e380)->>'a';
} Inf
do_execsql_test json-20.3 {
  SELECT json_object('a',2e370,'b',-3e380)->>'b';
} {-Inf}

# 2023-05-02 https://sqlite.org/forum/forumpost/06c6334412
# JSON functions should normally return NULL when given
# a NULL value as the JSON input.
#
db null NULL
do_execsql_test json-21.1 {
  SELECT json_valid(NULL);
} NULL
do_execsql_test json-21.2 {
  SELECT json_error_position(NULL);
} NULL
do_execsql_test json-21.3 {
  SELECT json(NULL);
} NULL
do_execsql_test json-21.4 {
  SELECT json_array(NULL);
} {[null]}
do_execsql_test json-21.5 {
  SELECT json_extract(NULL);
} NULL
do_execsql_test json-21.6 {
  SELECT json_insert(NULL,'$',123);
} NULL
do_execsql_test json-21.7 {
  SELECT NULL->0;
} NULL
do_execsql_test json-21.8 {
  SELECT NULL->>0;
} NULL
do_execsql_test json-21.9 {
  SELECT '{a:5}'->NULL;
} NULL
do_execsql_test json-21.10 {
  SELECT '{a:5}'->>NULL;
} NULL
do_catchsql_test json-21.11 {
  SELECT json_object(NULL,5);
} {1 {json_object() labels must be TEXT}}
do_execsql_test json-21.12 {
  SELECT json_patch(NULL,'{a:5}');
} NULL
do_execsql_test json-21.13 {
  SELECT json_patch('{a:5}',NULL);
} NULL
do_execsql_test json-21.14 {
  SELECT json_patch(NULL,NULL);
} NULL
do_execsql_test json-21.15 {
  SELECT json_remove(NULL,'$');
} NULL
do_execsql_test json-21.16 {
  SELECT json_remove('{a:5,b:7}',NULL);
} NULL
do_execsql_test json-21.17 {
  SELECT json_replace(NULL,'$.a',123);
} NULL
do_execsql_test json-21.18 {
  SELECT json_replace('{a:5,b:7}',NULL,NULL);
} {{{"a":5,"b":7}}}
do_execsql_test json-21.19 {
  SELECT json_set(NULL,'$.a',123);
} NULL
do_execsql_test json-21.20 {
  SELECT json_set('{a:5,b:7}',NULL,NULL);
} {{{"a":5,"b":7}}}
do_execsql_test json-21.21 {
  SELECT json_type(NULL);
} NULL
do_execsql_test json-21.22 {
  SELECT json_type('{a:5,b:7}',NULL);
} NULL
do_execsql_test json-21.23 {
  SELECT json_quote(NULL);
} null
do_execsql_test json-21.24 {
  SELECT count(*) FROM json_each(NULL);
} 0
do_execsql_test json-21.25 {
  SELECT count(*) FROM json_tree(NULL);
} 0
do_execsql_test json-21.26 {
  WITH c(x) AS (VALUES(1),(2.0),(NULL),('three'))
  SELECT json_group_array(x) FROM c;
} {[1,2.0,null,"three"]}
do_execsql_test json-21.27 {
  WITH c(x,y) AS (VALUES('a',1),('b',2.0),('c',NULL),(NULL,'three'),('e','four'))
  SELECT json_group_object(x,y) FROM c;
} {{{"a":1,"b":2.0,"c":null,:"three","e":"four"}}}





finish_test