diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-08-29 21:25:03 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-08-29 21:49:56 +0900 |
| commit | b678465c11517a5342b1ec5aa4fe21570f13a3ed (patch) | |
| tree | 6ef19e02b1cf18f5ac451e6104f6e20b475a6f52 /src/json.h | |
| parent | 8f352553faec69a6f29854898b93be47604aff28 (diff) | |
| download | ducc-b678465c11517a5342b1ec5aa4fe21570f13a3ed.tar.gz ducc-b678465c11517a5342b1ec5aa4fe21570f13a3ed.tar.zst ducc-b678465c11517a5342b1ec5aa4fe21570f13a3ed.zip | |
feat: add utility function to dump any internal object as JSON
Diffstat (limited to 'src/json.h')
| -rw-r--r-- | src/json.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/json.h b/src/json.h new file mode 100644 index 0000000..b542f4a --- /dev/null +++ b/src/json.h @@ -0,0 +1,34 @@ +#ifndef DUCC_JSON_H +#define DUCC_JSON_H + +#include "std.h" + +struct JsonBuilder; +typedef struct JsonBuilder JsonBuilder; + +JsonBuilder* jsonbuilder_new(); +const char* jsonbuilder_get_output(JsonBuilder* b); + +void jsonbuilder_object_start(JsonBuilder* b); +void jsonbuilder_object_end(JsonBuilder* b); +void jsonbuilder_object_member_start(JsonBuilder* b, const char* key); +void jsonbuilder_object_member_end(JsonBuilder* b); + +void jsonbuilder_array_start(JsonBuilder* b); +void jsonbuilder_array_end(JsonBuilder* b); +void jsonbuilder_array_element_start(JsonBuilder* b); +void jsonbuilder_array_element_end(JsonBuilder* b); + +void jsonbuilder_null(JsonBuilder* b); +void jsonbuilder_boolean(JsonBuilder* b, BOOL value); +void jsonbuilder_integer(JsonBuilder* b, int value); +void jsonbuilder_string(JsonBuilder* b, const char* value); + +#define JSON_DUMP(x, f) \ + do { \ + JsonBuilder* _builder = jsonbuilder_new(); \ + f(_builder, (x)); \ + fprintf(stderr, "%s", jsonbuilder_get_output(_builder)); \ + } while (0) + +#endif |
