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_STRUCT 6
54#define FIELD_TYPE_BOOL 7
55
57 void* instance_ptr;
58 int in_array;
59 int in_struct;
60 char* struct_name;
61 char* field_name;
62};
63
64struct json_pos {
65 int line;
66 int col;
67 long offset;
68};
69
70static int json_unmarshal_struct_internal(sstr_t content, struct json_pos* pos,
71 struct json_parse_param* param,
72 sstr_t txt);
73static int json_unmarshal_array_internal(sstr_t content, struct json_pos* pos,
74 struct json_parse_param* param,
75 int* len, sstr_t txt);
76static int json_unmarshal_array_internal_sstr_t(sstr_t content,
77 struct json_pos* pos,
78 sstr_t** ptr, int* ptrlen,
79 sstr_t txt);
80static int json_unmarshal_array_internal_int(sstr_t content,
81 struct json_pos* pos, int** ptr,
82 int* ptrlen, sstr_t txt);
83static int json_unmarshal_array_internal_long(sstr_t content,
84 struct json_pos* pos, long** ptr,
85 int* ptrlen, sstr_t txt);
86static int json_unmarshal_array_internal_float(sstr_t content,
87 struct json_pos* pos,
88 float** ptr, int* ptrlen,
89 sstr_t txt);
90static int json_unmarshal_array_internal_double(sstr_t content,
91 struct json_pos* pos,
92 double** ptr, int* ptrlen,
93 sstr_t txt);
94
95int json_marshal_array_indent_int(int* obj, int len, int indent, int curindent,
96 sstr_t out) {
97 int i;
98 sstr_append_of(out, "[", 1);
99 sstr_append_of_if(out, "\n", 1, indent);
100 curindent += indent;
101 for (i = 0; i < len; i++) {
102 sstr_append_indent(out, curindent);
103 sstr_printf_append(out, "%d", obj[i]);
104 if (i != len - 1) {
105 sstr_append_of(out, ",", 1);
106 }
107 sstr_append_of_if(out, "\n", 1, indent);
108 }
109 curindent -= indent;
110 sstr_append_indent(out, curindent);
111 sstr_append_of(out, "]", 1);
112 return 0;
113}
114
115int json_marshal_array_indent_long(long* obj, int len, int indent,
116 int curindent, sstr_t out) {
117 int i;
118 sstr_append_of(out, "[", 1);
119 sstr_append_of_if(out, "\n", 1, indent);
120 curindent += indent;
121 for (i = 0; i < len; i++) {
122 sstr_append_indent(out, curindent);
123 sstr_printf_append(out, "%l", obj[i]);
124 if (i != len - 1) {
125 sstr_append_of(out, ",", 1);
126 }
127 sstr_append_of_if(out, "\n", 1, indent);
128 }
129 curindent -= indent;
130 sstr_append_indent(out, curindent);
131 sstr_append_of(out, "]", 1);
132 return 0;
133}
134
135int json_marshal_array_indent_float(float* obj, int len, int indent,
136 int curindent, sstr_t out) {
137 int i;
138 sstr_append_of(out, "[", 1);
139 sstr_append_of_if(out, "\n", 1, indent);
140 curindent += indent;
141 for (i = 0; i < len; i++) {
142 sstr_append_indent(out, curindent);
143 sstr_printf_append(out, "%f", (double)obj[i]);
144 if (i != len - 1) {
145 sstr_append_of(out, ",", 1);
146 }
147 sstr_append_of_if(out, "\n", 1, indent);
148 }
149 curindent -= indent;
150 sstr_append_indent(out, curindent);
151 sstr_append_of(out, "]", 1);
152 return 0;
153}
154
155int json_marshal_array_indent_double(double* obj, int len, int indent,
156 int curindent, sstr_t out) {
157 int i;
158 sstr_append_of(out, "[", 1);
159 sstr_append_of_if(out, "\n", 1, indent);
160 curindent += indent;
161 for (i = 0; i < len; i++) {
162 sstr_append_indent(out, curindent);
163 sstr_printf_append(out, "%f", obj[i]);
164 if (i != len - 1) {
165 sstr_append_of(out, ",", 1);
166 }
167 sstr_append_of_if(out, "\n", 1, indent);
168 }
169 curindent -= indent;
170 sstr_append_indent(out, curindent);
171 sstr_append_of(out, "]", 1);
172 return 0;
173}
174
175int json_marshal_array_indent_sstr_t(sstr_t* obj, int len, int indent,
176 int curindent, sstr_t out) {
177 int i;
178 sstr_append_of(out, "[", 1);
179 sstr_append_of_if(out, "\n", 1, indent);
180 curindent += indent;
181 for (i = 0; i < len; i++) {
182 sstr_append_indent(out, curindent);
183 sstr_append_cstr(out, "\"");
184 sstr_json_escape_string_append(out, obj[i]);
185 sstr_append_cstr(out, "\"");
186 if (i != len - 1) {
187 sstr_append_of(out, ",", 1);
188 }
189 sstr_append_of_if(out, "\n", 1, indent);
190 }
191 curindent -= indent;
192 sstr_append_indent(out, curindent);
193 sstr_append_of(out, "]", 1);
194 return 0;
195}
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:226
void sstr_append_of_if(sstr_t s, const void *data, size_t length, int cond)
Append if cond is true, otherwise do nothing.
Definition sstr.c:813
void sstr_append_cstr(sstr_t dst, const char *src)
Extends the sstr_t by appending additional characters contained in src.
Definition sstr.c:172
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:161
void sstr_append_indent(sstr_t s, size_t indent)
append spaces at the end of the sstr_t.
Definition sstr.c:822
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:73
Definition json_parse.h:56
Definition json_parse.h:64
position of string
Definition struct_parse.h:75