summaryrefslogtreecommitdiff
path: root/include/json.h
blob: db508dff1f92f1ce8c71e2f923c5813b59992ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);