Tue, 29 Oct 2024 16:01:10 +0100
fix compile regression on some platforms after removing sys/types.h include
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> |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include "cx/json.h" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | #include "cx/allocator.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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | #define PARSER_STATES_ALLOC 32 |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
41 | #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
|
42 | |
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
|
43 | 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
|
44 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | 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
|
47 | if (len == 0) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | return 0; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | 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
|
52 | if (token->alloc < newlen) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | char *newbuf = realloc( |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | 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
|
55 | newlen); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | if (!newbuf) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | token->content = newbuf; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | token->alloc = newlen; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } |
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 | 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
|
64 | token->length = newlen; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | return 0; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | static CxJsonToken get_content(CxJson *p, size_t start, size_t end) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | CxJsonToken token = {0}; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | size_t part2 = end - start; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | if (p->uncompleted.tokentype == CX_JSON_NO_TOKEN) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | token.content = p->buffer + start; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | token.length = part2; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | } else if (part2 == 0) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | token = p->uncompleted; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | if (token_append(&p->uncompleted, p->buffer + start, end - start)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | // 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
|
79 | return (CxJsonToken){0}; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | token = p->uncompleted; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | p->uncompleted = (CxJsonToken){0}; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | return token; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | 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
|
88 | if (length == 4) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | if (!memcmp(content, "true", 4)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | } 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
|
92 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | } 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
|
95 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | return 0; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | 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
|
101 | if (pos >= length) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | int ok = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | 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
|
107 | char c = content[i]; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | if (isdigit(c)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | ok = 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | } else if (i == pos) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | if (!(c == '+' || c == '-')) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | } |
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 | return ok; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | 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
|
123 | 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
|
124 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | 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
|
126 | return CX_JSON_TOKEN_ERROR; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | 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
|
130 | 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
|
131 | if (content[i] == '.') { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | 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
|
133 | 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
|
134 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
135 | type = CX_JSON_TOKEN_NUMBER; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | } 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
|
137 | 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
|
138 | } else if (!isdigit(content[i])) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | 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
|
140 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | } |
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 | return type; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | static CxJsonToken get_token(CxJson *p, size_t start, size_t end) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
147 | CxJsonToken token = get_content(p, start, end); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
148 | if (token_isliteral(token.content, token.length)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
149 | token.tokentype = CX_JSON_TOKEN_LITERAL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
150 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | token.tokentype = token_numbertype(token.content, token.length); |
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 | p->pos = end; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
154 | return token; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | static CxJsonTokenType char2ttype(char c) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
158 | switch (c) { |
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_BEGIN_ARRAY; |
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_BEGIN_OBJECT; |
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_END_ARRAY; |
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_END_OBJECT; |
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 | case ':': { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
172 | 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
|
173 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | case ',': { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | 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
|
176 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
177 | case '"': { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
178 | return CX_JSON_TOKEN_STRING; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | default: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | if (isspace(c)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
182 | return CX_JSON_TOKEN_SPACE; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
183 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
184 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
185 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | return CX_JSON_NO_TOKEN; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | static CxJsonToken json_parser_next_token(CxJson *p) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | // current token type and start index |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
191 | CxJsonTokenType ttype = p->uncompleted.tokentype; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
192 | size_t token_start = p->pos; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
194 | for (size_t i = p->pos; i < p->size; i++) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | char c = p->buffer[i]; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | 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
|
197 | // currently non-string token |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | 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
|
200 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
201 | 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
|
202 | 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
|
203 | continue; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | } 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
|
205 | // begin string |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
206 | ttype = CX_JSON_TOKEN_STRING; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
207 | token_start = i; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
208 | } 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
|
209 | // single-char token |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | p->pos = i + 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
211 | 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
|
212 | return token; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
213 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
214 | 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
|
215 | token_start = i; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
216 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
217 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
218 | // finish token |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
219 | 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
|
220 | return get_token(p, token_start, i); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
221 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
222 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
223 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
224 | // currently inside a string |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
225 | if (!p->tokenizer_escape) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
226 | if (c == '"') { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
227 | CxJsonToken ret = get_content(p, token_start, i + 1); |
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; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
229 | p->pos = i + 1; |
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 == '\\') { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
232 | p->tokenizer_escape = 1; |
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 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
235 | p->tokenizer_escape = 0; |
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 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
239 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
240 | 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
|
241 | // uncompleted token |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
242 | size_t uncompeted_len = p->size - token_start; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
243 | if (p->uncompleted.tokentype == CX_JSON_NO_TOKEN) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
244 | // current token is uncompleted |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
245 | // 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
|
246 | CxJsonToken uncompleted; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | uncompleted.tokentype = ttype; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
248 | uncompleted.length = uncompeted_len; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
249 | uncompleted.alloc = uncompeted_len + 16; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
250 | char *tmp = malloc(uncompleted.alloc); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
251 | if (tmp) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
252 | memcpy(tmp, p->buffer + token_start, uncompeted_len); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
253 | uncompleted.content = tmp; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
254 | p->uncompleted = uncompleted; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
255 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
256 | p->error = 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
257 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
258 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
259 | // 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
|
260 | // combine the uncompleted token with the current token |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
261 | if (token_append(&p->uncompleted, p->buffer + token_start, uncompeted_len)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
262 | p->error = 1; |
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 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
266 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
267 | 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
|
268 | return ret; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
269 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
270 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
271 | static cxmutstr unescape_string(const char *str, size_t len) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
272 | // TODO: support more escape sequences |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
273 | // 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
|
274 | cxmutstr result; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
275 | result.length = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
276 | result.ptr = malloc(len - 1); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
277 | if (result.ptr == NULL) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
278 | // TODO: check if this actually leads to correct error handling |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
279 | return result; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
280 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
281 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
282 | bool u = false; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
283 | 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
|
284 | char c = str[i]; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
285 | if (u) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
286 | u = false; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
287 | if (c == 'n') { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
288 | c = '\n'; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
289 | } else if (c == 't') { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
290 | c = '\t'; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
291 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
292 | result.ptr[result.length++] = c; |
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 | if (c == '\\') { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
295 | u = true; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
296 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
297 | result.ptr[result.length++] = c; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
298 | } |
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 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
301 | result.ptr[result.length] = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
302 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
303 | return result; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
304 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
305 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
306 | static int parse_integer(const char *str, size_t len, int64_t *value) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
307 | char *endptr = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
308 | char buf[32]; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
309 | if (len > 30) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
310 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
311 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
312 | memcpy(buf, str, len); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
313 | buf[len] = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
314 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
315 | long long v = strtoll(buf, &endptr, 10); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
316 | if (endptr != &buf[len]) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
317 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
318 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
319 | *value = (int64_t) v; |
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 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
322 | } |
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 | static int parse_number(const char *str, size_t len, double *value) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
325 | char *endptr = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
326 | char buf[32]; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
327 | if (len > 30) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
328 | return 1; |
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 | memcpy(buf, str, len); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
331 | buf[len] = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
332 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
333 | double v = strtod(buf, &endptr); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
334 | if (endptr != &buf[len]) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
335 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
336 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
337 | *value = v; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
338 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
339 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
340 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
341 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
342 | static int add_state(CxJson *p, int state) { |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
343 | // TODO: this is such a common pattern, try to reuse code here |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
344 | if (p->nstates >= p->states_alloc) { |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
345 | unsigned newcap = PARSER_STATES_ALLOC; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
346 | if (p->states == p->states_internal) { |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
347 | void *mem = malloc(newcap * sizeof(int)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
348 | if (mem == NULL) return 1; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
349 | memcpy(mem, p->states, p->nstates * sizeof(int)); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
350 | p->states = mem; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
351 | } else { |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
352 | newcap += p->states_alloc; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
353 | if (cx_reallocate(&p->states, newcap * sizeof(int))) { |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
354 | return 1; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
355 | } |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
356 | } |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
357 | p->states_alloc = newcap; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
358 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
359 | p->states[++p->nstates] = state; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
360 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
361 | } |
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 | static void end_elm(CxJson *p, CxJsonReaderType type) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
364 | p->reader_type = type; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
365 | p->nstates--; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
366 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
367 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
368 | #define JP_STATE_VALUE_BEGIN 0 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
369 | #define JP_STATE_VALUE_BEGIN_OBJ 1 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
370 | #define JP_STATE_VALUE_BEGIN_AR 2 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
371 | #define JP_STATE_ARRAY_SEP_OR_CLOSE 3 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
372 | #define JP_STATE_OBJ_NAME_OR_CLOSE 4 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
373 | #define JP_STATE_OBJ_NAME 5 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
374 | #define JP_STATE_OBJ_COLON 6 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
375 | #define JP_STATE_OBJ_SEP_OR_CLOSE 7 |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
376 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
377 | static int next_state_after_value(int current) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
378 | switch (current) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
379 | default: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
380 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
381 | // after value JSON complete, expect nothing |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
382 | case JP_STATE_VALUE_BEGIN: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
383 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
384 | // after obj value, expect ',' or '}' |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
385 | case JP_STATE_VALUE_BEGIN_OBJ: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
386 | return JP_STATE_OBJ_SEP_OR_CLOSE; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
387 | // after array value, expect ',' or ']' |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
388 | case JP_STATE_VALUE_BEGIN_AR: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
389 | return JP_STATE_ARRAY_SEP_OR_CLOSE; |
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 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
392 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
393 | static void clear_valuename(CxJson *p) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
394 | free(p->value_name); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
395 | p->value_name = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
396 | p->value_name_len = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
397 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
398 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
399 | static void clear_values(CxJson *p) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
400 | free(p->value_str); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
401 | p->value_str = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
402 | p->value_str_len = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
403 | p->value_int = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
404 | p->value_double = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
405 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
406 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
407 | static int json_read(CxJson *p) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
408 | int state = p->states[p->nstates]; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
409 | clear_values(p); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
410 | CxJsonToken token = json_parser_next_token(p); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
411 | p->reader_token = token; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
412 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
413 | p->value_ready = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
414 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
415 | if (token.tokentype == CX_JSON_NO_TOKEN) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
416 | return 0; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
419 | int ret = 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
420 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
421 | // 0 JP_STATE_VALUE_BEGIN value begin |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
422 | // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object) |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
423 | // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array) |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
424 | // 3 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
425 | // 4 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
426 | // 5 JP_STATE_OBJ_NAME object, expect name |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
427 | // 6 JP_STATE_OBJ_COLON object, expect ':' |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
428 | // 7 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
429 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
430 | if (state == JP_STATE_VALUE_BEGIN_AR || state == JP_STATE_OBJ_SEP_OR_CLOSE) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
431 | clear_valuename(p); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
432 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
433 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
434 | if (state < 3) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
435 | // expect value |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
436 | p->states[p->nstates] = next_state_after_value(state); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
437 | p->value_ready = 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
438 | switch (token.tokentype) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
439 | case CX_JSON_TOKEN_BEGIN_ARRAY: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
440 | p->reader_type = CX_JSON_READER_ARRAY_BEGIN; |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
441 | ret = add_state(p, JP_STATE_VALUE_BEGIN_AR) ? -1 : 1; |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
442 | break; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
443 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
444 | case CX_JSON_TOKEN_BEGIN_OBJECT: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
445 | p->reader_type = CX_JSON_READER_OBJECT_BEGIN; |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
446 | ret = add_state(p, JP_STATE_OBJ_NAME_OR_CLOSE) ? -1 : 1; |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
447 | break; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
448 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
449 | case CX_JSON_TOKEN_END_ARRAY: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
450 | p->value_ready = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
451 | end_elm(p, CX_JSON_READER_ARRAY_END); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
452 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
453 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
454 | case CX_JSON_TOKEN_STRING: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
455 | p->reader_type = CX_JSON_READER_STRING; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
456 | cxmutstr str = unescape_string(token.content, token.length); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
457 | if (str.ptr) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
458 | p->value_str = str.ptr; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
459 | p->value_str_len = str.length; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
460 | } else { |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
461 | ret = -1; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
462 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
463 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
464 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
465 | case CX_JSON_TOKEN_INTEGER: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
466 | p->reader_type = CX_JSON_READER_INTEGER; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
467 | int64_t value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
468 | if (parse_integer(token.content, token.length, &value)) { |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
469 | ret = -1; |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
470 | } else { |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
471 | p->value_int = value; |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
472 | p->value_double = (double) value; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
473 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
474 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
475 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
476 | case CX_JSON_TOKEN_NUMBER: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
477 | p->reader_type = CX_JSON_READER_NUMBER; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
478 | double value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
479 | if (parse_number(token.content, token.length, &value)) { |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
480 | ret = -1; |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
481 | } else { |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
482 | p->value_double = value; |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
483 | p->value_int = (int64_t) value; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
484 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
485 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
486 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
487 | case CX_JSON_TOKEN_LITERAL: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
488 | p->reader_type = CX_JSON_READER_LITERAL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
489 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
490 | } |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
491 | default: ret = -1; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
492 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
493 | } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
494 | // expect ',' or ']' |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
495 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
496 | p->states[p->nstates] = JP_STATE_VALUE_BEGIN_AR; |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
497 | ret = json_read(p); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
498 | } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
499 | end_elm(p, CX_JSON_READER_ARRAY_END); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
500 | } else { |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
501 | ret = -1; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
502 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
503 | } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
504 | if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
505 | clear_valuename(p); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
506 | end_elm(p, CX_JSON_READER_OBJECT_END); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
507 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
508 | // expect string |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
509 | if (token.tokentype != CX_JSON_TOKEN_STRING) return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
510 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
511 | if (p->value_name) free(p->value_name); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
512 | cxmutstr valname = unescape_string(token.content, token.length); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
513 | p->value_name = valname.ptr; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
514 | p->value_name_len = valname.length; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
515 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
516 | // next state |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
517 | p->states[p->nstates] = JP_STATE_OBJ_COLON; |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
518 | ret = json_read(p); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
519 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
520 | } else if (state == JP_STATE_OBJ_COLON) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
521 | // expect ':' |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
522 | if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
523 | // next state |
942
8a5bbdb7f87f
use json reader state macros everywhere
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
941
diff
changeset
|
524 | p->states[p->nstates] = JP_STATE_VALUE_BEGIN_OBJ; |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
525 | ret = json_read(p); |
942
8a5bbdb7f87f
use json reader state macros everywhere
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
941
diff
changeset
|
526 | } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) { |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
940
diff
changeset
|
527 | // expect ',' or '}' |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
528 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
529 | p->states[p->nstates] = JP_STATE_OBJ_NAME; |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
530 | ret = json_read(p); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
531 | } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
532 | end_elm(p, CX_JSON_READER_OBJECT_END); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
533 | } else { |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
534 | ret = -1; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
535 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
536 | } |
940
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
537 | |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
538 | if (token.alloc > 0) { |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
539 | free((char*)token.content); |
bbf41b9c2658
fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
938
diff
changeset
|
540 | } |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
541 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
542 | return ret; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
543 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
544 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
545 | static CxJsonLiteralType json_reader_literal(CxJson *p) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
546 | const char *l = p->reader_token.content; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
547 | size_t token_len = p->reader_token.length; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
548 | if (token_len == 4 && !memcmp(l, "true", 4)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
549 | return CX_JSON_TRUE; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
550 | } else if (token_len == 5 && !memcmp(l, "false", 5)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
551 | return CX_JSON_FALSE; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
552 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
553 | return CX_JSON_NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
554 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
555 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
556 | /* -------------------- read value functions -------------------- */ |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
557 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
558 | static int setup_read_value(CxJson *p) { |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
559 | p->readvalue_alloc = PARSER_READVALUE_ALLOC; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
560 | p->readvalue_nelm = 0; |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
561 | p->readvalue_stack = calloc(p->readvalue_alloc, sizeof(CxJsonValue *)); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
562 | if (!p->readvalue_stack) return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
563 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
564 | p->read_value = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
565 | p->readvalue_stack[0] = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
566 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
567 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
568 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
569 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
570 | static int obj_init_values(CxJson *p, CxJsonValue *v) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
571 | v->value.object.values = calloc(p->reader_array_alloc, sizeof(CxJsonObjValue)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
572 | if (!v->value.object.values) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
573 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
574 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
575 | v->value.object.alloc = p->reader_array_alloc; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
576 | v->value.object.size = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
577 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
578 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
579 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
580 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
581 | static int obj_add_value(CxJson *p, CxJsonValue *parent, CxJsonObjValue v) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
582 | if (!parent->value.object.values) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
583 | if (obj_init_values(p, parent)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
584 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
585 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
586 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
587 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
588 | if (parent->value.object.size == parent->value.object.alloc) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
589 | parent->value.object.alloc *= 2; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
590 | if (cx_reallocate(&parent->value.object.values, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
591 | sizeof(CxJsonObjValue) * parent->value.object.alloc)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
592 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
593 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
594 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
595 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
596 | parent->value.object.values[parent->value.object.size++] = v; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
597 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
598 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
599 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
600 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
601 | static int array_init(CxJson *p, CxJsonValue *v) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
602 | v->value.array.array = calloc(p->reader_array_alloc, sizeof(CxJsonValue *)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
603 | if (!v->value.array.array) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
604 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
605 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
606 | v->value.array.alloc = p->reader_array_alloc; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
607 | v->value.array.size = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
608 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
609 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
610 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
611 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
612 | static int array_add_value(CxJson *p, CxJsonValue *parent, CxJsonValue *v) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
613 | if (!parent->value.array.array) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
614 | if (array_init(p, parent)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
615 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
616 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
617 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
618 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
619 | if (parent->value.array.size == parent->value.array.alloc) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
620 | parent->value.array.alloc *= 2; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
621 | if (cx_reallocate(parent->value.array.array, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
622 | sizeof(CxJsonValue *) * parent->value.array.alloc)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
623 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
624 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
625 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
626 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
627 | parent->value.array.array[parent->value.array.size++] = v; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
628 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
629 | return 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
630 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
631 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
632 | static int add_to_parent(CxJson *p, CxJsonValue *parent, CxJsonValue *v) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
633 | if (!parent) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
634 | return -1; // shouldn't happen but who knows |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
635 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
636 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
637 | int ret = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
638 | if (parent->type == CX_JSON_OBJECT) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
639 | if (!p->value_name || p->value_name_len == 0) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
640 | return -1; |
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 | char *valuename = p->value_name; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
643 | p->value_name = NULL; |
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 | CxJsonObjValue newvalue; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
646 | newvalue.name = valuename; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
647 | newvalue.value = v; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
648 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
649 | ret = obj_add_value(p, parent, newvalue); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
650 | } else if (parent->type == CX_JSON_ARRAY) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
651 | ret = array_add_value(p, parent, v); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
652 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
653 | ret = -1; // should also never happen |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
656 | return ret; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
657 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
658 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
659 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
660 | static int readvaluestack_add(CxJson *p, CxJsonValue *v) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
661 | if (p->readvalue_nelm == p->readvalue_alloc) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
662 | p->readvalue_alloc *= 2; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
663 | if (cx_reallocate(&p->readvalue_stack, sizeof(CxJsonValue *) * p->readvalue_alloc)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
664 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
665 | } |
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 | p->readvalue_stack[p->readvalue_nelm++] = v; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
668 | return 0; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
671 | void cxJsonInit(CxJson *json) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
672 | memset(json, 0, sizeof(CxJson)); |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
673 | json->states = json->states_internal; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
674 | json->states_alloc = cx_nmemb(json->states_internal); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
675 | // TODO: find better way to configure the initial allocation size for arrays and objects |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
676 | json->reader_array_alloc = 8; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
679 | void cxJsonDestroy(CxJson *p) { |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
680 | if (p->states != p->states_internal) { |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
681 | free(p->states); |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
682 | } |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
683 | free(p->readvalue_stack); |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
940
diff
changeset
|
684 | cxJsonValueFree(p->read_value); |
943
9f1eb30dbc84
fix memory leak in case of json parser errors
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
942
diff
changeset
|
685 | free(p->value_name); |
9f1eb30dbc84
fix memory leak in case of json parser errors
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
942
diff
changeset
|
686 | free(p->value_str); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
687 | } |
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 | void cxJsonFill(CxJson *p, const char *buf, size_t size) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
690 | // TODO: implement rescue buffer like in CxProperties to allow subsequent fills |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
691 | p->buffer = buf; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
692 | p->size = size; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
693 | p->pos = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
694 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
695 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
696 | int cxJsonNext(CxJson *p, CxJsonValue **value) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
697 | // TODO: replace int with a status enum like in CxProperties |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
698 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
944
diff
changeset
|
699 | *value = NULL; // TODO: maybe better initialize with NOTHING? |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
700 | if (!p->readvalue_stack) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
701 | if (setup_read_value(p)) return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
702 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
703 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
704 | while (p->readvalue_nelm > 0 || !p->read_value) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
705 | if (p->value_ready) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
706 | // value available without another read |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
707 | CxJsonValue *v = calloc(1, sizeof(CxJsonValue)); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
708 | if (!v) return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
709 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
710 | if (p->readvalue_nelm > 0) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
711 | if (add_to_parent(p, p->readvalue_stack[p->readvalue_nelm - 1], v)) { |
941
9077724b75a0
fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
940
diff
changeset
|
712 | free(v); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
713 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
714 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
715 | } else { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
716 | // set this value as root |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
717 | p->read_value = v; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
718 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
719 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
720 | switch (p->reader_type) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
721 | case CX_JSON_READER_OBJECT_BEGIN: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
722 | v->type = CX_JSON_OBJECT; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
723 | if (readvaluestack_add(p, v)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
724 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
725 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
726 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
727 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
728 | case CX_JSON_READER_OBJECT_END: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
729 | return -1; // should not happen |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
730 | case CX_JSON_READER_ARRAY_BEGIN: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
731 | v->type = CX_JSON_ARRAY; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
732 | if (readvaluestack_add(p, v)) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
733 | return -1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
734 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
735 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
736 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
737 | case CX_JSON_READER_ARRAY_END: |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
738 | return -1; // should not happen |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
739 | case CX_JSON_READER_STRING: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
740 | v->type = CX_JSON_STRING; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
741 | if (p->value_str) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
742 | v->value.string.ptr = p->value_str; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
743 | v->value.string.length = p->value_str_len; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
744 | p->value_str = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
745 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
746 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
747 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
748 | case CX_JSON_READER_INTEGER: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
749 | v->type = CX_JSON_INTEGER; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
750 | v->value.integer.value = p->value_int; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
751 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
752 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
753 | case CX_JSON_READER_NUMBER: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
754 | v->type = CX_JSON_NUMBER; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
755 | v->value.number.value = p->value_double; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
756 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
757 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
758 | case CX_JSON_READER_LITERAL: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
759 | v->type = CX_JSON_LITERAL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
760 | v->value.literal.literal = json_reader_literal(p); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
761 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
762 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
763 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
764 | } else if (p->readvalue_initialized) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
765 | CxJsonReaderType rt = p->reader_type; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
766 | if (rt == CX_JSON_READER_OBJECT_END || rt == CX_JSON_READER_ARRAY_END) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
767 | p->readvalue_nelm--; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
768 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
769 | // else: p->value_ready is 1, this will be handled in the next run |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
770 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
771 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
772 | if (p->readvalue_nelm > 0 || !p->read_value) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
773 | int r = json_read(p); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
774 | if (r != 1) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
775 | p->readvalue_initialized = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
776 | return r; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
777 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
778 | p->readvalue_initialized = 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
779 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
780 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
781 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
782 | *value = p->read_value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
783 | p->readvalue_initialized = 0; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
784 | p->read_value = NULL; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
785 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
786 | return 1; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
787 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
788 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
789 | void cxJsonValueFree(CxJsonValue *value) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
790 | 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
|
791 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
792 | // TODO: discuss if we should keep freeing the stuff recursively |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
793 | switch (value->type) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
794 | case CX_JSON_OBJECT: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
795 | CxJsonObject obj = value->value.object; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
796 | for (size_t i = 0; i < obj.size; i++) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
797 | cxJsonValueFree(obj.values[i].value); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
798 | free(obj.values[i].name); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
799 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
800 | free(obj.values); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
801 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
802 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
803 | case CX_JSON_ARRAY: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
804 | CxJsonArray array = value->value.array; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
805 | for (size_t i = 0; i < array.size; i++) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
806 | cxJsonValueFree(array.array[i]); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
807 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
808 | free(array.array); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
809 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
810 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
811 | case CX_JSON_STRING: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
812 | free(value->value.string.ptr); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
813 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
814 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
815 | default: { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
816 | break; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
817 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
818 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
819 | free(value); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
820 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
821 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
822 | CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
823 | if (index >= value->value.array.size) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
824 | return &cx_json_value_nothing; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
825 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
826 | 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
|
827 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
828 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
829 | 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
|
830 | CxJsonObject *obj = &(value->value.object); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
831 | // TODO: think about sorting the object so that we can use binary search here |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
832 | for (size_t i = 0; i < obj->size; i++) { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
833 | // 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
|
834 | 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
|
835 | return obj->values[i].value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
836 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
837 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
838 | return &cx_json_value_nothing; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
839 | } |