src/cx/json.h

Tue, 22 Oct 2024 22:20:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 22 Oct 2024 22:20:50 +0200
changeset 944
c26299cc9897
parent 937
10123f4d5618
child 946
b428424c0214
permissions
-rw-r--r--

make both gcc and clang happy with how cx_json_value_nothing is initialized

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"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 #ifdef __cplusplus
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 extern "C" {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 #endif
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 enum cx_json_token_type {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 CX_JSON_NO_TOKEN,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 CX_JSON_TOKEN_ERROR,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 CX_JSON_TOKEN_BEGIN_ARRAY,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 CX_JSON_TOKEN_BEGIN_OBJECT,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 CX_JSON_TOKEN_END_ARRAY,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_JSON_TOKEN_END_OBJECT,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 CX_JSON_TOKEN_NAME_SEPARATOR,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 CX_JSON_TOKEN_VALUE_SEPARATOR,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 CX_JSON_TOKEN_STRING,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 CX_JSON_TOKEN_INTEGER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_JSON_TOKEN_NUMBER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 CX_JSON_TOKEN_LITERAL,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CX_JSON_TOKEN_SPACE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 };
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 enum cx_json_value_type {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 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
64 CX_JSON_OBJECT,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 CX_JSON_ARRAY,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CX_JSON_STRING,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 CX_JSON_INTEGER, // TODO: the spec does not know integer types
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 CX_JSON_NUMBER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 CX_JSON_LITERAL
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 enum cx_json_literal_type {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 CX_JSON_NULL,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 CX_JSON_TRUE,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 CX_JSON_FALSE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 };
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 enum cx_json_reader_type {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 CX_JSON_READER_OBJECT_BEGIN,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 CX_JSON_READER_OBJECT_END,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 CX_JSON_READER_ARRAY_BEGIN,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 CX_JSON_READER_ARRAY_END,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 CX_JSON_READER_STRING,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 CX_JSON_READER_INTEGER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 CX_JSON_READER_NUMBER,
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 CX_JSON_READER_LITERAL
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 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
90 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
91 typedef enum cx_json_literal_type CxJsonLiteralType;
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;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 typedef struct cx_json_integer_s CxJsonInteger;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 typedef struct cx_json_number_s CxJsonNumber;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 typedef struct cx_json_literal_s CxJsonLiteral;
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;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 int nstates;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 int states_alloc;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 CxJsonToken reader_token;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 CxJsonReaderType reader_type;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 int value_ready;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 char *value_name;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 size_t value_name_len;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 char *value_str;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 size_t value_str_len;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 int64_t value_int;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 double value_double;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 CxJsonValue **readvalue_stack;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 int readvalue_nelm;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 int readvalue_alloc;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 CxJsonValue *read_value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 int readvalue_initialized;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 int reader_array_alloc;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 int error;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 struct cx_json_array_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 CxJsonValue **array;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 size_t alloc;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 size_t size;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 struct cx_json_object_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 CxJsonObjValue *values;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 size_t alloc;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 size_t size;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 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
161 char *name;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 CxJsonValue *value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 // TODO: remove single member structs
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 struct cx_json_integer_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 int64_t value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 struct cx_json_number_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 double value;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 struct cx_json_literal_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 CxJsonLiteralType literal;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 struct cx_json_value_s {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 CxJsonValueType type;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 union {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 CxJsonArray array;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 CxJsonObject object;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 CxJsonString string;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 CxJsonInteger integer;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 CxJsonNumber number;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 CxJsonLiteral literal;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 } value;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 };
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 // TODO: add support for CxAllocator
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 void cxJsonInit(CxJson *json);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 void cxJsonDestroy(CxJson *json);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 void cxJsonFill(CxJson *json, const char *buf, size_t len);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 // TODO: discuss if it is intentional that cxJsonNext() will usually parse an entire file in one go
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 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
205
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 void cxJsonValueFree(CxJsonValue *value);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 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
210 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
211 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 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
215 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
216 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 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
220 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
221 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 static inline bool cxJsonIsNumber(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 // TODO: this is not good, because an integer is also a number
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 return value->type == CX_JSON_NUMBER;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 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
231 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
232 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 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
236 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
237 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 static inline bool cxJsonIsBool(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 return cxJsonIsLiteral(value) && value->value.literal.literal != CX_JSON_NULL;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 static inline bool cxJsonIsTrue(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_TRUE;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 static inline bool cxJsonIsFalse(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_FALSE;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 static inline bool cxJsonIsNull(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_NULL;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 static inline cxmutstr cxJsonAsString(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 // TODO: do we need a separate method to return this directly as cxstring?
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 return value->value.string;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 static inline double cxJsonAsDouble(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 return value->value.number.value;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 static inline int64_t cxJsonAsInteger(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 return value->value.integer.value;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 static inline bool cxJsonAsBool(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 return value->value.literal.literal == CX_JSON_TRUE;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 __attribute__((__nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 static inline size_t cxJsonArrSize(CxJsonValue *value) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 return value->value.array.size;
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
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 __attribute__((__nonnull__, __returns_nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286 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
287
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 // TODO: add cxJsonArrIter()
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 // TODO: implement cxJsonObjGet as a _Generic with support for cxstring
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291 __attribute__((__nonnull__, __returns_nonnull__))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 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
293
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 #ifdef __cplusplus
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 #endif
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 #endif /* UCX_JSON_H */
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299

mercurial