-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.h
576 lines (526 loc) · 14.4 KB
/
json.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#ifndef _json_hpp__
#define _json_hpp__
// Copyright (c) 2012, Andre Caron (andre.l.caron@gmail.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*!
* @file json.h
* @brief C++ interface to the sJSON library.
*/
#include "sjson.h"
#include "eastl/string.h"
#include "eastl/vector.h"
#include "eastl/extra/murmurhash.h"
namespace json {
/*!
* @brief Dynamically typed value.
*
* @note Instances of this class must be entirely scoped within the
* lifetime of the root @c Document object from which they are extracted.
*/
class Any {
private:
::sJSON * myData;
/* construction. */
public:
/*!
* @internal
* @brief Wraps the underlying implementation.
* @param data Handle to the JSON data structure.
*/
explicit Any(::sJSON * data)
: myData(data)
{}
/* methods. */
public:
/*!
* @internal
* @brief Access the underlying implementation.
* @return Handle to the JSON data structure.
*/
::sJSON *data() const {
return myData;
}
/*!
* @brief Checks if the value is null.
* @return @c true if the value is null, else @c false.
*/
bool isNull() const {
return (myData->type == sJSON_NULL);
}
/*!
* @brief Checks if the value is a boolean.
* @return @c true if the value is a boolean, else @c false.
*
* @see operator bool()
*/
bool isBool() const {
return ((myData->type == sJSON_True) || (myData->type == sJSON_False));
}
/*!
* @brief Checks if the value is a number.
* @return @c true if the value is a number, else @c false.
*
* @note There is no way to determine if the value is an integer or a
* real number. The application should determine if it can deal with
* real values or if it only accepts integers.
*
* @see operator int()
* @see operator double()
*/
bool isNumber() const {
return (myData->type == sJSON_Number);
}
/*!
* @brief Checks if the value is a string.
* @return @c true if the value is a string, else @c false.
*
* @see operator eastl::string()
*/
bool isString() const {
return (myData->type == sJSON_String);
}
/*!
* @brief Checks if the value is a list.
* @return @c true if the value is a list, else @c false.
*
* @see Array::Array(const Any&)
*/
bool isArray() const {
return (myData->type == sJSON_Array);
}
/*!
* @brief Checks if the value is a map.
* @return @c true if the value is a map, else @c false.
*
* @see Map::Map(const Any&)
*/
bool isMap() const {
return (myData->type == sJSON_Object);
}
/* operators. */
public:
/*!
* @brief Interpret the value as a boolean.
* @return The underlying boolean value.
*
* @pre isBool()
*/
// operator bool() const {
// switch (myData->type) {
// case sJSON_False: {
// return (false);
// }
// case sJSON_True: {
// return (true);
// }
// default: {
// XASSERT(0, "bool-operator used, but json-object is not bool");
// }
// }
// }
bool asBool() const {
switch (myData->type) {
case sJSON_False: {
return (false);
}
case sJSON_True: {
return (true);
}
default: {
XASSERT(0, "bool-operator used, but json-object is not bool");
}
}
}
/*!
* @brief Interpret the value as an integer.
* @return The underlying integer value.
*
* @pre isNumber()
*
* @see operator double()
*/
// operator int() const {
// XASSERT(isNumber(), "operator int used on non int json object");
// return (myData->valueInt);
// }
int asInt() const {
XASSERT(isNumber(), "operator int used on non int json object");
return (myData->valueInt);
}
/*!
* @brief Interpret the value as a real number.
* @return The underlying real number.
*
* @pre isNumber()
*
* @see operator int()
*/
// operator double() const {
// XASSERT(isNumber(), "operator double used on non double json object");
// return (myData->valueDouble);
// }
double asDouble() const {
XASSERT(isNumber(), "operator double used on non double json object");
return (myData->valueDouble);
}
/*!
* @brief Interpret the value as a string.
* @return The underlying string.
*
* @pre isString()
*/
// operator eastl::string() const {
// XASSERT(isString(), "operator string used on non string json object");
// return (myData->valueString);
// }
eastl::string asString() const {
XASSERT(isString(), "operator string used on non string json object");
return myData->valueString;
}
};
/*!
* @brief Parser, placeholder for document root object.
*
* @note Instances of this class must outlive the lifetime of @c Any, @c
* Array and @c Map objects extracted from it.
*/
class Document {
/* class methods. */
private:
/*!
* @internal
* @brief Parse the JSON document in @a text.
* @param text Serialized JSON document.
* @return A handle to the JSON data structure.
*/
static ::sJSON* parse(const eastl::string& text) {
::sJSON *const root = ::sJSONparse(text.c_str());
XASSERT(root != nullptr, "json parse error!");
// sJSONgetErrorPtr()
return root;
}
/* data. */
private:
::sJSON *const myData;
/* construction. */
public:
/*!
* @brief Parse the JSON document in @a text.
* @param text Serialized JSON document.
*/
explicit Document(const eastl::string& text)
: myData(parse(text.c_str()))
{}
private:
Document(const Document&);
public:
/*!
* @brief Release the memory held by the underlying data structure.
*/
~Document() {
::sJSONdelete(myData);
}
/* methods. */
public:
/*!
* @internal
* @brief Access the underlying implementation.
* @return Handle to the JSON data structure.
*
* @note The data may be a list or a map.
*/
::sJSON* data() const {
return (myData);
}
/*!
* @brief Checks if the root object is an array.
*
* @see isMap()
* @see Array(Document&)
*/
bool isArray() const {
return (myData->type == sJSON_Array);
}
/*!
* @brief Checks if the root object is a map.
*
* @see isArray()
* @see Map(Document&)
*/
bool isMap() const {
return (myData->type == sJSON_Object);
}
/* operators. */
private:
Document& operator= (const Document&);
};
/*!
* @brief Ordered group of values.
*
* @note Instances of this class must be entirely scoped within the
* lifetime of the root @c Document object from which they are extracted.
*/
class Array {
/* data. */
private:
::sJSON * myData;
/* construction. */
public:
/*!
* @internal
* @brief Wraps the underlying implementation.
* @param data Handle to the JSON data structure.
*
* @pre data->type == sJSON_Array
*/
explicit Array(::sJSON* data)
: myData(data)
{
XASSERT(myData->type == sJSON_Array, "json object is not an array");
}
/*!
* @brief Extract the root object of @a document as an array.
* @param document Document who'se root object we're interested in.
*
* @pre The document's root object is a list.
*
* @see Map(Document&)
*/
explicit Array(Document& document)
: myData(document.data())
{
XASSERT(myData->type == sJSON_Array, "json object is not an array");
}
/*!
* @brief Converts from a dynamically typed value.
* @param object Value to interpret as an @c Array.
*
* @pre object.isList() == true
*/
Array(const Any& object)
: myData(object.data())
{
XASSERT(myData->type == sJSON_Array, "json object is not an array");
}
/* methods. */
public:
/*!
* @internal
* @brief Access the underlying implementation.
* @return Handle to the JSON data structure.
*/
::sJSON* data() const {
return myData;
}
/*!
* @brief Obtain the number of items in the array.
* @return Number of items in the array.
*/
uint_t size() const {
return ::sJSONgetArraySize(myData);
}
/* operators. */
public:
/*!
* @brief Convert to dynamically typed value.
*/
operator Any() const {
return (Any(myData));
}
/*!
* @brief Access a field by position.
* @param key Position of the field to extract.
* @return The field value.
*/
Any operator[](int key) const
{
::sJSON *const item = ::sJSONgetArrayItem(myData, key);
XASSERT(item != 0, "json array item not found");
return (Any(item));
}
};
/*!
* @brief Group of named values.
*
* @note Instances of this class must be entirely scoped within the
* lifetime of the root @c Document object from which they are extracted.
*/
class Map {
/* data. */
private:
::sJSON* myData;
/* construction. */
public:
/*!
* @internal
* @brief Wraps the underlying implementation.
* @param data Handle to the JSON data structure.
*
* @pre data->type == sJSON_Object
*/
explicit Map(::sJSON * data)
: myData(data)
{
XASSERT(myData->type == sJSON_Object, "json object is not map");
}
/*!
* @brief Extract the root object of @a document as a map.
* @param document Document who'se root object we're interested in.
*
* @pre The document's root object is a map.
*
* @see Map(Document&)
*/
explicit Map(Document& document)
: myData(document.data())
{
XASSERT(myData->type == sJSON_Object, "json object is not map");
}
/*!
* @brief Converts from a dynamically typed value.
* @param object Value to interpret as a @c Map.
*
* @pre object.isMap() == true
*/
Map(const Any& object)
: myData(object.data())
{
XASSERT(myData->type == sJSON_Object, "json object is not map");
}
/* operators. */
public:
/*!
* @internal
* @brief Access the underlying implementation.
* @return Handle to the JSON data structure.
*/
::sJSON* data() const {
return myData;
}
/*!
* @brief Convert to dynamically typed value.
*/
operator Any() const {
return (Any(myData));
}
/*!
* @brief Access a field by name.
* @param key Name of the field to extract.
* @return The field value.
*/
Any operator[](const eastl::FixedMurmurHash key) const
{
::sJSON *const item = ::sJSONgetObjectItem(myData, key);
XASSERT(item != 0, "json map item not found");
return (Any(item));
}
bool hasMapMember(uint32_t nameHash) const {
if(sJSONgetObjectItem(myData, nameHash) != nullptr)
return true;
return false;
}
bool hasMapMember(eastl::FixedMurmurHash nameHash) const {
if(sJSONgetObjectItem(myData, nameHash) != nullptr)
return true;
return false;
}
};
// // Forward declared.
// std::ostream& operator<< (std::ostream& stream, const Any& value);
// /*!
// * @brief Serialize @a list.
// * @param stream The output stream.
// * @param list The value to serialize.
// * @return @a stream
// */
// std::ostream& operator<< (std::ostream& stream, const List& list)
// {
// stream << '[';
// for (int i=0; (i < list.size()-1); ++i) {
// stream << list[i] << ',';
// }
// if (list.size() > 0) {
// stream << list[list.size()-1];
// }
// return (stream << ']');
// }
// /*!
// * @brief Serialize @a map.
// * @param stream The output stream.
// * @param map The value to serialize.
// * @return @a stream
// */
// std::ostream& operator<< (std::ostream& stream, const Map& map)
// {
// stream << '{';
// ::sJSON * node = map.data()->child;
// for (; (node != 0); node = node->next)
// {
// stream
// << "\"" << node->string << "\":" << Any(node);
// if (node->next != 0) {
// stream << ",";
// }
// }
// return (stream << '}');
// }
// /*!
// * @brief Serialize @a value.
// * @param stream The output stream.
// * @param value The value to serialize.
// * @return @a stream
// *
// * @bug Boolean values output depends on @c std::boolalpha and locale
// * settings. They should always be serialized as @c "true" and @c
// * "false".
// * @bug Double quotes are not escaped in strings.
// */
// std::ostream& operator<< (std::ostream& stream, const Any& value)
// {
// if (value.is_null()) {
// return (stream << "null");
// }
// if (value.is_bool()) {
// // TODO: boolalpha?
// return (stream << bool(value));
// }
// if (value.is_number()) {
// return (stream << double(value));
// }
// if (value.is_string()) {
// // TODO: escape double quotes.
// return (stream << '"' << std::string(value) << '"');
// }
// if (value.is_list()) {
// return (stream << List(value));
// }
// if (value.is_map()) {
// return (stream << Map(value));
// }
// return (stream);
// }
}
#endif /* _json_hpp__ */