src/json.c

Sat, 07 Dec 2024 23:59:54 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 07 Dec 2024 23:59:54 +0100
changeset 1001
5c9ec5a0a4ef
parent 1000
1aecddf7e209
child 1002
1483c47063a8
permissions
-rw-r--r--

change cx_strcat variants to allow handling of ENOMEM

937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include <string.h>
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include <ctype.h>
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
31 #include <assert.h>
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 #include "cx/json.h"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 /*
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 * RFC 8259
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 * https://tools.ietf.org/html/rfc8259
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 */
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 944
diff changeset
40 #define PARSER_READVALUE_ALLOC 32
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
944
c26299cc9897 make both gcc and clang happy with how cx_json_value_nothing is initialized
Mike Becker <universe@uap-core.de>
parents: 943
diff changeset
42 static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING};
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
44 static void token_destroy(CxJsonToken *token) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
45 if (token->alloc > 0) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
46 free((char*) token->content);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
47 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
48 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 static int token_append(CxJsonToken *token, const char *buf, size_t len) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 if (len == 0) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 size_t newlen = token->length + len;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 if (token->alloc < newlen) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 char *newbuf = realloc(
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 token->alloc == 0 ? NULL : (char *) token->content,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 newlen);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 if (!newbuf) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 return 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 token->content = newbuf;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 token->alloc = newlen;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 memcpy((char *) token->content + token->length, buf, len);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 token->length = newlen;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
72 static CxJsonToken token_create(CxJson *json, size_t start, size_t end) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 CxJsonToken token = {0};
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
74 size_t len = end - start;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
75 if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
76 token.content = json->buffer + start;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
77 token.length = len;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 } else {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
79 if (token_append(&json->uncompleted, json->buffer + start, len)) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 // TODO: this does certainly not lead to correct error handling
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 return (CxJsonToken){0};
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
83 token = json->uncompleted;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
85 json->uncompleted = (CxJsonToken){0};
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 return token;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 static int token_isliteral(const char *content, size_t length) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 if (length == 4) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 if (!memcmp(content, "true", 4)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 return 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 } else if (!memcmp(content, "null", 4)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 return 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 } else if (length == 5 && !memcmp(content, "false", 5)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 return 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 static int num_isexp(const char *content, size_t length, size_t pos) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 if (pos >= length) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 int ok = 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 for (size_t i = pos; i < length; i++) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 char c = content[i];
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 if (isdigit(c)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 ok = 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 } else if (i == pos) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 if (!(c == '+' || c == '-')) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 return ok;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 static CxJsonTokenType token_numbertype(const char *content, size_t length) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 if (length == 0) return CX_JSON_TOKEN_ERROR;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 if (content[0] != '-' && !isdigit(content[0])) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 return CX_JSON_TOKEN_ERROR;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 CxJsonTokenType type = CX_JSON_TOKEN_INTEGER;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 for (size_t i = 1; i < length; i++) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 if (content[i] == '.') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 if (type == CX_JSON_TOKEN_NUMBER) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 return CX_JSON_TOKEN_ERROR; // more than one decimal separator
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 type = CX_JSON_TOKEN_NUMBER;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 } else if (content[i] == 'e' || content[i] == 'E') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 return num_isexp(content, length, i + 1) ? CX_JSON_TOKEN_NUMBER : CX_JSON_TOKEN_ERROR;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 } else if (!isdigit(content[i])) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 return CX_JSON_TOKEN_ERROR; // char is not a digit, decimal separator or exponent sep
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 return type;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 static CxJsonTokenType char2ttype(char c) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 switch (c) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 case '[': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 return CX_JSON_TOKEN_BEGIN_ARRAY;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 case '{': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 return CX_JSON_TOKEN_BEGIN_OBJECT;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 case ']': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 return CX_JSON_TOKEN_END_ARRAY;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 case '}': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 return CX_JSON_TOKEN_END_OBJECT;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 case ':': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 return CX_JSON_TOKEN_NAME_SEPARATOR;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 case ',': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 return CX_JSON_TOKEN_VALUE_SEPARATOR;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 case '"': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 return CX_JSON_TOKEN_STRING;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 default: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 if (isspace(c)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 return CX_JSON_TOKEN_SPACE;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 return CX_JSON_NO_TOKEN;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
180 static CxJsonToken token_parse_next(CxJson *json) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 // current token type and start index
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
182 CxJsonTokenType ttype = json->uncompleted.tokentype;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
183 size_t token_start = json->pos;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
185 for (size_t i = json->pos; i < json->size; i++) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
186 char c = json->buffer[i];
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 if (ttype != CX_JSON_TOKEN_STRING) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 // currently non-string token
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 CxJsonTokenType ctype = char2ttype(c); // start of new token?
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 if (ttype == CX_JSON_NO_TOKEN) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 if (ctype == CX_JSON_TOKEN_SPACE) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 continue;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 } else if (ctype == CX_JSON_TOKEN_STRING) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 // begin string
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 ttype = CX_JSON_TOKEN_STRING;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 token_start = i;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 } else if (ctype != CX_JSON_NO_TOKEN) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 // single-char token
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
201 json->pos = i + 1;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 CxJsonToken token = {ctype, NULL, 0, 0};
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 return token;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 ttype = CX_JSON_TOKEN_LITERAL; // number or literal
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 token_start = i;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 // finish token
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 if (ctype != CX_JSON_NO_TOKEN) {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
211 CxJsonToken ret = token_create(json, token_start, i);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
212 if (token_isliteral(ret.content, ret.length)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
213 ret.tokentype = CX_JSON_TOKEN_LITERAL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
214 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
215 ret.tokentype = token_numbertype(ret.content, ret.length);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
216 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
217 json->pos = i;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
218 return ret;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 // currently inside a string
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
223 if (json->tokenizer_escape) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
224 json->tokenizer_escape = false;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
225 } else {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 if (c == '"') {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
227 CxJsonToken ret = token_create(json, token_start, i + 1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 ret.tokentype = CX_JSON_TOKEN_STRING;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
229 json->pos = i + 1;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 return ret;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 } else if (c == '\\') {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
232 json->tokenizer_escape = true;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 if (ttype != CX_JSON_NO_TOKEN) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 // uncompleted token
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
240 size_t uncompeted_len = json->size - token_start;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
241 if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 // current token is uncompleted
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 // save current token content in p->uncompleted
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 CxJsonToken uncompleted;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 uncompleted.tokentype = ttype;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 uncompleted.length = uncompeted_len;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 uncompleted.alloc = uncompeted_len + 16;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 char *tmp = malloc(uncompleted.alloc);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 if (tmp) {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
250 memcpy(tmp, json->buffer + token_start, uncompeted_len);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 uncompleted.content = tmp;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
252 json->uncompleted = uncompleted;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 } else {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
254 json->error = 1;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 // previously we also had an uncompleted token
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 // combine the uncompleted token with the current token
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
259 if (token_append(&json->uncompleted, json->buffer + token_start, uncompeted_len)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
260 json->error = 1;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 CxJsonToken ret = {CX_JSON_NO_TOKEN, NULL, 0, 0};
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 return ret;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
269 static cxmutstr unescape_string(const CxAllocator *a, const char *str, size_t len) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 // TODO: support more escape sequences
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 // we know that the unescaped string will be shorter by at least 2 chars
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 cxmutstr result;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 result.length = 0;
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
274 result.ptr = cxMalloc(a, len - 1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 if (result.ptr == NULL) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 return result;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 bool u = false;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 for (size_t i = 1; i < len - 1; i++) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 char c = str[i];
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 if (u) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 u = false;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 if (c == 'n') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 c = '\n';
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286 } else if (c == 't') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 c = '\t';
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 result.ptr[result.length++] = c;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291 if (c == '\\') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 u = true;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 result.ptr[result.length++] = c;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 result.ptr[result.length] = 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 return result;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302
969
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
303 static int parse_number(const char *str, size_t len, void *value, bool asint) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
304 char *endptr = NULL;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305 char buf[32];
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306 if (len > 30) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 return 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308 }
969
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
309 // TODO: if we can guarantee that we are working on a copied string already, we can avoid this memcpy
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310 memcpy(buf, str, len);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 buf[len] = 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312
969
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
313 if (asint) {
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
314 long long v = strtoll(buf, &endptr, 10);
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
315 *((int64_t*)value) = (int64_t) v;
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
316 } else {
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
317 // TODO: proper JSON spec number parser
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
318 double v = strtod(buf, &endptr);
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
319 *((double*)value) = v;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
321
969
72e5432f6b42 simplify parsing of numbers
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
322 return (endptr != &buf[len]);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
325 static CxJsonValue* create_json_value(CxJson *json, CxJsonValueType type) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
326 CxJsonValue *v = cxMalloc(json->allocator, sizeof(CxJsonValue));
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
327 if (v == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
328 return NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
330
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
331 // initialize the value
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
332 if (type == CX_JSON_ARRAY) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
333 cx_array_initialize_a(json->allocator, v->value.array.array, 16);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
334 if (v->value.array.array == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
335 cxFree(json->allocator, v);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
336 return NULL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
337 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
338 } else if (type == CX_JSON_OBJECT) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
339 cx_array_initialize_a(json->allocator, v->value.object.values, 16);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
340 if (v->value.object.values == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
341 cxFree(json->allocator, v);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
342 return NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
343 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
344 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
345 memset(v, 0, sizeof(CxJsonValue));
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
346 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
347 v->type = type;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
348 v->allocator = json->allocator;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
350 // add the new value to a possible parent
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
351 CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
352 if (json->vbuf_size > 0) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
353 CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
354 if (parent->type == CX_JSON_ARRAY) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
355 cx_array_simple_add_a(&value_realloc, parent->value.array.array, v);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
356 } else if (parent->type == CX_JSON_OBJECT) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
357 assert(parent->value.object.values_size > 0);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
358 assert(parent->value.object.values[parent->value.object.values_size - 1].value == NULL);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
359 parent->value.object.values[parent->value.object.values_size - 1].value = v;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 } else {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
361 assert(false);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
364
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
365 // add the new value to the stack, if it is an array or object
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
366 if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
367 CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
368 if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
369 cxFree(json->allocator, v);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
370 return NULL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
371 }
940
bbf41b9c2658 fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 938
diff changeset
372 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
373
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
374 // if currently no value is parsed, this is now the value of interest
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
375 if (json->parsed == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
376 json->parsed = v;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
378
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
379 return v;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
380 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
381
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
382 static int json_obj_add_entry(CxJson *json, char *name) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
383 CxJsonObjValue kv = {name, NULL};
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
384 assert(json->vbuf_size > 0);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
385 CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
386 assert(parent != NULL);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
387 assert(parent->type == CX_JSON_OBJECT);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
388 CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
389 return cx_array_simple_add_a(&value_realloc, parent->value.object.values, kv);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
390 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
392 #define JP_STATE_VALUE_BEGIN 0
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
393 #define JP_STATE_VALUE_END 10
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
394 #define JP_STATE_VALUE_BEGIN_OBJ 1
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
395 #define JP_STATE_OBJ_SEP_OR_CLOSE 11
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
396 #define JP_STATE_VALUE_BEGIN_AR 2
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
397 #define JP_STATE_ARRAY_SEP_OR_CLOSE 12
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
398 #define JP_STATE_OBJ_NAME_OR_CLOSE 5
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
399 #define JP_STATE_OBJ_NAME 6
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
400 #define JP_STATE_OBJ_COLON 7
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
402 void cxJsonInit(CxJson *json, const CxAllocator *allocator) {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
403 if (allocator == NULL) {
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
404 allocator = cxDefaultAllocator;
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
405 }
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
406
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407 memset(json, 0, sizeof(CxJson));
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
408 json->allocator = allocator;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
409
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 944
diff changeset
410 json->states = json->states_internal;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
411 json->states_capacity = cx_nmemb(json->states_internal);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
412 json->states[0] = JP_STATE_VALUE_BEGIN;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
413 json->states_size = 1;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
414
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
415 json->vbuf = json->vbuf_internal;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
416 json->vbuf_capacity = cx_nmemb(json->vbuf_internal);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
417 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
418
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
419 void cxJsonDestroy(CxJson *json) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
420 if (json->states != json->states_internal) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
421 free(json->states);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 944
diff changeset
422 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
423 if (json->vbuf != json->vbuf_internal) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
424 free(json->vbuf);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
425 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
426 cxJsonValueFree(json->parsed);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
427 json->parsed = NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
428 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
429
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
430 int cxJsonFilln(CxJson *json, const char *buf, size_t size) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
431 // TODO: implement rescue buffer like in CxProperties to allow subsequent fills
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
432 json->buffer = buf;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
433 json->size = size;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
434 json->pos = 0;
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 969
diff changeset
435 return 0;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
436 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
437
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
438 static void json_add_state(CxJson *json, int state) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
439 // we have guaranteed the necessary space with cx_array_simple_reserve()
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
440 // therefore, we can safely add the state in the simplest way possible
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
441 json->states[json->states_size++] = state;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
442 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
443
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
444 #define return_rec(code) \
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
445 token_destroy(&token); \
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
446 return code
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
447
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
448 static int json_parse(CxJson *json) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
449 // Reserve a pointer for a possibly read value
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
450 CxJsonValue *vbuf = NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
451
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
452 // grab the next token
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
453 CxJsonToken token = token_parse_next(json);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
454 if (token.tokentype == CX_JSON_NO_TOKEN) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
455 // nothing found, wait for more data
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
456 return 0;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
457 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
458
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
459 // pop the current state
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
460 assert(json->states_size > 0);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
461 int state = json->states[--json->states_size];
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
462
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
463 // guarantee that at least two more states fit on the stack
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
464 CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
465 if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
466 return -1;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
467 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
468
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
469
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
470 // 0 JP_STATE_VALUE_BEGIN value begin
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
471 // 10 JP_STATE_VALUE_END expect value end
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
472 // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object)
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
473 // 11 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
474 // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array)
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
475 // 12 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
476 // 5 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
477 // 6 JP_STATE_OBJ_NAME object, expect name
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
478 // 7 JP_STATE_OBJ_COLON object, expect ':'
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
479
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
480 if (state < 3) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
481 // push expected end state to the stack
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
482 json_add_state(json, 10 + state);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
483 switch (token.tokentype) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
484 case CX_JSON_TOKEN_BEGIN_ARRAY: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
485 if (create_json_value(json, CX_JSON_ARRAY) == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
486 // TODO: error code - no memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
487 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
488 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
489 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
490 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
491 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
492 case CX_JSON_TOKEN_BEGIN_OBJECT: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
493 if (create_json_value(json, CX_JSON_OBJECT) == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
494 // TODO: error code - no memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
495 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
496 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
497 json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
498 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
499 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
500 case CX_JSON_TOKEN_STRING: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
501 if ((vbuf = create_json_value(json, CX_JSON_STRING)) == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
502 // TODO: error code - no memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
503 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
504 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
505 cxmutstr str = unescape_string(json->allocator, token.content, token.length);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
506 if (str.ptr == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
507 // TODO: error code - no memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
508 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
509 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
510 vbuf->value.string = str;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
511 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
512 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
513 case CX_JSON_TOKEN_INTEGER:
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
514 case CX_JSON_TOKEN_NUMBER: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
515 int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
516 if (NULL == (vbuf = create_json_value(json, type))) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
517 // TODO: error code - no memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
518 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
519 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
520 if (parse_number(token.content, token.length, &vbuf->value,type == CX_JSON_INTEGER)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
521 // TODO: error code - format error
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
522 return_rec(-1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
523 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
524 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
525 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
526 case CX_JSON_TOKEN_LITERAL: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
527 if ((vbuf = create_json_value(json, CX_JSON_LITERAL)) == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
528 // TODO: error code - no memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
529 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
530 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
531 const char *l = token.content;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
532 size_t token_len = token.length;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
533 if (token_len == 4 && !memcmp(l, "true", 4)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
534 vbuf->value.literal = CX_JSON_TRUE;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
535 } else if (token_len == 5 && !memcmp(l, "false", 5)) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
536 vbuf->value.literal = CX_JSON_FALSE;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
537 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
538 vbuf->value.literal = CX_JSON_NULL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
539 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
540 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
541 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
542 default: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
543 // TODO: error code - unexpected token
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
544 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
545 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
546 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
547 } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
548 // expect ',' or ']'
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
549 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
550 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
551 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
552 } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
553 // discard the array from the value buffer
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
554 json->vbuf_size--;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
555 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
556 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
557 // TODO: error code - unexpected token
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
558 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
559 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
560 } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
561 if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
562 // discard the obj from the value buffer
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
563 json->vbuf_size--;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
564 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
565 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
566 // expect string
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
567 if (token.tokentype != CX_JSON_TOKEN_STRING) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
568 // TODO: error code - unexpected token
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
569 return_rec(-1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
570 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
571
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
572 // add new entry
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
573 cxmutstr name = unescape_string(json->allocator, token.content, token.length);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
574 if (name.ptr == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
575 // TODO: error code - no mem
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
576 return_rec(-1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
577 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
578 json_obj_add_entry(json, name.ptr);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
579
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
580 // next state
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
581 json_add_state(json, JP_STATE_OBJ_COLON);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
582 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
583 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
584 } else if (state == JP_STATE_OBJ_COLON) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
585 // expect ':'
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
586 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
587 // TODO: error code - unexpected token
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
588 return_rec(-1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
589 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
590 // next state
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
591 json_add_state(json, JP_STATE_VALUE_BEGIN_OBJ);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
592 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
593 } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
594 // expect ',' or '}'
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
595 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
596 json_add_state(json, JP_STATE_OBJ_NAME);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
597 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
598 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
599 // discard the obj from the value buffer
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
600 json->vbuf_size--;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
601 return_rec(1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
602 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
603 // TODO: error code - unexpected token
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
604 return_rec(-1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
605 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
606 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
607 // should be unreachable
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
608 assert(false);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
609 return_rec(-1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
610 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
611 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
612
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
613 int cxJsonNext(CxJson *json, CxJsonValue **value) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
614 // TODO: replace int with a status enum like in CxProperties
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
615
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
616 // initialize output value
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
617 *value = &cx_json_value_nothing;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
618
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
619 // parse data
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
620 int result;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
621 do {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
622 result = json_parse(json);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
623 if (result == 1 && json->states_size == 1) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
624 // final state reached
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
625 assert(json->states[0] == JP_STATE_VALUE_END);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
626 assert(json->vbuf_size == 0);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
627
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
628 // write output value
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
629 *value = json->parsed;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
630 json->parsed = NULL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
631
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
632 // re-initialize state machine
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
633 json->states[0] = JP_STATE_VALUE_BEGIN;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
634
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
635 return 1;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
636 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
637 } while (result == 1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
638
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
639 return result;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
640 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
641
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
642 void cxJsonValueFree(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
643 if (value == NULL || value == &cx_json_value_nothing) return;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
644
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
645 switch (value->type) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
646 case CX_JSON_OBJECT: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
647 CxJsonObject obj = value->value.object;
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
648 for (size_t i = 0; i < obj.values_size; i++) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
649 cxJsonValueFree(obj.values[i].value);
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
650 cxFree(value->allocator, obj.values[i].name);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
651 }
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
652 cxFree(value->allocator, obj.values);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
653 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
654 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
655 case CX_JSON_ARRAY: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
656 CxJsonArray array = value->value.array;
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
657 for (size_t i = 0; i < array.array_size; i++) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
658 cxJsonValueFree(array.array[i]);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
659 }
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
660 cxFree(value->allocator, array.array);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
661 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
662 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
663 case CX_JSON_STRING: {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
664 cxFree(value->allocator, value->value.string.ptr);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
665 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
666 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
667 default: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
668 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
669 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
670 }
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
671 cxFree(value->allocator, value);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
672 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
673
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
674 CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index) {
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
675 if (index >= value->value.array.array_size) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
676 return &cx_json_value_nothing;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
677 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
678 return value->value.array.array[index];
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
679 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
680
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
681 CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char *name) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
682 CxJsonObject *obj = &(value->value.object);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
683 // TODO: think about sorting the object so that we can use binary search here
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
684 for (size_t i = 0; i < obj->values_size; i++) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
685 // TODO: we might want to store names as cxmutstr
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
686 if (0 == strcmp(name, obj->values[i].name)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
687 return obj->values[i].value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
688 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
689 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
690 return &cx_json_value_nothing;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
691 }

mercurial