diff options
author | drh <> | 2023-12-19 21:39:58 +0000 |
---|---|---|
committer | drh <> | 2023-12-19 21:39:58 +0000 |
commit | 3262ca83a7956582c6eded431e7d5374a63f8bbe (patch) | |
tree | 725b5f87e781a01468686fc90d0a9d662dcb3e5a /src | |
parent | c52aebb443751e01a8800bb493dd62bb435fd98b (diff) | |
download | sqlite-3262ca83a7956582c6eded431e7d5374a63f8bbe.tar.gz sqlite-3262ca83a7956582c6eded431e7d5374a63f8bbe.zip |
In JSON - minor code cleanup and refactoring with a small size reduction
and performance increase.
FossilOrigin-Name: 215fabda38daecdbd38b1eca5a6aafbc61b6a36a8303f1d7164d5a1138e63134
Diffstat (limited to 'src')
-rw-r--r-- | src/json.c | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/src/json.c b/src/json.c index d0e7b3fc6..8fae3d114 100644 --- a/src/json.c +++ b/src/json.c @@ -20,9 +20,9 @@ ** All generated JSON text still conforms strictly to RFC-8259, but text ** with JSON-5 extensions is accepted as input. ** -** Beginning with version 3.45.0 (pending), these routines also accept -** BLOB values that have JSON encoded using a binary representation we -** call JSONB. The name JSONB comes from PostgreSQL, however the on-disk +** Beginning with version 3.45.0 (circa 2024-01-01), these routines also +** accept BLOB values that have JSON encoded using a binary representation +** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk ** format SQLite JSONB is completely different and incompatible with ** PostgreSQL JSONB. ** @@ -220,6 +220,8 @@ typedef struct JsonParse JsonParse; ** * The aBlob[] array must be owned by the JsonParse object. In other ** words, nBlobAlloc must be non-zero. ** +** * eEdit and delta must be zero. +** ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true. */ struct JsonCache { @@ -279,8 +281,8 @@ struct JsonString { ** json_replace() or json_patch() or similar). ** ** 4. New JSON text is generated from the aBlob[] for output. This step -** is skipped the function is one of the jsonb_* functions that returns -** JSONB instead of text JSON. +** is skipped if the function is one of the jsonb_* functions that +** returns JSONB instead of text JSON. */ struct JsonParse { u8 *aBlob; /* JSONB representation of JSON value */ @@ -288,14 +290,14 @@ struct JsonParse { u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */ char *zJson; /* Json text used for parsing */ int nJson; /* Length of the zJson string in bytes */ + u32 nJPRef; /* Number of references to this object */ + u32 iErr; /* Error location in zJson[] */ u16 iDepth; /* Nesting depth */ u8 nErr; /* Number of errors seen */ u8 oom; /* Set to true if out of memory */ u8 bJsonIsRCStr; /* True if zJson is an RCStr */ u8 hasNonstd; /* True if input uses non-standard features like JSON5 */ u8 bReadOnly; /* Do not modify. */ - u32 nJPRef; /* Number of references to this object */ - u32 iErr; /* Error location in zJson[] */ /* Search and edit information. See jsonLookupStep() */ u8 eEdit; /* Edit operation to apply */ int delta; /* Size change due to the edit */ @@ -334,7 +336,7 @@ struct JsonParse { **************************************************************************/ static void jsonReturnStringAsBlob(JsonString*); static int jsonFuncArgMightBeBinary(sqlite3_value *pJson); -static u32 jsonXlateBlobToText(const JsonParse*,u32,JsonString*); +static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*); static void jsonReturnParse(sqlite3_context*,JsonParse*); static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32); static void jsonParseFree(JsonParse*); @@ -709,7 +711,7 @@ static void jsonAppendSqlValue( memset(&px, 0, sizeof(px)); px.aBlob = (u8*)sqlite3_value_blob(pValue); px.nBlob = sqlite3_value_bytes(pValue); - jsonXlateBlobToText(&px, 0, p); + jsonTranslateBlobToText(&px, 0, p); }else if( p->eErr==0 ){ sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); p->eErr = JSTRING_ERR; @@ -1226,15 +1228,11 @@ static int jsonBlobChangePayloadSize( */ static int jsonIs4HexB(const char *z, int *pOp){ if( z[0]!='u' ) return 0; - if( !sqlite3Isxdigit(z[1]) ) return 0; - if( !sqlite3Isxdigit(z[2]) ) return 0; - if( !sqlite3Isxdigit(z[3]) ) return 0; - if( !sqlite3Isxdigit(z[4]) ) return 0; + if( !jsonIs4Hex(&z[1]) ) return 0; *pOp = JSONB_TEXTJ; return 1; } - /* ** Check a single element of the JSONB in pParse for validity. ** @@ -1451,7 +1449,7 @@ static u32 jsonbValidityCheck( ** -4 ',' seen / the index in zJson[] of the seen character ** -5 ':' seen / */ -static int jsonXlateTextToBlob(JsonParse *pParse, u32 i){ +static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){ char c; u32 j; u32 iThis, iStart; @@ -1471,7 +1469,7 @@ json_parse_restart: iStart = pParse->nBlob; for(j=i+1;;j++){ u32 iBlob = pParse->nBlob; - x = jsonXlateTextToBlob(pParse, j); + x = jsonTranslateTextToBlob(pParse, j); if( x<=0 ){ int op; if( x==(-2) ){ @@ -1517,7 +1515,7 @@ json_parse_restart: goto parse_object_value; } } - x = jsonXlateTextToBlob(pParse, j); + x = jsonTranslateTextToBlob(pParse, j); if( x!=(-5) ){ if( x!=(-1) ) pParse->iErr = j; return -1; @@ -1525,7 +1523,7 @@ json_parse_restart: j = pParse->iErr+1; } parse_object_value: - x = jsonXlateTextToBlob(pParse, j); + x = jsonTranslateTextToBlob(pParse, j); if( x<=0 ){ if( x!=(-1) ) pParse->iErr = j; return -1; @@ -1544,7 +1542,7 @@ json_parse_restart: break; } } - x = jsonXlateTextToBlob(pParse, j); + x = jsonTranslateTextToBlob(pParse, j); if( x==(-4) ){ j = pParse->iErr; continue; @@ -1572,7 +1570,7 @@ json_parse_restart: return -1; } for(j=i+1;;j++){ - x = jsonXlateTextToBlob(pParse, j); + x = jsonTranslateTextToBlob(pParse, j); if( x<=0 ){ if( x==(-3) ){ j = pParse->iErr; @@ -1596,7 +1594,7 @@ json_parse_restart: break; } } - x = jsonXlateTextToBlob(pParse, j); + x = jsonTranslateTextToBlob(pParse, j); if( x==(-4) ){ j = pParse->iErr; continue; @@ -1919,7 +1917,7 @@ static int jsonConvertTextToBlob( ){ int i; const char *zJson = pParse->zJson; - i = jsonXlateTextToBlob(pParse, 0); + i = jsonTranslateTextToBlob(pParse, 0); if( pParse->oom ) i = -1; if( i>0 ){ #ifdef SQLITE_DEBUG @@ -1964,7 +1962,7 @@ static void jsonReturnStringAsBlob(JsonString *pStr){ jsonStringTerminate(pStr); px.zJson = pStr->zBuf; px.nJson = pStr->nUsed; - (void)jsonXlateTextToBlob(&px, 0); + (void)jsonTranslateTextToBlob(&px, 0); if( px.oom ){ sqlite3_free(px.aBlob); sqlite3_result_error_nomem(pStr->pCtx); @@ -2052,7 +2050,7 @@ static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){ ** ** The pOut->eErr JSTRING_OOM flag is set on a OOM. */ -static u32 jsonXlateBlobToText( +static u32 jsonTranslateBlobToText( const JsonParse *pParse, /* the complete parse of the JSON */ u32 i, /* Start rendering at this index */ JsonString *pOut /* Write JSON here */ @@ -2223,7 +2221,7 @@ static u32 jsonXlateBlobToText( j = i+n; iEnd = j+sz; while( j<iEnd ){ - j = jsonXlateBlobToText(pParse, j, pOut); + j = jsonTranslateBlobToText(pParse, j, pOut); jsonAppendChar(pOut, ','); } if( sz>0 ) pOut->nUsed--; @@ -2236,7 +2234,7 @@ static u32 jsonXlateBlobToText( j = i+n; iEnd = j+sz; while( j<iEnd ){ - j = jsonXlateBlobToText(pParse, j, pOut); + j = jsonTranslateBlobToText(pParse, j, pOut); jsonAppendChar(pOut, (x++ & 1) ? ',' : ':'); } if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED; @@ -2818,7 +2816,7 @@ static void jsonReturnTextJsonFromBlob( x.aBlob = (u8*)aBlob; x.nBlob = nBlob; jsonStringInit(&s, ctx); - jsonXlateBlobToText(&x, 0, &s); + jsonTranslateBlobToText(&x, 0, &s); jsonReturnString(&s, 0, 0); } @@ -3320,7 +3318,7 @@ static void jsonReturnParse( JsonString s; jsonStringInit(&s, ctx); p->delta = 0; - jsonXlateBlobToText(p, 0, &s); + jsonTranslateBlobToText(p, 0, &s); jsonReturnString(&s, p, ctx); sqlite3_result_subtype(ctx, JSON_SUBTYPE); } @@ -3648,7 +3646,7 @@ static void jsonExtractFunc( if( argc==2 ){ if( flags & JSON_JSON ){ jsonStringInit(&jx, ctx); - jsonXlateBlobToText(p, j, &jx); + jsonTranslateBlobToText(p, j, &jx); jsonReturnString(&jx, 0, 0); jsonStringReset(&jx); assert( (flags & JSON_BLOB)==0 ); @@ -3663,7 +3661,7 @@ static void jsonExtractFunc( } }else{ jsonAppendSeparator(&jx); - jsonXlateBlobToText(p, j, &jx); + jsonTranslateBlobToText(p, j, &jx); } }else if( j==JSON_LOOKUP_NOTFOUND ){ if( argc==2 ){ |