rippled
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 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;
145
146private:
148 {
149 public:
151 CZString(int index);
152 CZString(char const* cstr, DuplicationPolicy allocate);
153 CZString(CZString const& other);
154 ~CZString();
155 CZString&
156 operator=(CZString const& other) = delete;
157 bool
158 operator<(CZString const& other) const;
159 bool
160 operator==(CZString const& other) const;
161 int
162 index() const;
163 char const*
164 c_str() const;
165 bool
166 isStaticString() const;
167
168 private:
169 char const* cstr_;
171 };
172
173public:
175
176public:
193 Value(Int value);
194 Value(UInt value);
195 Value(double value);
196 Value(char const* value);
197 Value(xrpl::Number const& value);
209 Value(StaticString const& value);
210 Value(std::string const& value);
211 Value(bool value);
212 Value(Value const& other);
213 ~Value();
214
215 Value&
216 operator=(Value const& other);
217 Value&
218 operator=(Value&& other);
219
220 Value(Value&& other) noexcept;
221
223 void
224 swap(Value& other) noexcept;
225
227 type() const;
228
229 char const*
230 asCString() const;
233 asString() const;
234 Int
235 asInt() const;
236 UInt
237 asUInt() const;
238 double
239 asDouble() const;
240 bool
241 asBool() const;
242
244 UInt
245 asAbsUInt() 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;
380 bool
381 isMember(StaticString const& key) const;
382
388 Members
389 getMemberNames() const;
390
392 toStyledString() const;
393
395 begin() const;
397 end() const;
398
400 begin();
402 end();
403
404 friend bool
405 operator==(Value const&, Value const&);
406 friend bool
407 operator<(Value const&, Value const&);
408
409private:
410 Value&
411 resolveReference(char const* key, bool isStatic);
412
413private:
424 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
425};
426
427inline Value
428to_json(xrpl::Number const& number)
429{
430 return to_string(number);
431}
432
433bool
434operator==(Value const&, Value const&);
435
436inline bool
437operator!=(Value const& x, Value const& y)
438{
439 return !(x == y);
440}
441
442bool
443operator<(Value const&, Value const&);
444
445inline bool
446operator<=(Value const& x, Value const& y)
447{
448 return !(y < x);
449}
450
451inline bool
452operator>(Value const& x, Value const& y)
453{
454 return y < x;
455}
456
457inline bool
458operator>=(Value const& x, Value const& y)
459{
460 return !(x < y);
461}
462
472{
473public:
474 enum { unknown = (unsigned)-1 };
475
476 virtual ~ValueAllocator() = default;
477
478 virtual char*
479 makeMemberName(char const* memberName) = 0;
480 virtual void
481 releaseMemberName(char* memberName) = 0;
482 virtual char*
483 duplicateStringValue(char const* value, unsigned int length = unknown) = 0;
484 virtual void
485 releaseStringValue(char* value) = 0;
486};
487
492{
493public:
494 using size_t = unsigned int;
495 using difference_type = int;
497
499
500 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
501
502 bool
503 operator==(SelfType const& other) const
504 {
505 return isEqual(other);
506 }
507
508 bool
509 operator!=(SelfType const& other) const
510 {
511 return !isEqual(other);
512 }
513
516 Value
517 key() const;
518
520 UInt
521 index() const;
522
525 char const*
526 memberName() const;
527
528protected:
529 Value&
530 deref() const;
531
532 void
533 increment();
534
535 void
536 decrement();
537
539 computeDistance(SelfType const& other) const;
540
541 bool
542 isEqual(SelfType const& other) const;
543
544 void
545 copy(SelfType const& other);
546
547private:
548 Value::ObjectValues::iterator current_;
549 // Indicates that iterator is for a null value.
551};
552
557{
558 friend class Value;
559
560public:
561 using size_t = unsigned int;
562 using difference_type = int;
563 using reference = Value const&;
564 using pointer = Value const*;
566
568
569private:
572 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
573
574public:
575 SelfType&
576 operator=(ValueIteratorBase const& other);
577
580 {
581 SelfType temp(*this);
582 ++*this;
583 return temp;
584 }
585
588 {
589 SelfType temp(*this);
590 --*this;
591 return temp;
592 }
593
594 SelfType&
596 {
597 decrement();
598 return *this;
599 }
600
601 SelfType&
603 {
604 increment();
605 return *this;
606 }
607
609 operator*() const
610 {
611 return deref();
612 }
613};
614
618{
619 friend class Value;
620
621public:
622 using size_t = unsigned int;
623 using difference_type = int;
624 using reference = Value&;
625 using pointer = Value*;
627
628 ValueIterator() = default;
629 ValueIterator(ValueConstIterator const& other);
630 ValueIterator(ValueIterator const& other);
631
632private:
635 explicit ValueIterator(Value::ObjectValues::iterator const& current);
636
637public:
638 SelfType&
639 operator=(SelfType const& other);
640
643 {
644 SelfType temp(*this);
645 ++*this;
646 return temp;
647 }
648
651 {
652 SelfType temp(*this);
653 --*this;
654 return temp;
655 }
656
657 SelfType&
659 {
660 decrement();
661 return *this;
662 }
663
664 SelfType&
666 {
667 increment();
668 return *this;
669 }
670
672 operator*() const
673 {
674 return deref();
675 }
676};
677
678} // namespace Json
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:472
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:557
SelfType & operator=(ValueIteratorBase const &other)
ValueConstIterator SelfType
Definition json_value.h:565
SelfType operator--(int)
Definition json_value.h:587
Value const & reference
Definition json_value.h:563
SelfType operator++(int)
Definition json_value.h:579
reference operator*() const
Definition json_value.h:609
base class for Value iterators.
Definition json_value.h:492
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:509
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition json_value.h:548
ValueIteratorBase SelfType
Definition json_value.h:496
bool operator==(SelfType const &other) const
Definition json_value.h:503
Iterator for object and array value.
Definition json_value.h:618
SelfType operator--(int)
Definition json_value.h:650
SelfType & operator=(SelfType const &other)
SelfType & operator--()
Definition json_value.h:658
reference operator*() const
Definition json_value.h:672
SelfType & operator++()
Definition json_value.h:665
ValueIterator SelfType
Definition json_value.h:626
unsigned int size_t
Definition json_value.h:622
ValueIterator()=default
SelfType operator++(int)
Definition json_value.h:642
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
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
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: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
static constexpr Int maxInt
Definition json_value.h:143
bool isIntegral() const
bool asBool() const
ValueType type_
Definition json_value.h:423
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:142
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:144
bool isInt() const
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
T max(T... args)
T min(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:5
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:446
bool operator>(Value const &x, Value const &y)
Definition json_value.h:452
Value to_json(xrpl::Number const &number)
Definition json_value.h:428
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
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:458
unsigned int UInt