Add the ability to render a binary BLOB back into valid canonical JSON.

FossilOrigin-Name: 0b70cb77a4c8e3f17932f1ecca3942e0b0b03de637fb9656a130fe045f7ef826
This commit is contained in:
drh 2023-09-22 14:33:39 +00:00
parent 90189be7ce
commit c1c1d4d4a6
3 changed files with 177 additions and 42 deletions

View File

@ -1,5 +1,5 @@
C Begin\sadding\scode\sto\srender\sbinary\sJSON\sback\sinto\stext.\s\sVery\sincomplete.\nThis\sis\san\sincremental\scheck-in.
D 2023-09-22T12:16:56.105
C Add\sthe\sability\sto\srender\sa\sbinary\sBLOB\sback\sinto\svalid\scanonical\sJSON.
D 2023-09-22T14:33:39.536
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -670,7 +670,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
F src/json.c 351f2b99846c6b1afdbab553c26f7cf87c303f0caa005171017a30b5d9a1aece
F src/json.c 574ed4c90fc5a18d40665557c56987d6ad1cd78644fdd698b755002538252de0
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 98cfba10989b3da6f1807ad42444017742db7f100a54f1032af7a8b1295912c0
F src/main.c 618aeb399e993cf561864f4b0cf6a331ee4f355cf663635f8d9da3193a46aa40
@ -2121,8 +2121,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 2c89ae5d02f6a40ef869e2a162e2c72871df60572b27959fd1d7171f495ce881
R b78d29e507a66c69faecba07ef4a8c9a
P b817dd865ed60fc4da0b662a9edec0fceb8921b02ce98133bdd565988939fd0f
R f9832ae6b1e64d76b924944a3c617b61
U drh
Z 8e9aefce50e8026f8d315b79ff0edc06
Z 86dcd161b824c66e770c11b16ee3a065
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
b817dd865ed60fc4da0b662a9edec0fceb8921b02ce98133bdd565988939fd0f
0b70cb77a4c8e3f17932f1ecca3942e0b0b03de637fb9656a130fe045f7ef826

View File

@ -124,18 +124,19 @@ struct JsonCleanup {
/* JSON BLOB node types
*/
#define JSONB_NULL 0
#define JSONB_TRUE 1
#define JSONB_FALSE 2
#define JSONB_INT 3
#define JSONB_INT5 4
#define JSONB_FLOAT 5
#define JSONB_FLOAT5 6
#define JSONB_TEXT 7
#define JSONB_TEXTJ 8
#define JSONB_TEXT5 9
#define JSONB_ARRAY 10
#define JSONB_OBJECT 11
#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" */
@ -2493,7 +2494,7 @@ json_parse_restart:
k++;
}
assert( iBlob==pParse->nBlob );
jsonBlobAppendNodeType(pParse, JSONB_TEXT5, k-j);
jsonBlobAppendNodeType(pParse, JSONB_TEXTJ, k-j);
jsonBlobAppendNBytes(pParse, (const u8*)&z[j], k-j);
pParse->hasNonstd = 1;
x = k;
@ -2504,7 +2505,7 @@ json_parse_restart:
}
if( pParse->oom ) return -1;
t = pParse->aBlob[iBlob] & 0x0f;
if( t<JSONB_TEXT || t>JSONB_TEXT5 ){
if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){
pParse->iErr = j;
return -1;
}
@ -2661,8 +2662,8 @@ json_parse_restart:
return -1;
}
}
jsonBlobAppendNodeType(pParse, opcode, j+1-i);
jsonBlobAppendNBytes(pParse, (const u8*)&z[i], j+1-i);
jsonBlobAppendNodeType(pParse, opcode, j-1-i);
jsonBlobAppendNBytes(pParse, (const u8*)&z[i+1], j-1-i);
return j+1;
}
case 't': {
@ -2684,7 +2685,7 @@ json_parse_restart:
case '+': {
u8 seenE;
pParse->hasNonstd = 1;
t = 0x01; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
goto parse_number;
case '.':
if( sqlite3Isdigit(z[i+1]) ){
@ -2820,6 +2821,7 @@ json_parse_restart:
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;
@ -2948,10 +2950,8 @@ static int jsonParseB(
/* 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 number of additional
** bytes following the typecode that are used to hold size information.
** Enhance, if the return value is N, then the start of the payload
** is at i+N+1.
** payload size in to *pSz. It returns the offset from i to the
** beginning of the payload.
*/
static u32 jsonbPayloadSize(JsonParse *pParse, u32 i, u32 *pSz){
u8 x;
@ -2960,37 +2960,37 @@ static u32 jsonbPayloadSize(JsonParse *pParse, u32 i, u32 *pSz){
if( i>pParse->nBlob ){
pParse->iErr = 1;
*pSz = 0;
return 0;
return 1;
}
x = pParse->aBlob[i]>>4;
if( x<=11 ){
sz = x;
n = 0;
n = 1;
}else if( x==12 ){
if( i+1>pParse->nBlob ){
pParse->iErr = 1;
*pSz = 0;
return 0;
return 1;
}
sz = pParse->aBlob[i+1];
n = 1;
n = 2;
}else if( x==13 ){
if( i+2>pParse->nBlob ){
pParse->iErr = 1;
*pSz = 0;
return 0;
return 1;
}
sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
n = 2;
n = 3;
}else{
if( i+4>pParse->nBlob ){
pParse->iErr = 1;
*pSz = 0;
return 0;
return 1;
}
sz = (pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
(pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
n = 4;
n = 5;
}
if( i+sz+n>pParse->nBlob ){
sz = pParse->nBlob - (i+n);
@ -3011,6 +3011,8 @@ static u32 jsonRenderBlob(
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;
@ -3029,19 +3031,152 @@ static u32 jsonRenderBlob(
return i+1;
}
case JSONB_INT:
case JSONB_FLOAT:
case JSONB_TEXTJ: {
u32 sz, n;
case JSONB_FLOAT: {
n = jsonbPayloadSize(pParse, i, &sz);
jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n+1], sz);
return i+n+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( 1 /* exit by break */ ){
j = jsonRenderBlob(pParse, j, pOut);
if( j>=iEnd ) break;
jsonAppendChar(pOut, ',');
}
jsonAppendChar(pOut, ']');
break;
}
case JSONB_OBJECT: {
int x = 0;
jsonAppendChar(pOut, '{');
n = jsonbPayloadSize(pParse, i, &sz);
j = i+n;
iEnd = j+sz;
while( 1 /* edit by break */ ){
j = jsonRenderBlob(pParse, j, pOut);
if( j>=iEnd ) break;
jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
}
jsonAppendChar(pOut, '}');
break;
}
default: {
n = jsonbPayloadSize(pParse, i, &sz);
break;
}
}
return 0;
return i+n+sz;
}