Sat, 23 Nov 2024 14:45:32 +0100
improve consistency for allocator arguments - fixes #485
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" |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | #include "string.h" |
967
a58f602ed2fe
simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
41 | #include "array_list.h" |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | #ifdef __cplusplus |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | extern "C" { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | #endif |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | enum cx_json_token_type { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | CX_JSON_NO_TOKEN, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | CX_JSON_TOKEN_ERROR, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | CX_JSON_TOKEN_BEGIN_ARRAY, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | CX_JSON_TOKEN_BEGIN_OBJECT, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | CX_JSON_TOKEN_END_ARRAY, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | CX_JSON_TOKEN_END_OBJECT, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | CX_JSON_TOKEN_NAME_SEPARATOR, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | CX_JSON_TOKEN_VALUE_SEPARATOR, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | CX_JSON_TOKEN_STRING, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | CX_JSON_TOKEN_INTEGER, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | CX_JSON_TOKEN_NUMBER, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | CX_JSON_TOKEN_LITERAL, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | CX_JSON_TOKEN_SPACE |
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 | enum cx_json_value_type { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | 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
|
65 | CX_JSON_OBJECT, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | CX_JSON_ARRAY, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | CX_JSON_STRING, |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
68 | CX_JSON_INTEGER, |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | CX_JSON_NUMBER, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | CX_JSON_LITERAL |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | }; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
73 | enum cx_json_literal { |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | CX_JSON_NULL, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | CX_JSON_TRUE, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | CX_JSON_FALSE |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | }; |
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 | enum cx_json_reader_type { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | CX_JSON_READER_OBJECT_BEGIN, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | CX_JSON_READER_OBJECT_END, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | CX_JSON_READER_ARRAY_BEGIN, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | CX_JSON_READER_ARRAY_END, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | CX_JSON_READER_STRING, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | CX_JSON_READER_INTEGER, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | CX_JSON_READER_NUMBER, |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | CX_JSON_READER_LITERAL |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | }; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | 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
|
91 | 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
|
92 | typedef enum cx_json_reader_type CxJsonReaderType; |
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 | 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
|
95 | 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
|
96 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | 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
|
98 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | 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
|
100 | 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
|
101 | typedef struct cx_mutstr_s CxJsonString; |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
102 | typedef int64_t CxJsonInteger; |
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
103 | typedef double CxJsonNumber; |
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
104 | 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
|
105 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | 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
|
107 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | struct cx_json_token_s { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | CxJsonTokenType tokentype; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | const char *content; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | size_t length; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | size_t alloc; |
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 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | struct cx_json_s { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | const char *buffer; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | size_t size; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | size_t pos; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | CxJsonToken uncompleted; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | int tokenizer_escape; |
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 | int *states; |
954
a1d87e8fff6d
use cx_array_add() instead of reimplementing the magic
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
124 | size_t nstates; |
a1d87e8fff6d
use cx_array_add() instead of reimplementing the magic
Mike Becker <universe@uap-core.de>
parents:
946
diff
changeset
|
125 | size_t states_alloc; |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
937
diff
changeset
|
126 | int states_internal[8]; |
937
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 | CxJsonToken reader_token; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | CxJsonReaderType reader_type; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | int value_ready; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | char *value_name; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | size_t value_name_len; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | char *value_str; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | size_t value_str_len; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
135 | int64_t value_int; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | double value_double; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | CxJsonValue **readvalue_stack; |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
937
diff
changeset
|
139 | unsigned readvalue_nelm; |
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
937
diff
changeset
|
140 | unsigned readvalue_alloc; |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | CxJsonValue *read_value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | int readvalue_initialized; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | |
946
b428424c0214
avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents:
937
diff
changeset
|
144 | unsigned reader_array_alloc; |
937
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 | int error; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
147 | }; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
148 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
149 | struct cx_json_array_s { |
967
a58f602ed2fe
simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
150 | 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
|
151 | }; |
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 | struct cx_json_object_s { |
967
a58f602ed2fe
simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents:
966
diff
changeset
|
154 | 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
|
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 | 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
|
158 | char *name; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
159 | CxJsonValue *value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | }; |
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 | struct cx_json_value_s { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
163 | CxJsonValueType type; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
164 | union { |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
165 | CxJsonArray array; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
166 | CxJsonObject object; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
167 | CxJsonString string; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
168 | CxJsonInteger integer; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
169 | CxJsonNumber number; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | CxJsonLiteral literal; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
171 | } value; |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
172 | }; |
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 | // TODO: add support for CxAllocator |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
176 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
177 | void cxJsonInit(CxJson *json); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
178 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
179 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | void cxJsonDestroy(CxJson *json); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
182 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
183 | cx_attr_access_r(2, 3) |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
184 | 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
|
185 | |
988
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
186 | #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
|
187 | } // 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
|
188 | |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
189 | 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
|
190 | 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
|
191 | 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
|
192 | 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
|
193 | ) { |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
194 | 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
|
195 | } |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
196 | |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
197 | 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
|
198 | 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
|
199 | 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
|
200 | 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
|
201 | ) { |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
202 | 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
|
203 | } |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
204 | |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
205 | 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
|
206 | 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
|
207 | 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
|
208 | 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
|
209 | 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
|
210 | ) { |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
211 | 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
|
212 | } |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
213 | |
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
214 | 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
|
215 | #else // __cplusplus |
974
ed44741d8ab5
fix copy-pasted parameter name
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
216 | #define cxJsonFill(json, str) _Generic((str), \ |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
217 | cxstring: cx_json_fill_cxstr, \ |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
218 | cxmutstr: cx_json_fill_mutstr, \ |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
219 | char*: cx_json_fill_str, \ |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
220 | const char*: cx_json_fill_str) \ |
974
ed44741d8ab5
fix copy-pasted parameter name
Mike Becker <universe@uap-core.de>
parents:
973
diff
changeset
|
221 | (json, str) |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
222 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
223 | cx_attr_nonnull |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
224 | static inline int cx_json_fill_cxstr( |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
225 | CxJson *json, |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
226 | cxstring str |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
227 | ) { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
228 | return cxJsonFilln(json, str.ptr, str.length); |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
229 | } |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
230 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
231 | cx_attr_nonnull |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
232 | static inline int cx_json_fill_mutstr( |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
233 | CxJson *json, |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
234 | cxmutstr str |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
235 | ) { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
236 | return cxJsonFilln(json, str.ptr, str.length); |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
237 | } |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
238 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
239 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
240 | cx_attr_cstr_arg(2) |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
241 | static inline int cx_json_fill_str( |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
242 | CxJson *json, |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
243 | const char *str |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
244 | ) { |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
245 | return cxJsonFilln(json, str, strlen(str)); |
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
246 | } |
988
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
985
diff
changeset
|
247 | #endif |
973
05910a8994f7
add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents:
967
diff
changeset
|
248 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
249 | void cxJsonValueFree(CxJsonValue *value); |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
250 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
251 | // TODO: if the CxJsonValue was a returned value, we could reference cxJsonValueFree() as deallocator |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
252 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
253 | int cxJsonNext(CxJson *json, CxJsonValue **value); |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
254 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
255 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
256 | 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
|
257 | 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
|
258 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
259 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
260 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
261 | 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
|
262 | 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
|
263 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
264 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
265 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
266 | 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
|
267 | 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
|
268 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
269 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
270 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
271 | 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
|
272 | 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
|
273 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
274 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
275 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
276 | 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
|
277 | 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
|
278 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
279 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
280 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
281 | 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
|
282 | 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
|
283 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
284 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
285 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
286 | static inline bool cxJsonIsBool(CxJsonValue *value) { |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
287 | 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
|
288 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
289 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
290 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
291 | static inline bool cxJsonIsTrue(CxJsonValue *value) { |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
292 | 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
|
293 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
294 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
295 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
296 | static inline bool cxJsonIsFalse(CxJsonValue *value) { |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
297 | 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
|
298 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
299 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
300 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
301 | static inline bool cxJsonIsNull(CxJsonValue *value) { |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
302 | 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
|
303 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
304 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
305 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
306 | cx_attr_returns_nonnull |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
307 | 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
|
308 | return value->value.string.ptr; |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
309 | } |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
310 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
311 | cx_attr_nonnull |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
312 | 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
|
313 | 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
|
314 | } |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
315 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
316 | cx_attr_nonnull |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
317 | 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
|
318 | return value->value.string; |
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 double cxJsonAsDouble(CxJsonValue *value) { |
966
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
323 | 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
|
324 | return (double) value->value.integer; |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
325 | } else { |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
326 | return value->value.number; |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
327 | } |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
328 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
329 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
330 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
331 | 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
|
332 | 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
|
333 | return value->value.integer; |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
334 | } else { |
1aa7ec3e46e7
treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents:
965
diff
changeset
|
335 | 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
|
336 | } |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
337 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
338 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
339 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
340 | static inline bool cxJsonAsBool(CxJsonValue *value) { |
965
dfdfedbe2c86
remove single-member structs
Mike Becker <universe@uap-core.de>
parents:
954
diff
changeset
|
341 | 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
|
342 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
343 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
344 | cx_attr_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
345 | 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
|
346 | 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
|
347 | } |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
348 | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
349 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
350 | cx_attr_returns_nonnull |
937
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
351 | 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
|
352 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
353 | // TODO: add cxJsonArrIter() |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
354 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
355 | // 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
|
356 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
357 | cx_attr_returns_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
974
diff
changeset
|
358 | 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
|
359 | 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
|
360 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
361 | #ifdef __cplusplus |
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 | #endif |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
364 | |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
365 | #endif /* UCX_JSON_H */ |
10123f4d5618
add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
366 |