rippled
Loading...
Searching...
No Matches
json_value.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_JSON_JSON_VALUE_H_INCLUDED
21#define RIPPLE_JSON_JSON_VALUE_H_INCLUDED
22
23#include <xrpl/json/json_forwards.h>
24#include <cstring>
25#include <map>
26#include <string>
27#include <vector>
28
31namespace Json {
32
44};
45
61{
62public:
63 constexpr explicit StaticString(const char* czstring) : str_(czstring)
64 {
65 }
66
67 constexpr
68 operator const char*() const
69 {
70 return str_;
71 }
72
73 constexpr const char*
74 c_str() const
75 {
76 return str_;
77 }
78
79private:
80 const char* str_;
81};
82
83inline bool
85{
86 return strcmp(x.c_str(), y.c_str()) == 0;
87}
88
89inline bool
91{
92 return !(x == y);
93}
94
95inline bool
97{
98 return strcmp(x.c_str(), y.c_str()) == 0;
99}
100
101inline bool
103{
104 return !(x == y);
105}
106
107inline bool
109{
110 return y == x;
111}
112
113inline bool
115{
116 return !(y == x);
117}
118
146class Value
147{
148 friend class ValueIteratorBase;
149
150public:
155 using Int = Json::Int;
157
158 static const Value null;
159 static const Int minInt;
160 static const Int maxInt;
161 static const UInt maxUInt;
162
163private:
165 {
166 public:
171 };
172 CZString(int index);
173 CZString(const char* cstr, DuplicationPolicy allocate);
174 CZString(const CZString& other);
175 ~CZString();
176 CZString&
177 operator=(const CZString& other) = delete;
178 bool
179 operator<(const CZString& other) const;
180 bool
181 operator==(const CZString& other) const;
182 int
183 index() const;
184 const char*
185 c_str() const;
186 bool
187 isStaticString() const;
188
189 private:
190 const char* cstr_;
192 };
193
194public:
196
197public:
214 Value(Int value);
215 Value(UInt value);
216 Value(double value);
217 Value(const char* value);
229 Value(const StaticString& value);
230 Value(std::string const& value);
231 Value(bool value);
232 Value(const Value& other);
233 ~Value();
234
235 Value&
236 operator=(Value const& other);
237 Value&
238 operator=(Value&& other);
239
240 Value(Value&& other) noexcept;
241
243 void
244 swap(Value& other) noexcept;
245
247 type() const;
248
249 const char*
250 asCString() const;
253 asString() const;
254 Int
255 asInt() const;
256 UInt
257 asUInt() const;
258 double
259 asDouble() const;
260 bool
261 asBool() const;
262
263 // TODO: What is the "empty()" method this docstring mentions?
266 bool
267 isNull() const;
268 bool
269 isBool() const;
270 bool
271 isInt() const;
272 bool
273 isUInt() const;
274 bool
275 isIntegral() const;
276 bool
277 isDouble() const;
278 bool
279 isNumeric() const;
280 bool
281 isString() const;
282 bool
283 isArray() const;
284 bool
285 isArrayOrNull() const;
286 bool
287 isObject() const;
288 bool
289 isObjectOrNull() const;
290
291 bool
292 isConvertibleTo(ValueType other) const;
293
295 UInt
296 size() const;
297
300 explicit
301 operator bool() const;
302
306 void
307 clear();
308
314 Value&
315 operator[](UInt index);
319 const Value&
320 operator[](UInt index) const;
323 Value
324 get(UInt index, const Value& defaultValue) const;
326 bool
327 isValidIndex(UInt index) const;
331 Value&
332 append(const Value& value);
333 Value&
334 append(Value&& value);
335
338 Value&
339 operator[](const char* key);
342 const Value&
343 operator[](const char* key) const;
346 Value&
347 operator[](std::string const& key);
350 const Value&
351 operator[](std::string const& key) const;
365 Value&
366 operator[](const StaticString& key);
367
369 Value
370 get(const char* key, const Value& defaultValue) const;
372 Value
373 get(std::string const& key, const Value& defaultValue) const;
374
381 Value
382 removeMember(const char* key);
384 Value
385 removeMember(std::string const& key);
386
388 bool
389 isMember(const char* key) const;
391 bool
392 isMember(std::string const& key) const;
393
399 Members
400 getMemberNames() const;
401
403 toStyledString() const;
404
406 begin() const;
408 end() const;
409
411 begin();
413 end();
414
415 friend bool
416 operator==(const Value&, const Value&);
417 friend bool
418 operator<(const Value&, const Value&);
419
420private:
421 Value&
422 resolveReference(const char* key, bool isStatic);
423
424private:
426 {
429 double real_;
430 bool bool_;
431 char* string_;
435 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
436};
437
438bool
439operator==(const Value&, const Value&);
440
441inline bool
442operator!=(const Value& x, const Value& y)
443{
444 return !(x == y);
445}
446
447bool
448operator<(const Value&, const Value&);
449
450inline bool
451operator<=(const Value& x, const Value& y)
452{
453 return !(y < x);
454}
455
456inline bool
457operator>(const Value& x, const Value& y)
458{
459 return y < x;
460}
461
462inline bool
463operator>=(const Value& x, const Value& y)
464{
465 return !(x < y);
466}
467
477{
478public:
479 enum { unknown = (unsigned)-1 };
480
481 virtual ~ValueAllocator() = default;
482
483 virtual char*
484 makeMemberName(const char* memberName) = 0;
485 virtual void
486 releaseMemberName(char* memberName) = 0;
487 virtual char*
488 duplicateStringValue(const char* value, unsigned int length = unknown) = 0;
489 virtual void
490 releaseStringValue(char* value) = 0;
491};
492
497{
498public:
499 using size_t = unsigned int;
500 using difference_type = int;
502
504
505 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
506
507 bool
508 operator==(const SelfType& other) const
509 {
510 return isEqual(other);
511 }
512
513 bool
514 operator!=(const SelfType& other) const
515 {
516 return !isEqual(other);
517 }
518
521 Value
522 key() const;
523
525 UInt
526 index() const;
527
530 const char*
531 memberName() const;
532
533protected:
534 Value&
535 deref() const;
536
537 void
538 increment();
539
540 void
541 decrement();
542
544 computeDistance(const SelfType& other) const;
545
546 bool
547 isEqual(const SelfType& other) const;
548
549 void
550 copy(const SelfType& other);
551
552private:
553 Value::ObjectValues::iterator current_;
554 // Indicates that iterator is for a null value.
556};
557
562{
563 friend class Value;
564
565public:
566 using size_t = unsigned int;
567 using difference_type = int;
568 using reference = const Value&;
569 using pointer = const Value*;
571
573
574private:
577 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
578
579public:
580 SelfType&
581 operator=(const ValueIteratorBase& other);
582
585 {
586 SelfType temp(*this);
587 ++*this;
588 return temp;
589 }
590
593 {
594 SelfType temp(*this);
595 --*this;
596 return temp;
597 }
598
599 SelfType&
601 {
602 decrement();
603 return *this;
604 }
605
606 SelfType&
608 {
609 increment();
610 return *this;
611 }
612
614 operator*() const
615 {
616 return deref();
617 }
618};
619
623{
624 friend class Value;
625
626public:
627 using size_t = unsigned int;
628 using difference_type = int;
629 using reference = Value&;
630 using pointer = Value*;
632
633 ValueIterator() = default;
634 ValueIterator(const ValueConstIterator& other);
635 ValueIterator(const ValueIterator& other);
636
637private:
640 explicit ValueIterator(const Value::ObjectValues::iterator& current);
641
642public:
643 SelfType&
644 operator=(const SelfType& other);
645
648 {
649 SelfType temp(*this);
650 ++*this;
651 return temp;
652 }
653
656 {
657 SelfType temp(*this);
658 --*this;
659 return temp;
660 }
661
662 SelfType&
664 {
665 decrement();
666 return *this;
667 }
668
669 SelfType&
671 {
672 increment();
673 return *this;
674 }
675
677 operator*() const
678 {
679 return deref();
680 }
681};
682
683} // namespace Json
684
685#endif // CPPTL_JSON_H_INCLUDED
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition: json_value.h:61
const char * str_
Definition: json_value.h:80
constexpr StaticString(const char *czstring)
Definition: json_value.h:63
constexpr const char * c_str() const
Definition: json_value.h:74
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:477
virtual char * makeMemberName(const char *memberName)=0
virtual ~ValueAllocator()=default
virtual void releaseStringValue(char *value)=0
virtual void releaseMemberName(char *memberName)=0
virtual char * duplicateStringValue(const char *value, unsigned int length=unknown)=0
const iterator for object and array value.
Definition: json_value.h:562
SelfType & operator--()
Definition: json_value.h:600
const Value & reference
Definition: json_value.h:568
ValueConstIterator SelfType
Definition: json_value.h:570
SelfType operator--(int)
Definition: json_value.h:592
SelfType & operator++()
Definition: json_value.h:607
SelfType operator++(int)
Definition: json_value.h:584
SelfType & operator=(const ValueIteratorBase &other)
reference operator*() const
Definition: json_value.h:614
base class for Value iterators.
Definition: json_value.h:497
bool isEqual(const SelfType &other) const
bool operator==(const SelfType &other) const
Definition: json_value.h:508
Value key() const
Return either the index or the member name of the referenced value as a Value.
void copy(const SelfType &other)
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
const char * memberName() const
Return the member name of the referenced Value.
bool operator!=(const SelfType &other) const
Definition: json_value.h:514
Value::ObjectValues::iterator current_
Definition: json_value.h:553
ValueIteratorBase SelfType
Definition: json_value.h:501
difference_type computeDistance(const SelfType &other) const
Iterator for object and array value.
Definition: json_value.h:623
SelfType operator--(int)
Definition: json_value.h:655
SelfType & operator--()
Definition: json_value.h:663
reference operator*() const
Definition: json_value.h:677
SelfType & operator++()
Definition: json_value.h:670
ValueIterator SelfType
Definition: json_value.h:631
unsigned int size_t
Definition: json_value.h:627
ValueIterator()=default
SelfType & operator=(const SelfType &other)
SelfType operator++(int)
Definition: json_value.h:647
bool isStaticString() const
Definition: json_value.cpp:163
const char * c_str() const
Definition: json_value.cpp:157
const char * cstr_
Definition: json_value.h:190
CZString & operator=(const CZString &other)=delete
Represents a JSON value.
Definition: json_value.h:147
const_iterator begin() const
friend bool operator==(const Value &, const Value &)
Definition: json_value.cpp:416
Json::UInt UInt
Definition: json_value.h:154
Value & resolveReference(const char *key, bool isStatic)
Definition: json_value.cpp:818
bool isArray() const
const char * asCString() const
Definition: json_value.cpp:462
Value get(UInt index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Definition: json_value.cpp:841
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:706
std::string toStyledString() const
const_iterator end() const
bool isObjectOrNull() const
bool isDouble() const
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:753
static const Value null
Definition: json_value.h:158
Int asInt() const
Definition: json_value.cpp:503
union Json::Value::ValueHolder value_
bool isString() const
UInt ArrayIndex
Definition: json_value.h:156
UInt asUInt() const
Definition: json_value.cpp:545
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:959
static const Int minInt
Definition: json_value.h:159
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:891
ValueType type() const
Definition: json_value.cpp:350
bool isObject() const
Value & operator=(Value const &other)
Definition: json_value.cpp:313
friend bool operator<(const Value &, const Value &)
Definition: json_value.cpp:367
static const Int maxInt
Definition: json_value.h:160
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:336
Value removeMember(const char *key)
Remove and return the named member.
Definition: json_value.cpp:916
Json::Int Int
Definition: json_value.h:155
bool isValidIndex(UInt index) const
Return true if index < size().
Definition: json_value.cpp:848
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
bool isBool() const
Definition: json_value.cpp:986
bool isIntegral() const
bool asBool() const
Definition: json_value.cpp:619
ValueType type_
Definition: json_value.h:434
bool isUInt() const
Definition: json_value.cpp:998
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:980
bool isArrayOrNull() const
static const UInt maxUInt
Definition: json_value.h:161
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:943
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:651
int allocated_
Definition: json_value.h:435
bool isNumeric() const
double asDouble() const
Definition: json_value.cpp:587
Value & operator[](UInt index)
Access an array element (zero based index ).
Definition: json_value.cpp:772
bool isInt() const
Definition: json_value.cpp:992
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
bool operator<(const Value &, const Value &)
Definition: json_value.cpp:367
bool operator>=(const Value &x, const Value &y)
Definition: json_value.h:463
bool operator!=(StaticString x, StaticString y)
Definition: json_value.h:90
ValueType
Type of the value held by a Value object.
Definition: json_value.h:35
@ booleanValue
bool value
Definition: json_value.h:41
@ nullValue
'null' value
Definition: json_value.h:36
@ stringValue
UTF-8 string value.
Definition: json_value.h:40
@ realValue
double value
Definition: json_value.h:39
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
@ intValue
signed integer value
Definition: json_value.h:37
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
@ uintValue
unsigned integer value
Definition: json_value.h:38
bool operator<=(const Value &x, const Value &y)
Definition: json_value.h:451
bool operator==(StaticString x, StaticString y)
Definition: json_value.h:84
int Int
Definition: json_forwards.h:26
unsigned int UInt
Definition: json_forwards.h:27
bool operator>(const Value &x, const Value &y)
Definition: json_value.h:457
ObjectValues * map_
Definition: json_value.h:432