rippled
Loading...
Searching...
No Matches
json_value.h
1#ifndef XRPL_JSON_JSON_VALUE_H_INCLUDED
2#define XRPL_JSON_JSON_VALUE_H_INCLUDED
3
4#include <xrpl/basics/Number.h>
5#include <xrpl/json/json_forwards.h>
6
7#include <cstring>
8#include <limits>
9#include <map>
10#include <string>
11#include <vector>
12
15namespace Json {
16
29
45{
46public:
47 constexpr explicit StaticString(char const* czString) : str_(czString)
48 {
49 }
50
51 constexpr
52 operator char const*() const
53 {
54 return str_;
55 }
56
57 constexpr char const*
58 c_str() const
59 {
60 return str_;
61 }
62
63private:
64 char const* str_;
65};
66
67inline bool
69{
70 return strcmp(x.c_str(), y.c_str()) == 0;
71}
72
73inline bool
75{
76 return !(x == y);
77}
78
79inline bool
81{
82 return strcmp(x.c_str(), y.c_str()) == 0;
83}
84
85inline bool
87{
88 return !(x == y);
89}
90
91inline bool
93{
94 return y == x;
95}
96
97inline bool
99{
100 return !(y == x);
101}
102
130class Value
131{
132 friend class ValueIteratorBase;
133
134public:
139 using Int = Json::Int;
141
142 static Value const null;
146
147private:
149 {
150 public:
156 CZString(int index);
157 CZString(char const* cstr, DuplicationPolicy allocate);
158 CZString(CZString const& other);
159 ~CZString();
160 CZString&
161 operator=(CZString const& other) = delete;
162 bool
163 operator<(CZString const& other) const;
164 bool
165 operator==(CZString const& other) const;
166 int
167 index() const;
168 char const*
169 c_str() const;
170 bool
171 isStaticString() const;
172
173 private:
174 char const* cstr_;
176 };
177
178public:
180
181public:
198 Value(Int value);
199 Value(UInt value);
200 Value(double value);
201 Value(char const* value);
202 Value(xrpl::Number const& value);
214 Value(StaticString const& value);
215 Value(std::string const& value);
216 Value(bool value);
217 Value(Value const& other);
218 ~Value();
219
220 Value&
221 operator=(Value const& other);
222 Value&
223 operator=(Value&& other);
224
225 Value(Value&& other) noexcept;
226
228 void
229 swap(Value& other) noexcept;
230
232 type() const;
233
234 char const*
235 asCString() const;
238 asString() const;
239 Int
240 asInt() const;
241 UInt
242 asUInt() const;
243 double
244 asDouble() const;
245 bool
246 asBool() const;
247
249 UInt
250 asAbsUInt() const;
251
252 // TODO: What is the "empty()" method this docstring mentions?
255 bool
256 isNull() const;
257 bool
258 isBool() const;
259 bool
260 isInt() const;
261 bool
262 isUInt() const;
263 bool
264 isIntegral() const;
265 bool
266 isDouble() const;
267 bool
268 isNumeric() const;
269 bool
270 isString() const;
271 bool
272 isArray() const;
273 bool
274 isArrayOrNull() const;
275 bool
276 isObject() const;
277 bool
278 isObjectOrNull() const;
279
280 bool
281 isConvertibleTo(ValueType other) const;
282
284 UInt
285 size() const;
286
289 explicit
290 operator bool() const;
291
295 void
296 clear();
297
303 Value&
304 operator[](UInt index);
308 Value const&
309 operator[](UInt index) const;
312 Value
313 get(UInt index, Value const& defaultValue) const;
315 bool
316 isValidIndex(UInt index) const;
320 Value&
321 append(Value const& value);
322 Value&
323 append(Value&& value);
324
327 Value&
328 operator[](char const* key);
331 Value const&
332 operator[](char const* key) const;
335 Value&
336 operator[](std::string const& key);
339 Value const&
340 operator[](std::string const& key) const;
354 Value&
355 operator[](StaticString const& key);
356 Value const&
357 operator[](StaticString const& key) const;
358
360 Value
361 get(char const* key, Value const& defaultValue) const;
363 Value
364 get(std::string const& key, Value const& defaultValue) const;
365
372 Value
373 removeMember(char const* key);
375 Value
376 removeMember(std::string const& key);
377
379 bool
380 isMember(char const* key) const;
382 bool
383 isMember(std::string const& key) const;
385 bool
386 isMember(StaticString const& key) const;
387
393 Members
394 getMemberNames() const;
395
397 toStyledString() const;
398
400 begin() const;
402 end() const;
403
405 begin();
407 end();
408
409 friend bool
410 operator==(Value const&, Value const&);
411 friend bool
412 operator<(Value const&, Value const&);
413
414private:
415 Value&
416 resolveReference(char const* key, bool isStatic);
417
418private:
429 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
430};
431
432inline Value
433to_json(xrpl::Number const& number)
434{
435 return to_string(number);
436}
437
438bool
439operator==(Value const&, Value const&);
440
441inline bool
442operator!=(Value const& x, Value const& y)
443{
444 return !(x == y);
445}
446
447bool
448operator<(Value const&, Value const&);
449
450inline bool
451operator<=(Value const& x, Value const& y)
452{
453 return !(y < x);
454}
455
456inline bool
457operator>(Value const& x, Value const& y)
458{
459 return y < x;
460}
461
462inline bool
463operator>=(Value const& x, Value const& y)
464{
465 return !(x < y);
466}
467
477{
478public:
479 enum { unknown = (unsigned)-1 };
480
481 virtual ~ValueAllocator() = default;
482
483 virtual char*
484 makeMemberName(char const* memberName) = 0;
485 virtual void
486 releaseMemberName(char* memberName) = 0;
487 virtual char*
488 duplicateStringValue(char const* value, unsigned int length = unknown) = 0;
489 virtual void
490 releaseStringValue(char* value) = 0;
491};
492
497{
498public:
499 using size_t = unsigned int;
500 using difference_type = int;
502
504
505 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
506
507 bool
508 operator==(SelfType const& other) const
509 {
510 return isEqual(other);
511 }
512
513 bool
514 operator!=(SelfType const& other) const
515 {
516 return !isEqual(other);
517 }
518
521 Value
522 key() const;
523
525 UInt
526 index() const;
527
530 char const*
531 memberName() const;
532
533protected:
534 Value&
535 deref() const;
536
537 void
538 increment();
539
540 void
541 decrement();
542
544 computeDistance(SelfType const& other) const;
545
546 bool
547 isEqual(SelfType const& other) const;
548
549 void
550 copy(SelfType const& other);
551
552private:
553 Value::ObjectValues::iterator current_;
554 // Indicates that iterator is for a null value.
556};
557
562{
563 friend class Value;
564
565public:
566 using size_t = unsigned int;
567 using difference_type = int;
568 using reference = Value const&;
569 using pointer = Value const*;
571
573
574private:
577 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
578
579public:
580 SelfType&
581 operator=(ValueIteratorBase const& other);
582
585 {
586 SelfType temp(*this);
587 ++*this;
588 return temp;
589 }
590
593 {
594 SelfType temp(*this);
595 --*this;
596 return temp;
597 }
598
599 SelfType&
601 {
602 decrement();
603 return *this;
604 }
605
606 SelfType&
608 {
609 increment();
610 return *this;
611 }
612
614 operator*() const
615 {
616 return deref();
617 }
618};
619
623{
624 friend class Value;
625
626public:
627 using size_t = unsigned int;
628 using difference_type = int;
629 using reference = Value&;
630 using pointer = Value*;
632
633 ValueIterator() = default;
634 ValueIterator(ValueConstIterator const& other);
635 ValueIterator(ValueIterator const& other);
636
637private:
640 explicit ValueIterator(Value::ObjectValues::iterator const& current);
641
642public:
643 SelfType&
644 operator=(SelfType const& other);
645
648 {
649 SelfType temp(*this);
650 ++*this;
651 return temp;
652 }
653
656 {
657 SelfType temp(*this);
658 --*this;
659 return temp;
660 }
661
662 SelfType&
664 {
665 decrement();
666 return *this;
667 }
668
669 SelfType&
671 {
672 increment();
673 return *this;
674 }
675
677 operator*() const
678 {
679 return deref();
680 }
681};
682
683} // namespace Json
684
685#endif // XRPL_JSON_JSON_VALUE_H_INCLUDED
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:45
constexpr char const * c_str() const
Definition json_value.h:58
char const * str_
Definition json_value.h:64
constexpr StaticString(char const *czString)
Definition json_value.h:47
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition json_value.h:477
virtual char * makeMemberName(char const *memberName)=0
virtual ~ValueAllocator()=default
virtual void releaseStringValue(char *value)=0
virtual char * duplicateStringValue(char const *value, unsigned int length=unknown)=0
virtual void releaseMemberName(char *memberName)=0
const iterator for object and array value.
Definition json_value.h:562
SelfType & operator=(ValueIteratorBase const &other)
ValueConstIterator SelfType
Definition json_value.h:570
SelfType operator--(int)
Definition json_value.h:592
Value const & reference
Definition json_value.h:568
SelfType operator++(int)
Definition json_value.h:584
reference operator*() const
Definition json_value.h:614
base class for Value iterators.
Definition json_value.h:497
void copy(SelfType const &other)
Value key() const
Return either the index or the member name of the referenced value as a Value.
bool isEqual(SelfType const &other) const
char const * memberName() const
Return the member name of the referenced Value.
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
bool operator!=(SelfType const &other) const
Definition json_value.h:514
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition json_value.h:553
ValueIteratorBase SelfType
Definition json_value.h:501
bool operator==(SelfType const &other) const
Definition json_value.h:508
Iterator for object and array value.
Definition json_value.h:623
SelfType operator--(int)
Definition json_value.h:655
SelfType & operator=(SelfType const &other)
SelfType & operator--()
Definition json_value.h:663
reference operator*() const
Definition json_value.h:677
SelfType & operator++()
Definition json_value.h:670
ValueIterator SelfType
Definition json_value.h:631
unsigned int size_t
Definition json_value.h:627
ValueIterator()=default
SelfType operator++(int)
Definition json_value.h:647
bool isStaticString() const
CZString & operator=(CZString const &other)=delete
char const * c_str() const
Represents a JSON value.
Definition json_value.h:131
const_iterator begin() const
Json::UInt UInt
Definition json_value.h:138
bool isArray() const
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
std::string toStyledString() const
const_iterator end() const
bool isObjectOrNull() const
static Value const null
Definition json_value.h:142
bool isDouble() const
void clear()
Remove all object members and array elements.
char const * asCString() const
Int asInt() const
union Json::Value::ValueHolder value_
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
bool isString() const
UInt ArrayIndex
Definition json_value.h:140
friend bool operator==(Value const &, Value const &)
UInt asUInt() const
Members getMemberNames() const
Return a list of the member names.
ValueType type() const
bool isObject() const
Value & operator=(Value const &other)
Value removeMember(char const *key)
Remove and return the named member.
void swap(Value &other) noexcept
Swap values.
Json::Int Int
Definition json_value.h:139
bool isValidIndex(UInt index) const
Return true if index < size().
std::string asString() const
Returns the unquoted string value.
bool isBool() const
static constexpr Int maxInt
Definition json_value.h:144
bool isIntegral() const
bool asBool() const
ValueType type_
Definition json_value.h:428
bool isUInt() const
bool isNull() const
isNull() tests to see if this field is null.
bool isMember(char const *key) const
Return true if the object has a member named key.
bool isArrayOrNull() const
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
friend bool operator<(Value const &, Value const &)
static constexpr Int minInt
Definition json_value.h:143
Value & resolveReference(char const *key, bool isStatic)
bool isConvertibleTo(ValueType other) const
bool isNumeric() const
double asDouble() const
Value & operator[](UInt index)
Access an array element (zero based index ).
static constexpr UInt maxUInt
Definition json_value.h:145
bool isInt() const
T max(T... args)
T min(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:6
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:451
bool operator>(Value const &x, Value const &y)
Definition json_value.h:457
Value to_json(xrpl::Number const &number)
Definition json_value.h:433
std::string to_string(Value const &)
Writes a Json::Value to an std::string.
Definition to_string.cpp:9
bool operator!=(StaticString x, StaticString y)
Definition json_value.h:74
ValueType
Type of the value held by a Value object.
Definition json_value.h:19
@ booleanValue
bool value
Definition json_value.h:25
@ nullValue
'null' value
Definition json_value.h:20
@ stringValue
UTF-8 string value.
Definition json_value.h:24
@ realValue
double value
Definition json_value.h:23
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ intValue
signed integer value
Definition json_value.h:21
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
@ uintValue
unsigned integer value
Definition json_value.h:22
bool operator<(Value const &, Value const &)
bool operator==(StaticString x, StaticString y)
Definition json_value.h:68
int Int
bool operator>=(Value const &x, Value const &y)
Definition json_value.h:463
unsigned int UInt