-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathJsonObject.h
453 lines (379 loc) · 15.9 KB
/
JsonObject.h
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#pragma once
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/StlAllocator.h>
#include <aws/crt/Types.h>
struct aws_json_value;
namespace Aws
{
namespace Crt
{
class JsonView;
/**
* JSON DOM manipulation class.
* To read or serialize use @ref View function.
*/
class AWS_CRT_CPP_API JsonObject
{
public:
/**
* Constructs empty JSON DOM.
*/
JsonObject();
/**
* Constructs a JSON DOM by parsing the input string.
* Call WasParseSuccessful() on new object to determine if parse was successful.
*/
JsonObject(const String &stringToParse);
/**
* Construct a deep copy.
* Prefer using a @ref JsonView if copying is not needed.
*/
JsonObject(const JsonObject &other);
/**
* Move constructor.
* No copying is performed.
*/
JsonObject(JsonObject &&other) noexcept;
~JsonObject();
/**
* Performs a deep copy.
*/
JsonObject &operator=(const JsonObject &other);
/**
* Moves the ownership of the internal JSON DOM of the parameter to the current object.
* No copying is performed.
* A DOM currently owned by the object will be freed prior to copying.
* @warning This will result in invalidating any outstanding views of the current DOM. However, views
* to the moved-from DOM would still valid.
*/
JsonObject &operator=(JsonObject &&other) noexcept;
bool operator==(const JsonObject &other) const;
bool operator!=(const JsonObject &other) const;
/**
* Adds a string to the top level of this node with key.
*/
JsonObject &WithString(const String &key, const String &value);
JsonObject &WithString(const char *key, const String &value);
/**
* Converts the current JSON node to a string.
*/
JsonObject &AsString(const String &value);
/**
* Adds a bool value with key to the top level of this node.
*/
JsonObject &WithBool(const String &key, bool value);
JsonObject &WithBool(const char *key, bool value);
/**
* Converts the current JSON node to a bool.
*/
JsonObject &AsBool(bool value);
/**
* Adds a number value at key at the top level of this node.
* Precision may be lost.
*/
JsonObject &WithInteger(const String &key, int value);
JsonObject &WithInteger(const char *key, int value);
/**
* Converts the current JSON node to a number.
* Precision may be lost.
*/
JsonObject &AsInteger(int value);
/**
* Adds a number value at key to the top level of this node.
* Precision may be lost.
*/
JsonObject &WithInt64(const String &key, int64_t value);
JsonObject &WithInt64(const char *key, int64_t value);
/**
* Converts the current JSON node to a number.
* Precision may be lost.
*/
JsonObject &AsInt64(int64_t value);
/**
* Adds a number value at key at the top level of this node.
*/
JsonObject &WithDouble(const String &key, double value);
JsonObject &WithDouble(const char *key, double value);
/**
* Converts the current JSON node to a number.
*/
JsonObject &AsDouble(double value);
/**
* Adds an array of strings to the top level of this node at key.
*/
JsonObject &WithArray(const String &key, const Vector<String> &array);
JsonObject &WithArray(const char *key, const Vector<String> &array);
/**
* Adds an array of arbitrary JSON objects to the top level of this node at key.
* The values in the array parameter will be deep-copied.
*/
JsonObject &WithArray(const String &key, const Vector<JsonObject> &array);
/**
* Adds an array of arbitrary JSON objects to the top level of this node at key.
* The values in the array parameter will be moved-from.
*/
JsonObject &WithArray(const String &key, Vector<JsonObject> &&array);
/**
* Converts the current JSON node to an array whose values are deep-copied from the array parameter.
*/
JsonObject &AsArray(const Vector<JsonObject> &array);
/**
* Converts the current JSON node to an array whose values are moved from the array parameter.
*/
JsonObject &AsArray(Vector<JsonObject> &&array);
/**
* Sets the current JSON node as null.
*/
JsonObject &AsNull();
/**
* Adds a JSON object to the top level of this node at key.
* The object parameter is deep-copied.
*/
JsonObject &WithObject(const String &key, const JsonObject &value);
JsonObject &WithObject(const char *key, const JsonObject &value);
/**
* Adds a JSON object to the top level of this node at key.
*/
JsonObject &WithObject(const String &key, JsonObject &&value);
JsonObject &WithObject(const char *key, JsonObject &&value);
/**
* Converts the current JSON node to a JSON object by deep-copying the parameter.
*/
JsonObject &AsObject(const JsonObject &value);
/**
* Converts the current JSON node to a JSON object by moving from the parameter.
*/
JsonObject &AsObject(JsonObject &&value);
/**
* Returns true if the last parse request was successful.
*/
inline bool WasParseSuccessful() const { return m_value != nullptr; }
/**
* @deprecated
*/
const String &GetErrorMessage() const;
/**
* Creates a view of this JSON node.
*/
JsonView View() const;
private:
/**
* Construct a duplicate of this JSON value.
*/
JsonObject(const aws_json_value *valueToCopy);
/**
* Helper for all AsXYZ() functions.
* Destroys any pre-existing value and takes ownership of new value.
*/
JsonObject &AsNewValue(aws_json_value *valueToOwn);
/**
* Helper for all WithXZY() functions.
* Take ownership of new value and add at key, replacing any previous value.
* Converts this node to JSON object if necessary.
*/
JsonObject &WithNewKeyValue(const char *key, aws_json_value *valueToOwn);
/**
* Return new aws_json_value, an array containing duplicates of everything in objectsToCopy.
*/
static aws_json_value *NewArray(const Vector<JsonObject> &objectsToCopy);
/**
* Return new aws_json_value, an array which has taken ownership of everything in objectsToMove
*/
static aws_json_value *NewArray(Vector<JsonObject> &&objectsToMove);
aws_json_value *m_value;
/* Once upon a time each class instance had an m_errorMessage string member,
* and if parse failed the string would explain why.
* When we switched json implementations, there was no longer a unique string
* explaining why parse failed so we dropped that member from the class.
* To avoid breaking the GetErrorMessage() API, which returns the string by REFERENCE,
* we now use singletons that are created/destroyed along with library init/cleanup. */
static std::unique_ptr<String> s_errorMessage;
static std::unique_ptr<String> s_okMessage;
static void OnLibraryInit();
static void OnLibraryCleanup();
friend class JsonView;
friend class ApiHandle;
};
/**
* Provides read-only view to an existing JsonObject. This allows lightweight copying without making deep
* copies of the JsonObject.
* Note: This class does not extend the lifetime of the given JsonObject. It's your responsibility to ensure
* the lifetime of the JsonObject is extended beyond the lifetime of its view.
*/
class AWS_CRT_CPP_API JsonView
{
public:
/* constructors */
JsonView();
JsonView(const JsonObject &val);
JsonView &operator=(const JsonObject &val);
/**
* Gets a string from this node by its key.
*/
String GetString(const String &key) const;
/**
* Gets a string from this node by its key.
*/
String GetString(const char *key) const;
/**
* Returns the value of this node as a string.
* The behavior is undefined if the node is _not_ of type string.
*/
String AsString() const;
/**
* Gets a boolean value from this node by its key.
*/
bool GetBool(const String &key) const;
/**
* Gets a boolean value from this node by its key.
*/
bool GetBool(const char *key) const;
/**
* Returns the value of this node as a boolean.
*/
bool AsBool() const;
/**
* Gets an integer value from this node by its key.
* The integer is of the same size as an int on the machine.
*/
int GetInteger(const String &key) const;
/**
* Gets an integer value from this node by its key.
* The integer is of the same size as an int on the machine.
*/
int GetInteger(const char *key) const;
/**
* Returns the value of this node as an int.
*/
int AsInteger() const;
/**
* Gets a 64-bit integer value from this node by its key.
* The value is 64-bit regardless of the platform/machine.
*/
int64_t GetInt64(const String &key) const;
/**
* Gets a 64-bit integer value from this node by its key.
* The value is 64-bit regardless of the platform/machine.
*/
int64_t GetInt64(const char *key) const;
/**
* Returns the value of this node as 64-bit integer.
*/
int64_t AsInt64() const;
/**
* Gets a double precision floating-point value from this node by its key.
*/
double GetDouble(const String &key) const;
/**
* Gets a double precision floating-point value from this node by its key.
*/
double GetDouble(const char *key) const;
/**
* Returns the value of this node as a double precision floating-point.
*/
double AsDouble() const;
/**
* Gets an array of JsonView objects from this node by its key.
*/
Vector<JsonView> GetArray(const String &key) const;
/**
* Gets an array of JsonView objects from this node by its key.
*/
Vector<JsonView> GetArray(const char *key) const;
/**
* Returns the value of this node as an array of JsonView objects.
*/
Vector<JsonView> AsArray() const;
/**
* Gets a JsonView object from this node by its key.
*/
JsonView GetJsonObject(const String &key) const;
/**
* Gets a JsonView object from this node by its key.
*/
JsonView GetJsonObject(const char *key) const;
JsonObject GetJsonObjectCopy(const String &key) const;
JsonObject GetJsonObjectCopy(const char *key) const;
/**
* Returns the value of this node as a JsonView object.
*/
JsonView AsObject() const;
/**
* Reads all json objects at the top level of this node (does not traverse the tree any further)
* along with their keys.
*/
Map<String, JsonView> GetAllObjects() const;
/**
* Tests whether a value exists at the current node level for the given key.
* Returns true if a value has been found and its value is not null, false otherwise.
*/
bool ValueExists(const String &key) const;
/**
* Tests whether a value exists at the current node level for the given key.
* Returns true if a value has been found and its value is not null, false otherwise.
*/
bool ValueExists(const char *key) const;
/**
* Tests whether a key exists at the current node level.
*/
bool KeyExists(const String &key) const;
/**
* Tests whether a key exists at the current node level.
*/
bool KeyExists(const char *key) const;
/**
* Tests whether the current value is a JSON object.
*/
bool IsObject() const;
/**
* Tests whether the current value is a boolean.
*/
bool IsBool() const;
/**
* Tests whether the current value is a string.
*/
bool IsString() const;
/**
* Tests whether the current value is a number.
*/
bool IsNumber() const;
/**
* Tests whether the current value is a number that can convert to an int64_t without losing precision.
*/
bool IsIntegerType() const;
/**
* Tests whether the current value is a number that will lose precision if converted to an int64_t.
*/
bool IsFloatingPointType() const;
/**
* Tests whether the current value is a JSON array.
*/
bool IsListType() const;
/**
* Tests whether the current value is a JSON null.
*/
bool IsNull() const;
/**
* Writes the current JSON view without whitespace characters starting at the current level to a string.
* @param treatAsObject if the current value is empty, writes out '{}' rather than an empty string.
*/
String WriteCompact(bool treatAsObject = true) const;
/**
* Writes the current JSON view to a string in a human friendly format.
* @param treatAsObject if the current value is empty, writes out '{}' rather than an empty string.
*/
String WriteReadable(bool treatAsObject = true) const;
/**
* Creates a deep copy of the JSON value rooted in the current JSON view.
*/
JsonObject Materialize() const;
private:
JsonView(const aws_json_value *val);
String Write(bool treatAsObject, bool readable) const;
const aws_json_value *m_value;
};
} // namespace Crt
} // namespace Aws