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 <limits>
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 [[nodiscard]] constexpr char const*
57 cStr() const
58 {
59 return str_;
60 }
61
62private:
63 char const* str_;
64};
65
66inline bool
68{
69 return strcmp(x.cStr(), y.cStr()) == 0;
70}
71
72inline bool
74{
75 return !(x == y);
76}
77
78inline bool
80{
81 return strcmp(x.c_str(), y.cStr()) == 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 kNull;
145
146private:
148 {
149 public:
151
152 CZString(int index);
153 CZString(char const* cstr, DuplicationPolicy allocate);
154 CZString(CZString const& other);
155 ~CZString();
156 CZString&
157 operator=(CZString const& other) = delete;
158 bool
159 operator<(CZString const& other) const;
160 bool
161 operator==(CZString const& other) const;
162 [[nodiscard]] int
163 index() const;
164 [[nodiscard]] char const*
165 cStr() const;
166 [[nodiscard]] bool
167 isStaticString() const;
168
169 private:
170 char const* cstr_;
172 };
173
174public:
176
177public:
194 Value(Int value);
195 Value(UInt value);
196 Value(double value);
197 Value(char const* value);
198 Value(xrpl::Number const& value);
210 Value(StaticString const& value);
211 Value(std::string const& value);
212 Value(bool value);
213 Value(Value const& other);
214 ~Value();
215
216 Value&
217 operator=(Value const& other);
218 Value&
219 operator=(Value&& other);
220
221 Value(Value&& other) noexcept;
222
224 void
225 swap(Value& other) noexcept;
226
227 [[nodiscard]] ValueType
228 type() const;
229
230 [[nodiscard]] char const*
231 asCString() const;
233 [[nodiscard]] std::string
234 asString() const;
235 [[nodiscard]] Int
236 asInt() const;
237 [[nodiscard]] UInt
238 asUInt() const;
239 [[nodiscard]] double
240 asDouble() const;
241 [[nodiscard]] bool
242 asBool() const;
243
245 [[nodiscard]] UInt
246 asAbsUInt() const;
247
248 // TODO: What is the "empty()" method this docstring mentions?
251 [[nodiscard]] bool
252 isNull() const;
253 [[nodiscard]] bool
254 isBool() const;
255 [[nodiscard]] bool
256 isInt() const;
257 [[nodiscard]] bool
258 isUInt() const;
259 [[nodiscard]] bool
260 isIntegral() const;
261 [[nodiscard]] bool
262 isDouble() const;
263 [[nodiscard]] bool
264 isNumeric() const;
265 [[nodiscard]] bool
266 isString() const;
267 [[nodiscard]] bool
268 isArray() const;
269 [[nodiscard]] bool
270 isArrayOrNull() const;
271 [[nodiscard]] bool
272 isObject() const;
273 [[nodiscard]] bool
274 isObjectOrNull() const;
275
276 [[nodiscard]] bool
277 isConvertibleTo(ValueType other) const;
278
280 [[nodiscard]] UInt
281 size() const;
282
285 explicit
286 operator bool() const;
287
291 void
292 clear();
293
299 Value&
300 operator[](UInt index);
304 Value const&
305 operator[](UInt index) const;
308 [[nodiscard]] Value
309 get(UInt index, Value const& defaultValue) const;
311 [[nodiscard]] bool
312 isValidIndex(UInt index) const;
316 Value&
317 append(Value const& value);
318 Value&
319 append(Value&& value);
320
323 Value&
324 operator[](char const* key);
327 Value const&
328 operator[](char const* key) const;
331 Value&
332 operator[](std::string const& key);
335 Value const&
336 operator[](std::string const& key) const;
350 Value&
351 operator[](StaticString const& key);
352 Value const&
353 operator[](StaticString const& key) const;
354
356 Value
357 get(char const* key, Value const& defaultValue) const;
359 [[nodiscard]] Value
360 get(std::string const& key, Value const& defaultValue) const;
361
368 Value
369 removeMember(char const* key);
371 Value
372 removeMember(std::string const& key);
373
375 bool
376 isMember(char const* key) const;
378 [[nodiscard]] bool
379 isMember(std::string const& key) const;
381 [[nodiscard]] bool
382 isMember(StaticString const& key) const;
383
389 [[nodiscard]] Members
390 getMemberNames() const;
391
392 [[nodiscard]] std::string
393 toStyledString() const;
394
395 [[nodiscard]] const_iterator
396 begin() const;
397 [[nodiscard]] const_iterator
398 end() const;
399
401 begin();
403 end();
404
405 friend bool
406 operator==(Value const&, Value const&);
407 friend bool
408 operator<(Value const&, Value const&);
409
410private:
411 Value&
412 resolveReference(char const* key, bool isStatic);
413
414private:
425 int allocated_ : 1 {}; // Notes: if declared as bool, bitfield is useless.
426};
427
428inline Value
429toJson(xrpl::Number const& number)
430{
431 return to_string(number);
432}
433
434bool
435operator==(Value const&, Value const&);
436
437inline bool
438operator!=(Value const& x, Value const& y)
439{
440 return !(x == y);
441}
442
443bool
444operator<(Value const&, Value const&);
445
446inline bool
447operator<=(Value const& x, Value const& y)
448{
449 return !(y < x);
450}
451
452inline bool
453operator>(Value const& x, Value const& y)
454{
455 return y < x;
456}
457
458inline bool
459operator>=(Value const& x, Value const& y)
460{
461 return !(x < y);
462}
463
473{
474public:
475 static constexpr auto kUnknown = (unsigned)-1;
476
477 virtual ~ValueAllocator() = default;
478
479 virtual char*
480 makeMemberName(char const* memberName) = 0;
481 virtual void
482 releaseMemberName(char* memberName) = 0;
483 virtual char*
484 duplicateStringValue(char const* value, unsigned int length = kUnknown) = 0;
485 virtual void
486 releaseStringValue(char* value) = 0;
487};
488
493{
494public:
495 using size_t = unsigned int;
496 using difference_type = int;
498
500
501 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
502
503 bool
504 operator==(SelfType const& other) const
505 {
506 return isEqual(other);
507 }
508
509 bool
510 operator!=(SelfType const& other) const
511 {
512 return !isEqual(other);
513 }
514
517 [[nodiscard]] Value
518 key() const;
519
521 [[nodiscard]] UInt
522 index() const;
523
526 [[nodiscard]] char const*
527 memberName() const;
528
529protected:
530 [[nodiscard]] Value&
531 deref() const;
532
533 void
534 increment();
535
536 void
537 decrement();
538
539 [[nodiscard]] difference_type
540 computeDistance(SelfType const& other) const;
541
542 [[nodiscard]] bool
543 isEqual(SelfType const& other) const;
544
545 void
546 copy(SelfType const& other);
547
548private:
549 Value::ObjectValues::iterator current_;
550 // Indicates that iterator is for a null value.
552};
553
558{
559 friend class Value;
560
561public:
562 using size_t = unsigned int;
563 using difference_type = int;
564 using reference = Value const&;
565 using pointer = Value const*;
567
569 ValueConstIterator(ValueConstIterator const& other) = default;
570
571private:
574 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
575
576public:
577 SelfType&
578 operator=(SelfType const& other);
579
582 {
583 SelfType const temp(*this);
584 ++*this;
585 return temp;
586 }
587
590 {
591 SelfType const 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);
633
634private:
637 explicit ValueIterator(Value::ObjectValues::iterator const& current);
638
639public:
640 SelfType&
641 operator=(SelfType const& other);
642
645 {
646 SelfType const temp(*this);
647 ++*this;
648 return temp;
649 }
650
653 {
654 SelfType const 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
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:44
constexpr StaticString(char const *czString)
Definition json_value.h:46
char const * str_
Definition json_value.h:63
constexpr char const * cStr() const
Definition json_value.h:57
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition json_value.h:473
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:475
const iterator for object and array value.
Definition json_value.h:558
Value const & reference
Definition json_value.h:564
SelfType operator++(int)
Definition json_value.h:581
SelfType & operator=(SelfType const &other)
ValueConstIterator SelfType
Definition json_value.h:566
ValueConstIterator(ValueConstIterator const &other)=default
reference operator*() const
Definition json_value.h:611
SelfType operator--(int)
Definition json_value.h:589
bool isEqual(SelfType const &other) const
ValueIteratorBase SelfType
Definition json_value.h:497
Value::ObjectValues::iterator current_
Definition json_value.h:549
bool operator==(SelfType const &other) const
Definition json_value.h:504
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. -1 if it is not an ValueType::Array.
bool operator!=(SelfType const &other) const
Definition json_value.h:510
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:620
reference operator*() const
Definition json_value.h:674
ValueIterator SelfType
Definition json_value.h:628
unsigned int size_t
Definition json_value.h:624
SelfType operator--(int)
Definition json_value.h:652
SelfType & operator++()
Definition json_value.h:667
SelfType operator++(int)
Definition json_value.h:644
ValueIterator()=default
SelfType & operator=(SelfType const &other)
friend class Value
Definition json_value.h:621
SelfType & operator--()
Definition json_value.h:660
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:130
static constexpr Int kMaxInt
Definition json_value.h:143
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:138
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:136
std::vector< std::string > Members
Definition json_value.h:134
bool isBool() const
union json::Value::ValueHolder value_
bool isValidIndex(UInt index) const
Return true if index < size().
UInt ArrayIndex
Definition json_value.h:139
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:137
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:424
Members getMemberNames() const
Return a list of the member names.
bool isNumeric() const
static constexpr Int kMinInt
Definition json_value.h:142
bool isArrayOrNull() const
const_iterator end() const
bool isUInt() const
ValueIterator iterator
Definition json_value.h:135
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:144
Value & operator[](UInt index)
Access an array element (zero based index ).
friend class ValueIteratorBase
Definition json_value.h:131
static Value const kNull
Definition json_value.h:141
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:175
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:306
T max(T... args)
T min(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:5
bool operator!=(StaticString x, StaticString y)
Definition json_value.h:73
int Int
bool operator>=(Value const &x, Value const &y)
Definition json_value.h:459
Value toJson(xrpl::Number const &number)
Definition json_value.h:429
bool operator>(Value const &x, Value const &y)
Definition json_value.h:453
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:67
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:447
ValueType
Type of the value held by a Value object.
Definition json_value.h:18
@ String
UTF-8 string value.
Definition json_value.h:23
@ Boolean
bool value
Definition json_value.h:24
@ Array
array value (ordered list)
Definition json_value.h:25
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
@ Real
double value
Definition json_value.h:22
@ Null
'null' value
Definition json_value.h:19
bool operator<(Value const &, Value const &)
ObjectValues * mapVal
Definition json_value.h:422