1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-05-28 12:41:31 +03:00

Add the json_error(X) function that returns the 1-based character offset to

the first syntax error in JSON5 string X, or 0 if there are no errors.

FossilOrigin-Name: 901ad995d5a722ca2672516205ff488e9acd703a828ca5fc43f11fca5f2af120
This commit is contained in:
drh 2023-04-28 14:48:11 +00:00
parent 016f46666b
commit 272ae627c5
3 changed files with 111 additions and 18 deletions

View File

@ -1,5 +1,5 @@
C Fix\sindentation\sand\somit\strailing\swhitespace\sin\sthe\srandom\sJSON\sgenerator\nscript.
D 2023-04-28T13:25:35.641
C Add\sthe\sjson_error(X)\sfunction\sthat\sreturns\sthe\s1-based\scharacter\soffset\sto\nthe\sfirst\ssyntax\serror\sin\sJSON5\sstring\sX,\sor\s0\sif\sthere\sare\sno\serrors.
D 2023-04-28T14:48:11.699
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -593,7 +593,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
F src/hwtime.h b638809e083b601b618df877b2e89cb87c2a47a01f4def10be4c4ebb54664ac7
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c a8de1db43335fc4946370a7a7e47d89975ad678ddb15078a150e993ba2fb37d4
F src/json.c 7d03a1185d7aee74f53a3f2381355ff4b71d955f287d29d30c0b0979afc96b67
F src/json.c b532d42d310e570d7f620a692a26f21dc7306063b866088c4e43c647a17118b3
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c be5af440f3192c58681b5d43167dbca3ccbfce394d89faa22378a14264781136
F src/main.c 09bc5191f75dc48fc4dfddda143cb864c0c3dbc3297eb9a9c8e01fea58ff847d
@ -2066,8 +2066,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P f56528d413d8e622f7c4f18b2f9f2e620bfb441c020461299b35a90072ee6c13
R 5da141bb06598ed103d496559df7ae36
P 629db09fceb7bf37561b52ccee06ebf4df261291e9a8ffcca82b243f6db5ff07
R cda10fe16274848e02e4d5ea3713e4b9
U drh
Z 790f8e2b554134402e5889a19faba52a
Z 80f17384e6d82e3c8657040553f2124e
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
629db09fceb7bf37561b52ccee06ebf4df261291e9a8ffcca82b243f6db5ff07
901ad995d5a722ca2672516205ff488e9acd703a828ca5fc43f11fca5f2af120

View File

