aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/json.c1186
1 files changed, 1152 insertions, 34 deletions
diff --git a/src/json.c b/src/json.c
index 253fce9f4..cfd37754f 100644
--- a/src/json.c
+++ b/src/json.c
@@ -122,6 +122,22 @@ struct JsonCleanup {
#define JSON_ARRAY 7
#define JSON_OBJECT 8
+/* JSON BLOB node types
+*/
+#define JSONB_NULL 0 /* "null" */
+#define JSONB_TRUE 1 /* "true" */
+#define JSONB_FALSE 2 /* "false" */
+#define JSONB_INT 3 /* integer acceptable to JSON and SQL */
+#define JSONB_INT5 4 /* integer in 0x000 notation */
+#define JSONB_FLOAT 5 /* float acceptable to JSON and SQL */
+#define JSONB_FLOAT5 6 /* float with JSON5 extensions */
+#define JSONB_TEXT 7 /* Text compatible with both JSON and SQL */
+#define JSONB_TEXTJ 8 /* Text with JSON escapes */
+#define JSONB_TEXT5 9 /* Text with JSON-5 escape */
+#define JSONB_TEXTRAW 10 /* SQL text that needs escaping for JSON */
+#define JSONB_ARRAY 11 /* An array */
+#define JSONB_OBJECT 12 /* An object */
+
/* The "subtype" set for JSON values */
#define JSON_SUBTYPE 74 /* Ascii for "J" */
@@ -204,12 +220,17 @@ struct JsonParse {
u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
u8 useMod; /* Actually use the edits contain inside aNode */
u8 hasMod; /* aNode contains edits from the original zJson */
+ u8 isBinary; /* True if zJson is the binary encoding */
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 JSON_SUBST entry in aNode[] */
u32 iHold; /* Age of this entry in the cache for LRU replacement */
+ /* Binary format */
+ u32 nBlob; /* Bytes of aBlob[] actually used */
+ u32 nBlobAlloc; /* Bytes allocated to aBlob[] */
+ u8 *aBlob; /* BLOB representation of zJson */
};
/*
@@ -1762,6 +1783,36 @@ json_parse_restart:
} /* End switch(z[i]) */
}
+/* Mark node i of pParse as being a child of iParent. Call recursively
+** to fill in all the descendants of node i.
+*/
+static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
+ JsonNode *pNode = &pParse->aNode[i];
+ u32 j;
+ pParse->aUp[i] = iParent;
+ switch( pNode->eType ){
+ case JSON_ARRAY: {
+ for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
+ jsonParseFillInParentage(pParse, i+j, i);
+ }
+ break;
+ }
+ case JSON_OBJECT: {
+ for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
+ pParse->aUp[i+j] = i;
+ jsonParseFillInParentage(pParse, i+j+1, i);
+ }
+ break;
+ }
+ default: {
+ break;
+ }
+ }
+}
+
+/* Forward reference */
+static int jsonParseValueFromBinary(JsonParse *pParse, u32 i);
+
/*
** Parse a complete JSON string. Return 0 on success or non-zero if there
** are any errors. If an error occurs, free all memory held by pParse,
@@ -1776,7 +1827,13 @@ static int jsonParse(
){
int i;
const char *zJson = pParse->zJson;
- i = jsonParseValue(pParse, 0);
+ if( pParse->isBinary ){
+ pParse->aBlob = (u8*)pParse->zJson;
+ pParse->nBlob = pParse->nJson;
+ i = jsonParseValueFromBinary(pParse, 0);
+ }else{
+ i = jsonParseValue(pParse, 0);
+ }
if( pParse->oom ) i = -1;
if( i>0 ){
assert( pParse->iDepth==0 );
@@ -1804,34 +1861,6 @@ 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.
-*/
-static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
- JsonNode *pNode = &pParse->aNode[i];
- u32 j;
- pParse->aUp[i] = iParent;
- switch( pNode->eType ){
- case JSON_ARRAY: {
- for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
- jsonParseFillInParentage(pParse, i+j, i);
- }
- break;
- }
- case JSON_OBJECT: {
- for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
- pParse->aUp[i+j] = i;
- jsonParseFillInParentage(pParse, i+j+1, i);
- }
- break;
- }
- default: {
- break;
- }
- }
-}
-
/*
** Compute the parentage of all nodes in a completed parse.
*/
@@ -1853,6 +1882,10 @@ static int jsonParseFindParents(JsonParse *pParse){
#define JSON_CACHE_ID (-429938) /* First cache entry */
#define JSON_CACHE_SZ 4 /* Max number of cache entries */
+/* Forward reference */
+static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
+
+
/*
** Obtain a complete parse of the JSON found in the pJson argument
**
@@ -1887,8 +1920,8 @@ static JsonParse *jsonParseCached(
sqlite3_context *pErrCtx, /* Write parse errors here if not NULL */
int bUnedited /* No prior edits allowed */
){
- char *zJson = (char*)sqlite3_value_text(pJson);
- int nJson = sqlite3_value_bytes(pJson);
+ char *zJson;
+ int nJson;
JsonParse *p;
JsonParse *pMatch = 0;
int iKey;
@@ -1896,6 +1929,16 @@ static JsonParse *jsonParseCached(
u32 iMinHold = 0xffffffff;
u32 iMaxHold = 0;
int bJsonRCStr;
+ int isBinary;
+
+ if( jsonFuncArgMightBeBinary(pJson) ){
+ zJson = (char*)sqlite3_value_blob(pJson);
+ isBinary = 1;
+ }else{
+ zJson = (char*)sqlite3_value_text(pJson);
+ isBinary = 0;
+ }
+ nJson = sqlite3_value_bytes(pJson);
if( zJson==0 ) return 0;
for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
@@ -1954,9 +1997,11 @@ static JsonParse *jsonParseCached(
p->bJsonIsRCStr = 1;
}else{
p->zJson = (char*)&p[1];
- memcpy(p->zJson, zJson, nJson+1);
+ memcpy(p->zJson, zJson, nJson+(isBinary==0));
}
p->nJPRef = 1;
+ p->isBinary = isBinary;
+ p->nJson = nJson;
if( jsonParse(p, pErrCtx) ){
if( pErrCtx==0 ){
p->nErr = 1;
@@ -1966,7 +2011,6 @@ static JsonParse *jsonParseCached(
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,
@@ -2298,6 +2342,998 @@ static void jsonRemoveAllNulls(JsonNode *pNode){
}
}
+/****************************************************************************
+** Utility routines for dealing with the binary BLOB representation of JSON
+****************************************************************************/
+
+
+/*
+** Expand pParse->aBlob so that it holds at least N bytes.
+**
+** Return the number of errors.
+*/
+static int jsonBlobExpand(JsonParse *pParse, u32 N){
+ u8 *aNew;
+ u32 t;
+ if( N<=pParse->nBlobAlloc ) return 0;
+ if( pParse->nBlobAlloc==0 ){
+ t = 100;
+ }else{
+ t = pParse->nBlobAlloc*2;
+ }
+ if( t<N ) t = N+100;
+ aNew = sqlite3_realloc64( pParse->aBlob, t );
+ if( aNew==0 ){ pParse->oom = 1; return 1; }
+ pParse->aBlob = aNew;
+ pParse->nBlobAlloc = t;
+ return 0;
+}
+
+/* Expand pParse->aBlob and append N bytes.
+**
+** Return the number of errors.
+*/
+static SQLITE_NOINLINE int jsonBlobExpandAndAppend(
+ JsonParse *pParse,
+ const u8 *aData,
+ u32 N
+){
+ if( jsonBlobExpand(pParse, pParse->nBlob+N) ) return 1;
+ memcpy(&pParse->aBlob[pParse->nBlob], aData, N);
+ pParse->nBlob += N;
+ return 0;
+}
+
+/* Append a single character. Return 1 if an error occurs.
+*/
+static int jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
+ if( pParse->nBlob >= pParse->nBlobAlloc ){
+ return jsonBlobExpandAndAppend(pParse, &c, 1);
+ }
+ pParse->aBlob[pParse->nBlob++] = c;
+ return 0;
+}
+
+/* Append bytes. Return 1 if an error occurs.
+*/
+static int jsonBlobAppendNBytes(JsonParse *pParse, const u8 *aData, u32 N){
+ if( pParse->nBlob+N > pParse->nBlobAlloc ){
+ return jsonBlobExpandAndAppend(pParse, aData, N);
+ }
+ memcpy(&pParse->aBlob[pParse->nBlob], aData, N);
+ pParse->nBlob += N;
+ return 0;
+}
+
+/* Append an node type byte together with the payload size.
+*/
+static void jsonBlobAppendNodeType(
+ JsonParse *pParse,
+ u8 eType,
+ u32 szPayload
+){
+ u8 a[5];
+ if( szPayload<=11 ){
+ jsonBlobAppendOneByte(pParse, eType | (szPayload<<4));
+ }else if( szPayload<=0xff ){
+ a[0] = eType | 0xc0;
+ a[1] = szPayload & 0xff;
+ jsonBlobAppendNBytes(pParse, a, 2);
+ }else if( szPayload<=0xffff ){
+ a[0] = eType | 0xd0;
+ a[1] = (szPayload >> 8) & 0xff;
+ a[2] = szPayload & 0xff;
+ jsonBlobAppendNBytes(pParse, a, 3);
+ }else{
+ a[0] = eType | 0xe0;
+ a[1] = (szPayload >> 24) & 0xff;
+ a[2] = (szPayload >> 16) & 0xff;
+ a[3] = (szPayload >> 8) & 0xff;
+ a[4] = szPayload & 0xff;
+ jsonBlobAppendNBytes(pParse, a, 5);
+ }
+}
+
+/* Change the payload size for the node at index i to be szPayload.
+*/
+static void jsonBlobChangePayloadSize(
+ JsonParse *pParse,
+ u32 i,
+ u32 szPayload
+){
+ u8 *a = &pParse->aBlob[i];
+ u8 szType = a[0]>>4;
+ if( szType<=11 ){
+ assert( szPayload<=11 );
+ a[0] = (a[0] & 0x0f) | (szPayload<<4);
+ }else if( szType==0xc ){
+ assert( szPayload<=0xff );
+ assert( i+1<pParse->nBlob );
+ a[1] = szPayload & 0xff;
+ }else if( szType==0xd ){
+ assert( szPayload<=0xffff );
+ assert( i+2<pParse->nBlob );
+ a[1] = (szPayload >> 8) & 0xff;
+ a[2] = szPayload & 0xff;
+ }else{
+ assert( szType==0xe );
+ assert( i+4<pParse->nBlob );
+ a[1] = (szPayload >> 24) & 0xff;
+ a[2] = (szPayload >> 16) & 0xff;
+ a[3] = (szPayload >> 8) & 0xff;
+ a[4] = szPayload & 0xff;
+ }
+}
+
+/*
+** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
+** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
+** any changes to *pOp and return false.
+*/
+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;
+ *pOp = JSONB_TEXTJ;
+ return 1;
+}
+
+/*
+** Parse a single JSON text value which begins at pParse->zJson[i] into
+** its equivalent BLOB representation in pParse->aBlob[]. The parse is
+** appended to pParse->aBlob[] beginning at pParse->nBlob. The size of
+** pParse->aBlob[] is increased as necessary.
+**
+** Return the index of the first character past the end of the value parsed,
+** or one of the following special result codes:
+**
+** 0 End of input
+** -1 Syntax error
+** -2 '}' seen
+** -3 ']' seen
+** -4 ',' seen
+** -5 ':' seen
+*/
+static int jsonParseValueB(JsonParse *pParse, u32 i){
+ char c;
+ u32 j;
+ u32 iThis, iStart;
+ int x;
+ u8 t;
+ const char *z = pParse->zJson;
+json_parse_restart:
+ switch( (u8)z[i] ){
+ case '{': {
+ /* Parse object */
+ iThis = pParse->nBlob;
+ jsonBlobAppendNodeType(pParse, JSONB_OBJECT, (pParse->nJson-i)*2);
+ if( ++pParse->iDepth > JSON_MAX_DEPTH ){
+ pParse->iErr = i;
+ return -1;
+ }
+ iStart = pParse->nBlob;
+ for(j=i+1;;j++){
+ u32 iBlob = pParse->nBlob;
+ x = jsonParseValueB(pParse, j);
+ if( x<=0 ){
+ int op;
+ if( x==(-2) ){
+ j = pParse->iErr;
+ if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
+ break;
+ }
+ j += json5Whitespace(&z[j]);
+ op = JSONB_TEXT;
+ if( sqlite3JsonId1(z[j])
+ || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
+ ){
+ int k = j+1;
+ while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
+ || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
+ ){
+ k++;
+ }
+ assert( iBlob==pParse->nBlob );
+ jsonBlobAppendNodeType(pParse, op, k-j);
+ jsonBlobAppendNBytes(pParse, (const u8*)&z[j], k-j);
+ pParse->hasNonstd = 1;
+ x = k;
+ }else{
+ if( x!=-1 ) pParse->iErr = j;
+ return -1;
+ }
+ }
+ if( pParse->oom ) return -1;
+ t = pParse->aBlob[iBlob] & 0x0f;
+ if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){
+ pParse->iErr = j;
+ return -1;
+ }
+ j = x;
+ if( z[j]==':' ){
+ j++;
+ }else{
+ if( fast_isspace(z[j]) ){
+ do{ j++; }while( fast_isspace(z[j]) );
+ if( z[j]==':' ){
+ j++;
+ goto parse_object_value;
+ }
+ }
+ x = jsonParseValueB(pParse, j);
+ if( x!=(-5) ){
+ if( x!=(-1) ) pParse->iErr = j;
+ return -1;
+ }
+ j = pParse->iErr+1;
+ }
+ parse_object_value:
+ x = jsonParseValueB(pParse, j);
+ if( x<=0 ){
+ if( x!=(-1) ) pParse->iErr = j;
+ return -1;
+ }
+ j = x;
+ if( z[j]==',' ){
+ continue;
+ }else if( z[j]=='}' ){
+ break;
+ }else{
+ if( fast_isspace(z[j]) ){
+ do{ j++; }while( fast_isspace(z[j]) );
+ if( z[j]==',' ){
+ continue;
+ }else if( z[j]=='}' ){
+ break;
+ }
+ }
+ x = jsonParseValueB(pParse, j);
+ if( x==(-4) ){
+ j = pParse->iErr;
+ continue;
+ }
+ if( x==(-2) ){
+ j = pParse->iErr;
+ break;
+ }
+ }
+ pParse->iErr = j;
+ return -1;
+ }
+ if( pParse->oom==0 ){
+ jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
+ }
+ pParse->iDepth--;
+ return j+1;
+ }
+ case '[': {
+ /* Parse array */
+ iThis = pParse->nBlob;
+ jsonBlobAppendNodeType(pParse, JSONB_ARRAY, pParse->nJson - i);
+ iStart = pParse->nBlob;
+ if( pParse->oom ) return -1;
+ if( ++pParse->iDepth > JSON_MAX_DEPTH ){
+ pParse->iErr = i;
+ return -1;
+ }
+ for(j=i+1;;j++){
+ x = jsonParseValueB(pParse, j);
+ if( x<=0 ){
+ if( x==(-3) ){
+ j = pParse->iErr;
+ if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
+ break;
+ }
+ if( x!=(-1) ) pParse->iErr = j;
+ return -1;
+ }
+ j = x;
+ if( z[j]==',' ){
+ continue;
+ }else if( z[j]==']' ){
+ break;
+ }else{
+ if( fast_isspace(z[j]) ){
+ do{ j++; }while( fast_isspace(z[j]) );
+ if( z[j]==',' ){
+ continue;
+ }else if( z[j]==']' ){
+ break;
+ }
+ }
+ x = jsonParseValueB(pParse, j);
+ if( x==(-4) ){
+ j = pParse->iErr;
+ continue;
+ }
+ if( x==(-3) ){
+ j = pParse->iErr;
+ break;
+ }
+ }
+ pParse->iErr = j;
+ return -1;
+ }
+ if( pParse->oom==0 ){
+ jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
+ }
+ pParse->iDepth--;
+ return j+1;
+ }
+ case '\'': {
+ u8 opcode;
+ char cDelim;
+ pParse->hasNonstd = 1;
+ opcode = JSONB_TEXT;
+ goto parse_string;
+ case '"':
+ /* Parse string */
+ opcode = JSONB_TEXT;
+ parse_string:
+ cDelim = z[i];
+ for(j=i+1; 1; j++){
+ if( jsonIsOk[(unsigned char)z[j]] ) continue;
+ c = z[j];
+ if( c==cDelim ){
+ break;
+ }else if( c=='\\' ){
+ c = z[++j];
+ if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
+ || c=='n' || c=='r' || c=='t'
+ || (c=='u' && jsonIs4Hex(&z[j+1])) ){
+ if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ;
+ }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
+ || (0xe2==(u8)c && 0x80==(u8)z[j+1]
+ && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
+ || (c=='x' && jsonIs2Hex(&z[j+1])) ){
+ opcode = JSONB_TEXT5;
+ pParse->hasNonstd = 1;
+ }else if( c=='\r' ){
+ if( z[j+1]=='\n' ) j++;
+ opcode = JSONB_TEXT5;
+ pParse->hasNonstd = 1;
+ }else{
+ pParse->iErr = j;
+ return -1;
+ }
+ }else if( c<=0x1f ){
+ /* Control characters are not allowed in strings */
+ pParse->iErr = j;
+ return -1;
+ }
+ }
+ jsonBlobAppendNodeType(pParse, opcode, j-1-i);
+ jsonBlobAppendNBytes(pParse, (const u8*)&z[i+1], j-1-i);
+ return j+1;
+ }
+ case 't': {
+ if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
+ jsonBlobAppendOneByte(pParse, JSONB_TRUE);
+ return i+4;
+ }
+ pParse->iErr = i;
+ return -1;
+ }
+ case 'f': {
+ if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
+ jsonBlobAppendOneByte(pParse, JSONB_FALSE);
+ return i+5;
+ }
+ pParse->iErr = i;
+ return -1;
+ }
+ case '+': {
+ u8 seenE;
+ pParse->hasNonstd = 1;
+ t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
+ goto parse_number;
+ case '.':
+ if( sqlite3Isdigit(z[i+1]) ){
+ pParse->hasNonstd = 1;
+ t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
+ seenE = 0;
+ goto parse_number_2;
+ }
+ pParse->iErr = i;
+ return -1;
+ case '-':
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ /* Parse number */
+ t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
+ parse_number:
+ seenE = 0;
+ assert( '-' < '0' );
+ assert( '+' < '0' );
+ assert( '.' < '0' );
+ c = z[i];
+
+ if( c<='0' ){
+ if( c=='0' ){
+ if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
+ assert( t==0x00 );
+ pParse->hasNonstd = 1;
+ t = 0x01;
+ for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
+ goto parse_number_finish;
+ }else if( sqlite3Isdigit(z[i+1]) ){
+ pParse->iErr = i+1;
+ return -1;
+ }
+ }else{
+ if( !sqlite3Isdigit(z[i+1]) ){
+ /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
+ ** that case. SQLite also allows these in any case and it allows
+ ** "+inf" and "-inf". */
+ if( (z[i+1]=='I' || z[i+1]=='i')
+ && sqlite3StrNICmp(&z[i+1], "inf",3)==0
+ ){
+ pParse->hasNonstd = 1;
+ if( z[i]=='-' ){
+ jsonBlobAppendNodeType(pParse, JSONB_FLOAT, 6);
+ jsonBlobAppendNBytes(pParse, (const u8*)"-9e999", 6);
+ }else{
+ jsonBlobAppendNodeType(pParse, JSONB_FLOAT, 5);
+ jsonBlobAppendNBytes(pParse, (const u8*)"9e999", 5);
+ }
+ return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4);
+ }
+ if( z[i+1]=='.' ){
+ pParse->hasNonstd = 1;
+ t |= 0x03;
+ goto parse_number_2;
+ }
+ pParse->iErr = i;
+ return -1;
+ }
+ if( z[i+1]=='0' ){
+ if( sqlite3Isdigit(z[i+2]) ){
+ pParse->iErr = i+1;
+ return -1;
+ }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
+ pParse->hasNonstd = 1;
+ t |= 0x01;
+ for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
+ goto parse_number_finish;
+ }
+ }
+ }
+ }
+ parse_number_2:
+ for(j=i+1;; j++){
+ c = z[j];
+ if( sqlite3Isdigit(c) ) continue;
+ if( c=='.' ){
+ if( (t & 0x02)!=0 ){
+ pParse->iErr = j;
+ return -1;
+ }
+ t |= 0x02;
+ continue;
+ }
+ if( c=='e' || c=='E' ){
+ if( z[j-1]<'0' ){
+ if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
+ pParse->hasNonstd = 1;
+ t |= 0x01;
+ }else{
+ pParse->iErr = j;
+ return -1;
+ }
+ }
+ if( seenE ){
+ pParse->iErr = j;
+ return -1;
+ }
+ t |= 0x02;
+ seenE = 1;
+ c = z[j+1];
+ if( c=='+' || c=='-' ){
+ j++;
+ c = z[j+1];
+ }
+ if( c<'0' || c>'9' ){
+ pParse->iErr = j;
+ return -1;
+ }
+ continue;
+ }
+ break;
+ }
+ if( z[j-1]<'0' ){
+ if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
+ pParse->hasNonstd = 1;
+ t |= 0x01;
+ }else{
+ pParse->iErr = j;
+ return -1;
+ }
+ }
+ parse_number_finish:
+ assert( JSONB_INT+0x01==JSONB_INT5 );
+ assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 );
+ assert( JSONB_INT+0x02==JSONB_FLOAT );
+ if( z[i]=='+' ) i++;
+ jsonBlobAppendNodeType(pParse, JSONB_INT+t, j-i);
+ jsonBlobAppendNBytes(pParse, (const u8*)&z[i], j-i);
+ return j;
+ }
+ case '}': {
+ pParse->iErr = i;
+ return -2; /* End of {...} */
+ }
+ case ']': {
+ pParse->iErr = i;
+ return -3; /* End of [...] */
+ }
+ case ',': {
+ pParse->iErr = i;
+ return -4; /* List separator */
+ }
+ case ':': {
+ pParse->iErr = i;
+ return -5; /* Object label/value separator */
+ }
+ case 0: {
+ return 0; /* End of file */
+ }
+ case 0x09:
+ case 0x0a:
+ case 0x0d:
+ case 0x20: {
+ do{
+ i++;
+ }while( fast_isspace(z[i]) );
+ goto json_parse_restart;
+ }
+ case 0x0b:
+ case 0x0c:
+ case '/':
+ case 0xc2:
+ case 0xe1:
+ case 0xe2:
+ case 0xe3:
+ case 0xef: {
+ j = json5Whitespace(&z[i]);
+ if( j>0 ){
+ i += j;
+ pParse->hasNonstd = 1;
+ goto json_parse_restart;
+ }
+ pParse->iErr = i;
+ return -1;
+ }
+ case 'n': {
+ if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
+ jsonBlobAppendOneByte(pParse, JSONB_NULL);
+ return i+4;
+ }
+ /* fall-through into the default case that checks for NaN */
+ }
+ default: {
+ u32 k;
+ int nn;
+ c = z[i];
+ for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
+ if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
+ nn = aNanInfName[k].n;
+ if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
+ continue;
+ }
+ if( sqlite3Isalnum(z[i+nn]) ) continue;
+ if( aNanInfName[k].eType==JSON_REAL ){
+ jsonBlobAppendOneByte(pParse, JSONB_FLOAT);
+ jsonBlobAppendOneByte(pParse, 5);
+ jsonBlobAppendNBytes(pParse, (const u8*)"9e999", 5);
+ }else{
+ jsonBlobAppendOneByte(pParse, JSONB_NULL);
+ }
+ pParse->hasNonstd = 1;
+ return i + nn;
+ }
+ pParse->iErr = i;
+ return -1; /* Syntax error */
+ }
+ } /* End switch(z[i]) */
+}
+
+
+/*
+** Parse a complete JSON string. Return 0 on success or non-zero if there
+** are any errors. If an error occurs, free all memory held by pParse,
+** but not pParse itself.
+**
+** pParse must be initialized to an empty parse object prior to calling
+** this routine.
+*/
+static int jsonParseB(
+ JsonParse *pParse, /* Initialize and fill this JsonParse object */
+ sqlite3_context *pCtx /* Report errors here */
+){
+ int i;
+ const char *zJson = pParse->zJson;
+ i = jsonParseValueB(pParse, 0);
+ if( pParse->oom ) i = -1;
+ if( i>0 ){
+ assert( pParse->iDepth==0 );
+ while( fast_isspace(zJson[i]) ) i++;
+ if( zJson[i] ){
+ i += json5Whitespace(&zJson[i]);
+ if( zJson[i] ){
+ jsonParseReset(pParse);
+ return 1;
+ }
+ pParse->hasNonstd = 1;
+ }
+ }
+ if( i<=0 ){
+ if( pCtx!=0 ){
+ if( pParse->oom ){
+ sqlite3_result_error_nomem(pCtx);
+ }else{
+ sqlite3_result_error(pCtx, "malformed JSON", -1);
+ }
+ }
+ jsonParseReset(pParse);
+ return 1;
+ }
+ return 0;
+}
+
+/* The byte at index i is a node type-code. This routine
+** determines the payload size for that node and writes that
+** payload size in to *pSz. It returns the offset from i to the
+** beginning of the payload. Return 0 on error.
+*/
+static u32 jsonbPayloadSize(JsonParse *pParse, u32 i, u32 *pSz){
+ u8 x;
+ u32 sz;
+ u32 n;
+ if( i>pParse->nBlob ){
+ *pSz = 0;
+ return 0;
+ }
+ x = pParse->aBlob[i]>>4;
+ if( x<=11 ){
+ sz = x;
+ n = 1;
+ }else if( x==12 ){
+ if( i+1>pParse->nBlob ){
+ *pSz = 0;
+ return 0;
+ }
+ sz = pParse->aBlob[i+1];
+ n = 2;
+ }else if( x==13 ){
+ if( i+2>pParse->nBlob ){
+ *pSz = 0;
+ return 0;
+ }
+ sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
+ n = 3;
+ }else{
+ if( i+4>pParse->nBlob ){
+ *pSz = 0;
+ return 0;
+ }
+ sz = (pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
+ (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
+ n = 5;
+ }
+ if( i+sz+n>pParse->nBlob ){
+ sz = pParse->nBlob - (i+n);
+ }
+ *pSz = sz;
+ return n;
+}
+
+
+/*
+** Convert the binary BLOB representation of JSON beginning at
+** aBlob[0] (and extending for no more than nBlob bytes) into
+** a pure JSON string. The string is appended to pOut.
+*/
+static u32 jsonRenderBlob(
+ JsonParse *pParse, /* the complete parse of the JSON */
+ u32 i, /* Start rendering at this index */
+ JsonString *pOut /* Write JSON here */
+){
+ u32 sz, n, j, iEnd;
+
+ if( i>=pParse->nBlob ){
+ pOut->bErr = 1;
+ return 0;
+ }
+ switch( pParse->aBlob[i] & 0x0f ){
+ case JSONB_NULL: {
+ jsonAppendRawNZ(pOut, "null", 4);
+ return i+1;
+ }
+ case JSONB_TRUE: {
+ jsonAppendRawNZ(pOut, "true", 4);
+ return i+1;
+ }
+ case JSONB_FALSE: {
+ jsonAppendRawNZ(pOut, "false", 5);
+ return i+1;
+ }
+ case JSONB_INT:
+ case JSONB_FLOAT: {
+ n = jsonbPayloadSize(pParse, i, &sz);
+ jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
+ break;
+ }
+ case JSONB_INT5: { /* Integer literal in hexadecimal notation */
+ int k;
+ sqlite3_uint64 u = 0;
+ n = jsonbPayloadSize(pParse, i, &sz);
+ const char *zIn = (const char*)&pParse->aBlob[i+n];
+ if( zIn[0]=='-' ){
+ zIn++;
+ sz--;
+ jsonAppendChar(pOut, '-');
+ }
+ for(k=2; k<sz; k++){
+ u = u*16 + sqlite3HexToInt(zIn[k]);
+ }
+ jsonPrintf(100,pOut,"%llu",u);
+ break;
+ }
+ case JSONB_FLOAT5: { /* Float literal missing digits beside "." */
+ int k;
+ n = jsonbPayloadSize(pParse, i, &sz);
+ const char *zIn = (const char*)&pParse->aBlob[i+n];
+ if( zIn[0]=='-' ){
+ zIn++;
+ sz--;
+ jsonAppendChar(pOut, '-');
+ }
+ if( zIn[0]=='.' ){
+ jsonAppendChar(pOut, '0');
+ }
+ for(k=0; k<sz; k++){
+ if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){
+ k++;
+ jsonAppendRaw(pOut, zIn, k);
+ zIn += k;
+ sz -= k;
+ jsonAppendChar(pOut, '0');
+ break;
+ }
+ }
+ if( sz>0 ){
+ jsonAppendRawNZ(pOut, zIn, sz);
+ }
+ break;
+ }
+ case JSONB_TEXT:
+ case JSONB_TEXTJ: {
+ n = jsonbPayloadSize(pParse, i, &sz);
+ jsonAppendChar(pOut, '"');
+ jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
+ jsonAppendChar(pOut, '"');
+ break;
+ }
+ case JSONB_TEXT5: {
+ const char *zIn;
+ u32 k;
+ n = jsonbPayloadSize(pParse, i, &sz);
+ zIn = (const char*)&pParse->aBlob[i+n];
+ jsonAppendChar(pOut, '"');
+ while( sz>0 ){
+ for(k=0; k<sz && zIn[k]!='\\'; k++){}
+ if( k>0 ){
+ jsonAppendRawNZ(pOut, zIn, k);
+ zIn += k;
+ sz -= k;
+ if( sz==0 ) break;
+ }
+ assert( zIn[0]=='\\' );
+ switch( (u8)zIn[1] ){
+ case '\'':
+ jsonAppendChar(pOut, '\'');
+ break;
+ case 'v':
+ jsonAppendRawNZ(pOut, "\\u0009", 6);
+ break;
+ case 'x':
+ jsonAppendRawNZ(pOut, "\\u00", 4);
+ jsonAppendRawNZ(pOut, &zIn[2], 2);
+ zIn += 2;
+ sz -= 2;
+ break;
+ case '0':
+ jsonAppendRawNZ(pOut, "\\u0000", 6);
+ break;
+ case '\r':
+ if( zIn[2]=='\n' ){
+ zIn++;
+ sz--;
+ }
+ break;
+ case '\n':
+ break;
+ case 0xe2:
+ assert( sz>=4 );
+ assert( 0x80==(u8)zIn[2] );
+ assert( 0xa8==(u8)zIn[3] || 0xa9==(u8)zIn[3] );
+ zIn += 2;
+ sz -= 2;
+ break;
+ default:
+ jsonAppendRawNZ(pOut, zIn, 2);
+ break;
+ }
+ zIn += 2;
+ sz -= 2;
+ }
+ jsonAppendChar(pOut, '"');
+ break;
+ }
+ case JSONB_ARRAY: {
+ jsonAppendChar(pOut, '[');
+ n = jsonbPayloadSize(pParse, i, &sz);
+ j = i+n;
+ iEnd = j+sz;
+ while( j<iEnd ){
+ j = jsonRenderBlob(pParse, j, pOut);
+ jsonAppendChar(pOut, ',');
+ }
+ if( sz>0 ) pOut->nUsed--;
+ jsonAppendChar(pOut, ']');
+ break;
+ }
+ case JSONB_OBJECT: {
+ int x = 0;
+ jsonAppendChar(pOut, '{');
+ n = jsonbPayloadSize(pParse, i, &sz);
+ j = i+n;
+ iEnd = j+sz;
+ while( j<iEnd ){
+ j = jsonRenderBlob(pParse, j, pOut);
+ jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
+ }
+ if( sz>0 ) pOut->nUsed--;
+ jsonAppendChar(pOut, '}');
+ break;
+ }
+
+ default: {
+ n = jsonbPayloadSize(pParse, i, &sz);
+ break;
+ }
+ }
+ return i+n+sz;
+}
+
+/* Return true if the input pJson
+**
+** For performance reasons, this routine does not do a detailed check of the
+** input BLOB to ensure that it is well-formed. Hence, false positives are
+** possible. False negatives should never occur, however.
+*/
+static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
+ u32 sz, n;
+ const u8 *aBlob;
+ int nBlob;
+ JsonParse s;
+ if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0;
+ nBlob = sqlite3_value_bytes(pJson);
+ if( nBlob<1 ) return 0;
+ aBlob = sqlite3_value_blob(pJson);
+ if( (aBlob[0] & 0x0f)>JSONB_OBJECT ) return 0;
+ memset(&s, 0, sizeof(s));
+ s.aBlob = (u8*)aBlob;
+ s.nBlob = nBlob;
+ n = jsonbPayloadSize(&s, 0, &sz);
+ if( n==0 ) return 0;
+ return sz+n==(u32)nBlob;
+}
+
+/* Parse a single element of binary JSON into the legacy Node structure.
+** Return the index of the first byte past the end of the binary JSON element.
+**
+** This routine is a temporary translator while the legacy Node encoding
+** is still in use. It will be removed after all processing translates
+** to the new BLOB encoding.
+*/
+static int jsonParseValueFromBinary(JsonParse *pParse, u32 i){
+ u8 t; /* Node type */
+ u32 sz; /* Node size */
+ u32 x; /* Index of payload start */
+
+ const char *zPayload;
+ x = jsonbPayloadSize(pParse, i, &sz);
+ if( x==0 ) return -1;
+ t = pParse->zJson[i] & 0x0f;
+ zPayload = &pParse->zJson[i+x];
+ switch( t ){
+ case JSONB_NULL: {
+ jsonParseAddNode(pParse, JSON_NULL, 0, 0);
+ break;
+ }
+ case JSONB_TRUE: {
+ jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
+ break;
+ }
+ case JSONB_FALSE: {
+ jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
+ break;
+ }
+ case JSONB_INT: {
+ jsonParseAddNode(pParse, JSON_INT, sz, zPayload);
+ break;
+ }
+ case JSONB_INT5: {
+ pParse->hasNonstd = 1;
+ jsonParseAddNode(pParse, JSON_INT | (JNODE_JSON5<<8), sz, zPayload);
+ break;
+ }
+ case JSONB_FLOAT: {
+ jsonParseAddNode(pParse, JSON_REAL, sz, zPayload);
+ break;
+ }
+ case JSONB_FLOAT5: {
+ pParse->hasNonstd = 1;
+ jsonParseAddNode(pParse, JSON_REAL | (JNODE_JSON5<<8), sz, zPayload);
+ break;
+ }
+ case JSONB_TEXT: {
+ jsonParseAddNode(pParse, JSON_STRING | (JNODE_RAW<<8), sz, zPayload);
+ break;
+ }
+ case JSONB_TEXTJ: {
+ jsonParseAddNode(pParse, JSON_STRING | (JNODE_ESCAPE<<8), sz, zPayload);
+ break;
+ }
+ case JSONB_TEXT5: {
+ pParse->hasNonstd = 1;
+ jsonParseAddNode(pParse, JSON_STRING | ((JNODE_ESCAPE|JNODE_JSON5)<<8),
+ sz, zPayload);
+ break;
+ }
+ case JSONB_TEXTRAW: {
+ jsonParseAddNode(pParse, JSON_STRING | (JNODE_RAW<<8), sz, zPayload);
+ break;
+ }
+ case JSONB_ARRAY: {
+ int iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
+ u32 j = i+x;
+ while( j<i+x+sz ){
+ int r = jsonParseValueFromBinary(pParse, j);
+ if( r<=0 ) return -1;
+ j = (u32)r;
+ }
+ pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
+ break;
+ }
+ case JSONB_OBJECT: {
+ int iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
+ u32 j = i+x, k = 0;
+ while( j<i+x+sz ){
+ int r = jsonParseValueFromBinary(pParse, j);
+ if( r<=0 ) return -1;
+ if( (k++&1)==0 ){
+ pParse->aNode[pParse->nNode-1].jnFlags |= JNODE_LABEL;
+ }
+ j = (u32)r;
+ }
+ pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
+ break;
+ }
+ }
+ return i+x+sz;
+}
/****************************************************************************
** SQL functions used for testing and debugging
@@ -2408,8 +3444,88 @@ static void jsonTest1Func(
** Scalar SQL function implementations
****************************************************************************/
+/* SQL Function: jsonb(JSON)
+**
+** Convert the input argument into JSONB (the SQLite binary encoding of
+** JSON).
+**
+** If the input is TEXT, or NUMERIC, try to parse it as JSON. If the fails,
+** raise an error. Otherwise, return the resulting BLOB value.
+**
+** If the input is a BLOB, check to see if the input is a plausible
+** JSONB. If it is, return it unchanged. Raise an error if it is not.
+** Note that there could be internal inconsistencies in the BLOB - this
+** routine does not do a full byte-for-byte validity check of the
+** JSON blob.
+**
+** If the input is NULL, return NULL.
+*/
+static void jsonbFunc(
+ sqlite3_context *ctx,
+ int argc,
+ sqlite3_value **argv
+){
+ JsonParse *pParse;
+ int nJson;
+ const char *zJson;
+ JsonParse x;
+ UNUSED_PARAMETER(argc);
+
+ if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
+ /* No-op */
+ }else if( jsonFuncArgMightBeBinary(argv[0]) ){
+ sqlite3_result_value(ctx, argv[0]);
+ }else{
+ zJson = (const char*)sqlite3_value_text(argv[0]);
+ if( zJson==0 ) return;
+ nJson = sqlite3_value_bytes(argv[0]);
+ pParse = &x;
+ memset(&x, 0, sizeof(x));
+ x.zJson = (char*)zJson;
+ x.nJson = nJson;
+ if( jsonParseB(pParse, ctx) ){
+ sqlite3_result_error(ctx, "malformed JSON", -1);
+ }else{
+ sqlite3_result_blob(ctx, pParse->aBlob, pParse->nBlob, sqlite3_free);
+ pParse->aBlob = 0;
+ pParse->nBlob = 0;
+ pParse->nBlobAlloc = 0;
+ }
+ jsonParseReset(pParse);
+ }
+}
+
+/* SQL Function: jsonb_test2(BLOB_JSON)
+**
+** Render BLOB_JSON back into text.
+** Development testing only.
+*/
+static void jsonbTest2(
+ sqlite3_context *ctx,
+ int argc,
+ sqlite3_value **argv
+){
+ JsonParse *pParse;
+ const u8 *aBlob;
+ int nBlob;
+ JsonParse x;
+ JsonString s;
+ UNUSED_PARAMETER(argc);
+
+ aBlob = (const u8*)sqlite3_value_blob(argv[0]);
+ if( aBlob==0 ) return;
+ nBlob = sqlite3_value_bytes(argv[0]);
+ pParse = &x;
+ memset(&x, 0, sizeof(x));
+ x.aBlob = (u8*)aBlob;
+ x.nBlob = nBlob;
+ jsonInit(&s, ctx);
+ jsonRenderBlob(pParse, 0, &s);
+ jsonResult(&s);
+}
+
/*
-** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
+** Implementation of the json_quote(VALUE) function. Return a JSON value
** 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.
@@ -3815,6 +4931,8 @@ void sqlite3RegisterJsonFunctions(void){
JFUNCTION(json_type, 1, 0, jsonTypeFunc),
JFUNCTION(json_type, 2, 0, jsonTypeFunc),
JFUNCTION(json_valid, 1, 0, jsonValidFunc),
+ JFUNCTION(jsonb, 1, 0, jsonbFunc),
+ JFUNCTION(jsonb_test2, 1, 0, jsonbTest2),
#if SQLITE_DEBUG
JFUNCTION(json_parse, 1, 0, jsonParseFunc),
JFUNCTION(json_test1, 1, 0, jsonTest1Func),