Add the json_pretty(J) function for pretty-printing of JSON. An optional
2nd argument is text used for indentation, with a default value being four spaces. FossilOrigin-Name: 39552bd36c06fe9ee346cb71e0659baceccde031b67c0974f2dd14eb11ebc055
This commit is contained in:
parent
a64342ee9c
commit
b4e7d59f4f
15
manifest
15
manifest
@ -1,5 +1,5 @@
|
||||
C Correction\sto\sthe\sprevious\scheck-in.
|
||||
D 2024-03-06T12:28:55.128
|
||||
C Add\sthe\sjson_pretty(J)\sfunction\sfor\spretty-printing\sof\sJSON.\s\sAn\soptional\n2nd\sargument\sis\stext\sused\sfor\sindentation,\swith\sa\sdefault\svalue\sbeing\sfour\nspaces.
|
||||
D 2024-03-06T14:30:42.046
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||
@ -710,7 +710,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 9337dac9d9d4c77461db55fd3e06e4d006f4283cb1da2b3a9b1f8d750701583e
|
||||
F src/json.c e2e40760d6689134c3e2ece38c6a496b34ff5e2661a8f238444a119af666fdce
|
||||
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
|
||||
F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
|
||||
F src/main.c 438b95162acfa17b7d218f586f5bde11d6ae82bcf030c9611fc537556870ad6b
|
||||
@ -2176,8 +2176,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||
P 027e5336acc26f57f21df4980928731026c30cf88688fa0b66f13ffa0b5da3a0
|
||||
R ee785ecad1867a820bb9e665fd92d3e8
|
||||
P 483fa2969e1e10cd8e8d2f9e3027871c65b1360b6c23897efe3ce63a3a55ae13
|
||||
R cdcb4f4f681bccfe01ba6f525ed68e3e
|
||||
T *branch * json-pretty
|
||||
T *sym-json-pretty *
|
||||
T -sym-trunk *
|
||||
U drh
|
||||
Z 74daf43f110405d8b8bf503998b8174f
|
||||
Z 040cd116c25b3e34e2b8acda05bc14c7
|
||||
# Remove this line to create a well-formed Fossil manifest.
|
||||
|
@ -1 +1 @@
|
||||
483fa2969e1e10cd8e8d2f9e3027871c65b1360b6c23897efe3ce63a3a55ae13
|
||||
39552bd36c06fe9ee346cb71e0659baceccde031b67c0974f2dd14eb11ebc055
|
143
src/json.c
143
src/json.c
@ -563,7 +563,6 @@ static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Append formatted text (not to exceed N bytes) to the JsonString.
|
||||
*/
|
||||
static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
|
||||
@ -2336,6 +2335,112 @@ static u32 jsonTranslateBlobToText(
|
||||
return i+n+sz;
|
||||
}
|
||||
|
||||
/* Context for recursion of json_pretty()
|
||||
*/
|
||||
typedef struct JsonPretty JsonPretty;
|
||||
struct JsonPretty {
|
||||
JsonParse *pParse; /* The BLOB being rendered */
|
||||
JsonString *pOut; /* Generate pretty output into this string */
|
||||
const char *zIndent; /* Use this text for indentation */
|
||||
u32 szIndent; /* Bytes in zIndent[] */
|
||||
u32 nIndent; /* Current level of indentation */
|
||||
};
|
||||
|
||||
/* Append indentation to the pretty JSON under construction */
|
||||
static void jsonPrettyIndent(JsonPretty *pPretty){
|
||||
u32 jj;
|
||||
for(jj=0; jj<pPretty->nIndent; jj++){
|
||||
jsonAppendRaw(pPretty->pOut, pPretty->zIndent, pPretty->szIndent);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Translate the binary JSONB representation of JSON beginning at
|
||||
** pParse->aBlob[i] into a JSON text string. Append the JSON
|
||||
** text onto the end of pOut. Return the index in pParse->aBlob[]
|
||||
** of the first byte past the end of the element that is translated.
|
||||
**
|
||||
** This is a variant of jsonTranslateBlobToText() that "pretty-prints"
|
||||
** the output. Extra whitespace is inserted to make the JSON easier
|
||||
** for humans to read.
|
||||
**
|
||||
** If an error is detected in the BLOB input, the pOut->eErr flag
|
||||
** might get set to JSTRING_MALFORMED. But not all BLOB input errors
|
||||
** are detected. So a malformed JSONB input might either result
|
||||
** in an error, or in incorrect JSON.
|
||||
**
|
||||
** The pOut->eErr JSTRING_OOM flag is set on a OOM.
|
||||
*/
|
||||
static u32 jsonTranslateBlobToPrettyText(
|
||||
JsonPretty *pPretty, /* Pretty-printing context */
|
||||
u32 i /* Start rendering at this index */
|
||||
){
|
||||
u32 sz, n, j, iEnd;
|
||||
const JsonParse *pParse = pPretty->pParse;
|
||||
JsonString *pOut = pPretty->pOut;
|
||||
n = jsonbPayloadSize(pParse, i, &sz);
|
||||
if( n==0 ){
|
||||
pOut->eErr |= JSTRING_MALFORMED;
|
||||
return pParse->nBlob+1;
|
||||
}
|
||||
switch( pParse->aBlob[i] & 0x0f ){
|
||||
case JSONB_ARRAY: {
|
||||
j = i+n;
|
||||
iEnd = j+sz;
|
||||
jsonAppendChar(pOut, '[');
|
||||
if( j<iEnd ){
|
||||
jsonAppendChar(pOut, '\n');
|
||||
pPretty->nIndent++;
|
||||
while( pOut->eErr==0 ){
|
||||
jsonPrettyIndent(pPretty);
|
||||
j = jsonTranslateBlobToPrettyText(pPretty, j);
|
||||
if( j>=iEnd ) break;
|
||||
jsonAppendRawNZ(pOut, ",\n", 2);
|
||||
}
|
||||
jsonAppendChar(pOut, '\n');
|
||||
pPretty->nIndent--;
|
||||
jsonPrettyIndent(pPretty);
|
||||
}
|
||||
jsonAppendChar(pOut, ']');
|
||||
i = iEnd;
|
||||
break;
|
||||
}
|
||||
case JSONB_OBJECT: {
|
||||
j = i+n;
|
||||
iEnd = j+sz;
|
||||
jsonAppendChar(pOut, '{');
|
||||
if( j<iEnd ){
|
||||
jsonAppendChar(pOut, '\n');
|
||||
pPretty->nIndent++;
|
||||
while( pOut->eErr==0 ){
|
||||
jsonPrettyIndent(pPretty);
|
||||
j = jsonTranslateBlobToText(pParse, j, pOut);
|
||||
if( j>iEnd ){
|
||||
pOut->eErr |= JSTRING_MALFORMED;
|
||||
break;
|
||||
}
|
||||
jsonAppendRawNZ(pOut, ": ", 2);
|
||||
j = jsonTranslateBlobToPrettyText(pPretty, j);
|
||||
if( j>=iEnd ) break;
|
||||
jsonAppendRawNZ(pOut, ",\n", 2);
|
||||
}
|
||||
jsonAppendChar(pOut, '\n');
|
||||
pPretty->nIndent--;
|
||||
jsonPrettyIndent(pPretty);
|
||||
}
|
||||
jsonAppendChar(pOut, '}');
|
||||
i = iEnd;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
i = jsonTranslateBlobToText(pParse, i, pOut);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
/* Return true if the input pJson
|
||||
**
|
||||
** For performance reasons, this routine does not do a detailed check of the
|
||||
@ -4255,6 +4360,40 @@ json_type_done:
|
||||
jsonParseFree(p);
|
||||
}
|
||||
|
||||
/*
|
||||
** json_pretty(JSON)
|
||||
** json_pretty(JSON, INDENT)
|
||||
**
|
||||
** Return text that is a pretty-printed rendering of the input JSON.
|
||||
** If the argument is not valid JSON, return NULL.
|
||||
**
|
||||
** The INDENT argument is text that is used for indentation. If omitted,
|
||||
** it defaults to four spaces (the same as PostgreSQL).
|
||||
*/
|
||||
static void jsonPrettyFunc(
|
||||
sqlite3_context *ctx,
|
||||
int argc,
|
||||
sqlite3_value **argv
|
||||
){
|
||||
JsonString s; /* The output string */
|
||||
JsonPretty x; /* Pretty printing context */
|
||||
|
||||
memset(&x, 0, sizeof(x));
|
||||
x.pParse = jsonParseFuncArg(ctx, argv[0], 0);
|
||||
if( x.pParse==0 ) return;
|
||||
x.pOut = &s;
|
||||
jsonStringInit(&s, ctx);
|
||||
if( argc==1 || (x.zIndent = (const char*)sqlite3_value_text(argv[1]))==0 ){
|
||||
x.zIndent = " ";
|
||||
x.szIndent = 4;
|
||||
}else{
|
||||
x.szIndent = (u32)strlen(x.zIndent);
|
||||
}
|
||||
jsonTranslateBlobToPrettyText(&x, 0);
|
||||
jsonReturnString(&s, 0, 0);
|
||||
jsonParseFree(x.pParse);
|
||||
}
|
||||
|
||||
/*
|
||||
** json_valid(JSON)
|
||||
** json_valid(JSON, FLAGS)
|
||||
@ -5269,6 +5408,8 @@ void sqlite3RegisterJsonFunctions(void){
|
||||
JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc),
|
||||
JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc),
|
||||
JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc),
|
||||
JFUNCTION(json_pretty, 1,1,0, 0,0,0, jsonPrettyFunc),
|
||||
JFUNCTION(json_pretty, 2,1,0, 0,0,0, jsonPrettyFunc),
|
||||
JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc),
|
||||
JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc),
|
||||
JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc),
|
||||
|
Loading…
Reference in New Issue
Block a user