-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_internal.h
173 lines (147 loc) · 3.49 KB
/
json_internal.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef JSON_INTERNAL_H
#define JSON_INTERNAL_H
#include <ctype.h>
#include <stdio.h>
// using bit masks to define token types so we can combine
// multiple tokens using | and compare using & bit operators
//
// apparently binary literals are a GCC extension so using
// hex literals instead (to try to be less compiler-specific)
enum _token_e
{
// NULL token, used to initialize
NONE = 0x00, //0b00000000,
// {
OPEN_BODY = 0x01, //0b00000001,
// }
CLOSE_BODY = 0x02, //0b00000010,
// "
QUOTE = 0x04, //0b00000100,
// ,
COMMA = 0x08, //0b00001000,
// :
COLON = 0x10, //0b00010000,
// [A-Za-z] + punctuation
TEXT = 0x20, //0b00100000,
// [0-9] + decimal
NUMERIC = 0x40, //0b01000000
// ' '
SPACE = 0x80, //0b10000000
// [
OPEN_ARRAY = 0x100, //0b100000000
// ]
CLOSE_ARRAY = 0x200, //0b1000000000
// encountered unknown token
UNKNOWN = 0x400, //0b10000000000
};
struct _json_parse_info_t
{
const char* const json_string;
size_t json_string_idx;
char parsed_key[JSON_MAX_KEY_LEN];
char* parsed_value;
size_t parsed_value_len;
size_t parsed_value_capacity;
enum json_type_e parsed_value_type;
bool parsing_key;
bool parsing_value;
bool inside_quotes;
enum _token_e previous_token;
bool expecting_delimiter;
};
enum _token_e
_json_get_token_type(
const char current_char);
size_t
_json_get_key_index(
const struct json_t* const json,
const char* const key,
bool* key_exists);
uint16_t
_json_get_next_expected_token(
const enum _token_e current_token,
const bool inside_quotes);
bool
_json_array_item_cleanup(
const char* const array_string,
size_t* idx,
const size_t len);
char*
_json_fetch_body_string(
const char* const json_string,
size_t* idx);
char*
_json_fetch_array_string(
const char* const json_string,
size_t* idx);
void
_json_reset_parse_info(
struct _json_parse_info_t* const parse_info);
char*
_json_fetch_quote_string(
const char* const array_string,
size_t* idx);
char*
_json_fetch_numeric_string(
const char* const array_string,
size_t* idx,
bool* contains_decimal);
char*
_json_fetch_array_item_string(
const char* const array_string,
size_t* idx,
bool* contains_decimal);
enum json_type_e
_json_get_item_type(
const char* const item_string);
bool
_json_check_key_exists(
const struct json_t* const json,
const char* const search_key);
bool
_json_add_item(
struct json_t* const json,
struct _json_parse_info_t* const parse_info);
void
_json_append_char_to_key(
struct _json_parse_info_t* const parse_info,
const char current_char);
bool
_json_append_char_to_value(
struct _json_parse_info_t* const parse_info,
const char current_char);
bool
_json_perform_token_action(
struct json_t* const json,
const enum _token_e current_token,
const char current_char,
struct _json_parse_info_t* const parse_info);
void
_json_set_item_value(
struct json_item_t* item,
void* value);
void*
_json_get_item_value(
struct json_item_t* const item);
void
_json_deallocate_item(
struct json_item_t* item);
bool
_json_resize_string(
char** to_string,
size_t* capacity);
bool
_json_write_value_buffer_to_string(
char* formatted_buffer,
char** to_string,
size_t* to_string_len,
size_t* to_string_capacity);
bool
_json_value_to_string(
char* formatted_buffer,
size_t max_buffer_len,
const struct json_item_t* const item,
char** to_string,
size_t* to_string_len,
size_t* to_string_capacity);
#endif