-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_utils.h
59 lines (46 loc) · 1.86 KB
/
json_utils.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
#ifndef KAYOS_SRC_JSON_UTILS_H_
#define KAYOS_SRC_JSON_UTILS_H_
#include <forestdb.h>
#include <jansson.h>
typedef enum {
REQUIRED_KEY = 0,
OPTIONAL_KEY = 1,
KEY_NOT_FOUND = 2,
} json_key_type;
#define FOREACH_JSON_ERROR(ERROR) \
ERROR(COMMAND_MISSING) \
ERROR(INVALID_COMMAND) \
ERROR(KEY_REQUIRED) \
#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,
enum KAYOS_JSON_ERROR_ENUM {
FOREACH_JSON_ERROR(GENERATE_ENUM)
};
static const char *KAYOS_JSON_ERROR_STRING[] = {
FOREACH_JSON_ERROR(GENERATE_STRING)
};
void json_print_error(json_error_t error);
void json_fatal_error(const char *msg, json_error_t error);
json_t *make_json_base_error(const char *key);
json_t *new_json_errors();
void print_json_object(FILE *restrict stream, json_t *json);
void print_and_free_json_object(FILE *restrict stream, json_t *json);
void add_expected_json_key_error(json_t *json_errors, const char *key);
void add_unexpected_json_key_error(json_t *json_errors, const char *key);
void add_expected_json_string_error(json_t *json_errors, const char *key);
void add_expected_json_integer_error(json_t *json_errors, const char *key);
void add_custom_json_error(json_t *json_errors,
const char *key, const char *value,
const char *key2, const char *value2);
json_key_type which_key(const char *required_keys[],
const char *optional_keys[],
const char *candidate);
void ensure_json_keys(json_t *json_errors, json_t *json,
const char *required_keys[],
const char *optional_keys[]);
int json_errors_p(json_t *json_errors);
json_t *get_json_string_required(json_t *json_errors, json_t *json, const char *key);
json_t *get_json_integer_required(json_t *json_errors, json_t *json, const char *key);
json_t *get_json_string_optional(json_t *json_errors, json_t *json, const char *key);
json_t *get_json_integer_optional(json_t *json_errors, json_t *json, const char *key);
#endif