forked from lda/telodendria
Implement pretty-printing option in Json.
Telodendria itself doesn't use it, but the json CLI tool does.
This commit is contained in:
parent
cb8c4fceb5
commit
7b22fb02a2
7 changed files with 52 additions and 16 deletions
10
TODO.txt
10
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
|
||||
|
|
|
@ -74,7 +74,7 @@ CanonicalJsonEncodeValue(JsonValue * value, FILE * out)
|
|||
fputc(']', out);
|
||||
break;
|
||||
default:
|
||||
JsonEncodeValue(value, out);
|
||||
JsonEncodeValue(value, out, JSON_DEFAULT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
2
src/Db.c
2
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);
|
||||
|
|
43
src/Json.c
43
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;
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -31,6 +31,9 @@
|
|||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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 *);
|
||||
|
|
|
@ -38,7 +38,7 @@ main(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
JsonEncode(json, stdout);
|
||||
JsonEncode(json, stdout, JSON_PRETTY);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue