From 7b22fb02a266880ab1a9654639c4b455470c6e51 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Wed, 8 Mar 2023 17:15:43 +0000 Subject: [PATCH] Implement pretty-printing option in Json. Telodendria itself doesn't use it, but the json CLI tool does. --- TODO.txt | 10 ++++++---- src/CanonicalJson.c | 2 +- src/Db.c | 2 +- src/Json.c | 43 +++++++++++++++++++++++++++++++++++++------ src/Matrix.c | 2 +- src/include/Json.h | 7 +++++-- tools/src/json.c | 2 +- 7 files changed, 52 insertions(+), 16 deletions(-) diff --git a/TODO.txt b/TODO.txt index fa07521..8d7be9c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -20,15 +20,17 @@ Milestone: v0.3.0 [ ] HttpClient man page [ ] Uri man page [ ] Test on other platforms -[ ] Option to pretty-print Json +[~] Option to pretty-print Json + [ ] Document JsonEncode() and JsonEncodeValue() +[ ] Update man page for td [~] Simple command line tool to make matrix requests [x] Built on HTTP client API [ ] http man page [~] Simple command line tool for working with JSON - - Like a simpler version of jq - - Should pretty-print Json - - Should be able to query fields for use in shell scripts. + [x] Should pretty-print Json + [ ] Query fields for use in shell scripts. + [ ] json man page [ ] Move configuration to database [ ] Initial configuration diff --git a/src/CanonicalJson.c b/src/CanonicalJson.c index 8f47678..fae0ef1 100644 --- a/src/CanonicalJson.c +++ b/src/CanonicalJson.c @@ -74,7 +74,7 @@ CanonicalJsonEncodeValue(JsonValue * value, FILE * out) fputc(']', out); break; default: - JsonEncodeValue(value, out); + JsonEncodeValue(value, out, JSON_DEFAULT); break; } } diff --git a/src/Db.c b/src/Db.c index 7016d62..7a880a3 100644 --- a/src/Db.c +++ b/src/Db.c @@ -752,7 +752,7 @@ DbUnlock(Db * db, DbRef * ref) return 0; } - JsonEncode(ref->json, ref->fp); + JsonEncode(ref->json, ref->fp, JSON_DEFAULT); fflush(ref->fp); fclose(ref->fp); diff --git a/src/Json.c b/src/Json.c index e34e914..6db732c 100644 --- a/src/Json.c +++ b/src/Json.c @@ -561,7 +561,7 @@ JsonDecodeString(FILE * in) } void -JsonEncodeValue(JsonValue * value, FILE * out) +JsonEncodeValue(JsonValue * value, FILE * out, int level) { size_t i; size_t len; @@ -570,23 +570,30 @@ JsonEncodeValue(JsonValue * value, FILE * out) switch (value->type) { case JSON_OBJECT: - JsonEncode(value->as.object, out); + JsonEncode(value->as.object, out, level >= 0 ? level : level); break; case JSON_ARRAY: arr = value->as.array; len = ArraySize(arr); fputc('[', out); - for (i = 0; i < len; i++) { - JsonEncodeValue(ArrayGet(arr, i), out); + if (level >= 0) + { + fprintf(out, "\n%*s", level + 2, ""); + } + JsonEncodeValue(ArrayGet(arr, i), out, level >= 0 ? level + 2 : level); if (i < len - 1) { fputc(',', out); } } + if (level >= 0) + { + fprintf(out, "\n%*s", level, ""); + } fputc(']', out); break; case JSON_STRING: @@ -617,7 +624,7 @@ JsonEncodeValue(JsonValue * value, FILE * out) } int -JsonEncode(HashMap * object, FILE * out) +JsonEncode(HashMap * object, FILE * out, int level) { size_t index; size_t count; @@ -636,22 +643,46 @@ JsonEncode(HashMap * object, FILE * out) } fputc('{', out); + if (level >= 0) + { + fputc('\n', out); + } index = 0; while (HashMapIterate(object, &key, (void **) &value)) { + if (level >= 0) + { + fprintf(out, "%*s", level + 2, ""); + } + JsonEncodeString(key, out); + fputc(':', out); - JsonEncodeValue(value, out); + if (level >= 0) + { + fputc(' ', out); + } + + JsonEncodeValue(value, out, level >= 0 ? level + 2 : level); if (index < count - 1) { fputc(',', out); } + if (level >= 0) + { + fputc('\n', out); + } + index++; } + if (level >= 0) + { + fprintf(out, "%*s", level, ""); + } fputc('}', out); return 1; diff --git a/src/Matrix.c b/src/Matrix.c index 13d4b12..882d684 100644 --- a/src/Matrix.c +++ b/src/Matrix.c @@ -130,7 +130,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) stream = HttpStream(context); - JsonEncode(response, stream); + JsonEncode(response, stream, JSON_DEFAULT); JsonFree(response); fprintf(stream, "\n"); diff --git a/src/include/Json.h b/src/include/Json.h index a84c4b7..b34816b 100644 --- a/src/include/Json.h +++ b/src/include/Json.h @@ -31,6 +31,9 @@ #include #include +#define JSON_DEFAULT -1 +#define JSON_PRETTY 0 + typedef enum JsonType { JSON_NULL, @@ -96,10 +99,10 @@ extern void JsonEncodeString(const char *, FILE *); extern void - JsonEncodeValue(JsonValue * value, FILE * out); + JsonEncodeValue(JsonValue * value, FILE * out, int); extern int - JsonEncode(HashMap *, FILE *); + JsonEncode(HashMap *, FILE *, int); extern HashMap * JsonDecode(FILE *); diff --git a/tools/src/json.c b/tools/src/json.c index ae8a36f..a010523 100644 --- a/tools/src/json.c +++ b/tools/src/json.c @@ -38,7 +38,7 @@ main(int argc, char **argv) return 1; } - JsonEncode(json, stdout); + JsonEncode(json, stdout, JSON_PRETTY); printf("\n"); return 0;