-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.h
79 lines (62 loc) · 1.93 KB
/
json.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* xml2json
*
* Copyright (c) 2018 Partha Susarla <[email protected]>
*/
#ifndef XML2JSON_JSON_H_
#define XML2JSON_JSON_H_
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
JSON_NULL,
JSON_BOOL,
JSON_STRING,
JSON_NUMBER,
JSON_ARRAY,
JSON_OBJECT,
} JsonType;
typedef struct _JsonObject JsonObject;
struct _JsonObject {
JsonObject *parent;
JsonObject *prev;
JsonObject *next;
char *key;
JsonType type;
union {
bool bool_; /* JSON_BOOL */
char *str_; /* JSON_STRING */
double num_; /* JSON_NUMBER */
struct { /* JSON_ARRAY * JSON_OBJECT */
JsonObject *head;
JsonObject *tail;
} children;
};
};
extern char *json_encode(JsonObject *obj);
extern JsonObject *json_null_obj(void);
extern JsonObject *json_bool_obj(bool b);
extern JsonObject *json_string_obj(const char *str);
extern JsonObject *json_num_obj(double num);
extern JsonObject *json_array_obj(void);
extern JsonObject *json_new(void);
extern void json_free(JsonObject *obj);
/* array handler */
extern void json_append_to_array(JsonObject *array, JsonObject *element);
extern void json_prepend_to_array(JsonObject *array, JsonObject *element);
/* object handler */
extern void json_append_member(JsonObject *object, const char *key,
JsonObject *value);
extern void json_prepend_member(JsonObject *object, const char *key,
JsonObject *value);
extern bool json_validate(JsonObject *object);
/* Iterators */
extern JsonObject *json_first_child(JsonObject *object);
#define json_foreach(i, object) \
for ((i) = json_first_child(object); \
(i) != NULL; \
(i) = (i)->next)
#ifdef __cplusplus
}
#endif
#endif /* XML2JSON_JSON_H_ */