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(ripple::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;
384
390 Members
391 getMemberNames() const;
392
394 toStyledString() const;
395
397 begin() const;
399 end() const;
400
402 begin();
404 end();
405
406 friend bool
407 operator==(Value const&, Value const&);
408 friend bool
409 operator<(Value const&, Value const&);
410
411private:
412 Value&
413 resolveReference(char const* key, bool isStatic);
414
415private:
426 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
427};
428
429inline Value
431{
432 return to_string(number);
433}
434
435bool
436operator==(Value const&, Value const&);
437
438inline bool
439operator!=(Value const& x, Value const& y)
440{
441 return !(x == y);
442}
443
444bool
445operator<(Value const&, Value const&);
446
447inline bool
448operator<=(Value const& x, Value const& y)
449{
450 return !(y < x);
451}
452
453inline bool
454operator>(Value const& x, Value const& y)
455{
456 return y < x;
457}
458
459inline bool
460operator>=(Value const& x, Value const& y)
461{
462 return !(x < y);
463}
464
474{
475public:
476 enum { unknown = (unsigned)-1 };
477
478 virtual ~ValueAllocator() = default;
479
480 virtual char*
481 makeMemberName(char const* memberName) = 0;
482 virtual void
483 releaseMemberName(char* memberName) = 0;
484 virtual char*
485 duplicateStringValue(char const* value, unsigned int length = unknown) = 0;
486 virtual void
487 releaseStringValue(char* value) = 0;
488};
489
494{
495public:
496 using size_t = unsigned int;
497 using difference_type = int;
499
501
502 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
503
504 bool
505 operator==(SelfType const& other) const
506 {
507 return isEqual(other);
508 }
509
510 bool
511 operator!=(SelfType const& other) const
512 {
513 return !isEqual(other);
514 }
515
518 Value
519 key() const;
520
522 UInt
523 index() const;
524
527 char const*
528 memberName() const;
529
530protected:
531 Value&
532 deref() const;
533
534 void
535 increment();
536
537 void
538 decrement();
539
541 computeDistance(SelfType const& other) const;
542
543 bool
544 isEqual(SelfType const& other) const;
545
546 void
547 copy(SelfType const& other);
548
549private:
550 Value::ObjectValues::iterator current_;
551 // Indicates that iterator is for a null value.
553};
554
559{
560 friend class Value;
561
562public:
563 using size_t = unsigned int;
564 using difference_type = int;
565 using reference = Value const&;
566 using pointer = Value const*;
568
570
571private:
574 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
575
576public:
577 SelfType&
578 operator=(ValueIteratorBase const& other);
579
582 {
583 SelfType temp(*this);
584 ++*this;
585 return temp;
586 }
587
590 {
591 SelfType temp(*this);
592 --*this;
593 return temp;
594 }
595
596 SelfType&
598 {
599 decrement();
600 return *this;
601 }
602
603 SelfType&
605 {
606 increment();
607 return *this;
608 }
609
611 operator*() const
612 {
613 return deref();
614 }
615};
616
620{
621 friend class Value;
622
623public:
624 using size_t = unsigned int;
625 using difference_type = int;
626 using reference = Value&;
627 using pointer = Value*;
629
630 ValueIterator() = default;
631 ValueIterator(ValueConstIterator const& other);
632 ValueIterator(ValueIterator const& other);
633
634private:
637 explicit ValueIterator(Value::ObjectValues::iterator const& current);
638
639public:
640 SelfType&
641 operator=(SelfType const& other);
642
645 {
646 SelfType temp(*this);
647 ++*this;
648 return temp;
649 }
650
653 {
654 SelfType temp(*this);
655 --*this;
656 return temp;
657 }
658
659 SelfType&
661 {
662 decrement();
663 return *this;
664 }
665
666 SelfType&
668 {
669 increment();
670 return *this;
671 }
672
674 operator*() const
675 {
676 return deref();
677 }
678};
679
680} // namespace Json
681
682#endif // CPPTL_JSON_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:474
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:559
SelfType & operator=(ValueIteratorBase const &other)
ValueConstIterator SelfType
Definition json_value.h:567
SelfType operator--(int)
Definition json_value.h:589
Value const & reference
Definition json_value.h:565
SelfType operator++(int)
Definition json_value.h:581
reference operator*() const
Definition json_value.h:611
base class for Value iterators.
Definition json_value.h:494
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:511
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition json_value.h:550
ValueIteratorBase SelfType
Definition json_value.h:498
bool operator==(SelfType const &other) const
Definition json_value.h:505
Iterator for object and array value.
Definition json_value.h:620
SelfType operator--(int)
Definition json_value.h:652
SelfType & operator=(SelfType const &other)
SelfType & operator--()
Definition json_value.h:660
reference operator*() const
Definition json_value.h:674
SelfType & operator++()
Definition json_value.h:667
ValueIterator SelfType
Definition json_value.h:628
unsigned int size_t
Definition json_value.h:624
ValueIterator()=default
SelfType operator++(int)
Definition json_value.h:644
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:425
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:448
bool operator>(Value const &x, Value const &y)
Definition json_value.h:454
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
Value to_json(ripple::Number const &number)
Definition json_value.h:430
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:460
unsigned int UInt