xrpld
Loading...
Searching...
No Matches
json_value.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/json/json_forwards.h>
5
6#include <cstring>
7#include <iterator>
8#include <limits>
9#include <map>
10#include <string>
11#include <vector>
12
16namespace json {
17
31
48{
49public:
50 constexpr explicit StaticString(char const* czString) : str_(czString)
51 {
52 }
53
54 constexpr
55 operator char const*() const
56 {
57 return str_;
58 }
59
60 [[nodiscard]] constexpr char const*
61 cStr() const
62 {
63 return str_;
64 }
65
66private:
67 char const* str_;
68};
69
70inline bool
72{
73 return strcmp(x.cStr(), y.cStr()) == 0;
74}
75
76inline bool
78{
79 return strcmp(x.c_str(), y.cStr()) == 0;
80}
81
82inline bool
84{
85 return y == x;
86}
87
116class Value
117{
118 friend class ValueIteratorBase;
119
120public:
125 using Int = json::Int;
127
128 static Value const kNull;
132
133private:
135 {
136 public:
138
139 CZString(int index);
140 CZString(char const* cstr, DuplicationPolicy allocate);
141 CZString(CZString const& other);
142 ~CZString();
143 CZString&
144 operator=(CZString const& other) = delete;
145 bool
146 operator<(CZString const& other) const;
147 bool
148 operator==(CZString const& other) const;
149 [[nodiscard]] int
150 index() const;
151 [[nodiscard]] char const*
152 cStr() const;
153 [[nodiscard]] bool
154 isStaticString() const;
155
156 private:
157 char const* cstr_;
159 };
160
161public:
163
164public:
182 Value(Int value);
183 Value(UInt value);
184 Value(double value);
185 Value(char const* value);
186 Value(xrpl::Number const& value);
199 Value(StaticString const& value);
200 Value(std::string const& value);
201 Value(bool value);
202 Value(Value const& other);
203 ~Value();
204
205 Value&
206 operator=(Value const& other);
207 Value&
208 operator=(Value&& other);
209
210 Value(Value&& other) noexcept;
211
215 void
216 swap(Value& other) noexcept;
217
218 [[nodiscard]] ValueType
219 type() const;
220
221 [[nodiscard]] char const*
222 asCString() const;
226 [[nodiscard]] std::string
227 asString() const;
228 [[nodiscard]] Int
229 asInt() const;
230 [[nodiscard]] UInt
231 asUInt() const;
232 [[nodiscard]] double
233 asDouble() const;
234 [[nodiscard]] bool
235 asBool() const;
236
240 [[nodiscard]] UInt
241 asAbsUInt() const;
242
243 // TODO: What is the "empty()" method this docstring mentions?
248 [[nodiscard]] bool
249 isNull() const;
250 [[nodiscard]] bool
251 isBool() const;
252 [[nodiscard]] bool
253 isInt() const;
254 [[nodiscard]] bool
255 isUInt() const;
256 [[nodiscard]] bool
257 isIntegral() const;
258 [[nodiscard]] bool
259 isDouble() const;
260 [[nodiscard]] bool
261 isNumeric() const;
262 [[nodiscard]] bool
263 isString() const;
264 [[nodiscard]] bool
265 isArray() const;
266 [[nodiscard]] bool
267 isArrayOrNull() const;
268 [[nodiscard]] bool
269 isObject() const;
270 [[nodiscard]] bool
271 isObjectOrNull() const;
272
273 [[nodiscard]] bool
274 isConvertibleTo(ValueType other) const;
275
279 [[nodiscard]] UInt
280 size() const;
281
286 explicit
287 operator bool() const;
288
294 void
295 clear();
296
304 Value&
305 operator[](UInt index);
311 Value const&
312 operator[](UInt index) const;
317 [[nodiscard]] Value
318 get(UInt index, Value const& defaultValue) const;
322 [[nodiscard]] bool
323 isValidIndex(UInt index) const;
329 Value&
330 append(Value const& value);
331 Value&
332 append(Value&& value);
333
338 Value&
339 operator[](char const* key);
344 Value const&
345 operator[](char const* key) const;
350 Value&
351 operator[](std::string const& key);
356 Value const&
357 operator[](std::string const& key) const;
372 Value&
373 operator[](StaticString const& key);
374 Value const&
375 operator[](StaticString const& key) const;
376
380 Value
381 get(char const* key, Value const& defaultValue) const;
385 [[nodiscard]] Value
386 get(std::string const& key, Value const& defaultValue) const;
387
396 Value
397 removeMember(char const* key);
401 Value
402 removeMember(std::string const& key);
403
407 bool
408 isMember(char const* key) const;
412 [[nodiscard]] bool
413 isMember(std::string const& key) const;
417 [[nodiscard]] bool
418 isMember(StaticString const& key) const;
419
427 [[nodiscard]] Members
428 getMemberNames() const;
429
430 [[nodiscard]] std::string
431 toStyledString() const;
432
433 [[nodiscard]] const_iterator
434 begin() const;
435 [[nodiscard]] const_iterator
436 end() const;
437
439 begin();
441 end();
442
443 friend bool
444 operator==(Value const&, Value const&);
445 friend bool
446 operator<(Value const&, Value const&);
447
448private:
449 Value&
450 resolveReference(char const* key, bool isStatic);
451
452private:
463 int allocated_ : 1 {}; // Notes: if declared as bool, bitfield is useless.
464};
465
466inline Value
467toJson(xrpl::Number const& number)
468{
469 return to_string(number);
470}
471
472bool
473operator==(Value const&, Value const&);
474
475bool
476operator<(Value const&, Value const&);
477
478inline bool
479operator<=(Value const& x, Value const& y)
480{
481 return !(y < x);
482}
483
484inline bool
485operator>(Value const& x, Value const& y)
486{
487 return y < x;
488}
489
490inline bool
491operator>=(Value const& x, Value const& y)
492{
493 return !(x < y);
494}
495
506{
507public:
508 static constexpr auto kUnknown = (unsigned)-1;
509
510 virtual ~ValueAllocator() = default;
511
512 virtual char*
513 makeMemberName(char const* memberName) = 0;
514 virtual void
515 releaseMemberName(char* memberName) = 0;
516 virtual char*
517 duplicateStringValue(char const* value, unsigned int length = kUnknown) = 0;
518 virtual void
519 releaseStringValue(char* value) = 0;
520};
521
526{
527public:
529 using size_t = unsigned int;
530 using difference_type = int;
532
534
535 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
536
537 bool
538 operator==(SelfType const& other) const
539 {
540 return isEqual(other);
541 }
542
547 [[nodiscard]] Value
548 key() const;
549
553 [[nodiscard]] UInt
554 index() const;
555
560 [[nodiscard]] char const*
561 memberName() const;
562
563protected:
564 [[nodiscard]] Value&
565 deref() const;
566
567 void
568 increment();
569
570 void
571 decrement();
572
573 [[nodiscard]] difference_type
574 computeDistance(SelfType const& other) const;
575
576 [[nodiscard]] bool
577 isEqual(SelfType const& other) const;
578
579 void
580 copy(SelfType const& other);
581
582private:
583 Value::ObjectValues::iterator current_;
584 // Indicates that iterator is for a null value.
586};
587
592{
593 friend class Value;
594
595public:
596 using size_t = unsigned int;
597 using difference_type = int;
598 using value_type = Value const;
599 using reference = Value const&;
600 using pointer = Value const*;
602
604 ValueConstIterator(ValueConstIterator const& other) = default;
605
606private:
610 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
611
612public:
613 SelfType&
614 operator=(SelfType const& other);
615
618 {
619 SelfType const temp(*this);
620 ++*this;
621 return temp;
622 }
623
626 {
627 SelfType const temp(*this);
628 --*this;
629 return temp;
630 }
631
632 SelfType&
634 {
635 decrement();
636 return *this;
637 }
638
639 SelfType&
641 {
642 increment();
643 return *this;
644 }
645
647 operator*() const
648 {
649 return deref();
650 }
651};
652
657{
658 friend class Value;
659
660public:
661 using size_t = unsigned int;
662 using difference_type = int;
664 using reference = Value&;
665 using pointer = Value*;
667
668 ValueIterator() = default;
669 ValueIterator(ValueConstIterator const& other);
671
672private:
676 explicit ValueIterator(Value::ObjectValues::iterator const& current);
677
678public:
679 SelfType&
680 operator=(SelfType const& other);
681
684 {
685 SelfType const temp(*this);
686 ++*this;
687 return temp;
688 }
689
692 {
693 SelfType const temp(*this);
694 --*this;
695 return temp;
696 }
697
698 SelfType&
700 {
701 decrement();
702 return *this;
703 }
704
705 SelfType&
707 {
708 increment();
709 return *this;
710 }
711
713 operator*() const
714 {
715 return deref();
716 }
717};
718
719} // namespace json
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:48
constexpr StaticString(char const *czString)
Definition json_value.h:50
char const * str_
Definition json_value.h:67
constexpr char const * cStr() const
Definition json_value.h:61
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition json_value.h:506
virtual ~ValueAllocator()=default
virtual void releaseMemberName(char *memberName)=0
virtual void releaseStringValue(char *value)=0
virtual char * makeMemberName(char const *memberName)=0
virtual char * duplicateStringValue(char const *value, unsigned int length=kUnknown)=0
static constexpr auto kUnknown
Definition json_value.h:508
const iterator for object and array value.
Definition json_value.h:592
Value const & reference
Definition json_value.h:599
SelfType operator++(int)
Definition json_value.h:617
SelfType & operator=(SelfType const &other)
ValueConstIterator SelfType
Definition json_value.h:601
ValueConstIterator(ValueConstIterator const &other)=default
reference operator*() const
Definition json_value.h:647
SelfType operator--(int)
Definition json_value.h:625
bool isEqual(SelfType const &other) const
ValueIteratorBase SelfType
Definition json_value.h:531
std::bidirectional_iterator_tag iterator_category
Definition json_value.h:528
Value::ObjectValues::iterator current_
Definition json_value.h:583
bool operator==(SelfType const &other) const
Definition json_value.h:538
void copy(SelfType const &other)
char const * memberName() const
Return the member name of the referenced Value.
difference_type computeDistance(SelfType const &other) const
UInt index() const
Return the index of the referenced Value.
Value key() const
Return either the index or the member name of the referenced value as a Value.
Iterator for object and array value.
Definition json_value.h:657
reference operator*() const
Definition json_value.h:713
ValueIterator SelfType
Definition json_value.h:666
unsigned int size_t
Definition json_value.h:661
SelfType operator--(int)
Definition json_value.h:691
SelfType & operator++()
Definition json_value.h:706
SelfType operator++(int)
Definition json_value.h:683
ValueIterator()=default
SelfType & operator=(SelfType const &other)
friend class Value
Definition json_value.h:658
SelfType & operator--()
Definition json_value.h:699
ValueIterator(ValueIterator const &other)
CZString & operator=(CZString const &other)=delete
bool isStaticString() const
bool operator<(CZString const &other) const
char const * cStr() const
bool operator==(CZString const &other) const
Represents a JSON value.
Definition json_value.h:117
static constexpr Int kMaxInt
Definition json_value.h:130
const_iterator begin() const
bool isIntegral() const
bool isDouble() const
Value removeMember(char const *key)
Remove and return the named member.
bool isNull() const
isNull() tests to see if this field is null.
json::Int Int
Definition json_value.h:125
bool isObject() const
bool asBool() const
friend bool operator<(Value const &, Value 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...
ValueConstIterator const_iterator
Definition json_value.h:123
std::vector< std::string > Members
Definition json_value.h:121
bool isBool() const
union json::Value::ValueHolder value_
bool isValidIndex(UInt index) const
Return true if index < size().
UInt ArrayIndex
Definition json_value.h:126
Value & resolveReference(char const *key, bool isStatic)
std::string toStyledString() const
bool isArray() const
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
bool isString() const
json::UInt UInt
Definition json_value.h:124
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
ValueType type_
Definition json_value.h:462
Members getMemberNames() const
Return a list of the member names.
bool isNumeric() const
static constexpr Int kMinInt
Definition json_value.h:129
bool isArrayOrNull() const
const_iterator end() const
bool isUInt() const
ValueIterator iterator
Definition json_value.h:122
double asDouble() const
Value & operator=(Value const &other)
bool isInt() const
UInt asUInt() const
ValueType type() const
friend bool operator==(Value const &, Value const &)
bool isConvertibleTo(ValueType other) const
std::string asString() const
Returns the unquoted string value.
void swap(Value &other) noexcept
Swap values.
static constexpr UInt kMaxUInt
Definition json_value.h:131
Value & operator[](UInt index)
Access an array element (zero based index ).
friend class ValueIteratorBase
Definition json_value.h:118
static Value const kNull
Definition json_value.h:128
Value(ValueType type=ValueType::Null)
Create a default Value of the given type.
char const * asCString() const
std::map< CZString, Value > ObjectValues
Definition json_value.h:162
bool isObjectOrNull() const
bool isMember(char const *key) const
Return true if the object has a member named key.
Int asInt() const
void clear()
Remove all object members and array elements.
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
T max(T... args)
T min(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:5
int Int
bool operator>=(Value const &x, Value const &y)
Definition json_value.h:491
Value toJson(xrpl::Number const &number)
Definition json_value.h:467
bool operator>(Value const &x, Value const &y)
Definition json_value.h:485
std::string to_string(Value const &)
Writes a json::Value to an std::string.
Definition to_string.cpp:10
unsigned int UInt
bool operator==(StaticString x, StaticString y)
Definition json_value.h:71
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:479
ValueType
Type of the value held by a Value object.
Definition json_value.h:21
@ String
UTF-8 string value.
Definition json_value.h:26
@ Boolean
bool value
Definition json_value.h:27
@ Array
array value (ordered list)
Definition json_value.h:28
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
@ Real
double value
Definition json_value.h:25
@ Null
'null' value
Definition json_value.h:22
bool operator<(Value const &, Value const &)
ObjectValues * mapVal
Definition json_value.h:460