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(char const* czstring) : str_(czstring)
65 {
66 }
67
68 constexpr
69 operator char const*() const
70 {
71 return str_;
72 }
73
74 constexpr char const*
75 c_str() const
76 {
77 return str_;
78 }
79
80private:
81 char const* 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 Value const null;
160 static Int const minInt;
161 static Int const maxInt;
162 static UInt const maxUInt;
163
164private:
166 {
167 public:
172 };
173 CZString(int index);
174 CZString(char const* cstr, DuplicationPolicy allocate);
175 CZString(CZString const& other);
176 ~CZString();
177 CZString&
178 operator=(CZString const& other) = delete;
179 bool
180 operator<(CZString const& other) const;
181 bool
182 operator==(CZString const& other) const;
183 int
184 index() const;
185 char const*
186 c_str() const;
187 bool
188 isStaticString() const;
189
190 private:
191 char const* cstr_;
193 };
194
195public:
197
198public:
215 Value(Int value);
216 Value(UInt value);
217 Value(double value);
218 Value(char const* value);
230 Value(StaticString const& value);
231 Value(std::string const& value);
232 Value(bool value);
233 Value(Value const& 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 char const*
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 Value const&
321 operator[](UInt index) const;
324 Value
325 get(UInt index, Value const& defaultValue) const;
327 bool
328 isValidIndex(UInt index) const;
332 Value&
333 append(Value const& value);
334 Value&
335 append(Value&& value);
336
339 Value&
340 operator[](char const* key);
343 Value const&
344 operator[](char const* key) const;
347 Value&
348 operator[](std::string const& key);
351 Value const&
352 operator[](std::string const& key) const;
366 Value&
367 operator[](StaticString const& key);
368
370 Value
371 get(char const* key, Value const& defaultValue) const;
373 Value
374 get(std::string const& key, Value const& defaultValue) const;
375
382 Value
383 removeMember(char const* key);
385 Value
386 removeMember(std::string const& key);
387
389 bool
390 isMember(char const* 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==(Value const&, Value const&);
418 friend bool
419 operator<(Value const&, Value const&);
420
421private:
422 Value&
423 resolveReference(char const* 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==(Value const&, Value const&);
441
442inline bool
443operator!=(Value const& x, Value const& y)
444{
445 return !(x == y);
446}
447
448bool
449operator<(Value const&, Value const&);
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 y < x;
461}
462
463inline bool
464operator>=(Value const& x, Value const& 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(char const* memberName) = 0;
486 virtual void
487 releaseMemberName(char* memberName) = 0;
488 virtual char*
489 duplicateStringValue(char const* 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(Value::ObjectValues::iterator const& current);
507
508 bool
509 operator==(SelfType const& other) const
510 {
511 return isEqual(other);
512 }
513
514 bool
515 operator!=(SelfType const& other) const
516 {
517 return !isEqual(other);
518 }
519
522 Value
523 key() const;
524
526 UInt
527 index() const;
528
531 char const*
532 memberName() const;
533
534protected:
535 Value&
536 deref() const;
537
538 void
539 increment();
540
541 void
542 decrement();
543
545 computeDistance(SelfType const& other) const;
546
547 bool
548 isEqual(SelfType const& other) const;
549
550 void
551 copy(SelfType const& 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 = Value const&;
570 using pointer = Value const*;
572
574
575private:
578 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
579
580public:
581 SelfType&
582 operator=(ValueIteratorBase const& 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(ValueConstIterator const& other);
636 ValueIterator(ValueIterator const& other);
637
638private:
641 explicit ValueIterator(Value::ObjectValues::iterator const& current);
642
643public:
644 SelfType&
645 operator=(SelfType const& 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
constexpr char const * c_str() const
Definition: json_value.h:75
char const * str_
Definition: json_value.h:81
constexpr StaticString(char const *czstring)
Definition: json_value.h:64
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:478
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:563
SelfType & operator=(ValueIteratorBase const &other)
SelfType & operator--()
Definition: json_value.h:601
Value const * pointer
Definition: json_value.h:570
ValueConstIterator SelfType
Definition: json_value.h:571
SelfType operator--(int)
Definition: json_value.h:593
SelfType & operator++()
Definition: json_value.h:608
Value const & reference
Definition: json_value.h:569
SelfType operator++(int)
Definition: json_value.h:585
reference operator*() const
Definition: json_value.h:615
base class for Value iterators.
Definition: json_value.h:498
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:515
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition: json_value.h:554
ValueIteratorBase SelfType
Definition: json_value.h:502
bool operator==(SelfType const &other) const
Definition: json_value.h:509
Iterator for object and array value.
Definition: json_value.h:624
SelfType operator--(int)
Definition: json_value.h:656
SelfType & operator=(SelfType const &other)
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++(int)
Definition: json_value.h:648
bool isStaticString() const
Definition: json_value.cpp:169
char const * cstr_
Definition: json_value.h:191
CZString & operator=(CZString const &other)=delete
char const * c_str() const
Definition: json_value.cpp:163
Represents a JSON value.
Definition: json_value.h:148
const_iterator begin() const
Json::UInt UInt
Definition: json_value.h:155
bool isArray() const
static Int const minInt
Definition: json_value.h:160
Value & append(Value const &value)
Append value to array at the end.
Definition: json_value.cpp:897
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
static Value const null
Definition: json_value.h:159
bool isDouble() const
static UInt const maxUInt
Definition: json_value.h:162
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:759
char const * asCString() const
Definition: json_value.cpp:468
Int asInt() const
Definition: json_value.cpp:509
union Json::Value::ValueHolder value_
bool isString() const
UInt ArrayIndex
Definition: json_value.h:157
friend bool operator==(Value const &, Value const &)
Definition: json_value.cpp:422
UInt asUInt() const
Definition: json_value.cpp:551
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:965
ValueType type() const
Definition: json_value.cpp:356
bool isObject() const
Value & operator=(Value const &other)
Definition: json_value.cpp:319
Value removeMember(char const *key)
Remove and return the named member.
Definition: json_value.cpp:922
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:342
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 isMember(char const *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:949
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...
Definition: json_value.cpp:847
friend bool operator<(Value const &, Value const &)
Definition: json_value.cpp:373
static Int const maxInt
Definition: json_value.h:161
Value & resolveReference(char const *key, bool isStatic)
Definition: json_value.cpp:824
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<=(Value const &x, Value const &y)
Definition: json_value.h:452
bool operator>(Value const &x, Value const &y)
Definition: json_value.h:458
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<(Value const &, Value const &)
Definition: json_value.cpp:373
bool operator==(StaticString x, StaticString y)
Definition: json_value.h:85
int Int
Definition: json_forwards.h:26
bool operator>=(Value const &x, Value const &y)
Definition: json_value.h:464
unsigned int UInt
Definition: json_forwards.h:27
ObjectValues * map_
Definition: json_value.h:433