aboutsummaryrefslogtreecommitdiff
path: root/src/json.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c742
1 files changed, 559 insertions, 183 deletions
diff --git a/src/json.c b/src/json.c
index 05046b5b5..a4a3e65c9 100644
--- a/src/json.c
+++ b/src/json.c
@@ -59,6 +59,7 @@ static const char jsonIsSpace[] = {
typedef struct JsonString JsonString;
typedef struct JsonNode JsonNode;
typedef struct JsonParse JsonParse;
+typedef struct JsonTask JsonTask;
/* An instance of this object represents a JSON string
** under construction. Really, this is a generic string accumulator
@@ -74,16 +75,26 @@ struct JsonString {
char zSpace[100]; /* Initial static space */
};
+/* A deferred cleanup task. A list of JsonTask objects might be
+** run when the JsonParse object is destroyed.
+*/
+struct JsonTask {
+ JsonTask *pJTNext; /* Next in a list */
+ void (*xOp)(void*); /* Routine to run */
+ void *pArg; /* Argument to xOp() */
+};
+
/* JSON type values
*/
-#define JSON_NULL 0
-#define JSON_TRUE 1
-#define JSON_FALSE 2
-#define JSON_INT 3
-#define JSON_REAL 4
-#define JSON_STRING 5
-#define JSON_ARRAY 6
-#define JSON_OBJECT 7
+#define JSON_SUBST 0 /* Special edit node. Uses u.iPrev */
+#define JSON_NULL 1
+#define JSON_TRUE 2
+#define JSON_FALSE 3
+#define JSON_INT 4
+#define JSON_REAL 5
+#define JSON_STRING 6
+#define JSON_ARRAY 7
+#define JSON_OBJECT 8
/* The "subtype" set for JSON values */
#define JSON_SUBTYPE 74 /* Ascii for "J" */
@@ -92,19 +103,19 @@ struct JsonString {
** Names of the various JSON types:
*/
static const char * const jsonType[] = {
+ "subst",
"null", "true", "false", "integer", "real", "text", "array", "object"
};
/* Bit values for the JsonNode.jnFlag field
*/
-#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
-#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
-#define JNODE_REMOVE 0x04 /* Do not output */
-#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
-#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
-#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
-#define JNODE_LABEL 0x40 /* Is a label of an object */
-#define JNODE_JSON5 0x80 /* Node contains JSON5 enhancements */
+#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
+#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
+#define JNODE_REMOVE 0x04 /* Do not output */
+#define JNODE_REPLACE 0x08 /* Target of a JSON_SUBST node */
+#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
+#define JNODE_LABEL 0x20 /* Is a label of an object */
+#define JNODE_JSON5 0x40 /* Node contains JSON5 enhancements */
/* A single node of parsed JSON
@@ -113,30 +124,40 @@ struct JsonNode {
u8 eType; /* One of the JSON_ type values */
u8 jnFlags; /* JNODE flags */
u8 eU; /* Which union element to use */
- u32 n; /* Bytes of content, or number of sub-nodes */
+ u32 n; /* Bytes of content for INT, REAL or STRING
+ ** Number of sub-nodes for ARRAY and OBJECT
+ ** Node that SUBST applies to */
union {
const char *zJContent; /* 1: Content for INT, REAL, and STRING */
u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
- u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
- JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
+ u32 iPrev; /* 4: Previous SUBST node, or 0 */
} u;
};
+
/* A completely parsed JSON string
*/
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 */
- const char *zJson; /* Original JSON string */
+ char *zJson; /* Original JSON string (before edits) */
+ char *zAlt; /* Revised JSON after edits applied. Maybe NULL */
u32 *aUp; /* Index of parent of each node */
+ JsonTask *pClean; /* 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 */
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 */
};
@@ -153,6 +174,17 @@ struct JsonParse {
** 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){
@@ -175,7 +207,7 @@ static void jsonInit(JsonString *p, sqlite3_context *pCtx){
** initial state.
*/
static void jsonReset(JsonString *p){
- if( !p->bStatic ) sqlite3_free(p->zBuf);
+ if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf);
jsonZero(p);
}
@@ -196,7 +228,7 @@ static int jsonGrow(JsonString *p, u32 N){
char *zNew;
if( p->bStatic ){
if( p->bErr ) return 1;
- zNew = sqlite3_malloc64(nTotal);
+ zNew = sqlite3RCStrNew(nTotal);
if( zNew==0 ){
jsonOom(p);
return SQLITE_NOMEM;
@@ -205,17 +237,27 @@ static int jsonGrow(JsonString *p, u32 N){
p->zBuf = zNew;
p->bStatic = 0;
}else{
- zNew = sqlite3_realloc64(p->zBuf, nTotal);
- if( zNew==0 ){
- jsonOom(p);
+ p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
+ if( p->zBuf==0 ){
+ p->bErr = 1;
+ jsonZero(p);
return SQLITE_NOMEM;
}
- p->zBuf = zNew;
}
p->nAlloc = nTotal;
return SQLITE_OK;
}
+/* Try to force the string to be an RCStr string, rather than a
+** static string. Return true on success. The only reason this
+** might fail is due to an OOM fault.
+*/
+static int jsonForceRCStr(JsonString *p){
+ if( p->bStatic==0 ) return 1;
+ jsonGrow(p, p->nAlloc+1);
+ return p->bStatic==0;
+}
+
/* Append N bytes from zIn onto the end of the JsonString string.
*/
static SQLITE_NOINLINE void jsonAppendExpand(
@@ -498,16 +540,26 @@ static void jsonAppendValue(
/* Make the JSON in p the result of the SQL function.
+**
+** The JSON string is reset.
*/
static void jsonResult(JsonString *p){
if( p->bErr==0 ){
- jsonAppendChar(p, 0);
- sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed-1,
- p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
- SQLITE_UTF8);
- jsonZero(p);
+ 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);
+ }
+ }else if( p->bErr==1 ){
+ sqlite3_result_error_nomem(p->pCtx);
}
- assert( p->bStatic );
+ jsonReset(p);
}
/**************************************************************************
@@ -532,20 +584,77 @@ static u32 jsonNodeSize(JsonNode *pNode){
** delete the JsonParse object itself.
*/
static void jsonParseReset(JsonParse *pParse){
- sqlite3_free(pParse->aNode);
- pParse->aNode = 0;
+ while( pParse->pClean ){
+ JsonTask *pTask = pParse->pClean;
+ pParse->pClean = pTask->pJTNext;
+ pTask->xOp(pTask->pArg);
+ sqlite3_free(pTask);
+ }
+ assert( pParse->nJPRef<=1 );
+ if( pParse->aNode ){
+ sqlite3_free(pParse->aNode);
+ pParse->aNode = 0;
+ }
pParse->nNode = 0;
pParse->nAlloc = 0;
- sqlite3_free(pParse->aUp);
- pParse->aUp = 0;
+ if( pParse->aUp ){
+ sqlite3_free(pParse->aUp);
+ pParse->aUp = 0;
+ }
+ if( pParse->zAlt ){
+ char *z = 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);
+ }
}
/*
** Free a JsonParse object that was obtained from sqlite3_malloc().
+**
+** Note that destroying JsonParse might call sqlite3RCStrUnref() to
+** destroy the zJson value. The RCStr object might recursively invoke
+** JsonParse to destroy this pParse object again. Take care to ensure
+** that this recursive destructor sequence terminates harmlessly.
*/
static void jsonParseFree(JsonParse *pParse){
- jsonParseReset(pParse);
- sqlite3_free(pParse);
+ if( pParse->nJPRef>1 ){
+ pParse->nJPRef--;
+ }else{
+ jsonParseReset(pParse);
+ sqlite3_free(pParse);
+ }
+}
+
+/*
+** Add a cleanup task to the JsonParse object.
+**
+** If an OOM occurs, the cleanup operation happens immediately
+** and this function returns SQLITE_NOMEM.
+*/
+static int jsonParseAddCleanup(
+ JsonParse *pParse, /* Add the cleanup task to this parser */
+ void(*xOp)(void*), /* The cleanup task */
+ void *pArg /* Argument to the cleanup */
+){
+ JsonTask *pTask = sqlite3_malloc64( sizeof(*pTask) );
+ if( pTask==0 ){
+ pParse->oom = 1;
+ xOp(pArg);
+ return SQLITE_ERROR;
+ }
+ pTask->pJTNext = pParse->pClean;
+ pParse->pClean = pTask;
+ pTask->xOp = xOp;
+ pTask->pArg = pArg;
+ return SQLITE_OK;
}
/*
@@ -554,19 +663,25 @@ static void jsonParseFree(JsonParse *pParse){
** the number of JsonNode objects that are encoded.
*/
static void jsonRenderNode(
+ JsonParse *pParse, /* the complete parse of the JSON */
JsonNode *pNode, /* The node to render */
- JsonString *pOut, /* Write JSON here */
- sqlite3_value **aReplace /* Replacement values */
+ JsonString *pOut /* Write JSON here */
){
assert( pNode!=0 );
- if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
- if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){
- assert( pNode->eU==4 );
- jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
- return;
+ while( (pNode->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){
+ u32 idx = (u32)(pNode - pParse->aNode);
+ u32 i = pParse->iSubst;
+ while( 1 /*exit-by-break*/ ){
+ assert( i<pParse->nNode );
+ assert( pParse->aNode[i].eType==JSON_SUBST );
+ assert( pParse->aNode[i].eU==4 );
+ assert( pParse->aNode[i].u.iPrev<i );
+ if( pParse->aNode[i].n==idx ){
+ pNode = &pParse->aNode[i+1];
+ break;
+ }
+ i = pParse->aNode[i].u.iPrev;
}
- assert( pNode->eU==5 );
- pNode = pNode->u.pPatch;
}
switch( pNode->eType ){
default: {
@@ -625,15 +740,16 @@ static void jsonRenderNode(
jsonAppendChar(pOut, '[');
for(;;){
while( j<=pNode->n ){
- if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
+ if( (pNode[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
jsonAppendSeparator(pOut);
- jsonRenderNode(&pNode[j], pOut, aReplace);
+ jsonRenderNode(pParse, &pNode[j], pOut);
}
j += jsonNodeSize(&pNode[j]);
}
if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pNode->eU==2 );
- pNode = &pNode[pNode->u.iAppend];
+ pNode = &pParse->aNode[pNode->u.iAppend];
j = 1;
}
jsonAppendChar(pOut, ']');
@@ -644,17 +760,18 @@ static void jsonRenderNode(
jsonAppendChar(pOut, '{');
for(;;){
while( j<=pNode->n ){
- if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
+ if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
jsonAppendSeparator(pOut);
- jsonRenderNode(&pNode[j], pOut, aReplace);
+ jsonRenderNode(pParse, &pNode[j], pOut);
jsonAppendChar(pOut, ':');
- jsonRenderNode(&pNode[j+1], pOut, aReplace);
+ jsonRenderNode(pParse, &pNode[j+1], pOut);
}
j += 1 + jsonNodeSize(&pNode[j+1]);
}
if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pNode->eU==2 );
- pNode = &pNode[pNode->u.iAppend];
+ pNode = &pParse->aNode[pNode->u.iAppend];
j = 1;
}
jsonAppendChar(pOut, '}');
@@ -667,15 +784,28 @@ static void jsonRenderNode(
** Return a JsonNode and all its descendants as a JSON string.
*/
static void jsonReturnJson(
+ JsonParse *pParse, /* The complete JSON */
JsonNode *pNode, /* Node to return */
sqlite3_context *pCtx, /* Return value for this function */
- sqlite3_value **aReplace /* Array of replacement values */
+ int bGenerateAlt /* Also store the rendered text in zAlt */
){
JsonString s;
- jsonInit(&s, pCtx);
- jsonRenderNode(pNode, &s, aReplace);
- jsonResult(&s);
- sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
+ if( pParse->oom ){
+ sqlite3_result_error_nomem(pCtx);
+ return;
+ }
+ if( pParse->nErr==0 ){
+ jsonInit(&s, pCtx);
+ jsonRenderNode(pParse, pNode, &s);
+ if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){
+ jsonAppendChar(&s, 0);
+ s.nUsed--;
+ pParse->zAlt = sqlite3RCStrRef(s.zBuf);
+ pParse->nAlt = s.nUsed;
+ }
+ jsonResult(&s);
+ sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
+ }
}
/*
@@ -713,9 +843,9 @@ static u32 jsonHexToInt4(const char *z){
** Make the JsonNode the return value of the function.
*/
static void jsonReturn(
+ JsonParse *pParse, /* Complete JSON parse tree */
JsonNode *pNode, /* Node to return */
- sqlite3_context *pCtx, /* Return value for this function */
- sqlite3_value **aReplace /* Array of replacement values */
+ sqlite3_context *pCtx /* Return value for this function */
){
switch( pNode->eType ){
default: {
@@ -862,7 +992,7 @@ static void jsonReturn(
}
case JSON_ARRAY:
case JSON_OBJECT: {
- jsonReturnJson(pNode, pCtx, aReplace);
+ jsonReturnJson(pParse, pNode, pCtx, 0);
break;
}
}
@@ -884,6 +1014,12 @@ static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
#endif
+/*
+** Add a single node to pParse->aNode after first expanding the
+** size of the aNode array. Return the index of the new node.
+**
+** If an OOM error occurs, set pParse->oom and return -1.
+*/
static JSON_NOINLINE int jsonParseAddNodeExpand(
JsonParse *pParse, /* Append the node to this object */
u32 eType, /* Node type */
@@ -900,7 +1036,7 @@ static JSON_NOINLINE int jsonParseAddNodeExpand(
pParse->oom = 1;
return -1;
}
- pParse->nAlloc = nNew;
+ pParse->nAlloc = sqlite3_msize(pNew)/sizeof(JsonNode);
pParse->aNode = pNew;
assert( pParse->nNode<pParse->nAlloc );
return jsonParseAddNode(pParse, eType, n, zContent);
@@ -932,6 +1068,50 @@ static int jsonParseAddNode(
}
/*
+** Add an array of new nodes to the current pParse->aNode array.
+** Return the index of the first node added.
+**
+** If an OOM error occurs, set pParse->oom.
+*/
+static void jsonParseAddNodeArray(
+ JsonParse *pParse, /* Append the node to this object */
+ JsonNode *aNode, /* Array of nodes to add */
+ u32 nNode /* Number of elements in aNew */
+){
+ if( pParse->nNode + nNode > pParse->nAlloc ){
+ u32 nNew = pParse->nNode + nNode;
+ JsonNode *aNew = sqlite3_realloc64(pParse->aNode, nNew*sizeof(JsonNode));
+ if( aNew==0 ){
+ pParse->oom = 1;
+ return;
+ }
+ pParse->nAlloc = sqlite3_msize(aNew)/sizeof(JsonNode);
+ pParse->aNode = aNew;
+ }
+ memcpy(&pParse->aNode[pParse->nNode], aNode, nNode*sizeof(JsonNode));
+ pParse->nNode += nNode;
+}
+
+/*
+** Add a new JSON_SUBST node. The node immediately following
+** this new node will be the substitute content for iNode.
+*/
+static int jsonParseAddSubstNode(
+ JsonParse *pParse, /* Add the JSON_SUBST here */
+ u32 iNode /* References this node */
+){
+ int idx = jsonParseAddNode(pParse, JSON_SUBST, iNode, 0);
+ if( pParse->oom ) return -1;
+ pParse->aNode[iNode].jnFlags |= JNODE_REPLACE;
+ pParse->aNode[idx].eU = 4;
+ pParse->aNode[idx].u.iPrev = pParse->iSubst;
+ pParse->iSubst = idx;
+ pParse->hasMod = 1;
+ pParse->useMod = 1;
+ return idx;
+}
+
+/*
** Return true if z[] begins with 2 (or more) hexadecimal digits
*/
static int jsonIs2Hex(const char *z){
@@ -1097,7 +1277,7 @@ static const struct NanInfName {
**
** Special return values:
**
-** 0 End if input
+** 0 End of input
** -1 Syntax error
** -2 '}' seen
** -3 ']' seen
@@ -1560,16 +1740,27 @@ json_parse_restart:
** pParse.
**
** pParse is uninitialized when this routine is called.
+**
+** 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.
*/
static int jsonParse(
JsonParse *pParse, /* Initialize and fill this JsonParse object */
sqlite3_context *pCtx, /* Report errors here */
- const char *zJson /* Input JSON text to be parsed */
+ char *zJson, /* Input JSON text to be parsed */
+ int bTakeJson /* Assume ownership of zJson if true */
){
int i;
memset(pParse, 0, sizeof(*pParse));
if( zJson==0 ) return 1;
pParse->zJson = zJson;
+ pParse->bOwnsJson = bTakeJson;
+ pParse->nJPRef = 1;
i = jsonParseValue(pParse, 0);
if( pParse->oom ) i = -1;
if( i>0 ){
@@ -1598,6 +1789,7 @@ static int jsonParse(
return 0;
}
+
/* Mark node i of pParse as being a child of iParent. Call recursively
** to fill in all the descendants of node i.
*/
@@ -1650,32 +1842,44 @@ static int jsonParseFindParents(JsonParse *pParse){
** Obtain a complete parse of the JSON found in the first argument
** of the argv array. Use the sqlite3_get_auxdata() cache for this
** parse if it is available. If the cache is not available or if it
-** 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
+** is no longer valid, parse the JSON again and return the new parse.
+** 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.
+** The returned pointer (if it is not NULL) is owned by the cache in
+** most cases, not the caller. The caller does NOT need to invoke
+** jsonParseFree(), in most cases.
+**
+** Except, if an error occurs and pErrCtx==0 then return the JsonParse
+** object with JsonParse.nErr non-zero and the caller will own the JsonParse
+** object. In that case, it will be the responsibility of the caller to
+** invoke jsonParseFree(). To summarize:
+**
+** pErrCtx!=0 || p->nErr==0 ==> Return value p is owned by the
+** cache. Call does not need to
+** free it.
+**
+** pErrCtx==0 && p->nErr!=0 ==> Return value is owned by the caller
+** and so the caller must free it.
*/
static JsonParse *jsonParseCached(
- sqlite3_context *pCtx,
- sqlite3_value **argv,
- sqlite3_context *pErrCtx
+ sqlite3_context *pCtx, /* Context to use for cache search */
+ sqlite3_value *pJson, /* Function param containing JSON text */
+ sqlite3_context *pErrCtx, /* Write parse errors here if not NULL */
+ int bUnedited /* No prior edits allowed */
){
- const char *zJson = (const char*)sqlite3_value_text(argv[0]);
- int nJson = sqlite3_value_bytes(argv[0]);
+ char *zJson = (char*)sqlite3_value_text(pJson);
+ int nJson = sqlite3_value_bytes(pJson);
JsonParse *p;
JsonParse *pMatch = 0;
int iKey;
int iMinKey = 0;
u32 iMinHold = 0xffffffff;
u32 iMaxHold = 0;
+
if( zJson==0 ) return 0;
for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
@@ -1685,9 +1889,21 @@ static JsonParse *jsonParseCached(
}
if( pMatch==0
&& p->nJson==nJson
+ && (p->hasMod==0 || bUnedited==0)
&& memcmp(p->zJson,zJson,nJson)==0
){
p->nErr = 0;
+ p->useMod = 0;
+ pMatch = p;
+ }else
+ if( pMatch==0
+ && p->zAlt!=0
+ && bUnedited==0
+ && p->nAlt==nJson
+ && memcmp(p->zAlt, zJson, nJson)==0
+ ){
+ p->nErr = 0;
+ p->useMod = 1;
pMatch = p;
}else if( p->iHold<iMinHold ){
iMinHold = p->iHold;
@@ -1698,28 +1914,43 @@ static JsonParse *jsonParseCached(
}
}
if( pMatch ){
+ /* The input JSON text was found in the cache. Use the preexisting
+ ** parse of this JSON */
pMatch->nErr = 0;
pMatch->iHold = iMaxHold+1;
+ assert( pMatch->nJPRef>0 ); /* pMatch is owned by the cache */
return pMatch;
}
- p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
+
+ /* The input JSON was not found anywhere in the cache. We will need
+ ** to parse it ourselves and generate a new JsonParse object.
+ */
+ p = sqlite3_malloc64( sizeof(*p) );
if( p==0 ){
sqlite3_result_error_nomem(pCtx);
return 0;
}
memset(p, 0, sizeof(*p));
- p->zJson = (char*)&p[1];
- memcpy((char*)p->zJson, zJson, nJson+1);
- if( jsonParse(p, pErrCtx, p->zJson) ){
+ p->zJson = sqlite3RCStrNew( nJson );
+ if( p->zJson==0 ){
+ sqlite3_free(p);
+ sqlite3_result_error_nomem(pCtx);
+ return 0;
+ }
+ memcpy(p->zJson, zJson, nJson);
+ p->zJson[nJson] = 0;
+ if( jsonParse(p, pErrCtx, p->zJson, 1) ){
if( pErrCtx==0 ){
p->nErr = 1;
+ assert( p->nJPRef==1 ); /* Caller will own the new JsonParse object p */
return p;
}
- sqlite3_free(p);
+ jsonParseFree(p);
return 0;
}
p->nJson = nJson;
p->iHold = iMaxHold+1;
+ /* Transfer ownership of the new JsonParse to the cache */
sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
(void(*)(void*))jsonParseFree);
return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
@@ -1771,8 +2002,23 @@ static JsonNode *jsonLookupStep(
u32 i, j, nKey;
const char *zKey;
JsonNode *pRoot = &pParse->aNode[iRoot];
+ while( (pRoot->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){
+ u32 idx = (u32)(pRoot - pParse->aNode);
+ i = pParse->iSubst;
+ while( 1 /*exit-by-break*/ ){
+ assert( i<pParse->nNode );
+ assert( pParse->aNode[i].eType==JSON_SUBST );
+ assert( pParse->aNode[i].eU==4 );
+ assert( pParse->aNode[i].u.iPrev<i );
+ if( pParse->aNode[i].n==idx ){
+ pRoot = &pParse->aNode[i+1];
+ iRoot = i+1;
+ break;
+ }
+ i = pParse->aNode[i].u.iPrev;
+ }
+ }
if( zPath[0]==0 ) return pRoot;
- if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
if( zPath[0]=='.' ){
if( pRoot->eType!=JSON_OBJECT ) return 0;
zPath++;
@@ -1806,14 +2052,16 @@ static JsonNode *jsonLookupStep(
j += jsonNodeSize(&pRoot[j]);
}
if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pRoot->eU==2 );
- iRoot += pRoot->u.iAppend;
+ iRoot = pRoot->u.iAppend;
pRoot = &pParse->aNode[iRoot];
j = 1;
}
if( pApnd ){
u32 iStart, iLabel;
JsonNode *pNode;
+ assert( pParse->useMod );
iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
zPath += i;
@@ -1822,7 +2070,7 @@ static JsonNode *jsonLookupStep(
if( pNode ){
pRoot = &pParse->aNode[iRoot];
assert( pRoot->eU==0 );
- pRoot->u.iAppend = iStart - iRoot;
+ pRoot->u.iAppend = iStart;
pRoot->jnFlags |= JNODE_APPEND;
VVA( pRoot->eU = 2 );
pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
@@ -1843,12 +2091,13 @@ static JsonNode *jsonLookupStep(
if( pRoot->eType!=JSON_ARRAY ) return 0;
for(;;){
while( j<=pBase->n ){
- if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
+ if( (pBase[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i++;
j += jsonNodeSize(&pBase[j]);
}
if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pBase->eU==2 );
- iBase += pBase->u.iAppend;
+ iBase = pBase->u.iAppend;
pBase = &pParse->aNode[iBase];
j = 1;
}
@@ -1876,13 +2125,16 @@ static JsonNode *jsonLookupStep(
zPath += j + 1;
j = 1;
for(;;){
- while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
- if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
+ 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--;
j += jsonNodeSize(&pRoot[j]);
}
if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pRoot->eU==2 );
- iRoot += pRoot->u.iAppend;
+ iRoot = pRoot->u.iAppend;
pRoot = &pParse->aNode[iRoot];
j = 1;
}
@@ -1892,13 +2144,14 @@ static JsonNode *jsonLookupStep(
if( i==0 && pApnd ){
u32 iStart;
JsonNode *pNode;
+ assert( pParse->useMod );
iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
if( pParse->oom ) return 0;
if( pNode ){
pRoot = &pParse->aNode[iRoot];
assert( pRoot->eU==0 );
- pRoot->u.iAppend = iStart - iRoot;
+ pRoot->u.iAppend = iStart;
pRoot->jnFlags |= JNODE_APPEND;
VVA( pRoot->eU = 2 );
}
@@ -2025,6 +2278,51 @@ static void jsonRemoveAllNulls(JsonNode *pNode){
** SQL functions used for testing and debugging
****************************************************************************/
+#if 0 /* One for debugging. zero normally */
+/*
+** Print N node entries.
+*/
+static void jsonDebugPrintNodeEntries(
+ JsonNode *aNode, /* First node entry to print */
+ int N /* Number of node entries to print */
+){
+ int i;
+ for(i=0; i<N; i++){
+ const char *zType;
+ if( aNode[i].jnFlags & JNODE_LABEL ){
+ zType = "label";
+ }else{
+ zType = jsonType[aNode[i].eType];
+ }
+ if( (aNode[i].jnFlags & ~JNODE_LABEL)!=0 ){
+ printf("node %4u: %-7s %02x n=%-5d",
+ i, zType, aNode[i].jnFlags, aNode[i].n);
+ }else{
+ printf("node %4u: %-7s n=%-5d",
+ i, zType, aNode[i].n);
+ }
+ switch( aNode[i].eU ){
+ case 1: printf(" zJContent=[%.*s]\n",
+ aNode[i].n, aNode[i].u.zJContent); break;
+ case 2: printf(" iAppend=%u\n", aNode[i].u.iAppend); break;
+ case 3: printf(" iKey=%u\n", aNode[i].u.iKey); break;
+ case 4: printf(" iPrev=%u\n", aNode[i].u.iPrev); break;
+ default: printf("\n");
+ }
+ }
+}
+static void jsonDebugPrintParse(JsonParse *p){
+ jsonDebugPrintNodeEntries(p->aNode, p->nNode);
+}
+static void jsonDebugPrintNode(JsonNode *pNode){
+ jsonDebugPrintNodeEntries(pNode, jsonNodeSize(pNode));
+}
+#else
+ /* The usual case */
+# define jsonDebugPrintNode(X)
+# define jsonDebugPrintParse(X)
+#endif
+
#ifdef SQLITE_DEBUG
/*
** The json_parse(JSON) function returns a string which describes
@@ -2041,7 +2339,7 @@ static void jsonParseFunc(
u32 i;
assert( argc==1 );
- if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
+ if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0]), 0) ) return;
jsonParseFindParents(&x);
jsonInit(&s, ctx);
for(i=0; i<x.nNode; i++){
@@ -2149,7 +2447,7 @@ static void jsonArrayLengthFunc(
u32 i;
JsonNode *pNode;
- p = jsonParseCached(ctx, argv, ctx);
+ p = jsonParseCached(ctx, argv[0], ctx, 0);
if( p==0 ) return;
assert( p->nNode );
if( argc==2 ){
@@ -2162,9 +2460,14 @@ static void jsonArrayLengthFunc(
return;
}
if( pNode->eType==JSON_ARRAY ){
- assert( (pNode->jnFlags & JNODE_APPEND)==0 );
- for(i=1; i<=pNode->n; n++){
- i += jsonNodeSize(&pNode[i]);
+ while( 1 /*exit-by-break*/ ){
+ for(i=1; i<=pNode->n; n++){
+ i += jsonNodeSize(&pNode[i]);
+ }
+ if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
+ if( p->useMod==0 ) break;
+ assert( pNode->eU==2 );
+ pNode = &p->aNode[pNode->u.iAppend];
}
}
sqlite3_result_int64(ctx, n);
@@ -2211,7 +2514,7 @@ static void jsonExtractFunc(
JsonString jx;
if( argc<2 ) return;
- p = jsonParseCached(ctx, argv, ctx);
+ p = jsonParseCached(ctx, argv[0], ctx, 0);
if( p==0 ) return;
if( argc==2 ){
/* With a single PATH argument */
@@ -2244,15 +2547,15 @@ static void jsonExtractFunc(
}
if( pNode ){
if( flags & JSON_JSON ){
- jsonReturnJson(pNode, ctx, 0);
+ jsonReturnJson(p, pNode, ctx, 0);
}else{
- jsonReturn(pNode, ctx, 0);
+ jsonReturn(p, pNode, ctx);
sqlite3_result_subtype(ctx, 0);
}
}
}else{
pNode = jsonLookup(p, zPath, 0, ctx);
- if( p->nErr==0 && pNode ) jsonReturn(pNode, ctx, 0);
+ if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx);
}
}else{
/* Two or more PATH arguments results in a JSON array with each
@@ -2266,7 +2569,7 @@ static void jsonExtractFunc(
if( p->nErr ) break;
jsonAppendSeparator(&jx);
if( pNode ){
- jsonRenderNode(pNode, &jx, 0);
+ jsonRenderNode(p, pNode, &jx);
}else{
jsonAppendRawNZ(&jx, "null", 4);
}
@@ -2313,45 +2616,38 @@ static JsonNode *jsonMergePatch(
assert( pTarget[j].eType==JSON_STRING );
assert( pTarget[j].jnFlags & JNODE_LABEL );
if( jsonSameLabel(&pPatch[i], &pTarget[j]) ){
- if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
+ if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ) break;
if( pPatch[i+1].eType==JSON_NULL ){
pTarget[j+1].jnFlags |= JNODE_REMOVE;
}else{
JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
if( pNew==0 ) return 0;
- pTarget = &pParse->aNode[iTarget];
- if( pNew!=&pTarget[j+1] ){
- assert( pTarget[j+1].eU==0
- || pTarget[j+1].eU==1
- || pTarget[j+1].eU==2 );
- testcase( pTarget[j+1].eU==1 );
- testcase( pTarget[j+1].eU==2 );
- VVA( pTarget[j+1].eU = 5 );
- pTarget[j+1].u.pPatch = pNew;
- pTarget[j+1].jnFlags |= JNODE_PATCH;
+ if( pNew!=&pParse->aNode[iTarget+j+1] ){
+ jsonParseAddSubstNode(pParse, iTarget+j+1);
+ jsonParseAddNodeArray(pParse, pNew, jsonNodeSize(pNew));
}
+ pTarget = &pParse->aNode[iTarget];
}
break;
}
}
if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
- int iStart, iPatch;
- iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
+ int iStart;
+ JsonNode *pApnd;
+ u32 nApnd;
+ iStart = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
- iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
+ pApnd = &pPatch[i+1];
+ if( pApnd->eType==JSON_OBJECT ) jsonRemoveAllNulls(pApnd);
+ nApnd = jsonNodeSize(pApnd);
+ jsonParseAddNodeArray(pParse, pApnd, jsonNodeSize(pApnd));
if( pParse->oom ) return 0;
- jsonRemoveAllNulls(pPatch);
- pTarget = &pParse->aNode[iTarget];
- assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
- testcase( pParse->aNode[iRoot].eU==2 );
+ pParse->aNode[iStart].n = 1+nApnd;
pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
+ pParse->aNode[iRoot].u.iAppend = iStart;
VVA( pParse->aNode[iRoot].eU = 2 );
- pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
iRoot = iStart;
- assert( pParse->aNode[iPatch].eU==0 );
- VVA( pParse->aNode[iPatch].eU = 5 );
- pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
- pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
+ pTarget = &pParse->aNode[iTarget];
}
}
return pTarget;
@@ -2372,15 +2668,19 @@ static void jsonPatchFunc(
JsonNode *pResult; /* The result of the merge */
UNUSED_PARAMETER(argc);
- if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
- if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
+ if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0]), 0) ) return;
+ if( jsonParse(&y, ctx, (char*)sqlite3_value_text(argv[1]), 0) ){
jsonParseReset(&x);
return;
}
+ x.useMod = 1;
+ y.useMod = 1;
pResult = jsonMergePatch(&x, 0, y.aNode);
assert( pResult!=0 || x.oom );
- if( pResult ){
- jsonReturnJson(pResult, ctx, 0);
+ if( pResult && x.oom==0 ){
+ jsonDebugPrintParse(&x);
+ jsonDebugPrintNode(pResult);
+ jsonReturnJson(&x, pResult, ctx, 0);
}else{
sqlite3_result_error_nomem(ctx);
}
@@ -2441,29 +2741,117 @@ static void jsonRemoveFunc(
int argc,
sqlite3_value **argv
){
- JsonParse x; /* The parse */
+ JsonParse *pParse; /* The parse */
JsonNode *pNode;
const char *zPath;
u32 i;
if( argc<1 ) return;
- if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
- assert( x.nNode );
+ pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
+ 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;
- pNode = jsonLookup(&x, zPath, 0, ctx);
- if( x.nErr ) goto remove_done;
- if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
+ pNode = jsonLookup(pParse, zPath, 0, ctx);
+ if( pParse->nErr ) goto remove_done;
+ if( pNode ){
+ pNode->jnFlags |= JNODE_REMOVE;
+ pParse->hasMod = 1;
+ pParse->useMod = 1;
+ }
}
- if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
- jsonReturnJson(x.aNode, ctx, 0);
+ if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1);
}
remove_done:
- jsonParseReset(&x);
+ jsonDebugPrintParse(p);
}
/*
+** Substitute the value at iNode with the pValue parameter.
+*/
+static void jsonReplaceNode(
+ sqlite3_context *pCtx,
+ JsonParse *p,
+ int iNode,
+ sqlite3_value *pValue
+){
+ int idx = jsonParseAddSubstNode(p, iNode);
+ if( idx<=0 ){
+ assert( p->oom );
+ return;
+ }
+ switch( sqlite3_value_type(pValue) ){
+ case SQLITE_NULL: {
+ jsonParseAddNode(p, JSON_NULL, 0, 0);
+ break;
+ }
+ case SQLITE_FLOAT: {
+ char *z = sqlite3_mprintf("%!0.15g", sqlite3_value_double(pValue));
+ int n;
+ if( z==0 ){
+ p->oom = 1;
+ break;
+ }
+ n = sqlite3Strlen30(z);
+ jsonParseAddNode(p, JSON_REAL, n, z);
+ jsonParseAddCleanup(p, sqlite3_free, z);
+ break;
+ }
+ case SQLITE_INTEGER: {
+ char *z = sqlite3_mprintf("%lld", sqlite3_value_int64(pValue));
+ int n;
+ if( z==0 ){
+ p->oom = 1;
+ break;
+ }
+ n = sqlite3Strlen30(z);
+ jsonParseAddNode(p, JSON_INT, n, z);
+ jsonParseAddCleanup(p, sqlite3_free, z);
+
+ break;
+ }
+ case SQLITE_TEXT: {
+ const char *z = (const char*)sqlite3_value_text(pValue);
+ u32 n = (u32)sqlite3_value_bytes(pValue);
+ if( z==0 ){
+ p->oom = 1;
+ break;
+ }
+ if( sqlite3_value_subtype(pValue)!=JSON_SUBTYPE ){
+ int k = jsonParseAddNode(p, JSON_STRING, n, z);
+ char *zCopy = sqlite3DbStrDup(0, z);
+ if( k>0 ) p->aNode[k].jnFlags |= JNODE_RAW;
+ if( zCopy ){
+ jsonParseAddCleanup(p, sqlite3_free, zCopy);
+ }else{
+ sqlite3_result_error_nomem(pCtx);
+ }
+ }else{
+ JsonParse *pPatch = jsonParseCached(pCtx, pValue, pCtx, 1);
+ if( pPatch==0 ){
+ p->oom = 1;
+ break;
+ }
+ jsonParseAddNodeArray(p, pPatch->aNode, pPatch->nNode);
+ /* The nodes copied out of pPatch and into p likely contain
+ ** u.zJContent pointers into pPatch->zJson. So preserve the
+ ** content of pPatch until p is destroyed. */
+ assert( pPatch->nJPRef>=1 );
+ pPatch->nJPRef++;
+ jsonParseAddCleanup(p, (void(*)(void*))jsonParseFree, pPatch);
+ }
+ break;
+ }
+ case SQLITE_BLOB: {
+ sqlite3_result_error(pCtx, "JSON cannot hold BLOB values", -1);
+ p->nErr++;
+ break;
+ }
+ }
+}
+
+/*
** json_replace(JSON, PATH, VALUE, ...)
**
** Replace the value at PATH with VALUE. If PATH does not already exist,
@@ -2474,7 +2862,7 @@ static void jsonReplaceFunc(
int argc,
sqlite3_value **argv
){
- JsonParse x; /* The parse */
+ JsonParse *pParse; /* The parse */
JsonNode *pNode;
const char *zPath;
u32 i;
@@ -2484,28 +2872,20 @@ static void jsonReplaceFunc(
jsonWrongNumArgs(ctx, "replace");
return;
}
- if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
- assert( x.nNode );
+ pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
+ if( pParse==0 ) return;
for(i=1; i<(u32)argc; i+=2){
zPath = (const char*)sqlite3_value_text(argv[i]);
- pNode = jsonLookup(&x, zPath, 0, ctx);
- if( x.nErr ) goto replace_err;
+ pParse->useMod = 1;
+ pNode = jsonLookup(pParse, zPath, 0, ctx);
+ if( pParse->nErr ) goto replace_err;
if( pNode ){
- assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
- testcase( pNode->eU!=0 && pNode->eU!=1 );
- pNode->jnFlags |= (u8)JNODE_REPLACE;
- VVA( pNode->eU = 4 );
- pNode->u.iReplace = i + 1;
+ jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
}
}
- if( x.aNode[0].jnFlags & JNODE_REPLACE ){
- assert( x.aNode[0].eU==4 );
- sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
- }else{
- jsonReturnJson(x.aNode, ctx, argv);
- }
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1);
replace_err:
- jsonParseReset(&x);
+ jsonDebugPrintParse(pParse);
}
@@ -2526,7 +2906,7 @@ static void jsonSetFunc(
int argc,
sqlite3_value **argv
){
- JsonParse x; /* The parse */
+ JsonParse *pParse; /* The parse */
JsonNode *pNode;
const char *zPath;
u32 i;
@@ -2538,33 +2918,27 @@ static void jsonSetFunc(
jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
return;
}
- if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
- assert( x.nNode );
+ pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
+ if( pParse==0 ) return;
for(i=1; i<(u32)argc; i+=2){
zPath = (const char*)sqlite3_value_text(argv[i]);
bApnd = 0;
- pNode = jsonLookup(&x, zPath, &bApnd, ctx);
- if( x.oom ){
+ pParse->useMod = 1;
+ pNode = jsonLookup(pParse, zPath, &bApnd, ctx);
+ if( pParse->oom ){
sqlite3_result_error_nomem(ctx);
goto jsonSetDone;
- }else if( x.nErr ){
+ }else if( pParse->nErr ){
goto jsonSetDone;
}else if( pNode && (bApnd || bIsSet) ){
- testcase( pNode->eU!=0 && pNode->eU!=1 );
- assert( pNode->eU!=3 && pNode->eU!=5 );
- VVA( pNode->eU = 4 );
- pNode->jnFlags |= (u8)JNODE_REPLACE;
- pNode->u.iReplace = i + 1;
+ jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
}
}
- if( x.aNode[0].jnFlags & JNODE_REPLACE ){
- assert( x.aNode[0].eU==4 );
- sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
- }else{
- jsonReturnJson(x.aNode, ctx, argv);
- }
+ jsonDebugPrintParse(pParse);
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1);
+
jsonSetDone:
- jsonParseReset(&x);
+ /* no cleanup required */;
}
/*
@@ -2583,7 +2957,7 @@ static void jsonTypeFunc(
const char *zPath;
JsonNode *pNode;
- p = jsonParseCached(ctx, argv, ctx);
+ p = jsonParseCached(ctx, argv[0], ctx, 0);
if( p==0 ) return;
if( argc==2 ){
zPath = (const char*)sqlite3_value_text(argv[1]);
@@ -2610,12 +2984,12 @@ static void jsonValidFunc(
JsonParse *p; /* The parse */
UNUSED_PARAMETER(argc);
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
- p = jsonParseCached(ctx, argv, 0);
+ p = jsonParseCached(ctx, argv[0], 0, 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);
+ sqlite3_result_int(ctx, p->nErr==0 && (p->hasNonstd==0 || p->useMod));
if( p->nErr ) jsonParseFree(p);
}
}
@@ -2656,7 +3030,7 @@ static void jsonErrorFunc(
JsonParse *p; /* The parse */
UNUSED_PARAMETER(argc);
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
- p = jsonParseCached(ctx, argv, 0);
+ p = jsonParseCached(ctx, argv[0], 0, 0);
if( p==0 || p->oom ){
sqlite3_result_error_nomem(ctx);
sqlite3_free(p);
@@ -2665,7 +3039,7 @@ static void jsonErrorFunc(
}else{
int n = 1;
u32 i;
- const char *z = p->zJson;
+ const char *z = (const char*)sqlite3_value_text(argv[0]);
for(i=0; i<p->iErr && ALWAYS(z[i]); i++){
if( (z[i]&0xc0)!=0x80 ) n++;
}
@@ -2713,7 +3087,8 @@ static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
assert( pStr->bStatic );
}else if( isFinal ){
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
- pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
+ pStr->bStatic ? SQLITE_TRANSIENT :
+ (void(*)(void*))sqlite3RCStrUnref);
pStr->bStatic = 1;
}else{
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
@@ -2821,7 +3196,8 @@ static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
assert( pStr->bStatic );
}else if( isFinal ){
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
- pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
+ pStr->bStatic ? SQLITE_TRANSIENT :
+ (void(*)(void*))sqlite3RCStrUnref);
pStr->bStatic = 1;
}else{
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
@@ -3070,7 +3446,7 @@ static int jsonEachColumn(
case JEACH_KEY: {
if( p->i==0 ) break;
if( p->eType==JSON_OBJECT ){
- jsonReturn(pThis, ctx, 0);
+ jsonReturn(&p->sParse, pThis, ctx);
}else if( p->eType==JSON_ARRAY ){
u32 iKey;
if( p->bRecursive ){
@@ -3086,7 +3462,7 @@ static int jsonEachColumn(
}
case JEACH_VALUE: {
if( pThis->jnFlags & JNODE_LABEL ) pThis++;
- jsonReturn(pThis, ctx, 0);
+ jsonReturn(&p->sParse, pThis, ctx);
break;
}
case JEACH_TYPE: {
@@ -3097,7 +3473,7 @@ static int jsonEachColumn(
case JEACH_ATOM: {
if( pThis->jnFlags & JNODE_LABEL ) pThis++;
if( pThis->eType>=JSON_ARRAY ) break;
- jsonReturn(pThis, ctx, 0);
+ jsonReturn(&p->sParse, pThis, ctx);
break;
}
case JEACH_ID: {
@@ -3256,7 +3632,7 @@ static int jsonEachFilter(
p->zJson = sqlite3_malloc64( n+1 );
if( p->zJson==0 ) return SQLITE_NOMEM;
memcpy(p->zJson, z, (size_t)n+1);
- if( jsonParse(&p->sParse, 0, p->zJson) ){
+ if( jsonParse(&p->sParse, 0, p->zJson, 0) ){
int rc = SQLITE_NOMEM;
if( p->sParse.oom==0 ){
sqlite3_free(cur->pVtab->zErrMsg);