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 <vector>
30
33namespace Json {
34
47
63{
64public:
65 constexpr explicit StaticString(char const* czstring) : str_(czstring)
66 {
67 }
68
69 constexpr
70 operator char const*() const
71 {
72 return str_;
73 }
74
75 constexpr char const*
76 c_str() const
77 {
78 return str_;
79 }
80
81private:
82 char const* str_;
83};
84
85inline bool
87{
88 return strcmp(x.c_str(), y.c_str()) == 0;
89}
90
91inline bool
93{
94 return !(x == y);
95}
96
97inline bool
99{
100 return strcmp(x.c_str(), y.c_str()) == 0;
101}
102
103inline bool
105{
106 return !(x == y);
107}
108
109inline bool
111{
112 return y == x;
113}
114
115inline bool
117{
118 return !(y == x);
119}
120
148class Value
149{
150 friend class ValueIteratorBase;
151
152public:
157 using Int = Json::Int;
159
160 static Value const null;
161 static Int const minInt;
162 static Int const maxInt;
163 static UInt const maxUInt;
164
165private:
167 {
168 public:
174 CZString(int index);
175 CZString(char const* cstr, DuplicationPolicy allocate);
176 CZString(CZString const& other);
177 ~CZString();
178 CZString&
179 operator=(CZString const& other) = delete;
180 bool
181 operator<(CZString const& other) const;
182 bool
183 operator==(CZString const& other) const;
184 int
185 index() const;
186 char const*
187 c_str() const;
188 bool
189 isStaticString() const;
190
191 private:
192 char const* cstr_;
194 };
195
196public:
198
199public:
216 Value(Int value);
217 Value(UInt value);
218 Value(double value);
219 Value(char const* value);
220 Value(ripple::Number const& value);
232 Value(StaticString const& value);
233 Value(std::string const& value);
234 Value(bool value);
235 Value(Value const& other);
236 ~Value();
237
238 Value&
239 operator=(Value const& other);
240 Value&
241 operator=(Value&& other);
242
243 Value(Value&& other) noexcept;
244
246 void
247 swap(Value& other) noexcept;
248
250 type() const;
251
252 char const*
253 asCString() const;
256 asString() const;
257 Int
258 asInt() const;
259 UInt
260 asUInt() const;
261 double
262 asDouble() const;
263 bool
264 asBool() const;
265
266 // TODO: What is the "empty()" method this docstring mentions?
269 bool
270 isNull() const;
271 bool
272 isBool() const;
273 bool
274 isInt() const;
275 bool
276 isUInt() const;
277 bool
278 isIntegral() const;
279 bool
280 isDouble() const;
281 bool
282 isNumeric() const;
283 bool
284 isString() const;
285 bool
286 isArray() const;
287 bool
288 isArrayOrNull() const;
289 bool
290 isObject() const;
291 bool
292 isObjectOrNull() const;
293
294 bool
295 isConvertibleTo(ValueType other) const;
296
298 UInt
299 size() const;
300
303 explicit
304 operator bool() const;
305
309 void
310 clear();
311
317 Value&
318 operator[](UInt index);
322 Value const&
323 operator[](UInt index) const;
326 Value
327 get(UInt index, Value const& defaultValue) const;
329 bool
330 isValidIndex(UInt index) const;
334 Value&
335 append(Value const& value);
336 Value&
337 append(Value&& value);
338
341 Value&
342 operator[](char const* key);
345 Value const&
346 operator[](char const* key) const;
349 Value&
350 operator[](std::string const& key);
353 Value const&
354 operator[](std::string const& key) const;
368 Value&
369 operator[](StaticString const& key);
370 Value const&
371 operator[](StaticString const& key) const;
372
374 Value
375 get(char const* key, Value const& defaultValue) const;
377 Value
378 get(std::string const& key, Value const& defaultValue) const;
379
386 Value
387 removeMember(char const* key);
389 Value
390 removeMember(std::string const& key);
391
393 bool
394 isMember(char const* key) const;
396 bool
397 isMember(std::string const& key) const;
398
404 Members
405 getMemberNames() const;
406
408 toStyledString() const;
409
411 begin() const;
413 end() const;
414
416 begin();
418 end();
419
420 friend bool
421 operator==(Value const&, Value const&);
422 friend bool
423 operator<(Value const&, Value const&);
424
425private:
426 Value&
427 resolveReference(char const* key, bool isStatic);
428
429private:
440 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
441};
442
443inline Value
445{
446 return to_string(number);
447}
448
449bool
450operator==(Value const&, Value const&);
451
452inline bool
453operator!=(Value const& x, Value const& y)
454{
455 return !(x == y);
456}
457
458bool
459operator<(Value const&, Value const&);
460
461inline bool
462operator<=(Value const& x, Value const& y)
463{
464 return !(y < x);
465}
466
467inline bool
468operator>(Value const& x, Value const& y)
469{
470 return y < x;
471}
472
473inline bool
474operator>=(Value const& x, Value const& y)
475{
476 return !(x < y);
477}
478
488{
489public:
490 enum { unknown = (unsigned)-1 };
491
492 virtual ~ValueAllocator() = default;
493
494 virtual char*
495 makeMemberName(char const* memberName) = 0;
496 virtual void
497 releaseMemberName(char* memberName) = 0;
498 virtual char*
499 duplicateStringValue(char const* value, unsigned int length = unknown) = 0;
500 virtual void
501 releaseStringValue(char* value) = 0;
502};
503
508{
509public:
510 using size_t = unsigned int;
511 using difference_type = int;
513
515
516 explicit ValueIteratorBase(Value::ObjectValues::iterator const& current);
517
518 bool
519 operator==(SelfType const& other) const
520 {
521 return isEqual(other);
522 }
523
524 bool
525 operator!=(SelfType const& other) const
526 {
527 return !isEqual(other);
528 }
529
532 Value
533 key() const;
534
536 UInt
537 index() const;
538
541 char const*
542 memberName() const;
543
544protected:
545 Value&
546 deref() const;
547
548 void
549 increment();
550
551 void
552 decrement();
553
555 computeDistance(SelfType const& other) const;
556
557 bool
558 isEqual(SelfType const& other) const;
559
560 void
561 copy(SelfType const& other);
562
563private:
564 Value::ObjectValues::iterator current_;
565 // Indicates that iterator is for a null value.
567};
568
573{
574 friend class Value;
575
576public:
577 using size_t = unsigned int;
578 using difference_type = int;
579 using reference = Value const&;
580 using pointer = Value const*;
582
584
585private:
588 explicit ValueConstIterator(Value::ObjectValues::iterator const& current);
589
590public:
591 SelfType&
592 operator=(ValueIteratorBase const& other);
593
596 {
597 SelfType temp(*this);
598 ++*this;
599 return temp;
600 }
601
604 {
605 SelfType temp(*this);
606 --*this;
607 return temp;
608 }
609
610 SelfType&
612 {
613 decrement();
614 return *this;
615 }
616
617 SelfType&
619 {
620 increment();
621 return *this;
622 }
623
625 operator*() const
626 {
627 return deref();
628 }
629};
630
634{
635 friend class Value;
636
637public:
638 using size_t = unsigned int;
639 using difference_type = int;
640 using reference = Value&;
641 using pointer = Value*;
643
644 ValueIterator() = default;
645 ValueIterator(ValueConstIterator const& other);
646 ValueIterator(ValueIterator const& other);
647
648private:
651 explicit ValueIterator(Value::ObjectValues::iterator const& current);
652
653public:
654 SelfType&
655 operator=(SelfType const& other);
656
659 {
660 SelfType temp(*this);
661 ++*this;
662 return temp;
663 }
664
667 {
668 SelfType temp(*this);
669 --*this;
670 return temp;
671 }
672
673 SelfType&
675 {
676 decrement();
677 return *this;
678 }
679
680 SelfType&
682 {
683 increment();
684 return *this;
685 }
686
688 operator*() const
689 {
690 return deref();
691 }
692};
693
694} // namespace Json
695
696#endif // CPPTL_JSON_H_INCLUDED
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:63
constexpr char const * c_str() const
Definition json_value.h:76
char const * str_
Definition json_value.h:82
constexpr StaticString(char const *czstring)
Definition json_value.h:65
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition json_value.h:488
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:573
SelfType & operator=(ValueIteratorBase const &other)
ValueConstIterator SelfType
Definition json_value.h:581
SelfType operator--(int)
Definition json_value.h:603
Value const & reference
Definition json_value.h:579
SelfType operator++(int)
Definition json_value.h:595
reference operator*() const
Definition json_value.h:625
base class for Value iterators.
Definition json_value.h:508
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:525
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition json_value.h:564
ValueIteratorBase SelfType
Definition json_value.h:512
bool operator==(SelfType const &other) const
Definition json_value.h:519
Iterator for object and array value.
Definition json_value.h:634
SelfType operator--(int)
Definition json_value.h:666
SelfType & operator=(SelfType const &other)
SelfType & operator--()
Definition json_value.h:674
reference operator*() const
Definition json_value.h:688
SelfType & operator++()
Definition json_value.h:681
ValueIterator SelfType
Definition json_value.h:642
unsigned int size_t
Definition json_value.h:638
ValueIterator()=default
SelfType operator++(int)
Definition json_value.h:658
bool isStaticString() const
CZString & operator=(CZString const &other)=delete
char const * c_str() const
Represents a JSON value.
Definition json_value.h:149
const_iterator begin() const
Json::UInt UInt
Definition json_value.h:156
bool isArray() const
static Int const minInt
Definition json_value.h:161
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:160
bool isDouble() const
static UInt const maxUInt
Definition json_value.h:163
void clear()
Remove all object members and array elements.
char const * asCString() const
Int asInt() const
union Json::Value::ValueHolder value_
bool isString() const
UInt ArrayIndex
Definition json_value.h:158
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:157
bool isValidIndex(UInt index) const
Return true if index < size().
std::string asString() const
Returns the unquoted string value.
bool isBool() const
bool isIntegral() const
bool asBool() const
ValueType type_
Definition json_value.h:439
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 Int const maxInt
Definition json_value.h:162
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 ).
bool isInt() const
JSON (JavaScript Object Notation).
Definition json_errors.h:25
bool operator<=(Value const &x, Value const &y)
Definition json_value.h:462
bool operator>(Value const &x, Value const &y)
Definition json_value.h:468
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:92
Value to_json(ripple::Number const &number)
Definition json_value.h:444
ValueType
Type of the value held by a Value object.
Definition json_value.h:37
@ booleanValue
bool value
Definition json_value.h:43
@ nullValue
'null' value
Definition json_value.h:38
@ stringValue
UTF-8 string value.
Definition json_value.h:42
@ realValue
double value
Definition json_value.h:41
@ arrayValue
array value (ordered list)
Definition json_value.h:44
@ intValue
signed integer value
Definition json_value.h:39
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
@ uintValue
unsigned integer value
Definition json_value.h:40
bool operator<(Value const &, Value const &)
bool operator==(StaticString x, StaticString y)
Definition json_value.h:86
int Int
bool operator>=(Value const &x, Value const &y)
Definition json_value.h:474
unsigned int UInt