@ -1073,7 +1073,10 @@ json_parse_restart:
iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
if( iThis<0 ) return -1;
for(j=i+1;;j++){
if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
if( ++pParse->iDepth > JSON_MAX_DEPTH ){
pParse->iErr = j;
return -1;
}
x = jsonParseValue(pParse, j);
if( x<=0 ){
if( x==(-2) ){
@ -1090,6 +1093,7 @@ json_parse_restart:
pParse->has5 = 1;
x = k;
}else{
pParse->iErr = j;
return -1;
}
}
@ -1109,13 +1113,19 @@ json_parse_restart:
}
}
x = jsonParseValue(pParse, j);
if( x!=(-5) ) return -1;
if( x!=(-5) ){
pParse->iErr = j;
return -1;
}
j = pParse->iErr+1;
}
parse_object_value:
x = jsonParseValue(pParse, j);
pParse->iDepth--;
if( x<=0 ) return -1;
if( x<=0 ){
pParse->iErr = j;
return -1;
}
j = x;
if( z[j]==',' ){
continue;
@ -1140,6 +1150,7 @@ json_parse_restart:
break;
}
}
pParse->iErr = j;
return -1;
}
pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
@ -1151,7 +1162,10 @@ json_parse_restart:
if( iThis<0 ) return -1;
memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
for(j=i+1;;j++){
if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
if( ++pParse->iDepth > JSON_MAX_DEPTH ){
pParse->iErr = j;
return -1;
}
x = jsonParseValue(pParse, j);
pParse->iDepth--;
if( x<=0 ){
@ -1160,6 +1174,7 @@ json_parse_restart:
if( pParse->nNode!=(u32)iThis+1 ) pParse->has5 = 1;
break;
}
pParse->iErr = j;
return -1;
}
j = x;
@ -1186,6 +1201,7 @@ json_parse_restart:
break;
}
}
pParse->iErr = j;
return -1;
}
pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
@ -1207,6 +1223,7 @@ json_parse_restart:
c = z[j];
if( (c & ~0x1f)==0 ){
/* Control characters are not allowed in strings */
pParse->iErr = j;
return -1;
}
if( c=='\\' ){
@ -1227,6 +1244,7 @@ json_parse_restart:
jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
pParse->has5 = 1;
}else{
pParse->iErr = j;
return -1;
}
}else if( c==cDelim ){
@ -1242,6 +1260,7 @@ json_parse_restart:
jsonParseAddNode(pParse, JSON_NULL, 0, 0);
return i+4;
}
pParse->iErr = i;
return -1;
}
case 't': {
@ -1249,6 +1268,7 @@ json_parse_restart:
jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
return i+4;
}
pParse->iErr = i;
return -1;
}
case 'f': {
@ -1256,6 +1276,7 @@ json_parse_restart:
jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
return i+5;
}
pParse->iErr = i;
return -1;
}
case '+': {
@ -1271,6 +1292,7 @@ json_parse_restart:
seenDP = JSON_REAL;
goto parse_number_2;
}
pParse->iErr = i;
return -1;
case '-':
case '0':
@ -1338,6 +1360,7 @@ json_parse_restart:
jnFlags |= JNODE_JSON5;
goto parse_number_2;
}
pParse->iErr = i;
return -1;
}
if( z[i+1]=='0' ){
@ -1358,7 +1381,10 @@ json_parse_restart:
c = z[j];
if( sqlite3Isdigit(c) ) continue;
if( c=='.' ){
if( seenDP==JSON_REAL ) return -1;
if( seenDP==JSON_REAL ){
pParse->iErr = j;
return -1;
}
seenDP = JSON_REAL;
continue;
}
@ -1368,10 +1394,14 @@ json_parse_restart:
pParse->has5 = 1;
jnFlags |= JNODE_JSON5;
}else{
pParse->iErr = j;
return -1;
}
}
if( seenE ) return -1;
if( seenE ){
pParse->iErr = j;
return -1;
}
seenDP = JSON_REAL;
seenE = 1;
c = z[j+1];
@ -1379,7 +1409,10 @@ json_parse_restart:
j++;
c = z[j+1];
}
if( c<'0' || c>'9' ) return -1;
if( c<'0' || c>'9' ){
pParse->iErr = j;
return -1;
}
continue;
}
break;
@ -1389,6 +1422,7 @@ json_parse_restart:
pParse->has5 = 1;
jnFlags |= JNODE_JSON5;
}else{
pParse->iErr = j;
return -1;
}
}
@ -1402,6 +1436,7 @@ json_parse_restart:
pParse->has5 = 1;
return i+3;
}
pParse->iErr = i;
return -1;
}
case 'I': {
@ -1410,6 +1445,7 @@ json_parse_restart:
pParse->has5 = 1;
return i+8;
}
pParse->iErr = i;
return -1;
}
case '}': {
@ -1453,6 +1489,7 @@ json_parse_restart:
pParse->has5 = 1;
goto json_parse_restart;
}
pParse->iErr = i;
return -1;
}
default: {
@ -1471,6 +1508,7 @@ json_parse_restart:
return i + nn;
}
#endif
pParse->iErr = i;
return -1; /* Syntax error */
}
} /* End switch(z[i]) */
@ -1575,6 +1613,15 @@ static int jsonParseFindParents(JsonParse *pParse){
** is no longer valid, parse the JSON again and return the new parse,
** and also register the new parse so that it will be available for
** future sqlite3_get_auxdata() calls.
**
** If an error occurs and pErrCtx!=0 then report the error on pErrCtx
** and return NULL.
**
** If an error occurs and pErrCtx==0 then return the Parse object with
** JsonParse.nErr non-zero. If the caller invokes this routine with
** pErrCtx==0 and it gets back a JsonParse with nErr!=0, then the caller
** is responsible for invoking jsonParseFree() on the returned value.
** But the caller may invoke jsonParseFree() *only* if pParse->nErr!=0.
*/
static JsonParse *jsonParseCached(
sqlite3_context *pCtx,
@ -1624,6 +1671,10 @@ static JsonParse *jsonParseCached(
p->zJson = (char*)&p[1];
memcpy((char*)p->zJson, zJson, nJson+1);
if( jsonParse(p, pErrCtx, p->zJson) ){
if( pErrCtx==0 ){
p->nErr = 1;
return p;
}
sqlite3_free(p);
return 0;
}
@ -2501,8 +2552,8 @@ static void jsonTypeFunc(
/*
** json_valid(JSON)
**
** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
** Return 0 otherwise.
** Return 1 if JSON is a well-formed canonical JSON string according
** to RFC-7159. Return 0 otherwise.
*/
static void jsonValidFunc(
sqlite3_context *ctx,
@ -2512,8 +2563,17 @@ static void jsonValidFunc(
JsonParse *p; /* The parse */
UNUSED_PARAMETER(argc);
p = jsonParseCached(ctx, argv, 0);
sqlite3_result_int(ctx, p!=0 && p->has5==0);
sqlite3_result_int(ctx, p!=0 && p->nErr==0 && p->has5==0);
if( p!=0 && p->nErr ) jsonParseFree(p);
}
/*
** json_valid5(JSON)
**
** Return 1 if JSON is a well-formed JSON5 string.
** Return 0 otherwise.
*/
static void jsonValid5Func(
sqlite3_context *ctx,
int argc,
@ -2522,7 +2582,39 @@ static void jsonValid5Func(
JsonParse *p; /* The parse */
UNUSED_PARAMETER(argc);
p = jsonParseCached(ctx, argv, 0);
sqlite3_result_int(ctx, p!=0);
sqlite3_result_int(ctx, p!=0 && p->nErr==0);
if( p!=0 && p->nErr ) jsonParseFree(p);
}
/*
** json_error(JSON)
**
** If JSON is not a well-formed JSON5 string, then return the 1-based
** character offset to the location of the first error in that string.
** Return 0 otherwise.
*/
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 || p->nErr==0 ){
sqlite3_result_int(ctx, 0);
}else{
int n = 0;
int i;
const char *z = p->zJson;
for(i=0; i<p->iErr && z[i]; i++){
if( (z[i]&0xc0)!=80 ) n++;
}
sqlite3_result_int(ctx, n);
jsonParseFree(p);
}
}
@ -3235,6 +3327,7 @@ void sqlite3RegisterJsonFunctions(void){
JFUNCTION(json_array, -1, 0, jsonArrayFunc),
JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
JFUNCTION(json_error, 1, 0, jsonErrorFunc),
JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),