master
h 80 lines 2.47 KB
Raw
1 #ifndef CHECKIN_JSON_H
2 #define CHECKIN_JSON_H 1
3
4 #if ENABLE_JSONC
5 #include <json-c/json.h>
6 // fix an older json-c bug
7 // https://github.com/json-c/json-c/issues/135
8 #ifdef error_description
9 #undef error_description
10 #endif // error_description
11 #endif // ENABLE_JSONC
12
13 #include "libnetdata/json/vendored/jsmn.h"
14
15 //https://www.ibm.com/support/knowledgecenter/en/SS9H2Y_7.6.0/com.ibm.dp.doc/json_parserlimits.html
16 #define JSON_NAME_LEN 256
17 #define JSON_FULLNAME_LEN 1024
18
19 typedef enum {
20 JSON_OBJECT = 0,
21 JSON_ARRAY = 1,
22 JSON_STRING = 2,
23 JSON_NUMBER = 3,
24 JSON_BOOLEAN = 4,
25 JSON_NULL = 5,
26 } JSON_ENTRY_TYPE;
27
28 typedef struct json_entry {
29 JSON_ENTRY_TYPE type;
30 char name[JSON_NAME_LEN + 1];
31 char fullname[JSON_FULLNAME_LEN + 1];
32 union {
33 char *string; // type == JSON_STRING
34 NETDATA_DOUBLE number; // type == JSON_NUMBER
35 int boolean; // type == JSON_BOOLEAN
36 size_t items; // type == JSON_ARRAY
37 } data;
38 size_t pos; // the position of this item in its parent
39
40 char *original_string;
41
42 void *callback_data;
43 int (*callback_function)(struct json_entry *);
44 } JSON_ENTRY;
45
46 // ----------------------------------------------------------------------------
47 // public functions
48
49 #define JSON_OK 0
50 #define JSON_CANNOT_DOWNLOAD 1
51 #define JSON_CANNOT_PARSE 2
52
53 int json_parse(char *js, void *callback_data, int (*callback_function)(JSON_ENTRY *));
54
55
56 // ----------------------------------------------------------------------------
57 // private functions
58
59 #ifdef ENABLE_JSONC
60 json_object *json_tokenise(char *js);
61 size_t json_walk(json_object *t, void *callback_data, int (*callback_function)(struct json_entry *));
62 #else
63 jsmntok_t *json_tokenise(char *js, size_t len, size_t *count);
64 size_t json_walk_tree(char *js, jsmntok_t *t, void *callback_data, int (*callback_function)(struct json_entry *));
65 #endif
66
67 size_t json_walk_object(char *js, jsmntok_t *t, size_t nest, size_t start, JSON_ENTRY *e);
68 size_t json_walk_array(char *js, jsmntok_t *t, size_t nest, size_t start, JSON_ENTRY *e);
69 size_t json_walk_string(char *js, jsmntok_t *t, size_t start, JSON_ENTRY *e);
70 size_t json_walk_primitive(char *js, jsmntok_t *t, size_t start, JSON_ENTRY *e);
71
72 int json_callback_print(JSON_ENTRY *e);
73
74 static inline void cleanup_json_object_pp(struct json_object **jobj) {
75 if(*jobj)
76 json_object_put(*jobj);
77 }
78 #define CLEAN_JSON_OBJECT _cleanup_(cleanup_json_object_pp) struct json_object
79
80 #endif // CHECKIN_JSON_H