Implement pretty-printing option in Json.

Telodendria itself doesn't use it, but the json CLI tool does.
This commit is contained in:
Jordan Bancino 2023-03-08 17:15:43 +00:00
parent cb8c4fceb5
commit 7b22fb02a2
7 changed files with 52 additions and 16 deletions

View file

@ -20,15 +20,17 @@ Milestone: v0.3.0
[ ] HttpClient man page [ ] HttpClient man page
[ ] Uri man page [ ] Uri man page
[ ] Test on other platforms [ ] 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 [~] Simple command line tool to make matrix requests
[x] Built on HTTP client API [x] Built on HTTP client API
[ ] http man page [ ] http man page
[~] Simple command line tool for working with JSON [~] Simple command line tool for working with JSON
- Like a simpler version of jq [x] Should pretty-print Json
- Should pretty-print Json [ ] Query fields for use in shell scripts.
- Should be able to query fields for use in shell scripts. [ ] json man page
[ ] Move configuration to database [ ] Move configuration to database
[ ] Initial configuration [ ] Initial configuration

View file

@ -74,7 +74,7 @@ CanonicalJsonEncodeValue(JsonValue * value, FILE * out)
fputc(']', out); fputc(']', out);
break; break;
default: default:
JsonEncodeValue(value, out); JsonEncodeValue(value, out, JSON_DEFAULT);
break; break;
} }
} }

View file

@ -752,7 +752,7 @@ DbUnlock(Db * db, DbRef * ref)
return 0; return 0;
} }
JsonEncode(ref->json, ref->fp); JsonEncode(ref->json, ref->fp, JSON_DEFAULT);
fflush(ref->fp); fflush(ref->fp);
fclose(ref->fp); fclose(ref->fp);

View file

@ -561,7 +561,7 @@ JsonDecodeString(FILE * in)
} }
void void
JsonEncodeValue(JsonValue * value, FILE * out) JsonEncodeValue(JsonValue * value, FILE * out, int level)
{ {
size_t i; size_t i;
size_t len; size_t len;
@ -570,23 +570,30 @@ JsonEncodeValue(JsonValue * value, FILE * out)
switch (value->type) switch (value->type)
{ {
case JSON_OBJECT: case JSON_OBJECT:
JsonEncode(value->as.object, out); JsonEncode(value->as.object, out, level >= 0 ? level : level);
break; break;
case JSON_ARRAY: case JSON_ARRAY:
arr = value->as.array; arr = value->as.array;
len = ArraySize(arr); len = ArraySize(arr);
fputc('[', out); fputc('[', out);
for (i = 0; i < len; i++) 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) if (i < len - 1)
{ {
fputc(',', out); fputc(',', out);
} }
} }
if (level >= 0)
{
fprintf(out, "\n%*s", level, "");
}
fputc(']', out); fputc(']', out);
break; break;
case JSON_STRING: case JSON_STRING:
@ -617,7 +624,7 @@ JsonEncodeValue(JsonValue * value, FILE * out)
} }
int int
JsonEncode(HashMap * object, FILE * out) JsonEncode(HashMap * object, FILE * out, int level)
{ {
size_t index; size_t index;
size_t count; size_t count;
@ -636,22 +643,46 @@ JsonEncode(HashMap * object, FILE * out)
} }
fputc('{', out); fputc('{', out);
if (level >= 0)
{
fputc('\n', out);
}
index = 0; index = 0;
while (HashMapIterate(object, &key, (void **) &value)) while (HashMapIterate(object, &key, (void **) &value))
{ {
if (level >= 0)
{
fprintf(out, "%*s", level + 2, "");
}
JsonEncodeString(key, out); JsonEncodeString(key, out);
fputc(':', out); fputc(':', out);
JsonEncodeValue(value, out); if (level >= 0)
{
fputc(' ', out);
}
JsonEncodeValue(value, out, level >= 0 ? level + 2 : level);
if (index < count - 1) if (index < count - 1)
{ {
fputc(',', out); fputc(',', out);
} }
if (level >= 0)
{
fputc('\n', out);
}
index++; index++;
} }
if (level >= 0)
{
fprintf(out, "%*s", level, "");
}
fputc('}', out); fputc('}', out);
return 1; return 1;

View file

@ -130,7 +130,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
stream = HttpStream(context); stream = HttpStream(context);
JsonEncode(response, stream); JsonEncode(response, stream, JSON_DEFAULT);
JsonFree(response); JsonFree(response);
fprintf(stream, "\n"); fprintf(stream, "\n");

View file

@ -31,6 +31,9 @@
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#define JSON_DEFAULT -1
#define JSON_PRETTY 0
typedef enum JsonType typedef enum JsonType
{ {
JSON_NULL, JSON_NULL,
@ -96,10 +99,10 @@ extern void
JsonEncodeString(const char *, FILE *); JsonEncodeString(const char *, FILE *);
extern void extern void
JsonEncodeValue(JsonValue * value, FILE * out); JsonEncodeValue(JsonValue * value, FILE * out, int);
extern int extern int
JsonEncode(HashMap *, FILE *); JsonEncode(HashMap *, FILE *, int);
extern HashMap * extern HashMap *
JsonDecode(FILE *); JsonDecode(FILE *);

View file

@ -38,7 +38,7 @@ main(int argc, char **argv)
return 1; return 1;
} }
JsonEncode(json, stdout); JsonEncode(json, stdout, JSON_PRETTY);
printf("\n"); printf("\n");
return 0; return 0;