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 <concepts>
7#include <cstddef>
8#include <cstring>
9#include <iterator>
10#include <limits>
11#include <map>
12#include <string>
13#include <string_view>
14#include <type_traits>
15#include <vector>
16
20namespace json {
21
35
52{
53public:
54 constexpr explicit StaticString(char const* czString) : str_(czString)
55 {
56 }
57
58 constexpr
59 operator char const*() const
60 {
61 return str_;
62 }
63
64 [[nodiscard]] constexpr char const*
65 cStr() const
66 {
67 return str_;
68 }
69
70private:
71 char const* str_;
72};
73
74inline bool
76{
77 return strcmp(x.cStr(), y.cStr()) == 0;
78}
79
80inline bool
82{
83 return strcmp(x.c_str(), y.cStr()) == 0;
84}
85
86inline bool
88{
89 return y == x;
90}
91
120class Value
121{
122 friend class ValueIteratorBase;
123
124public:
129 using Int = json::Int;
131
132 static Value const kNull;
136
137private:
139 {
140 public:
142
143 CZString(int index);
144 CZString(char const* cstr, DuplicationPolicy allocate);
145 CZString(CZString const& other);
146 ~CZString();
147 CZString&
148 operator=(CZString const& other) = delete;
149 bool
150 operator<(CZString const& other) const;
151 bool
152 operator==(CZString const& other) const;
153 [[nodiscard]] int
154 index() const;
155 [[nodiscard]] char const*
156 cStr() const;
157 [[nodiscard]] bool
158 isStaticString() const;
159
160 private:
161 char const* cstr_;
163 };
164
165public:
167
168public:
186 Value(Int value);
187 Value(UInt value);
188 Value(double value);
189 Value(char const* value);
190 Value(xrpl::Number const& value);
191
204 Value(StaticString const& value);
205
213 Value(std::string const& value);
214
223 Value(std::string_view value);
224
225 Value(bool value);
226 Value(Value const& other);
227 ~Value();
228
229 Value&
230 operator=(Value const& other);
231 Value&
232 operator=(Value&& other);
233
234 Value(Value&& other) noexcept;
235
239 void
240 swap(Value& other) noexcept;
241
242 [[nodiscard]] ValueType
243 type() const;
244
245 [[nodiscard]] char const*
246 asCString() const;
250 [[nodiscard]] std::string
251 asString() const;
252 [[nodiscard]] Int
253 asInt() const;
254 [[nodiscard]] UInt
255 asUInt() const;
256 [[nodiscard]] double
257 asDouble() const;
258 [[nodiscard]] bool
259 asBool() const;
260
264 [[nodiscard]] UInt
265 asAbsUInt() const;
266
267 // TODO: What is the "empty()" method this docstring mentions?
272 [[nodiscard]] bool
273 isNull() const;
274 [[nodiscard]] bool
275 isBool() const;
276 [[nodiscard]] bool
277 isInt() const;
278 [[nodiscard]] bool
279 isUInt() const;
280 [[nodiscard]] bool
281 isIntegral() const;
282 [[nodiscard]] bool
283 isDouble() const;
284 [[nodiscard]] bool
285 isNumeric() const;
286 [[nodiscard]] bool
287 isString() const;
288 [[nodiscard]] bool
289 isArray() const;
290 [[nodiscard]] bool
291 isArrayOrNull() const;
292 [[nodiscard]] bool
293 isObject() const;
294 [[nodiscard]] bool
295 isObjectOrNull() const;
296
297 [[nodiscard]] bool
298 isConvertibleTo(ValueType other) const;
299
303 [[nodiscard]] UInt
304 size() const;
305
310 explicit
311 operator bool() const;
312
318 void
319 clear();
320
328 Value&
329 operator[](UInt index);
335 Value const&
336 operator[](UInt index) const;
341 [[nodiscard]] Value
342 get(UInt index, Value const& defaultValue) const;
346 [[nodiscard]] bool
347 isValidIndex(UInt index) const;
353 Value&
354 append(Value const& value);
355 Value&
356 append(Value&& value);
357
362 Value&
363 operator[](char const* key);
368 Value const&
369 operator[](char const* key) const;
374 Value&
375 operator[](std::string const& key);
380 Value const&
381 operator[](std::string const& key) const;
396 Value&
397 operator[](StaticString const& key);
398 Value const&
399 operator[](StaticString const& key) const;
400
404 Value
405 get(char const* key, Value const& defaultValue) const;
409 [[nodiscard]] Value
410 get(std::string const& key, Value const& defaultValue) const;
411
420 Value
421 removeMember(char const* key);
425 Value
426 removeMember(std::string const& key);
427
431 bool
432 isMember(char const* key) const;
436 [[nodiscard]] bool
437 isMember(std::string const& key) const;
441 [[nodiscard]] bool
442 isMember(StaticString const& key) const;
443
451 [[nodiscard]] Members
452 getMemberNames() const;
453
454 [[nodiscard]] std::string
455 toStyledString() const;
456
457 [[nodiscard]] const_iterator
458 begin() const;
459 [[nodiscard]] const_iterator
460 end() const;
461
463 begin();
465 end();
466
467 friend bool
468 operator==(Value const&, Value const&);
469 friend bool
470 operator<(Value const&, Value const&);
471
472private:
473 Value&
474 resolveReference(char const* key, bool isStatic);
475
476private:
487 int allocated_ : 1 {}; // Notes: if declared as bool, bitfield is useless.
488};
489
490inline Value
491toJson(xrpl::Number const& number)
492{
493 return to_string(number);
494}
495
496bool
497operator==(Value const&, Value const&);
498
516template <class T>
517 requires std::convertible_to<T const&, std::string_view> && (!std::same_as<T, std::nullptr_t>)
518bool
519operator==(Value const& value, T const& text)
520{
521 if (!value.isString())
522 return false;
523
524 // A string value holding a null pointer equals no text, not even the empty one.
525 char const* const s = value.asCString();
526 if (s == nullptr)
527 return false;
528
529 // A null pointer names no characters, as it does when it builds a Value.
530 if constexpr (std::is_pointer_v<T>)
531 {
532 if (text == nullptr)
533 return std::string_view{s}.empty();
534 }
535 return std::string_view{s} == std::string_view{text};
536}
537
538bool
539operator<(Value const&, Value const&);
540
541inline bool
542operator<=(Value const& x, Value const& y)
543{
544 return !(y < x);
545}
546
547inline bool
548operator>(Value const& x, Value const& y)
549{
550 return y < x;
551}
552
553inline bool
554operator>=(Value const& x, Value const& y)
555{
556 return !(x < y);
557}
558
569{
570public:
571 static constexpr auto kUnknown = (unsigned)-1;
572
573 virtual ~ValueAllocator() = default;
574
575 virtual char*
576 makeMemberName(char const* memberName) = 0;
577 virtual void
578 releaseMemberName(char* memberName) = 0;
579 virtual char*
580 duplicateStringValue(char const* value, unsigned int length = kUnknown) = 0;
581 virtual void
582 releaseStringValue(char* value) = 0;
583};
584
589{
590public:
592 using SizeT = unsigned int;
593 using difference_type = int;
595
597
598 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
599
600 bool
601 operator==(SelfType const& other) const
602 {
603 return isEqual(other);
604 }
605
610 [[nodiscard]] Value
611 key() const;
612
616 [[nodiscard]] UInt
617 index() const;
618
623 [[nodiscard]] char const*
624 memberName() const;
625
626protected:
627 [[nodiscard]] Value&
628 deref() const;
629
630 void
631 increment();
632
633 void
634 decrement();
635
636 [[nodiscard]] difference_type
637 computeDistance(SelfType const& other) const;
638
639 [[nodiscard]] bool
640 isEqual(SelfType const& other) const;
641
642 void
643 copy(SelfType const& other);
644
645private:
646 Value::ObjectValues::iterator current_;
647 // Indicates that iterator is for a null value.
649};
650
655{
656 friend class Value;
657
658public:
659 using SizeT = unsigned int;
660 using difference_type = int;
661 using value_type = Value const;
662 using reference = Value const&;
663 using pointer = Value const*;
665
667 ValueConstIterator(ValueConstIterator const& other) = default;
668
669private:
673 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
674
675public:
676 SelfType&
677 operator=(SelfType const& other);
678
681 {
682 SelfType const temp(*this);
683 ++*this;
684 return temp;
685 }
686
689 {
690 SelfType const temp(*this);
691 --*this;
692 return temp;
693 }
694
695 SelfType&
697 {
698 decrement();
699 return *this;
700 }
701
702 SelfType&
704 {
705 increment();
706 return *this;
707 }
708
710 operator*() const
711 {
712 return deref();
713 }
714};
715
720{
721 friend class Value;
722
723public:
724 using SizeT = unsigned int;
725 using difference_type = int;
727 using reference = Value&;
728 using pointer = Value*;
730
731 ValueIterator() = default;
732 ValueIterator(ValueConstIterator const& other);
734
735private:
739 explicit ValueIterator(Value::ObjectValues::iterator const& current);
740
741public:
742 SelfType&
743 operator=(SelfType const& other);
744
747 {
748 SelfType const temp(*this);
749 ++*this;
750 return temp;
751 }
752
755 {
756 SelfType const temp(*this);
757 --*this;
758 return temp;
759 }
760
761 SelfType&
763 {
764 decrement();
765 return *this;
766 }
767
768 SelfType&
770 {
771 increment();
772 return *this;
773 }
774
776 operator*() const
777 {
778 return deref();
779 }
780};
781
782} // namespace json
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:52
constexpr StaticString(char const *czString)
Definition json_value.h:54
char const * str_
Definition json_value.h:71
constexpr char const * cStr() const
Definition json_value.h:65
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition json_value.h:569
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:571
const iterator for object and array value.
Definition json_value.h:655
Value const & reference
Definition json_value.h:662
SelfType operator++(int)
Definition json_value.h:680
SelfType & operator=(SelfType const &other)
ValueConstIterator SelfType
Definition json_value.h:664
ValueConstIterator(ValueConstIterator const &other)=default
reference operator*() const
Definition json_value.h:710
SelfType operator--(int)
Definition json_value.h:688
bool isEqual(SelfType const &other) const
ValueIteratorBase SelfType
Definition json_value.h:594
std::bidirectional_iterator_tag iterator_category
Definition json_value.h:591
Value::ObjectValues::iterator current_
Definition json_value.h:646
bool operator==(SelfType const &other) const
Definition json_value.h:601
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:720
reference operator*() const
Definition json_value.h:776
ValueIterator SelfType
Definition json_value.h:729
unsigned int SizeT
Definition json_value.h:724
SelfType operator--(int)
Definition json_value.h:754
SelfType & operator++()
Definition json_value.h:769
SelfType operator++(int)
Definition json_value.h:746
ValueIterator()=default
SelfType & operator=(SelfType const &other)
friend class Value
Definition json_value.h:721
SelfType & operator--()
Definition json_value.h:762
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:121
static constexpr Int kMaxInt
Definition json_value.h:134
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:129
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:127
std::vector< std::string > Members
Definition json_value.h:125
bool isBool() const
union json::Value::ValueHolder value_
bool isValidIndex(UInt index) const
Return true if index < size().
UInt ArrayIndex
Definition json_value.h:130
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:128
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:486
Members getMemberNames() const
Return a list of the member names.
bool isNumeric() const
static constexpr Int kMinInt
Definition json_value.h:133
bool isArrayOrNull() const
const_iterator end() const
bool isUInt() const
ValueIterator iterator
Definition json_value.h:126
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:135
Value & operator[](UInt index)
Access an array element (zero based index ).
friend class ValueIteratorBase
Definition json_value.h:122
static Value const kNull
Definition json_value.h:132
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:166
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 empty(T... args)
T is_pointer_v
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:554
Value toJson(xrpl::Number const &number)
Definition json_value.h:491
bool operator>(Value const &x, Value const &y)
Definition json_value.h:548
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:75
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:542
ValueType
Type of the value held by a Value object.
Definition json_value.h:25
@ String
UTF-8 string value.
Definition json_value.h:30
@ Boolean
bool value
Definition json_value.h:31
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Real
double value
Definition json_value.h:29
@ Null
'null' value
Definition json_value.h:26
bool operator<(Value const &, Value const &)
ObjectValues * mapVal
Definition json_value.h:484