Add the json_pretty() SQL function.

FossilOrigin-Name: ceb51c1cc36dfb78db8f2be042a0d74ab9a877d14a53dadf876d09e1a0616d6a
This commit is contained in:
drh
2024-03-06 20:49:05 +00:00
6 changed files with 212 additions and 12 deletions
+7 -1
View File
@@ -761,7 +761,13 @@
" FROM m2 GROUP BY cy\n",
" )\n",
"SELECT group_concat(rtrim(t),x'0a') as Mandelbrot FROM a;\n",
]}
]},
{name: "JSON pretty-print",
sql: "select json_pretty(json_object('ex',json('[52,3.14159]')))"
},
{name: "JSON pretty-print (with tabs)",
sql: "select json_pretty(json_object('ex',json('[52,3.14159]')),char(0x09))"
}
];
const newOpt = function(lbl,val){
const o = document.createElement('option');
+11 -9
View File
@@ -1,5 +1,5 @@
C Work\saround\sobscure\sfloating\spoint\sissue\sseen\swith\solder\sversions\sof\sMSVC.
D 2024-03-06T20:24:02.178
C Add\sthe\sjson_pretty()\sSQL\sfunction.
D 2024-03-06T20:49:05.359
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -641,7 +641,7 @@ F ext/wasm/example_extra_init.c 2347cd69d19d839ef4e5e77b7855103a7fe3ef2af86f2e8c
F ext/wasm/fiddle.make 3c2eace29255d6ddd219f5d8cc2728cb28b9fe717ea80b6062c2a6178947a16b
F ext/wasm/fiddle/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
F ext/wasm/fiddle/fiddle-worker.js 850e66fce39b89d59e161d1abac43a181a4caa89ddeea162765d660277cd84ce
F ext/wasm/fiddle/fiddle.js 04a638e3ed8fc9ca7c05cbe73ac4196e4529ec564639a76fbcecaffdf62bd983
F ext/wasm/fiddle/fiddle.js b444a5646a9aac9f3fc06c53d78af5e1912eb235d69a8e6010723e4eb0e9d4a1
F ext/wasm/fiddle/index.html 5daf54e8f3d7777cbb1ca4f93affe28858dbfff25841cb4ab81d694efed28ec2
F ext/wasm/index-dist.html e91d76e4581185238fd3d42ed86ec600f7023ed3e3a944c5c356f25304bf1263
F ext/wasm/index.html b31ce41c0da476d5ffcef23069b9d3415b419d65af5779096ebcfbcbade453a9
@@ -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
@@ -1353,8 +1353,9 @@ F test/json102.test 557a46e16df1aa9bdbc4076a71a45814ea0e7503d6621d87d42a8c04cbc2
F test/json103.test 53df87f83a4e5fa0c0a56eb29ff6c94055c6eb919f33316d62161a8880112dbe
F test/json104.test 1b844a70cddcfa2e4cd81a5db0657b2e61e7f00868310f24f56a9ba0114348c1
F test/json105.test 043838b56e68f3252a0dcf5be1689016f6f3f05056f8dcfcdc9d074f4d932988
F test/json106.test 1d46a9294e2ced35c7f87cebbcb9626d01abab04f1969d7ded7b6f6a1d9be0f2
F test/json106.test 4aed3afd16549045d198a8d9cea00deea96e1f2ecf55864dce96cac558b8abef
F test/json107.test 59054e815c8f6b67d634d44ace421cf975828fb5651c4460aa66015c8e19d562
F test/json108.test 0a5f1e2d4b35a1bc33052563d2a5ede03052e2099e58cb424547656c898e0f49
F test/json501.test b95e2d14988b682a5cadf079dd6162f0f85fb74cd59c6b1f1624110104a974eb
F test/json502.test 84634d3dbb521d2814e43624025b760c6198456c8197bbec6c977c0236648f5b
F test/jsonb01.test f4cdfb4cf5a0c940091b17675ed9583f45add0c938f07d65b0de0e19d3a9a101
@@ -2176,8 +2177,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 483fa2969e1e10cd8e8d2f9e3027871c65b1360b6c23897efe3ce63a3a55ae13
R e5a2f85d4c7fe1bb0d133ab32d8d2d60
U mistachkin
Z 8ca80acfec7b6e2b98a8f7ce94a903f4
P 42d39f9140cc315df04b048f3811311fa50b51e1831d6cdf982fbce029778e43 6448b90708eeedef03e82dcb10d2879e1bc859d422b450c5fc403ffbe0343bed
R 88bdf8f2cb7f2d0936a740d66ef2bdb9
T +closed 6448b90708eeedef03e82dcb10d2879e1bc859d422b450c5fc403ffbe0343bed
U drh
Z 6f02a8d0c883d9be78c99bd4533f623f
# Remove this line to create a well-formed Fossil manifest.
+1 -1
View File
@@ -1 +1 @@
42d39f9140cc315df04b048f3811311fa50b51e1831d6cdf982fbce029778e43
ceb51c1cc36dfb78db8f2be042a0d74ab9a877d14a53dadf876d09e1a0616d6a
+142 -1
View File
@@ -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),
+6
View File
@@ -67,6 +67,12 @@ for {set ii 1} {$ii<=5000} {incr ii} {
FROM t1, kv
WHERE p->>key IS NOT val
} 0
do_execsql_test $ii.8 {
SELECT j0 FROM t1 WHERE json(j0)!=json(json_pretty(j0));
} {}
do_execsql_test $ii.9 {
SELECT j5 FROM t1 WHERE json(j5)!=json(json_pretty(j5));
} {}
}
+45
View File
@@ -0,0 +1,45 @@
# 2024-03-06
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# Invariant tests for JSON built around the randomjson extension
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix json108
# These tests require virtual table "json_tree" to run.
ifcapable !vtab { finish_test ; return }
load_static_extension db randomjson
db eval {
CREATE TEMP TABLE t1(j0,j5);
WITH RECURSIVE c(n) AS (VALUES(0) UNION ALL SELECT n+1 FROM c WHERE n<9)
INSERT INTO t1 SELECT random_json(n), random_json5(n) FROM c;
}
do_execsql_test 1.1 {
SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,NULL));
} 10
do_execsql_test 1.2 {
SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,NULL));
} 10
do_execsql_test 1.3 {
SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,''));
} 10
do_execsql_test 1.4 {
SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,char(9)));
} 10
do_execsql_test 1.5 {
SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,'/*hello*/'));
} 10
finish_test