1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// file : libbuild2/json.hxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#ifndef LIBBUILD2_JSON_HXX
#define LIBBUILD2_JSON_HXX
#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>
#include <libbuild2/export.hxx>
namespace butl
{
namespace json
{
enum class event: uint8_t;
class parser;
class buffer_serializer;
class stream_serializer;
class invalid_json_input;
class invalid_json_output;
}
}
namespace build2
{
using json_event = butl::json::event;
using json_parser = butl::json::parser;
using json_buffer_serializer = butl::json::buffer_serializer;
using json_stream_serializer = butl::json::stream_serializer;
using butl::json::invalid_json_input;
using butl::json::invalid_json_output;
#ifndef BUILD2_BOOTSTRAP
LIBBUILD2_SYMEXPORT const char*
to_string (json_event);
#endif
// @@ TODO:
//
// - provide swap().
// - provide operator=(uint64_t), etc.
// - provide std::hash specialization
// - tighted at()/[] interface in json_array and json_object
// - tighten noexcep where possible
// - operator bool() - in a sense null is like nullopt.
//
// This JSON representation has one extensions compared to the standard JSON
// model: it distinguishes between signed, unsigned, and hexadecimal
// numbers.
//
// Note also that we don't assume that object members are in a sorted order
// (but do assume there are no duplicates). However, we could add an
// argument to signal that this is the case to speed up some functions, for
// example, compare().
//
enum class json_type: uint8_t
{
null, // Note: keep first for comparison.
boolean,
signed_number,
unsigned_number,
hexadecimal_number,
string,
array,
object,
};
// Return the JSON type as string. If distinguish_numbers is true, then
// distinguish between the singned, unsigned, and hexadecimal types.
//
LIBBUILD2_SYMEXPORT const char*
to_string (json_type, bool distinguish_numbers = false) noexcept;
inline ostream&
operator<< (ostream& os, json_type t) {return os << to_string (t);}
struct json_member;
class LIBBUILD2_SYMEXPORT json_value
{
public:
using string_type = build2::string;
using array_type = vector<json_value>;
using object_type = vector<json_member>;
json_type type;
// Unchecked value access.
//
union
{
bool boolean;
int64_t signed_number;
uint64_t unsigned_number; // Also used for hexadecimal_number.
string_type string;
array_type array;
object_type object;
};
// Checked value access.
//
// If the type matches, return the corresponding member of the union.
// Otherwise throw std::invalid_argument.
//
bool as_bool () const;
bool& as_bool ();
int64_t as_int64 () const;
int64_t& as_int64 ();
uint64_t as_uint64 () const;
uint64_t& as_uint64 ();
const string_type& as_string () const;
string_type& as_string ();
const array_type& as_array () const;
array_type& as_array ();
const object_type& as_object () const;
object_type& as_object ();
// Construction.
//
explicit
json_value (json_type = json_type::null) noexcept;
explicit
json_value (std::nullptr_t) noexcept;
explicit
json_value (bool) noexcept;
explicit
json_value (int64_t) noexcept;
explicit
json_value (uint64_t, bool hexadecimal = false) noexcept;
explicit
json_value (string_type);
// If the expected type is specfied, then fail if it does not match
// parsed. Throws invalid_json_input.
//
explicit
json_value (json_parser&, optional<json_type> expected = {});
// If the expected type is specfied, then fail if it does not match the
// value's. Throws invalid_json_output.
//
void
serialize (json_buffer_serializer&,
optional<json_type> expected = {}) const;
// Note that values of different types are never equal, except for
// signed/unsigned/hexadecimal numbers. Null is equal to null and is less
// than any other value. Arrays are compared lexicographically. Object
// members are considered in the lexicographically-compared name-ascending
// order (see RFC8785). An absent member is less than a present member
// (even if it's null).
//
int
compare (const json_value&) const noexcept;
// Append/prepend one JSON value to another. Throw invalid_argument if the
// values are incompatible. Note that for numbers this can also lead to
// the change of the value type.
//
// Append/prepend to an object overrides existing members. Append/prepend
// an array to an array splices in the array elements rather than adding
// an element of the array type.
//
void
append (json_value&&);
void
prepend (json_value&&);
// Array element access.
//
// If the index is out of array bounds, the at() functions throw
// std::out_of_range, the const operator[] returns null_json_value, and
// the non-const operator[] inserts a new null value at the specified
// position (filling any missing elements in between with nulls) and
// returns that. All three functions throw std::invalid_argument if the
// value is not an array or null with null treated as (missing) array
// rather than wrong value type (and with at() functions throwing
// out_of_range in this case).
//
// Note that non-const operator[] will not only insert a new element but
// will also turn the value it is called upon into array if it is null.
// This semantics allows you to string several subscripts to build up a
// chain of values.
//
// Note also that while the operator[] interface is convenient for
// accessing and modifying (or building up) values deep in the tree, it
// can lead to inefficiencies or even undesirable semantics during
// otherwise read-only access of a non-const object due to the potential
// insertion of null values for missing array elements. As a result, it's
// recommended to always use a const reference for read-only access (or
// use the at() interface if this is deemed too easy to forget).
//
const json_value&
at (size_t) const;
json_value&
at (size_t);
#if 0
const json_value&
operator[] (size_t) const;
json_value&
operator[] (size_t);
#endif
// Object member access.
//
// If a member with the specified name is not found in the object, the
// at() functions throw std::out_of_range, the find() function returns
// NULL, the const operator[] returns null_json_value, and the non-const
// operator[] adds a new member with the specified name and null value and
// returns that value. All three functions throw std::invalid_argument if
// the value is not an object or null with null treated as (missing)
// object rather than wrong value type (and with at() functions throwing
// out_of_range in this case).
//
// Note that non-const operator[] will not only insert a new member but
// will also turn the value it is called upon into object if it is null.
// This semantics allows you to string several subscripts to build up a
// chain of values.
//
// Note also that while the operator[] interface is convenient for
// accessing and modifying (or building up) values deep in the tree, it
// can lead to inefficiencies or even undesirable semantics during
// otherwise read-only access of a non-const object due to the potential
// insertion of null values for missing object members. As a result, it's
// recommended to always use a const reference for read-only access (or
// use the at() interface if this is deemed too easy to forget).
//
const json_value&
at (const char*) const;
json_value&
at (const char*);
const json_value*
find (const char*) const;
json_value*
find (const char*);
#if 0
const json_value&
operator[] (const char*) const;
json_value&
operator[] (const char*);
#endif
const json_value&
at (const string_type&) const;
json_value&
at (const string_type&);
const json_value*
find (const string_type&) const;
json_value*
find (const string_type&);
#if 0
const json_value&
operator[] (const string_type&) const;
json_value&
operator[] (const string_type&);
#endif
// Note that the moved-from value becomes JSON null value.
//
json_value (json_value&&) noexcept;
json_value (const json_value&);
json_value& operator= (json_value&&) noexcept;
json_value& operator= (const json_value&);
~json_value () noexcept;
};
LIBBUILD2_SYMEXPORT extern const json_value null_json_value;
inline bool
operator== (const json_value& x, const json_value& y) {return x.compare (y) == 0;}
inline bool
operator!= (const json_value& x, const json_value& y) {return !(x == y);}
inline bool
operator< (const json_value& x, const json_value& y) {return x.compare (y) < 0;}
inline bool
operator<= (const json_value& x, const json_value& y) {return x.compare (y) <= 0;}
inline bool
operator> (const json_value& x, const json_value& y) {return !(x <= y);}
inline bool
operator>= (const json_value& x, const json_value& y) {return !(x < y);}
// A JSON object member.
//
struct json_member
{
// @@ TODO: add some convenience constructors?
string name;
json_value value;
};
// A JSON value that can only be an array.
//
class /*LIBBUILD2_SYMEXPORT*/ json_array: public json_value
{
public:
// Create empty array.
//
json_array () noexcept;
explicit
json_array (json_parser&);
void
serialize (json_buffer_serializer& s) const;
};
// A JSON value that can only be an object.
//
class /*LIBBUILD2_SYMEXPORT*/ json_object: public json_value
{
public:
// Create empty object.
//
json_object () noexcept;
explicit
json_object (json_parser&);
void
serialize (json_buffer_serializer& s) const;
};
}
#include <libbuild2/json.ixx>
#endif // LIBBUILD2_JSON_HXX
|