src/cx/json.h

Sun, 08 Dec 2024 00:18:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Dec 2024 00:18:47 +0100
changeset 1003
455afb252878
parent 1002
1483c47063a8
child 1007
81b2986d2b04
permissions
-rw-r--r--

add test case to cover new branches inside cx_strcat_ma()

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 * \file json.h
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 * \brief Interface for parsing data from JSON files.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 * \author Mike Becker
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 * \author Olaf Wintermann
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 * \copyright 2-Clause BSD License
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 #ifndef UCX_JSON_H
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 #define UCX_JSON_H
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 #include "common.h"
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 988
diff changeset
40 #include "allocator.h"
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 #include "string.h"
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
42 #include "array_list.h"
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 #ifdef __cplusplus
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 extern "C" {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 #endif
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 enum cx_json_token_type {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 CX_JSON_NO_TOKEN,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 CX_JSON_TOKEN_ERROR,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 CX_JSON_TOKEN_BEGIN_ARRAY,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_JSON_TOKEN_BEGIN_OBJECT,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 CX_JSON_TOKEN_END_ARRAY,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 CX_JSON_TOKEN_END_OBJECT,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 CX_JSON_TOKEN_NAME_SEPARATOR,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 CX_JSON_TOKEN_VALUE_SEPARATOR,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_JSON_TOKEN_STRING,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 CX_JSON_TOKEN_INTEGER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CX_JSON_TOKEN_NUMBER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 CX_JSON_TOKEN_LITERAL,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 CX_JSON_TOKEN_SPACE
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 enum cx_json_value_type {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 CX_JSON_NOTHING, // this allows us to always return non-NULL values
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CX_JSON_OBJECT,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 CX_JSON_ARRAY,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 CX_JSON_STRING,
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
69 CX_JSON_INTEGER,
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 CX_JSON_NUMBER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 CX_JSON_LITERAL
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
74 enum cx_json_literal {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 CX_JSON_NULL,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 CX_JSON_TRUE,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 CX_JSON_FALSE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 typedef enum cx_json_token_type CxJsonTokenType;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 typedef enum cx_json_value_type CxJsonValueType;
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 typedef struct cx_json_s CxJson;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 typedef struct cx_json_token_s CxJsonToken;
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 typedef struct cx_json_value_s CxJsonValue;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 typedef struct cx_json_array_s CxJsonArray;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 typedef struct cx_json_object_s CxJsonObject;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 typedef struct cx_mutstr_s CxJsonString;
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
91 typedef int64_t CxJsonInteger;
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
92 typedef double CxJsonNumber;
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
93 typedef enum cx_json_literal CxJsonLiteral;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 typedef struct cx_json_obj_value_s CxJsonObjValue;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 struct cx_json_array_s {
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
99 CX_ARRAY_DECLARE(CxJsonValue*, array);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 struct cx_json_object_s {
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
103 CX_ARRAY_DECLARE(CxJsonObjValue, values);
937
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 struct cx_json_obj_value_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 char *name;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 CxJsonValue *value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 struct cx_json_value_s {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 988
diff changeset
112 const CxAllocator *allocator;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 CxJsonValueType type;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 union {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 CxJsonArray array;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 CxJsonObject object;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 CxJsonString string;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 CxJsonInteger integer;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 CxJsonNumber number;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 CxJsonLiteral literal;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 } value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
124 struct cx_json_token_s {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
125 CxJsonTokenType tokentype;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
126 bool allocated;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
127 cxmutstr content;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
128 };
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
130 struct cx_json_s {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
131 const CxAllocator *allocator;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
132 const char *buffer;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
133 size_t size;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
134 size_t pos;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
135
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
136 CxJsonToken uncompleted;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
137
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
138 /**
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
139 * A pointer to an intermediate state of the currently parsed value.
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
140 *
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
141 * Never access this value manually.
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
142 */
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
143 CxJsonValue *parsed;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
144
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
145 /**
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
146 * State stack.
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
147 */
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
148 CX_ARRAY_DECLARE_SIZED(int, states, unsigned);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
149
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
150 /**
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
151 * Value buffer stack.
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
152 */
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
153 CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf, unsigned);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
154
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
155 /**
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
156 * Internally reserved memory for the state stack.
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
157 */
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
158 int states_internal[8];
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
159
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
160 /**
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
161 * Internally reserved memory for the value buffer stack.
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
162 */
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
163 CxJsonValue* vbuf_internal[8];
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
164
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
165 int error; // TODO: currently unused
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
166 bool tokenizer_escape; // TODO: check if it can be replaced with look-behind
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
167 };
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
168
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
169 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
170 * Status codes for the json interface.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
171 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
172 enum cx_json_status {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
173 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
174 * Everything is fine.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
175 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
176 CX_JSON_NO_ERROR,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
177 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
178 * The input buffer does not contain more data.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
179 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
180 CX_JSON_NO_DATA,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
181 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
182 * The input ends unexpectedly.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
183 *
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
184 * Refill the buffer with cxJsonFill() to complete the json data.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
185 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
186 CX_JSON_INCOMPLETE_DATA,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
187 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
188 * Not used as a status and never returned by any function.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
189 *
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
190 * You can use this enumerator to check for all "good" status results
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
191 * by checking if the status is less than \c CX_JSON_OK.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
192 *
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
193 * A "good" status means, that you can refill data and continue parsing.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
194 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
195 CX_JSON_OK,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
196 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
197 * Input buffer is \c NULL.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
198 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
199 CX_JSON_NULL_INPUT,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
200 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
201 * Allocating memory for the internal buffer failed.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
202 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
203 CX_JSON_BUFFER_ALLOC_FAILED,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
204 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
205 * Allocating memory for a json value failed.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
206 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
207 CX_JSON_VALUE_ALLOC_FAILED,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
208 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
209 * A number value is incorrectly formatted.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
210 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
211 CX_JSON_FORMAT_ERROR_NUMBER,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
212 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
213 * The tokenizer found something unexpected.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
214 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
215 CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
216 };
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
217
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
218 /**
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
219 * Typedef for the json status enum.
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
220 */
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
221 typedef enum cx_json_status CxJsonStatus;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
222
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
223 cx_attr_nonnull_arg(1)
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
224 void cxJsonInit(CxJson *json, const CxAllocator *allocator);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
226 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 void cxJsonDestroy(CxJson *json);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
229 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
230 cx_attr_access_r(2, 3)
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
231 int cxJsonFilln(CxJson *json, const char *buf, size_t len);
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
232
988
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
233 #ifdef __cplusplus
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
234 } // extern "C"
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
235
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
236 cx_attr_nonnull
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
237 static inline int cxJsonFill(
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
238 CxJson *json,
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
239 cxstring str
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
240 ) {
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
241 return cxJsonFilln(json, str.ptr, str.length);
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
242 }
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
243
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
244 cx_attr_nonnull
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
245 static inline int cxJsonFill(
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
246 CxJson *json,
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
247 cxmutstr str
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
248 ) {
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
249 return cxJsonFilln(json, str.ptr, str.length);
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
250 }
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
251
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
252 cx_attr_nonnull
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
253 cx_attr_cstr_arg(2)
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
254 static inline int cxJsonFill(
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
255 CxJson *json,
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
256 const char *str
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
257 ) {
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
258 return cxJsonFilln(json, str, strlen(str));
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
259 }
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
260
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
261 extern "C" {
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
262 #else // __cplusplus
974
ed44741d8ab5 fix copy-pasted parameter name
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
263 #define cxJsonFill(json, str) _Generic((str), \
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
264 cxstring: cx_json_fill_cxstr, \
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
265 cxmutstr: cx_json_fill_mutstr, \
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
266 char*: cx_json_fill_str, \
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
267 const char*: cx_json_fill_str) \
974
ed44741d8ab5 fix copy-pasted parameter name
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
268 (json, str)
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
269
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
270 cx_attr_nonnull
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
271 static inline int cx_json_fill_cxstr(
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
272 CxJson *json,
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
273 cxstring str
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
274 ) {
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
275 return cxJsonFilln(json, str.ptr, str.length);
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
276 }
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
277
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
278 cx_attr_nonnull
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
279 static inline int cx_json_fill_mutstr(
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
280 CxJson *json,
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
281 cxmutstr str
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
282 ) {
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
283 return cxJsonFilln(json, str.ptr, str.length);
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
284 }
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
285
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
286 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
287 cx_attr_cstr_arg(2)
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
288 static inline int cx_json_fill_str(
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
289 CxJson *json,
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
290 const char *str
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
291 ) {
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
292 return cxJsonFilln(json, str, strlen(str));
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
293 }
988
15b3ca7ee33f make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
294 #endif
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 967
diff changeset
295
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
296 void cxJsonValueFree(CxJsonValue *value);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
298 cx_attr_nonnull
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
299 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
301 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302 static inline bool cxJsonIsObject(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 return value->type == CX_JSON_OBJECT;
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
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
306 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 static inline bool cxJsonIsArray(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308 return value->type == CX_JSON_ARRAY;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
311 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312 static inline bool cxJsonIsString(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 return value->type == CX_JSON_STRING;
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
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
316 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 static inline bool cxJsonIsNumber(CxJsonValue *value) {
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
318 return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
319 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
321 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
322 static inline bool cxJsonIsInteger(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 return value->type == CX_JSON_INTEGER;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
326 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
327 static inline bool cxJsonIsLiteral(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328 return value->type == CX_JSON_LITERAL;
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
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
331 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332 static inline bool cxJsonIsBool(CxJsonValue *value) {
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
333 return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
335
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
336 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
337 static inline bool cxJsonIsTrue(CxJsonValue *value) {
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
338 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
339 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
340
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
341 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342 static inline bool cxJsonIsFalse(CxJsonValue *value) {
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
343 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
344 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
346 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347 static inline bool cxJsonIsNull(CxJsonValue *value) {
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
348 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
351 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
352 cx_attr_returns_nonnull
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
353 static inline char *cxJsonAsString(CxJsonValue *value) {
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
354 return value->value.string.ptr;
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
355 }
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
356
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
357 cx_attr_nonnull
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
358 static inline cxstring cxJsonAsCxString(CxJsonValue *value) {
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
359 return cx_strcast(value->value.string);
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
360 }
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
361
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
362 cx_attr_nonnull
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
363 static inline cxmutstr cxJsonAsCxMutStr(CxJsonValue *value) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
364 return value->value.string;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
365 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
366
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
367 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 static inline double cxJsonAsDouble(CxJsonValue *value) {
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
369 if (value->type == CX_JSON_INTEGER) {
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
370 return (double) value->value.integer;
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
371 } else {
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
372 return value->value.number;
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
373 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
374 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
375
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
376 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377 static inline int64_t cxJsonAsInteger(CxJsonValue *value) {
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
378 if (value->type == CX_JSON_INTEGER) {
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
379 return value->value.integer;
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
380 } else {
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
381 return (int64_t) value->value.number;
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
382 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
383 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
384
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
385 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
386 static inline bool cxJsonAsBool(CxJsonValue *value) {
965
dfdfedbe2c86 remove single-member structs
Mike Becker <universe@uap-core.de>
parents: 954
diff changeset
387 return value->value.literal == CX_JSON_TRUE;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
388 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
389
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
390 cx_attr_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391 static inline size_t cxJsonArrSize(CxJsonValue *value) {
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
392 return value->value.array.array_size;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
393 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
394
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
395 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
396 cx_attr_returns_nonnull
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
397 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
398
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 // TODO: add cxJsonArrIter()
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401 // TODO: implement cxJsonObjGet as a _Generic with support for cxstring
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
402 cx_attr_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
403 cx_attr_returns_nonnull
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 974
diff changeset
404 cx_attr_cstr_arg(2)
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405 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
406
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407 #ifdef __cplusplus
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
408 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
409 #endif
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
410
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
411 #endif /* UCX_JSON_H */
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
412

mercurial