typedef struct JsonString JsonString;
typedef struct JsonNode JsonNode;
typedef struct JsonParse JsonParse;
-typedef struct JsonTask JsonTask;
+typedef struct JsonCleanup JsonCleanup;
/* An instance of this object represents a JSON string
** under construction. Really, this is a generic string accumulator
char zSpace[100]; /* Initial static space */
};
-/* A deferred cleanup task. A list of JsonTask objects might be
+/* A deferred cleanup task. A list of JsonCleanup objects might be
** run when the JsonParse object is destroyed.
*/
-struct JsonTask {
- JsonTask *pJTNext; /* Next in a list */
+struct JsonCleanup {
+ JsonCleanup *pJCNext; /* Next in a list */
void (*xOp)(void*); /* Routine to run */
void *pArg; /* Argument to xOp() */
};
};
-/* A completely parsed JSON string
+/* A parsed and possibly edited JSON string. Lifecycle:
+**
+** 1. JSON comes in and is parsed into an array aNode[]. The original
+** JSON text is stored in zJson. This object may or may not be the
+** owner of the input JSON - the bOwnsJson variables determines which.
+**
+** 2. Zero or more changes are made (via json_remove() or json_replace()
+** or similar) to the aNode[] array.
+**
+** 3. A new, edited and mimified JSON string is generated from aNode
+** and stored in zAlt. The JsonParse object always owns zAlt.
+**
+** Step 1 always happens. Step 2 and 3 may or may not happen, depending
+** on the operation.
+**
+** aNode[].u.zJContent entries typically point into zJson. Hence zJson
+** must remain valid for the lifespan of the parse. For edits,
+** aNode[].u.zJContent might point to malloced space other than zJson.
+** Entries in pClup are responsible for freeing that extra malloced space.
+**
+** When walking the parse tree in aNode[], edits are ignored if useMod is
+** false.
*/
struct JsonParse {
u32 nNode; /* Number of slots of aNode[] used */
u32 nAlloc; /* Number of slots of aNode[] allocated */
JsonNode *aNode; /* Array of nodes containing the parse */
char *zJson; /* Original JSON string (before edits) */
- char *zAlt; /* Revised JSON after edits applied. Maybe NULL */
+ char *zAlt; /* Revised and/or mimified JSON */
u32 *aUp; /* Index of parent of each node */
- JsonTask *pClean; /* Cleanup operations prior to freeing this object */
+ JsonCleanup *pClup;/* Cleanup operations prior to freeing this object */
u16 iDepth; /* Nesting depth */
u8 nErr; /* Number of errors seen */
u8 oom; /* Set to true if out of memory */
u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
- u8 nJPRef; /* Number of references to this object */
u8 bOwnsJson; /* This object owns zJson and response for freeing it */
u8 useMod; /* Actually use the edits contain inside aNode */
u8 hasMod; /* aNode contains edits from the original zJson */
+ u32 nJPRef; /* Number of references to this object */
int nJson; /* Length of the zJson string in bytes */
int nAlt; /* Length of alternative JSON string zAlt, in bytes */
u32 iErr; /* Error location in zJson[] */
- u32 iSubst; /* Last known JSON_SUBST node */
- u32 iHold; /* Replace cache line with the lowest iHold value */
+ u32 iSubst; /* Last JSON_SUBST entry in aNode[] */
+ u32 iHold; /* Age of this entry in the cache for LRU replacement */
};
/*
** Utility routines for dealing with JsonString objects
**************************************************************************/
-#if 0
-/*
-** This is a destructor for JSON strings. We make it a separate function
-** so that the sqlite3ValueIsOfClass() function can be used to unambiguously
-** identify sqlite3_value objects that are known JSON strings.
-*/
-static void jsonStringClass(void *p){
- sqlite3RCStrUnref((char*)p);
-}
-#endif
-
/* Set the JsonString object to an empty string
*/
static void jsonZero(JsonString *p){
jsonZero(p);
}
-
/* Free all allocated memory and reset the JsonString object back to its
** initial state.
*/
jsonZero(p);
}
-
-/* Report an out-of-memory (OOM) condition
+/* Report an out-of-memory (OOM) condition
*/
static void jsonOom(JsonString *p){
p->bErr = 1;
p->nUsed--;
if( p->bStatic==0 ) return 1;
p->nAlloc = 0;
+ p->nUsed++;
jsonGrow(p, p->nUsed);
+ p->nUsed--;
return p->bStatic==0;
}
jsonAppendRawNZ(p, zIn, i);
zIn += i;
N -= i;
- if( N==0 ) break;
+ if( N==0 ) break;
}
assert( zIn[0]=='\\' );
switch( (u8)zIn[1] ){
/*
-** Append a function parameter value to the JSON string under
+** Append a function parameter value to the JSON string under
** construction.
*/
static void jsonAppendValue(
** The JSON string is reset.
*/
static void jsonResult(JsonString *p){
- if( p->bErr==0 ){
- if( p->bStatic ){
- sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, SQLITE_TRANSIENT,
- SQLITE_UTF8);
- }else{
- jsonAppendChar(p, 0);
- p->nUsed--;
- sqlite3RCStrRef(p->zBuf);
- sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
- (void(*)(void*))sqlite3RCStrUnref,
- SQLITE_UTF8);
- }
+ if( p->bErr==0 && jsonForceRCStr(p) ){
+ sqlite3RCStrRef(p->zBuf);
+ sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
+ (void(*)(void*))sqlite3RCStrUnref,
+ SQLITE_UTF8);
}else if( p->bErr==1 ){
sqlite3_result_error_nomem(p->pCtx);
}
** delete the JsonParse object itself.
*/
static void jsonParseReset(JsonParse *pParse){
- while( pParse->pClean ){
- JsonTask *pTask = pParse->pClean;
- pParse->pClean = pTask->pJTNext;
+ while( pParse->pClup ){
+ JsonCleanup *pTask = pParse->pClup;
+ pParse->pClup = pTask->pJCNext;
pTask->xOp(pTask->pArg);
sqlite3_free(pTask);
}
pParse->aUp = 0;
}
if( pParse->zAlt ){
- char *z = pParse->zAlt;
+ sqlite3RCStrUnref(pParse->zAlt);
pParse->zAlt = 0;
- sqlite3RCStrUnref(z);
}
if( pParse->bOwnsJson ){
- /* Order operations so that if the destructor for pParse->zJson
- ** invokes jsonParseFree(), the recursion will terminate harmlessly */
- char *z = pParse->zJson;
- pParse->zJson = 0;
pParse->bOwnsJson = 0;
- sqlite3RCStrUnref(z);
+ sqlite3RCStrUnref(pParse->zJson);
+ pParse->zJson = 0;
}
}
void(*xOp)(void*), /* The cleanup task */
void *pArg /* Argument to the cleanup */
){
- JsonTask *pTask = sqlite3_malloc64( sizeof(*pTask) );
+ JsonCleanup *pTask = sqlite3_malloc64( sizeof(*pTask) );
if( pTask==0 ){
pParse->oom = 1;
xOp(pArg);
return SQLITE_ERROR;
}
- pTask->pJTNext = pParse->pClean;
- pParse->pClean = pTask;
+ pTask->pJCNext = pParse->pClup;
+ pParse->pClup = pTask;
pTask->xOp = xOp;
pTask->pArg = pArg;
return SQLITE_OK;
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1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
if( aOk[(unsigned char)z[j]] ) continue;
c = z[j];
/*
** Parse a complete JSON string. Return 0 on success or non-zero if there
-** are any errors. If an error occurs, free all memory associated with
-** pParse.
+** are any errors. If an error occurs, free all memory held by pParse,
+** but not pParse itself.
**
** pParse is uninitialized when this routine is called.
**
-** pParse->nJPRef set to 1. The caller becomes the owner of the
+** pParse->nJPRef set to 1. The caller becomes the owner of the
** the JsonParse object.
**
-** pParse->bOwnsJson is to bTakeJson. If bTakeJson is 1, the newly initialized
-** JsonParse object will become the own the zJson input string. If bTakeJson
-** is 0, then the caller is responsible for preserving zJson for the lifetime
-** of the JsonParse object.
+** pParse->bOwnsJson is set to bTakeJson. If bTakeJson is 1, the newly
+** initialized JsonParse object will become the owner of the zJson input
+** string. If bTakeJson is 0, then the caller is responsible for
+** preserving zJson for the lifetime of the JsonParse object.
*/
static int jsonParse(
JsonParse *pParse, /* Initialize and fill this JsonParse object */
zPath += j + 1;
j = 1;
for(;;){
- while( j<=pRoot->n
+ while( j<=pRoot->n
&& (i>0 || ((pRoot[j].jnFlags & JNODE_REMOVE)!=0 && pParse->useMod))
){
if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i--;
char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
zFuncName);
sqlite3_result_error(pCtx, zMsg, -1);
- sqlite3_free(zMsg);
+ sqlite3_free(zMsg);
}
/*
/*
** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
-** corresponding to the SQL value input. Mostly this means putting
+** corresponding to the SQL value input. Mostly this means putting
** double-quotes around strings and returning the unquoted string "null"
** when given a NULL input.
*/
** json_array_length(JSON)
** json_array_length(JSON, PATH)
**
-** Return the number of elements in the top-level JSON array.
+** Return the number of elements in the top-level JSON array.
** Return 0 if the input is not a well-formed JSON array.
*/
static void jsonArrayLengthFunc(
if( argc<1 ) return;
pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
- if( pParse==0 ) return;
+ if( pParse==0 ) return;
for(i=1; i<(u32)argc; i++){
zPath = (const char*)sqlite3_value_text(argv[i]);
if( zPath==0 ) goto remove_done;
}
}
}
-
+
/*
** json_replace(JSON, PATH, VALUE, ...)
**
UNUSED_PARAMETER(argv);
UNUSED_PARAMETER(argc);
UNUSED_PARAMETER(pAux);
- rc = sqlite3_declare_vtab(db,
+ rc = sqlite3_declare_vtab(db,
"CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
"json HIDDEN,root HIDDEN)");
if( rc==SQLITE_OK ){
break;
}
case JEACH_ID: {
- sqlite3_result_int64(ctx,
+ sqlite3_result_int64(ctx,
(sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
break;
}
idxMask |= iMask;
}
}
- if( pIdxInfo->nOrderBy>0
- && pIdxInfo->aOrderBy[0].iColumn<0
+ if( pIdxInfo->nOrderBy>0
+ && pIdxInfo->aOrderBy[0].iColumn<0
&& pIdxInfo->aOrderBy[0].desc==0
){
pIdxInfo->orderByConsumed = 1;
JFUNCTION(json_parse, 1, 0, jsonParseFunc),
JFUNCTION(json_test1, 1, 0, jsonTest1Func),
#endif
- WAGGREGATE(json_group_array, 1, 0, 0,
+ WAGGREGATE(json_group_array, 1, 0, 0,
jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
- WAGGREGATE(json_group_object, 2, 0, 0,
+ WAGGREGATE(json_group_object, 2, 0, 0,
jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
};