SQLite

Check-in [64abb65d4d]
Login

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

Overview
Comment:Add the json_check() function, which returns its argument if the argument is well-formed JSON or which throws an error otherwise.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 64abb65d4df11e5b3bcc4afc8e7c18e907c6080a
User & Date: drh 2015-08-28 03:48:04.807
Context
2015-08-28
16:18
When searching the wal file for a frame, do not search that part that was already checkpointed when the transaction was opened. (check-in: a84cf4f5d3 user: dan tags: trunk)
15:50
Merge latest trunk into this branch. (Closed-Leaf check-in: ab93024da7 user: dan tags: wal-read-change)
09:27
Merge latest trunk changes with this branch. (check-in: 57bc0194f4 user: dan tags: begin-concurrent)
03:48
Add the json_check() function, which returns its argument if the argument is well-formed JSON or which throws an error otherwise. (check-in: 64abb65d4d user: drh tags: trunk)
03:33
Enhance the json_insert(), json_replace(), and json_set() functions with the ability to add JSON instead of text if the argument is text and if the PATH begins with '$$' instead of just '$'. (check-in: 44f103d886 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/misc/json1.c.
1044
1045
1046
1047
1048
1049
1050






















1051
1052
1053
1054
1055
1056
1057
        }
      }
    }
    jsonParseReset(&x);
  }
  if( !x.oom ) sqlite3_result_int64(ctx, n);
}























/*
** json_extract(JSON, PATH)
**
** Return the element described by PATH.  Return NULL if JSON is not
** valid JSON or if there is no PATH element or if PATH is malformed.
*/







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







1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
        }
      }
    }
    jsonParseReset(&x);
  }
  if( !x.oom ) sqlite3_result_int64(ctx, n);
}

/*
** json_check(JSON)
**
** Check the JSON argument to verify that it is well-formed.  Return a
** compacted version of the argument (with white-space removed) if the
** argument is well-formed.  Through an error if the argument is not
** correctly formatted JSON.
*/
static void jsonCheckFunc(
  sqlite3_context *ctx,
  int argc,
  sqlite3_value **argv
){
  JsonParse x;          /* The parse */
  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ){
    sqlite3_result_error(ctx, "malformed JSON", -1);
    return;
  }
  jsonReturn(x.aNode, ctx, argv);
  jsonParseReset(&x);
}

/*
** json_extract(JSON, PATH)
**
** Return the element described by PATH.  Return NULL if JSON is not
** valid JSON or if there is no PATH element or if PATH is malformed.
*/
1776
1777
1778
1779
1780
1781
1782

1783
1784
1785
1786
1787
1788
1789
     int nArg;
     int flag;
     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  } aFunc[] = {
    { "json_array",          -1, 0,   jsonArrayFunc         },
    { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
    { "json_array_length",    2, 0,   jsonArrayLengthFunc   },

    { "json_extract",         2, 0,   jsonExtractFunc       },
    { "json_insert",         -1, 0,   jsonSetFunc           },
    { "json_object",         -1, 0,   jsonObjectFunc        },
    { "json_remove",         -1, 0,   jsonRemoveFunc        },
    { "json_replace",        -1, 0,   jsonReplaceFunc       },
    { "json_set",            -1, 1,   jsonSetFunc           },
    { "json_type",            1, 0,   jsonTypeFunc          },







>







1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
     int nArg;
     int flag;
     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  } aFunc[] = {
    { "json_array",          -1, 0,   jsonArrayFunc         },
    { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
    { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
    { "json_check",           1, 0,   jsonCheckFunc         },
    { "json_extract",         2, 0,   jsonExtractFunc       },
    { "json_insert",         -1, 0,   jsonSetFunc           },
    { "json_object",         -1, 0,   jsonObjectFunc        },
    { "json_remove",         -1, 0,   jsonRemoveFunc        },
    { "json_replace",        -1, 0,   jsonReplaceFunc       },
    { "json_set",            -1, 1,   jsonSetFunc           },
    { "json_type",            1, 0,   jsonTypeFunc          },
Changes to test/json101.test.
58
59
60
61
62
63
64







65
66
} {{{"a":[3,4,5],"b":2}}}
do_execsql_test json1-3.3 {
  SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
} {text}
do_execsql_test json1-3.4 {
  SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b');
} {object}








finish_test







>
>
>
>
>
>
>


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
} {{{"a":[3,4,5],"b":2}}}
do_execsql_test json1-3.3 {
  SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
} {text}
do_execsql_test json1-3.4 {
  SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b');
} {object}

do_execsql_test json1-4.1 {
  SELECT json_check('  [  1,   2]   ');
} {[1,2]}
do_catchsql_test json1-4.2 {
  SELECT json_check('  [  1,   2    ');
} {1 {malformed JSON}}

finish_test