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
25#include <cstring>
26#include <map>
27#include <string>
28#include <vector>
29
32namespace Json {
33
45};
46
62{
63public:
64 constexpr explicit StaticString(const char* czstring) : str_(czstring)
65 {
66 }
67
68 constexpr
69 operator const char*() const
70 {
71 return str_;
72 }
73
74 constexpr const char*
75 c_str() const
76 {
77 return str_;
78 }
79
80private:
81 const char* str_;
82};
83
84inline bool
86{
87 return strcmp(x.c_str(), y.c_str()) == 0;
88}
89
90inline bool
92{
93 return !(x == y);
94}
95
96inline bool
98{
99 return strcmp(x.c_str(), y.c_str()) == 0;
100}
101
102inline bool
104{
105 return !(x == y);
106}
107
108inline bool
110{
111 return y == x;
112}
113
114inline bool
116{
117 return !(y == x);
118}
119
147class Value
148{
149 friend class ValueIteratorBase;
150
151public:
156 using Int = Json::Int;
158
159 static const Value null;
160 static const Int minInt;
161 static const Int maxInt;
162 static const UInt maxUInt;
163
164private:
166 {
167 public:
172 };
173 CZString(int index);
174 CZString(const char* cstr, DuplicationPolicy allocate);
175 CZString(const CZString& other);
176 ~CZString();
177 CZString&
178 operator=(const CZString& other) = delete;
179 bool
180 operator<(const CZString& other) const;
181 bool
182 operator==(const CZString& other) const;
183 int
184 index() const;
185 const char*
186 c_str() const;
187 bool
188 isStaticString() const;
189
190 private:
191 const char* cstr_;
193 };
194
195public:
197
198public:
215 Value(Int value);
216 Value(UInt value);
217 Value(double value);
218 Value(const char* value);
230 Value(const StaticString& value);
231 Value(std::string const& value);
232 Value(bool value);
233 Value(const Value& other);
234 ~Value();
235
236 Value&
237 operator=(Value const& other);
238 Value&
239 operator=(Value&& other);
240
241 Value(Value&& other) noexcept;
242
244 void
245 swap(Value& other) noexcept;
246
248 type() const;
249
250 const char*
251 asCString() const;
254 asString() const;
255 Int
256 asInt() const;
257 UInt
258 asUInt() const;
259 double
260 asDouble() const;
261 bool
262 asBool() const;
263
264 // TODO: What is the "empty()" method this docstring mentions?
267 bool
268 isNull() const;
269 bool
270 isBool() const;
271 bool
272 isInt() const;
273 bool
274 isUInt() const;
275 bool
276 isIntegral() const;
277 bool
278 isDouble() const;
279 bool
280 isNumeric() const;
281 bool
282 isString() const;
283 bool
284 isArray() const;
285 bool
286 isArrayOrNull() const;
287 bool
288 isObject() const;
289 bool
290 isObjectOrNull() const;
291
292 bool
293 isConvertibleTo(ValueType other) const;
294
296 UInt
297 size() const;
298
301 explicit
302 operator bool() const;
303
307 void
308 clear();
309
315 Value&
316 operator[](UInt index);
320 const Value&
321 operator[](UInt index) const;
324 Value
325 get(UInt index, const Value& defaultValue) const;
327 bool
328 isValidIndex(UInt index) const;
332 Value&
333 append(const Value& value);
334 Value&
335 append(Value&& value);
336
339 Value&
340 operator[](const char* key);
343 const Value&
344 operator[](const char* key) const;
347 Value&
348 operator[](std::string const& key);
351 const Value&
352 operator[](std::string const& key) const;
366 Value&
367 operator[](const StaticString& key);
368
370 Value
371 get(const char* key, const Value& defaultValue) const;
373 Value
374 get(std::string const& key, const Value& defaultValue) const;
375
382 Value
383 removeMember(const char* key);
385 Value
386 removeMember(std::string const& key);
387
389 bool
390 isMember(const char* key) const;
392 bool
393 isMember(std::string const& key) const;
394
400 Members
401 getMemberNames() const;
402
404 toStyledString() const;
405
407 begin() const;
409 end() const;
410
412 begin();
414 end();
415
416 friend bool
417 operator==(const Value&, const Value&);
418 friend bool
419 operator<(const Value&, const Value&);
420
421private:
422 Value&
423 resolveReference(const char* key, bool isStatic);
424
425private:
427 {
430 double real_;
431 bool bool_;
432 char* string_;
436 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
437};
438
439bool
440operator==(const Value&, const Value&);
441
442inline bool
443operator!=(const Value& x, const Value& y)
444{
445 return !(x == y);
446}
447
448bool
449operator<(const Value&, const Value&);
450
451inline bool
452operator<=(const Value& x, const Value& y)
453{
454 return !(y < x);
455}
456
457inline bool
458operator>(const Value& x, const Value& y)
459{
460 return y < x;
461}
462
463inline bool
464operator>=(const Value& x, const Value& y)
465{
466 return !(x < y);
467}
468
478{
479public:
480 enum { unknown = (unsigned)-1 };
481
482 virtual ~ValueAllocator() = default;
483
484 virtual char*
485 makeMemberName(const char* memberName) = 0;
486 virtual void
487 releaseMemberName(char* memberName) = 0;
488 virtual char*
489 duplicateStringValue(const char* value, unsigned int length = unknown) = 0;
490 virtual void
491 releaseStringValue(char* value) = 0;
492};
493
498{
499public:
500 using size_t = unsigned int;
501 using difference_type = int;
503
505
506 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
507
508 bool
509 operator==(const SelfType& other) const
510 {
511 return isEqual(other);
512 }
513
514 bool
515 operator!=(const SelfType& other) const
516 {
517 return !isEqual(other);
518 }
519
522 Value
523 key() const;
524
526 UInt
527 index() const;
528
531 const char*
532 memberName() const;
533
534protected:
535 Value&
536 deref() const;
537
538 void
539 increment();
540
541 void
542 decrement();
543
545 computeDistance(const SelfType& other) const;
546
547 bool
548 isEqual(const SelfType& other) const;
549
550 void
551 copy(const SelfType& other);
552
553private:
554 Value::ObjectValues::iterator current_;
555 // Indicates that iterator is for a null value.
557};
558
563{
564 friend class Value;
565
566public:
567 using size_t = unsigned int;
568 using difference_type = int;
569 using reference = const Value&;
570 using pointer = const Value*;
572
574
575private:
578 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
579
580public:
581 SelfType&
582 operator=(const ValueIteratorBase& other);
583
586 {
587 SelfType temp(*this);
588 ++*this;
589 return temp;
590 }
591
594 {
595 SelfType temp(*this);
596 --*this;
597 return temp;
598 }
599
600 SelfType&
602 {
603 decrement();
604 return *this;
605 }
606
607 SelfType&
609 {
610 increment();
611 return *this;
612 }
613
615 operator*() const
616 {
617 return deref();
618 }
619};
620
624{
625 friend class Value;
626
627public:
628 using size_t = unsigned int;
629 using difference_type = int;
630 using reference = Value&;
631 using pointer = Value*;
633
634 ValueIterator() = default;
635 ValueIterator(const ValueConstIterator& other);
636 ValueIterator(const ValueIterator& other);
637
638private:
641 explicit ValueIterator(const Value::ObjectValues::iterator& current);
642
643public:
644 SelfType&
645 operator=(const SelfType& other);
646
649 {
650 SelfType temp(*this);
651 ++*this;
652 return temp;
653 }
654
657 {
658 SelfType temp(*this);
659 --*this;
660 return temp;
661 }
662
663 SelfType&
665 {
666 decrement();
667 return *this;
668 }
669
670 SelfType&
672 {
673 increment();
674 return *this;
675 }
676
678 operator*() const
679 {
680 return deref();
681 }
682};
683
684} // namespace Json
685
686#endif // CPPTL_JSON_H_INCLUDED
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition: json_value.h:62
const char * str_
Definition: json_value.h:81
constexpr StaticString(const char *czstring)
Definition: json_value.h:64
constexpr const char * c_str() const
Definition: json_value.h:75
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:478
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:563
SelfType & operator--()
Definition: json_value.h:601
const Value & reference
Definition: json_value.h:569
ValueConstIterator SelfType
Definition: json_value.h:571
SelfType operator--(int)
Definition: json_value.h:593
SelfType & operator++()
Definition: json_value.h:608
SelfType operator++(int)
Definition: json_value.h:585
SelfType & operator=(const ValueIteratorBase &other)
reference operator*() const
Definition: json_value.h:615
base class for Value iterators.
Definition: json_value.h:498
bool isEqual(const SelfType &other) const
bool operator==(const SelfType &other) const
Definition: json_value.h:509
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:515
Value::ObjectValues::iterator current_
Definition: json_value.h:554
ValueIteratorBase SelfType
Definition: json_value.h:502
difference_type computeDistance(const SelfType &other) const
Iterator for object and array value.
Definition: json_value.h:624
SelfType operator--(int)
Definition: json_value.h:656
SelfType & operator--()
Definition: json_value.h:664
reference operator*() const
Definition: json_value.h:678
SelfType & operator++()
Definition: json_value.h:671
ValueIterator SelfType
Definition: json_value.h:632
unsigned int size_t
Definition: json_value.h:628
ValueIterator()=default
SelfType & operator=(const SelfType &other)
SelfType operator++(int)
Definition: json_value.h:648
bool isStaticString() const
Definition: json_value.cpp:169
const char * c_str() const
Definition: json_value.cpp:163
const char * cstr_
Definition: json_value.h:191
CZString & operator=(const CZString &other)=delete
Represents a JSON value.
Definition: json_value.h:148
const_iterator begin() const
friend bool operator==(const Value &, const Value &)
Definition: json_value.cpp:422
Json::UInt UInt
Definition: json_value.h:155
Value & resolveReference(const char *key, bool isStatic)
Definition: json_value.cpp:824
bool isArray() const
const char * asCString() const
Definition: json_value.cpp:468
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:847
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:712
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:759
static const Value null
Definition: json_value.h:159
Int asInt() const
Definition: json_value.cpp:509
union Json::Value::ValueHolder value_
bool isString() const
UInt ArrayIndex
Definition: json_value.h:157
UInt asUInt() const
Definition: json_value.cpp:551
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:965
static const Int minInt
Definition: json_value.h:160
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:897
ValueType type() const
Definition: json_value.cpp:356
bool isObject() const
Value & operator=(Value const &other)
Definition: json_value.cpp:319
friend bool operator<(const Value &, const Value &)
Definition: json_value.cpp:373
static const Int maxInt
Definition: json_value.h:161
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:342
Value removeMember(const char *key)
Remove and return the named member.
Definition: json_value.cpp:922
Json::Int Int
Definition: json_value.h:156
bool isValidIndex(UInt index) const
Return true if index < size().
Definition: json_value.cpp:854
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:475
bool isBool() const
Definition: json_value.cpp:992
bool isIntegral() const
bool asBool() const
Definition: json_value.cpp:625
ValueType type_
Definition: json_value.h:435
bool isUInt() const
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:986
bool isArrayOrNull() const
static const UInt maxUInt
Definition: json_value.h:162
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:949
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:657
int allocated_
Definition: json_value.h:436
bool isNumeric() const
double asDouble() const
Definition: json_value.cpp:593
Value & operator[](UInt index)
Access an array element (zero based index ).
Definition: json_value.cpp:778
bool isInt() const
Definition: json_value.cpp:998
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
bool operator<(const Value &, const Value &)
Definition: json_value.cpp:373
bool operator>=(const Value &x, const Value &y)
Definition: json_value.h:464
bool operator!=(StaticString x, StaticString y)
Definition: json_value.h:91
ValueType
Type of the value held by a Value object.
Definition: json_value.h:36
@ booleanValue
bool value
Definition: json_value.h:42
@ nullValue
'null' value
Definition: json_value.h:37
@ stringValue
UTF-8 string value.
Definition: json_value.h:41
@ realValue
double value
Definition: json_value.h:40
@ arrayValue
array value (ordered list)
Definition: json_value.h:43
@ intValue
signed integer value
Definition: json_value.h:38
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:44
@ uintValue
unsigned integer value
Definition: json_value.h:39
bool operator<=(const Value &x, const Value &y)
Definition: json_value.h:452
bool operator==(StaticString x, StaticString y)
Definition: json_value.h:85
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:458
ObjectValues * map_
Definition: json_value.h:433