json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
json_parse.h
1
2/*
3TOKEN_STRING = "([^"] | \")*"
4TOKEN_INT = [0-9]+
5TOKEN_FLOAT = [0-9]*\.[0-9]+
6TOKEN_TRUE = true
7TOKEN_FALSE = false
8TOKEN_NULL = null
9TOKEN_COMMA = ,
10TOKEN_COLON = :
11TOKEN_LEFT_BRACE = {
12TOKEN_RIGHT_BRACE = }
13TOKEN_LEFT_BRACKET = [
14TOKEN_RIGHT_BRACKET = ]
15
16---------------
17
18JSON = STRUCT | ARRAY
19STRUCT = { FIELD_LIST }
20FIELD_LIST = FIELD | FIELD, FIELD_LIST | empty
21FIELD = FIELD_KEY : FIELD_VALUE
22FIELD_KEY = TOKEN_STRING
23FIELD_VALUE = SCALAR_VALUE | STRUCT | ARRAY
24SCALAR_VALUE = TOKEN_STRING | TOKEN_FLOAT | TOKEN_INT | TOKEN_BOOL | TOKEN_NULL
25ELEMENT_LIST = FIELD_VALUE | FIELD_VALUE, ELEMENT_LIST | empty
26ARRAY = [ ELEMENT_LIST ]
27*/
28
29enum json_token {
30 JSON_ERROR = -1,
31 JSON_TOKEN_EOF = 0,
32 JSON_TOKEN_STRING,
33 JSON_TOKEN_INT,
34 JSON_TOKEN_FLOAT,
35 JSON_TOKEN_TRUE,
36 JSON_TOKEN_FALSE,
37 JSON_TOKEN_NULL,
38 JSON_TOKEN_COMMA = ',', // ,
39 JSON_TOKEN_COLON = ':', // :
40 JSON_TOKEN_LEFT_BRACE = '{', // {
41 JSON_TOKEN_RIGHT_BRACE = '}', // }
42 JSON_TOKEN_LEFT_BRACKET = '[', // [
43 JSON_TOKEN_RIGHT_BRACKET = ']', // ]
44};
45
46// field type id
47// !NOTE: MUST SAME AS IN src/struct/struct_parse.h
48#define FIELD_TYPE_INT 0
49#define FIELD_TYPE_LONG 1
50#define FIELD_TYPE_FLOAT 2
51#define FIELD_TYPE_DOUBLE 3
52#define FIELD_TYPE_SSTR 4
53#define FIELD_TYPE_ENUM 5
54#define FIELD_TYPE_STRUCT 6
55#define FIELD_TYPE_BOOL 7
56#define FIELD_TYPE_MAP 8
57#define FIELD_TYPE_INT8 9
58#define FIELD_TYPE_INT16 10
59#define FIELD_TYPE_INT32 11
60#define FIELD_TYPE_INT64 12
61#define FIELD_TYPE_UINT8 13
62#define FIELD_TYPE_UINT16 14
63#define FIELD_TYPE_UINT32 15
64#define FIELD_TYPE_UINT64 16
65#define FIELD_TYPE_ONEOF 17
66
67#ifndef JSON_MAX_DEPTH
68#define JSON_MAX_DEPTH 256
69#endif
70
71#include <stdint.h>
72
73#ifndef JSON_NESTED_MASK_DEFINED
74#define JSON_NESTED_MASK_DEFINED
76 int field_index;
77 const uint64_t* mask;
78 int mask_word_count;
79 const struct json_nested_mask* sub_masks;
80 int sub_mask_count;
81};
82#endif
83
85 void* instance_ptr;
86 int in_array;
87 int in_struct;
88 int depth;
89 const char* struct_name;
90 const char* field_name;
91 const uint64_t* field_mask;
92 int field_mask_word_count;
93 const struct json_nested_mask* nested_masks;
94 int nested_mask_count;
95};
96
97struct json_pos {
98 int line;
99 int col;
100 long offset;
101};
102
103static int json_next_token_(sstr_t content, struct json_pos* pos, sstr_t txt);
104static int json_next_token(sstr_t content, struct json_pos* pos, sstr_t txt);
105static int json_unmarshal_struct_internal(sstr_t content, struct json_pos* pos,
106 struct json_parse_param* param,
107 sstr_t txt);
108static int json_unmarshal_array_internal(sstr_t content, struct json_pos* pos,
109 struct json_parse_param* param,
110 int* len, sstr_t txt);
111static int json_unmarshal_array_internal_sstr_t(sstr_t content,
112 struct json_pos* pos,
113 sstr_t** ptr, int* ptrlen,
114 sstr_t txt);
115static int json_unmarshal_array_internal_int(sstr_t content,
116 struct json_pos* pos, int** ptr,
117 int* ptrlen, sstr_t txt);
118static int json_unmarshal_array_internal_long(sstr_t content,
119 struct json_pos* pos, long** ptr,
120 int* ptrlen, sstr_t txt);
121static int json_unmarshal_array_internal_float(sstr_t content,
122 struct json_pos* pos,
123 float** ptr, int* ptrlen,
124 sstr_t txt);
125static int json_unmarshal_array_internal_double(sstr_t content,
126 struct json_pos* pos,
127 double** ptr, int* ptrlen,
128 sstr_t txt);
129static int json_unmarshal_array_internal_int8_t(sstr_t content,
130 struct json_pos* pos,
131 int8_t** ptr, int* ptrlen,
132 sstr_t txt);
133static int json_unmarshal_array_internal_int16_t(sstr_t content,
134 struct json_pos* pos,
135 int16_t** ptr, int* ptrlen,
136 sstr_t txt);
137static int json_unmarshal_array_internal_int32_t(sstr_t content,
138 struct json_pos* pos,
139 int32_t** ptr, int* ptrlen,
140 sstr_t txt);
141static int json_unmarshal_array_internal_int64_t(sstr_t content,
142 struct json_pos* pos,
143 int64_t** ptr, int* ptrlen,
144 sstr_t txt);
145static int json_unmarshal_array_internal_uint8_t(sstr_t content,
146 struct json_pos* pos,
147 uint8_t** ptr, int* ptrlen,
148 sstr_t txt);
149static int json_unmarshal_array_internal_uint16_t(sstr_t content,
150 struct json_pos* pos,
151 uint16_t** ptr, int* ptrlen,
152 sstr_t txt);
153static int json_unmarshal_array_internal_uint32_t(sstr_t content,
154 struct json_pos* pos,
155 uint32_t** ptr, int* ptrlen,
156 sstr_t txt);
157static int json_unmarshal_array_internal_uint64_t(sstr_t content,
158 struct json_pos* pos,
159 uint64_t** ptr, int* ptrlen,
160 sstr_t txt);
161static int json_unmarshal_oneof_internal(sstr_t content, struct json_pos* pos,
162 void* instance,
163 const char* tag_field,
164 const char** variant_names,
165 const char** variant_struct_names,
166 int variant_count,
167 int tag_offset, int value_offset,
168 int depth, sstr_t txt);
169static int json_unmarshal_array_internal_oneof(
170 sstr_t content, struct json_pos* pos,
171 void** arr_pp, int* ptrlen, int element_size,
172 const char* tag_field,
173 const char** variant_names,
174 const char** variant_struct_names,
175 int variant_count,
176 int tag_offset, int value_offset,
177 int depth, sstr_t txt);
178static int json_unmarshal_ignore_value(sstr_t content, struct json_pos* pos,
179 sstr_t txt);
180
181int json_marshal_array_indent_int(int* obj, int len, int indent, int curindent,
182 sstr_t out) {
183 int i;
184 sstr_append_of(out, "[", 1);
185 sstr_append_of_if(out, "\n", 1, indent);
186 curindent += indent;
187 for (i = 0; i < len; i++) {
188 sstr_append_indent(out, curindent);
189 sstr_printf_append(out, "%d", obj[i]);
190 if (i != len - 1) {
191 sstr_append_of(out, ",", 1);
192 }
193 sstr_append_of_if(out, "\n", 1, indent);
194 }
195 curindent -= indent;
196 sstr_append_indent(out, curindent);
197 sstr_append_of(out, "]", 1);
198 return 0;
199}
200
201int json_marshal_array_indent_long(long* obj, int len, int indent,
202 int curindent, sstr_t out) {
203 int i;
204 sstr_append_of(out, "[", 1);
205 sstr_append_of_if(out, "\n", 1, indent);
206 curindent += indent;
207 for (i = 0; i < len; i++) {
208 sstr_append_indent(out, curindent);
209 sstr_printf_append(out, "%l", obj[i]);
210 if (i != len - 1) {
211 sstr_append_of(out, ",", 1);
212 }
213 sstr_append_of_if(out, "\n", 1, indent);
214 }
215 curindent -= indent;
216 sstr_append_indent(out, curindent);
217 sstr_append_of(out, "]", 1);
218 return 0;
219}
220
221int json_marshal_array_indent_float(float* obj, int len, int indent,
222 int curindent, sstr_t out) {
223 int i;
224 sstr_append_of(out, "[", 1);
225 sstr_append_of_if(out, "\n", 1, indent);
226 curindent += indent;
227 for (i = 0; i < len; i++) {
228 sstr_append_indent(out, curindent);
229 sstr_append_float_str(out, obj[i], -1);
230 if (i != len - 1) {
231 sstr_append_of(out, ",", 1);
232 }
233 sstr_append_of_if(out, "\n", 1, indent);
234 }
235 curindent -= indent;
236 sstr_append_indent(out, curindent);
237 sstr_append_of(out, "]", 1);
238 return 0;
239}
240
241int json_marshal_array_indent_double(double* obj, int len, int indent,
242 int curindent, sstr_t out) {
243 int i;
244 sstr_append_of(out, "[", 1);
245 sstr_append_of_if(out, "\n", 1, indent);
246 curindent += indent;
247 for (i = 0; i < len; i++) {
248 sstr_append_indent(out, curindent);
249 sstr_append_double_str(out, obj[i], -1);
250 if (i != len - 1) {
251 sstr_append_of(out, ",", 1);
252 }
253 sstr_append_of_if(out, "\n", 1, indent);
254 }
255 curindent -= indent;
256 sstr_append_indent(out, curindent);
257 sstr_append_of(out, "]", 1);
258 return 0;
259}
260
261int json_marshal_array_indent_sstr_t(sstr_t* obj, int len, int indent,
262 int curindent, sstr_t out) {
263 int i;
264 sstr_append_of(out, "[", 1);
265 sstr_append_of_if(out, "\n", 1, indent);
266 curindent += indent;
267 for (i = 0; i < len; i++) {
268 sstr_append_indent(out, curindent);
269 sstr_append_cstr(out, "\"");
271 sstr_append_cstr(out, "\"");
272 if (i != len - 1) {
273 sstr_append_of(out, ",", 1);
274 }
275 sstr_append_of_if(out, "\n", 1, indent);
276 }
277 curindent -= indent;
278 sstr_append_indent(out, curindent);
279 sstr_append_of(out, "]", 1);
280 return 0;
281}
282
283#define DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(TYPE, APPEND_FN, CAST) \
284int json_marshal_array_indent_##TYPE(TYPE* obj, int len, int indent, \
285 int curindent, sstr_t out) { \
286 int i; \
287 sstr_append_of(out, "[", 1); \
288 sstr_append_of_if(out, "\n", 1, indent); \
289 curindent += indent; \
290 for (i = 0; i < len; i++) { \
291 sstr_append_indent(out, curindent); \
292 APPEND_FN(out, (CAST)obj[i]); \
293 if (i != len - 1) { \
294 sstr_append_of(out, ",", 1); \
295 } \
296 sstr_append_of_if(out, "\n", 1, indent); \
297 } \
298 curindent -= indent; \
299 sstr_append_indent(out, curindent); \
300 sstr_append_of(out, "]", 1); \
301 return 0; \
302}
303
304DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(int8_t, sstr_append_int_str, int)
305DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(int16_t, sstr_append_int_str, int)
306DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(int32_t, sstr_append_int_str, int)
307DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(int64_t, sstr_append_long_str, long)
308DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(uint8_t, sstr_append_int_str, int)
309DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(uint16_t, sstr_append_int_str, int)
310DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(uint32_t, sstr_append_uint32_str, uint32_t)
311DEFINE_MARSHAL_ARRAY_INDENT_INTTYPE(uint64_t, sstr_append_uint64_str, uint64_t)
void sstr_append_uint32_str(sstr_t s, uint32_t u)
Append the decimal string representation of a uint32_t to a string.
Definition sstr.c:733
sstr_t sstr_printf_append(sstr_t buf, const char *fmt,...)
Same as sstr_printf(), but but print to buf instead of create a new one.
Definition sstr.c:279
void sstr_append_uint64_str(sstr_t s, uint64_t u)
Append the decimal string representation of a uint64_t to a string.
Definition sstr.c:742
void sstr_append_float_str(sstr_t s, float f, int precission)
Append the string representation of a float to a string.
Definition sstr.c:751
int sstr_json_escape_string_append(sstr_t out, sstr_t in)
Escape a string for JSON output and append it to a buffer.
Definition sstr.c:818
void sstr_append_long_str(sstr_t s, long l)
Append the decimal string representation of a long to a string.
Definition sstr.c:712
void sstr_append_double_str(sstr_t s, double f, int precision)
Append the string representation of a double to a string.
Definition sstr.c:767
void sstr_append_cstr(sstr_t dst, const char *src)
Extends the sstr_t by appending additional characters contained in src.
Definition sstr.c:225
void sstr_append_of(sstr_t s, const void *data, size_t length)
Extends the sstr_t by appending additional characters in data with length of length at the end of its...
Definition sstr.c:180
void sstr_append_indent(sstr_t s, size_t indent)
append spaces at the end of the sstr_t.
Definition sstr.c:896
void sstr_append_of_if(sstr_t s, const void *data, size_t length, bool cond)
Append if cond is true, otherwise do nothing.
Definition sstr.c:890
void sstr_append_int_str(sstr_t s, int i)
Append the decimal string representation of an int to a string.
Definition sstr.c:656
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:75
Definition json_parse.h:75
Definition json_parse.h:84
Definition json_parse.h:97
position of string
Definition struct_parse.h:175