summaryrefslogtreecommitdiff
path: root/include/json.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/json.h')
-rw-r--r--include/json.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/include/json.h b/include/json.h
new file mode 100644
index 0000000..db508df
--- /dev/null
+++ b/include/json.h
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#define JSON_MAX_DEPTH 8
+
+typedef enum {
+ JSON_CONTEXT_OBJECT,
+ JSON_CONTEXT_ARRAY
+} json_context_t;
+
+typedef struct {
+ char *data;
+ size_t length;
+ size_t capacity;
+ json_context_t context_stack[JSON_MAX_DEPTH];
+ int context_depth;
+ bool needs_comma[JSON_MAX_DEPTH];
+} json_t;
+
+void json_init(json_t *buf);
+void json_free(json_t *buf);
+const char *json_get(json_t *buf);
+
+void json_start_object(json_t *buf);
+void json_end_object(json_t *buf);
+void json_set_needs_comma(json_t *buf);
+bool json_needs_comma(json_t *buf);
+
+void json_append(json_t *buf, const char *fmt, ...);
+void json_escape_string(json_t *buf, const char *input);
+
+void json_add_string(json_t *buf, const char *key, const char *value);
+void json_add_string_or_null(json_t *buf, const char *key, const char *value);
+void json_add_int(json_t *buf, const char *key, int value);
+void json_add_int64(json_t *buf, const char *key, int64_t value);
+void json_add_uint64(json_t *buf, const char *key, uint64_t value);
+void json_add_double(json_t *buf, const char *key, double value);
+void json_add_bool(json_t *buf, const char *key, bool value);
+void json_add_null(json_t *buf, const char *key);
+
+void json_start_array(json_t *buf);
+void json_end_array(json_t *buf);
+void json_add_array_start(json_t *buf, const char *key);
+
+void json_array_add_string(json_t *buf, const char *value);
+void json_array_add_int(json_t *buf, int value);
+void json_array_add_int64(json_t *buf, int64_t value);
+void json_array_add_uint64(json_t *buf, uint64_t value);
+void json_array_add_double(json_t *buf, double value);
+void json_array_add_bool(json_t *buf, bool value);
+void json_array_add_null(json_t *buf);