4 * Simple JSON stream parser
6 * Copyright (c) 2018, SUSE LLC
8 * This program is licensed under the BSD license, read LICENSE.BSD
9 * for further information
16 #include "solv_jsonparser.h"
19 jsonparser_init(struct solv_jsonparser *jp, FILE *fp)
21 memset(jp, 0, sizeof(*jp));
24 jp->line = jp->nextline = 1;
26 queue_init(&jp->stateq);
30 jsonparser_free(struct solv_jsonparser *jp)
33 queue_free(&jp->stateq);
37 savec(struct solv_jsonparser *jp, char c)
39 if (jp->nspace == jp->aspace)
42 jp->space = solv_realloc(jp->space, jp->aspace);
44 jp->space[jp->nspace++] = c;
48 saveutf8(struct solv_jsonparser *jp, int c)
56 i = c < 0x800 ? 1 : c < 0x10000 ? 2 : 3;
57 savec(jp, (0x1f80 >> i) | (c >> (6 * i)));
59 savec(jp, 0x80 | ((c >> (6 * i)) & 0x3f));
63 nextc(struct solv_jsonparser *jp)
72 skipspace(struct solv_jsonparser *jp)
76 while (c == ' ' || c == '\t' || c == '\r' || c == '\n')
78 jp->line = jp->nextline;
83 parseliteral(struct solv_jsonparser *jp, int c)
85 size_t nspace = jp->nspace;
90 if (c < 'a' || c > 'z')
96 if (!strcmp(jp->space + nspace, "true"))
98 if (!strcmp(jp->space + nspace, "false"))
100 if (!strcmp(jp->space + nspace, "null"))
106 parsenumber(struct solv_jsonparser *jp, int c)
112 if ((c < '0' || c > '9') && c != '+' && c != '-' && c != '.' && c != 'e' && c != 'E')
122 parseutf8(struct solv_jsonparser *jp, int surrogate)
125 /* parse 4-digit hex */
126 for (i = 0; i < 4; i++)
129 if (c >= '0' && c <= '9')
131 else if (c >= 'a' && c <= 'f')
133 else if (c >= 'A' && c <= 'F')
139 if (!surrogate && r >= 0xd800 && r < 0xdc00)
141 /* utf16 surrogate pair encodes 0x10000 - 0x10ffff */
143 if (nextc(jp) != '\\' || nextc(jp) != 'u' || (r2 = parseutf8(jp, 1)) < 0xdc00 || r2 >= 0xe000)
145 r = 0x10000 + ((r & 0x3ff) << 10 | (r2 & 0x3ff));
151 parsestring(struct solv_jsonparser *jp)
156 if ((c = nextc(jp)) < 32)
162 switch (c = nextc(jp))
185 if ((c = parseutf8(jp, 0)) < 0)
200 parsevalue(struct solv_jsonparser *jp)
202 int c = skipspace(jp);
204 return parsestring(jp);
205 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
206 return parsenumber(jp, c);
207 if ((c >= 'a' && c <= 'z'))
208 return parseliteral(jp, c);
216 return JP_OBJECT_END;
221 jsonparser_parse(struct solv_jsonparser *jp)
226 jp->depth = jp->stateq.count;
227 jp->key = jp->value = 0;
228 jp->keylen = jp->valuelen = 0;
229 nspace = jp->nspace = 0;
231 if (jp->state == JP_END)
233 if (jp->state == JP_START)
235 type = parsevalue(jp);
238 if (type == JP_OBJECT_END || type == JP_ARRAY_END)
240 if (jp->state != type - 1)
242 jp->state = queue_pop(&jp->stateq);
244 else if (jp->state == JP_OBJECT)
247 if (type != JP_STRING)
249 if (skipspace(jp) != ':')
251 type = parsevalue(jp);
252 if (type == JP_OBJECT_END || type == JP_ARRAY_END)
255 jp->keylen = nspace - 1;
257 if (type == JP_STRING || type == JP_NUMBER || type == JP_BOOL || type == JP_NULL)
259 jp->value = jp->space + nspace;
260 jp->valuelen = jp->nspace - nspace - 1;
262 if (type == JP_OBJECT || type == JP_ARRAY)
264 queue_push(&jp->stateq, jp->state);
267 else if (jp->state == JP_OBJECT || jp->state == JP_ARRAY)
269 int c = skipspace(jp);
270 if (c == (jp->state == JP_OBJECT ? '}' : ']'))
279 jsonparser_skip(struct solv_jsonparser *jp, int type)
281 if (type == JP_ARRAY || type == JP_OBJECT)
283 int depth = jp->depth + 1, endtype = type + 1;
284 while (type > 0 && (type != endtype || jp->depth != depth))
285 type = jsonparser_parse(jp);