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/basics/Number.h>
24#include <xrpl/json/json_forwards.h>
25
26#include <cstring>
27#include <map>
28#include <string>
29#include <utility>
30#include <vector>
31
34namespace Json {
35
47};
48
64{
65public:
66 constexpr explicit StaticString(char const* czstring) : str_(czstring)
67 {
68 }
69
70 constexpr
71 operator char const*() const
72 {
73 return str_;
74 }
75
76 constexpr char const*
77 c_str() const
78 {
79 return str_;
80 }
81
82private:
83 char const* str_;
84};
85
86inline bool
88{
89 return strcmp(x.c_str(), y.c_str()) == 0;
90}
91
92inline bool
94{
95 return !(x == y);
96}
97
98inline bool
100{
101 return strcmp(x.c_str(), y.c_str()) == 0;
102}
103
104inline bool
106{
107 return !(x == y);
108}
109
110inline bool
112{
113 return y == x;
114}
115
116inline bool
118{
119 return !(y == x);
120}
121
149class Value
150{
151 friend class ValueIteratorBase;
152
153public:
158 using Int = Json::Int;
160
161 static Value const null;
162 static Int const minInt;
163 static Int const maxInt;
164 static UInt const maxUInt;
165
166private:
168 {
169 public:
174 };
175 CZString(int index);
176 CZString(char const* cstr, DuplicationPolicy allocate);
177 CZString(CZString const& other);
178 ~CZString();
179 CZString&
180 operator=(CZString const& other) = delete;
181 bool
182 operator<(CZString const& other) const;
183 bool
184 operator==(CZString const& other) const;
185 int
186 index() const;
187 char const*
188 c_str() const;
189 bool
190 isStaticString() const;
191
192 private:
193 char const* cstr_;
195 };
196
197public:
199
200public:
217 Value(Int value);
218 Value(UInt value);
219 Value(double value);
220 Value(char const* value);
221 Value(ripple::Number const& value);
233 Value(StaticString const& value);
234 Value(std::string const& value);
235 Value(bool value);
236 Value(Value const& other);
237 ~Value();
238
239 Value&
240 operator=(Value const& other);
241 Value&
242 operator=(Value&& other);
243
244 Value(Value&& other) noexcept;
245
247 void
248 swap(Value& other) noexcept;
249
251 type() const;
252
253 char const*
254 asCString() const;
257 asString() const;
258 Int
259 asInt() const;
260 UInt
261 asUInt() const;
262 double
263 asDouble() const;
264 bool
265 asBool() const;
266
267 // TODO: What is the "empty()" method this docstring mentions?
270 bool
271 isNull() const;
272 bool
273 isBool() const;
274 bool
275 isInt() const;
276 bool
277 isUInt() const;
278 bool
279 isIntegral() const;
280 bool
281 isDouble() const;
282 bool
283 isNumeric() const;
284 bool
285 isString() const;
286 bool
287 isArray() const;
288 bool
289 isArrayOrNull() const;
290 bool
291 isObject() const;
292 bool
293 isObjectOrNull() const;
294
295 bool
296 isConvertibleTo(ValueType other) const;
297
299 UInt
300 size() const;
301
304 explicit
305 operator bool() const;
306
310 void
311 clear();
312
318 Value&
319 operator[](UInt index);
323 Value const&
324 operator[](UInt index) const;
327 Value
328 get(UInt index, Value const& defaultValue) const;
330 bool
331 isValidIndex(UInt index) const;
335 Value&
336 append(Value const& value);
337 Value&
338 append(Value&& value);
339
342 Value&
343 operator[](char const* key);
346 Value const&
347 operator[](char const* key) const;
350 Value&
351 operator[](std::string const& key);
354 Value const&
355 operator[](std::string const& key) const;
369 Value&
370 operator[](StaticString const& key);
371 Value const&
372 operator[](StaticString const& key) const;
373
375 Value
376 get(char const* key, Value const& defaultValue) const;
378 Value
379 get(std::string const& key, Value const& defaultValue) const;
380
387 Value
388 removeMember(char const* key);
390 Value
391 removeMember(std::string const& key);
392
394 bool
395 isMember(char const* key) const;
397 bool
398 isMember(std::string const& key) const;
399
405 Members
406 getMemberNames() const;
407
409 toStyledString() const;
410
412 begin() const;
414 end() const;
415
417 begin();
419 end();
420
421 friend bool
422 operator==(Value const&, Value const&);
423 friend bool
424 operator<(Value const&, Value const&);
425
426private:
427 Value&
428 resolveReference(char const* key, bool isStatic);
429
430private:
432 {
435 double real_;
436 bool bool_;
437 char* string_;
441 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
442};
443
444inline Value
446{
447 return to_string(number);
448}
449
450bool
451operator==(Value const&, Value const&);
452
453inline bool
454operator!=(Value const& x, Value const& y)
455{
456 return !(x == y);
457}
458
459bool
460operator<(Value const&, Value const&);
461
462inline bool
463operator<=(Value const& x, Value const& y)
464{
465 return !(y < x);
466}
467
468inline bool
469operator>(Value const& x, Value const& y)
470{
471 return y < x;
472}
473
474inline bool
475operator>=(Value const& x, Value const& y)
476{
477 return !(x < y);
478}
479
489{
490public:
491 enum { unknown = (unsigned)-1 };
492
493 virtual ~ValueAllocator() = default;
494
495 virtual char*
496 makeMemberName(char const* memberName) = 0;
497 virtual void
498 releaseMemberName(char* memberName) = 0;
499 virtual char*
500 duplicateStringValue(char const* value, unsigned int length = unknown) = 0;
501 virtual void
502 releaseStringValue(char* value) = 0;
503};
504
509{
510public:
511 using size_t = unsigned int;
512 using difference_type = int;
514
516
517 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
518
519 bool
520 operator==(SelfType const& other) const
521 {
522 return isEqual(other);
523 }
524
525 bool
526 operator!=(SelfType const& other) const
527 {
528 return !isEqual(other);
529 }
530
533 Value
534 key() const;
535
537 UInt
538 index() const;
539
542 char const*
543 memberName() const;
544
545protected:
546 Value&
547 deref() const;
548
549 void
550 increment();
551
552 void
553 decrement();
554
556 computeDistance(SelfType const& other) const;
557
558 bool
559 isEqual(SelfType const& other) const;
560
561 void
562 copy(SelfType const& other);
563
564private:
565 Value::ObjectValues::iterator current_;
566 // Indicates that iterator is for a null value.
568};
569
574{
575 friend class Value;
576
577public:
578 using size_t = unsigned int;
579 using difference_type = int;
580 using reference = Value const&;
581 using pointer = Value const*;
583
585
586private:
589 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
590
591public:
592 SelfType&
593 operator=(ValueIteratorBase const& other);
594
597 {
598 SelfType temp(*this);
599 ++*this;
600 return temp;
601 }
602
605 {
606 SelfType temp(*this);
607 --*this;
608 return temp;
609 }
610
611 SelfType&
613 {
614 decrement();
615 return *this;
616 }
617
618 SelfType&
620 {
621 increment();
622 return *this;
623 }
624
626 operator*() const
627 {
628 return deref();
629 }
630};
631
635{
636 friend class Value;
637
638public:
639 using size_t = unsigned int;
640 using difference_type = int;
641 using reference = Value&;
642 using pointer = Value*;
644
645 ValueIterator() = default;
646 ValueIterator(ValueConstIterator const& other);
647 ValueIterator(ValueIterator const& other);
648
649private:
652 explicit ValueIterator(Value::ObjectValues::iterator const& current);
653
654public:
655 SelfType&
656 operator=(SelfType const& other);
657
660 {
661 SelfType temp(*this);
662 ++*this;
663 return temp;
664 }
665
668 {
669 SelfType temp(*this);
670 --*this;
671 return temp;
672 }
673
674 SelfType&
676 {
677 decrement();
678 return *this;
679 }
680
681 SelfType&
683 {
684 increment();
685 return *this;
686 }
687
689 operator*() const
690 {
691 return deref();
692 }
693};
694
695} // namespace Json
696
697#endif // CPPTL_JSON_H_INCLUDED
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition: json_value.h:64
constexpr char const * c_str() const
Definition: json_value.h:77
char const * str_
Definition: json_value.h:83
constexpr StaticString(char const *czstring)
Definition: json_value.h:66
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:489
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:574
SelfType & operator=(ValueIteratorBase const &other)
SelfType & operator--()
Definition: json_value.h:612
Value const * pointer
Definition: json_value.h:581
ValueConstIterator SelfType
Definition: json_value.h:582
SelfType operator--(int)
Definition: json_value.h:604
SelfType & operator++()
Definition: json_value.h:619
Value const & reference
Definition: json_value.h:580
SelfType operator++(int)
Definition: json_value.h:596
reference operator*() const
Definition: json_value.h:626
base class for Value iterators.
Definition: json_value.h:509
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:526
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition: json_value.h:565
ValueIteratorBase SelfType
Definition: json_value.h:513
bool operator==(SelfType const &other) const
Definition: json_value.h:520
Iterator for object and array value.
Definition: json_value.h:635
SelfType operator--(int)
Definition: json_value.h:667
SelfType & operator=(SelfType const &other)
SelfType & operator--()
Definition: json_value.h:675
reference operator*() const
Definition: json_value.h:689
SelfType & operator++()
Definition: json_value.h:682
ValueIterator SelfType
Definition: json_value.h:643
unsigned int size_t
Definition: json_value.h:639
ValueIterator()=default
SelfType operator++(int)
Definition: json_value.h:659
bool isStaticString() const
Definition: json_value.cpp:169
char const * cstr_
Definition: json_value.h:193
CZString & operator=(CZString const &other)=delete
char const * c_str() const
Definition: json_value.cpp:163
Represents a JSON value.
Definition: json_value.h:150
const_iterator begin() const
Json::UInt UInt
Definition: json_value.h:157
bool isArray() const
static Int const minInt
Definition: json_value.h:162
Value & append(Value const &value)
Append value to array at the end.
Definition: json_value.cpp:910
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:719
std::string toStyledString() const
const_iterator end() const
bool isObjectOrNull() const
static Value const null
Definition: json_value.h:161
bool isDouble() const
static UInt const maxUInt
Definition: json_value.h:164
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:766
char const * asCString() const
Definition: json_value.cpp:475
Int asInt() const
Definition: json_value.cpp:516
union Json::Value::ValueHolder value_
bool isString() const
UInt ArrayIndex
Definition: json_value.h:159
friend bool operator==(Value const &, Value const &)
Definition: json_value.cpp:429
UInt asUInt() const
Definition: json_value.cpp:558
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:978
ValueType type() const
Definition: json_value.cpp:363
bool isObject() const
Value & operator=(Value const &other)
Definition: json_value.cpp:326
Value removeMember(char const *key)
Remove and return the named member.
Definition: json_value.cpp:935
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:349
Json::Int Int
Definition: json_value.h:158
bool isValidIndex(UInt index) const
Return true if index < size().
Definition: json_value.cpp:861
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482
bool isBool() const
bool isIntegral() const
bool asBool() const
Definition: json_value.cpp:632
ValueType type_
Definition: json_value.h:440
bool isUInt() const
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:999
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:962
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:854
friend bool operator<(Value const &, Value const &)
Definition: json_value.cpp:380
static Int const maxInt
Definition: json_value.h:163
Value & resolveReference(char const *key, bool isStatic)
Definition: json_value.cpp:831
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:664
int allocated_
Definition: json_value.h:441
bool isNumeric() const
double asDouble() const
Definition: json_value.cpp:600
Value & operator[](UInt index)
Access an array element (zero based index ).
Definition: json_value.cpp:785
bool isInt() const
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
bool operator<=(Value const &x, Value const &y)
Definition: json_value.h:463
bool operator>(Value const &x, Value const &y)
Definition: json_value.h:469
std::string to_string(Value const &)
Writes a Json::Value to an std::string.
Definition: to_string.cpp:28
bool operator!=(StaticString x, StaticString y)
Definition: json_value.h:93
Value to_json(ripple::Number const &number)
Definition: json_value.h:445
ValueType
Type of the value held by a Value object.
Definition: json_value.h:38
@ booleanValue
bool value
Definition: json_value.h:44
@ nullValue
'null' value
Definition: json_value.h:39
@ stringValue
UTF-8 string value.
Definition: json_value.h:43
@ realValue
double value
Definition: json_value.h:42
@ arrayValue
array value (ordered list)
Definition: json_value.h:45
@ intValue
signed integer value
Definition: json_value.h:40
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:46
@ uintValue
unsigned integer value
Definition: json_value.h:41
bool operator<(Value const &, Value const &)
Definition: json_value.cpp:380
bool operator==(StaticString x, StaticString y)
Definition: json_value.h:87
int Int
Definition: json_forwards.h:26
bool operator>=(Value const &x, Value const &y)
Definition: json_value.h:475
unsigned int UInt
Definition: json_forwards.h:27
ObjectValues * map_
Definition: json_value.h:438