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 <map>
9#include <string>
10#include <vector>
11
14namespace Json {
15
28
44{
45public:
46 constexpr explicit StaticString(char const* czstring) : str_(czstring)
47 {
48 }
49
50 constexpr
51 operator char const*() const
52 {
53 return str_;
54 }
55
56 constexpr char const*
57 c_str() const
58 {
59 return str_;
60 }
61
62private:
63 char const* str_;
64};
65
66inline bool
68{
69 return strcmp(x.c_str(), y.c_str()) == 0;
70}
71
72inline bool
74{
75 return !(x == y);
76}
77
78inline bool
80{
81 return strcmp(x.c_str(), y.c_str()) == 0;
82}
83
84inline bool
86{
87 return !(x == y);
88}
89
90inline bool
92{
93 return y == x;
94}
95
96inline bool
98{
99 return !(y == x);
100}
101
129class Value
130{
131 friend class ValueIteratorBase;
132
133public:
138 using Int = Json::Int;
140
141 static Value const null;
142 static Int const minInt;
143 static Int const maxInt;
144 static UInt const maxUInt;
145
146private:
148 {
149 public:
155 CZString(int index);
156 CZString(char const* cstr, DuplicationPolicy allocate);
157 CZString(CZString const& other);
158 ~CZString();
159 CZString&
160 operator=(CZString const& other) = delete;
161 bool
162 operator<(CZString const& other) const;
163 bool
164 operator==(CZString const& other) const;
165 int
166 index() const;
167 char const*
168 c_str() const;
169 bool
170 isStaticString() const;
171
172 private:
173 char const* cstr_;
175 };
176
177public:
179
180public:
197 Value(Int value);
198 Value(UInt value);
199 Value(double value);
200 Value(char const* value);
201 Value(ripple::Number const& value);
213 Value(StaticString const& value);
214 Value(std::string const& value);
215 Value(bool value);
216 Value(Value const& other);
217 ~Value();
218
219 Value&
220 operator=(Value const& other);
221 Value&
222 operator=(Value&& other);
223
224 Value(Value&& other) noexcept;
225
227 void
228 swap(Value& other) noexcept;
229
231 type() const;
232
233 char const*
234 asCString() const;
237 asString() const;
238 Int
239 asInt() const;
240 UInt
241 asUInt() const;
242 double
243 asDouble() const;
244 bool
245 asBool() const;
246
247 // TODO: What is the "empty()" method this docstring mentions?
250 bool
251 isNull() const;
252 bool
253 isBool() const;
254 bool
255 isInt() const;
256 bool
257 isUInt() const;
258 bool
259 isIntegral() const;
260 bool
261 isDouble() const;
262 bool
263 isNumeric() const;
264 bool
265 isString() const;
266 bool
267 isArray() const;
268 bool
269 isArrayOrNull() const;
270 bool
271 isObject() const;
272 bool
273 isObjectOrNull() const;
274
275 bool
276 isConvertibleTo(ValueType other) const;
277
279 UInt
280 size() const;
281
284 explicit
285 operator bool() const;
286
290 void
291 clear();
292
298 Value&
299 operator[](UInt index);
303 Value const&
304 operator[](UInt index) const;
307 Value
308 get(UInt index, Value const& defaultValue) const;
310 bool
311 isValidIndex(UInt index) const;
315 Value&
316 append(Value const& value);
317 Value&
318 append(Value&& value);
319
322 Value&
323 operator[](char const* key);
326 Value const&
327 operator[](char const* key) const;
330 Value&
331 operator[](std::string const& key);
334 Value const&
335 operator[](std::string const& key) const;
349 Value&
350 operator[](StaticString const& key);
351 Value const&
352 operator[](StaticString const& key) const;
353
355 Value
356 get(char const* key, Value const& defaultValue) const;
358 Value
359 get(std::string const& key, Value const& defaultValue) const;
360
367 Value
368 removeMember(char const* key);
370 Value
371 removeMember(std::string const& key);
372
374 bool
375 isMember(char const* key) const;
377 bool
378 isMember(std::string const& key) const;
379
385 Members
386 getMemberNames() const;
387
389 toStyledString() const;
390
392 begin() const;
394 end() const;
395
397 begin();
399 end();
400
401 friend bool
402 operator==(Value const&, Value const&);
403 friend bool
404 operator<(Value const&, Value const&);
405
406private:
407 Value&
408 resolveReference(char const* key, bool isStatic);
409
410private:
421 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
422};
423
424inline Value
426{
427 return to_string(number);
428}
429
430bool
431operator==(Value const&, Value const&);
432
433inline bool
434operator!=(Value const& x, Value const& y)
435{
436 return !(x == y);
437}
438
439bool
440operator<(Value const&, Value const&);
441
442inline bool
443operator<=(Value const& x, Value const& y)
444{
445 return !(y < x);
446}
447
448inline bool
449operator>(Value const& x, Value const& y)
450{
451 return y < x;
452}
453
454inline bool
455operator>=(Value const& x, Value const& y)
456{
457 return !(x < y);
458}
459
469{
470public:
471 enum { unknown = (unsigned)-1 };
472
473 virtual ~ValueAllocator() = default;
474
475 virtual char*
476 makeMemberName(char const* memberName) = 0;
477 virtual void
478 releaseMemberName(char* memberName) = 0;
479 virtual char*
480 duplicateStringValue(char const* value, unsigned int length = unknown) = 0;
481 virtual void
482 releaseStringValue(char* value) = 0;
483};
484
489{
490public:
491 using size_t = unsigned int;
492 using difference_type = int;
494
496
497 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
498
499 bool
500 operator==(SelfType const& other) const
501 {
502 return isEqual(other);
503 }
504
505 bool
506 operator!=(SelfType const& other) const
507 {
508 return !isEqual(other);
509 }
510
513 Value
514 key() const;
515
517 UInt
518 index() const;
519
522 char const*
523 memberName() const;
524
525protected:
526 Value&
527 deref() const;
528
529 void
530 increment();
531
532 void
533 decrement();
534
536 computeDistance(SelfType const& other) const;
537
538 bool
539 isEqual(SelfType const& other) const;
540
541 void
542 copy(SelfType const& other);
543
544private:
545 Value::ObjectValues::iterator current_;
546 // Indicates that iterator is for a null value.
548};
549
554{
555 friend class Value;
556
557public:
558 using size_t = unsigned int;
559 using difference_type = int;
560 using reference = Value const&;
561 using pointer = Value const*;
563
565
566private:
569 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
570
571public:
572 SelfType&
573 operator=(ValueIteratorBase const& other);
574
577 {
578 SelfType temp(*this);
579 ++*this;
580 return temp;
581 }
582
585 {
586 SelfType temp(*this);
587 --*this;
588 return temp;
589 }
590
591 SelfType&
593 {
594 decrement();
595 return *this;
596 }
597
598 SelfType&
600 {
601 increment();
602 return *this;
603 }
604
606 operator*() const
607 {
608 return deref();
609 }
610};
611
615{
616 friend class Value;
617
618public:
619 using size_t = unsigned int;
620 using difference_type = int;
621 using reference = Value&;
622 using pointer = Value*;
624
625 ValueIterator() = default;
626 ValueIterator(ValueConstIterator const& other);
627 ValueIterator(ValueIterator const& other);
628
629private:
632 explicit ValueIterator(Value::ObjectValues::iterator const& current);
633
634public:
635 SelfType&
636 operator=(SelfType const& other);
637
640 {
641 SelfType temp(*this);
642 ++*this;
643 return temp;
644 }
645
648 {
649 SelfType temp(*this);
650 --*this;
651 return temp;
652 }
653
654 SelfType&
656 {
657 decrement();
658 return *this;
659 }
660
661 SelfType&
663 {
664 increment();
665 return *this;
666 }
667
669 operator*() const
670 {
671 return deref();
672 }
673};
674
675} // namespace Json
676
677#endif // CPPTL_JSON_H_INCLUDED
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:44
constexpr char const * c_str() const
Definition json_value.h:57
char const * str_
Definition json_value.h:63
constexpr StaticString(char const *czstring)
Definition json_value.h:46
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition json_value.h:469
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:554
SelfType & operator=(ValueIteratorBase const &other)
ValueConstIterator SelfType
Definition json_value.h:562
SelfType operator--(int)
Definition json_value.h:584
Value const & reference
Definition json_value.h:560
SelfType operator++(int)
Definition json_value.h:576
reference operator*() const
Definition json_value.h:606
base class for Value iterators.
Definition json_value.h:489
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:506
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition json_value.h:545
ValueIteratorBase SelfType
Definition json_value.h:493
bool operator==(SelfType const &other) const
Definition json_value.h:500
Iterator for object and array value.
Definition json_value.h:615
SelfType operator--(int)
Definition json_value.h:647
SelfType & operator=(SelfType const &other)
SelfType & operator--()
Definition json_value.h:655
reference operator*() const
Definition json_value.h:669
SelfType & operator++()
Definition json_value.h:662
ValueIterator SelfType
Definition json_value.h:623
unsigned int size_t
Definition json_value.h:619
ValueIterator()=default
SelfType operator++(int)
Definition json_value.h:639
bool isStaticString() const
CZString & operator=(CZString const &other)=delete
char const * c_str() const
Represents a JSON value.
Definition json_value.h:130
const_iterator begin() const
Json::UInt UInt
Definition json_value.h:137
bool isArray() const
static Int const minInt
Definition json_value.h:142
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:141
bool isDouble() const
static UInt const maxUInt
Definition json_value.h:144
void clear()
Remove all object members and array elements.
char const * asCString() const
Int asInt() const
union Json::Value::ValueHolder value_
bool isString() const
UInt ArrayIndex
Definition json_value.h:139
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:138
bool isValidIndex(UInt index) const
Return true if index < size().
std::string asString() const
Returns the unquoted string value.
bool isBool() const
bool isIntegral() const
bool asBool() const
ValueType type_
Definition json_value.h:420
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 Int const maxInt
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 ).
bool isInt() const
JSON (JavaScript Object Notation).
Definition json_errors.h:6
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:443
bool operator>(Value const &x, Value const &y)
Definition json_value.h:449
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:73
Value to_json(ripple::Number const &number)
Definition json_value.h:425
ValueType
Type of the value held by a Value object.
Definition json_value.h:18
@ booleanValue
bool value
Definition json_value.h:24
@ nullValue
'null' value
Definition json_value.h:19
@ stringValue
UTF-8 string value.
Definition json_value.h:23
@ realValue
double value
Definition json_value.h:22
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ intValue
signed integer value
Definition json_value.h:20
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
@ uintValue
unsigned integer value
Definition json_value.h:21
bool operator<(Value const &, Value const &)
bool operator==(StaticString x, StaticString y)
Definition json_value.h:67
int Int
bool operator>=(Value const &x, Value const &y)
Definition json_value.h:455
unsigned int UInt