rippled
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 <ripple/json/json_forwards.h>
24 #include <cstring>
25 #include <map>
26 #include <string>
27 #include <vector>
28 
31 namespace Json
32 {
33 
37 {
38  nullValue = 0,
46 };
47 
63 {
64 public:
65  constexpr explicit StaticString ( const char* czstring )
66  : str_ ( czstring )
67  {
68  }
69 
70  constexpr operator const char* () const
71  {
72  return str_;
73  }
74 
75  constexpr const char* c_str () const
76  {
77  return str_;
78  }
79 
80 private:
81  const char* str_;
82 };
83 
85 {
86  return strcmp (x.c_str(), y.c_str()) == 0;
87 }
88 
90 {
91  return ! (x == y);
92 }
93 
94 inline bool operator== (std::string const& x, StaticString y)
95 {
96  return strcmp(x.c_str(), y.c_str()) == 0;
97 }
98 
99 inline bool operator!= (std::string const& x, StaticString y)
100 {
101  return ! (x == y);
102 }
103 
104 inline bool operator== (StaticString x, std::string const& y)
105 {
106  return y == x;
107 }
108 
109 inline bool operator!= (StaticString x, std::string const& y)
110 {
111  return ! (y == x);
112 }
113 
141 class Value
142 {
143  friend class ValueIteratorBase;
144 
145 public:
149  using UInt = Json::UInt;
150  using Int = Json::Int;
151  using ArrayIndex = UInt;
152 
153  static const Value null;
154  static const Int minInt;
155  static const Int maxInt;
156  static const UInt maxUInt;
157 
158 private:
159  class CZString
160  {
161  public:
163  {
167  };
168  CZString ( int index );
169  CZString ( const char* cstr, DuplicationPolicy allocate );
170  CZString ( const CZString& other );
171  ~CZString ();
172  CZString& operator = ( const CZString& other ) = delete;
173  bool operator< ( const CZString& other ) const;
174  bool operator== ( const CZString& other ) const;
175  int index () const;
176  const char* c_str () const;
177  bool isStaticString () const;
178  private:
179  const char* cstr_;
180  int index_;
181  };
182 
183 public:
185 
186 public:
203  Value ( Int value );
204  Value ( UInt value );
205  Value ( double value );
206  Value ( const char* value );
217  Value ( const StaticString& value );
218  Value ( std::string const& value );
219  Value ( bool value );
220  Value ( const Value& other );
221  ~Value ();
222 
223  Value& operator= ( Value const& other );
224  Value& operator= ( Value&& other );
225 
226  Value ( Value&& other ) noexcept;
227 
229  void swap ( Value& other ) noexcept;
230 
231  ValueType type () const;
232 
233  const char* asCString () const;
235  std::string asString () const;
236  Int asInt () const;
237  UInt asUInt () const;
238  double asDouble () const;
239  bool asBool () const;
240 
241  // TODO: What is the "empty()" method this docstring mentions?
244  bool isNull () const;
245  bool isBool () const;
246  bool isInt () const;
247  bool isUInt () const;
248  bool isIntegral () const;
249  bool isDouble () const;
250  bool isNumeric () const;
251  bool isString () const;
252  bool isArray() const;
253  bool isArrayOrNull () const;
254  bool isObject() const;
255  bool isObjectOrNull () const;
256 
257  bool isConvertibleTo ( ValueType other ) const;
258 
260  UInt size () const;
261 
264  explicit
265  operator bool() const;
266 
270  void clear ();
271 
277  Value& operator[] ( UInt index );
281  const Value& operator[] ( UInt index ) const;
284  Value get ( UInt index,
285  const Value& defaultValue ) const;
287  bool isValidIndex ( UInt index ) const;
291  Value& append ( const Value& value );
292  Value& append ( Value&& value );
293 
295  Value& operator[] ( const char* key );
297  const Value& operator[] ( const char* key ) const;
299  Value& operator[] ( std::string const& key );
301  const Value& operator[] ( std::string const& key ) const;
313  Value& operator[] ( const StaticString& key );
314 
316  Value get ( const char* key,
317  const Value& defaultValue ) const;
319  Value get ( std::string const& key,
320  const Value& defaultValue ) const;
321 
328  Value removeMember ( const char* key );
330  Value removeMember ( std::string const& key );
331 
333  bool isMember ( const char* key ) const;
335  bool isMember ( std::string const& key ) const;
336 
342  Members getMemberNames () const;
343 
344  std::string toStyledString () const;
345 
346  const_iterator begin () const;
347  const_iterator end () const;
348 
349  iterator begin ();
350  iterator end ();
351 
352  friend bool operator== (const Value&, const Value&);
353  friend bool operator< (const Value&, const Value&);
354 
355 private:
356  Value& resolveReference ( const char* key,
357  bool isStatic );
358 
359 private:
361  {
364  double real_;
365  bool bool_;
366  char* string_;
367  ObjectValues* map_ {nullptr};
368  } value_;
370  int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
371 };
372 
373 bool operator== (const Value&, const Value&);
374 
375 inline
376 bool operator!= (const Value& x, const Value& y)
377 {
378  return ! (x == y);
379 }
380 
381 bool operator< (const Value&, const Value&);
382 
383 inline
384 bool operator<= (const Value& x, const Value& y)
385 {
386  return ! (y < x);
387 }
388 
389 inline
390 bool operator> (const Value& x, const Value& y)
391 {
392  return y < x;
393 }
394 
395 inline
396 bool operator>= (const Value& x, const Value& y)
397 {
398  return ! (x < y);
399 }
400 
409 {
410 public:
411  enum { unknown = (unsigned) - 1 };
412 
413  virtual ~ValueAllocator () = default;
414 
415  virtual char* makeMemberName ( const char* memberName ) = 0;
416  virtual void releaseMemberName ( char* memberName ) = 0;
417  virtual char* duplicateStringValue ( const char* value,
418  unsigned int length = unknown ) = 0;
419  virtual void releaseStringValue ( char* value ) = 0;
420 };
421 
426 {
427 public:
428  using size_t = unsigned int;
429  using difference_type = int;
431 
433 
434  explicit ValueIteratorBase ( const Value::ObjectValues::iterator& current );
435 
436  bool operator == ( const SelfType& other ) const
437  {
438  return isEqual ( other );
439  }
440 
441  bool operator != ( const SelfType& other ) const
442  {
443  return !isEqual ( other );
444  }
445 
447  Value key () const;
448 
450  UInt index () const;
451 
453  const char* memberName () const;
454 
455 protected:
456  Value& deref () const;
457 
458  void increment ();
459 
460  void decrement ();
461 
462  difference_type computeDistance ( const SelfType& other ) const;
463 
464  bool isEqual ( const SelfType& other ) const;
465 
466  void copy ( const SelfType& other );
467 
468 private:
469  Value::ObjectValues::iterator current_;
470  // Indicates that iterator is for a null value.
471  bool isNull_;
472 };
473 
478 {
479  friend class Value;
480 public:
481  using size_t = unsigned int;
482  using difference_type = int;
483  using reference = const Value&;
484  using pointer = const Value*;
486 
487  ValueConstIterator () = default;
488 private:
491  explicit ValueConstIterator ( const Value::ObjectValues::iterator& current );
492 public:
493  SelfType& operator = ( const ValueIteratorBase& other );
494 
496  {
497  SelfType temp ( *this );
498  ++*this;
499  return temp;
500  }
501 
503  {
504  SelfType temp ( *this );
505  --*this;
506  return temp;
507  }
508 
510  {
511  decrement ();
512  return *this;
513  }
514 
516  {
517  increment ();
518  return *this;
519  }
520 
522  {
523  return deref ();
524  }
525 };
526 
527 
531 {
532  friend class Value;
533 public:
534  using size_t = unsigned int;
535  using difference_type = int;
536  using reference = Value&;
537  using pointer = Value*;
539 
540  ValueIterator () = default;
541  ValueIterator ( const ValueConstIterator& other );
542  ValueIterator ( const ValueIterator& other );
543 private:
546  explicit ValueIterator ( const Value::ObjectValues::iterator& current );
547 public:
548 
549  SelfType& operator = ( const SelfType& other );
550 
552  {
553  SelfType temp ( *this );
554  ++*this;
555  return temp;
556  }
557 
559  {
560  SelfType temp ( *this );
561  --*this;
562  return temp;
563  }
564 
566  {
567  decrement ();
568  return *this;
569  }
570 
572  {
573  increment ();
574  return *this;
575  }
576 
578  {
579  return deref ();
580  }
581 };
582 
583 } // namespace Json
584 
585 
586 #endif // CPPTL_JSON_H_INCLUDED
Json::Value::isInt
bool isInt() const
Definition: json_value.cpp:1012
Json::ValueConstIterator::operator++
SelfType & operator++()
Definition: json_value.h:515
Json::Value::Int
Json::Int Int
Definition: json_value.h:150
Json::ValueIterator
Iterator for object and array value.
Definition: json_value.h:530
Json::operator<
bool operator<(const Value &x, const Value &y)
Definition: json_value.cpp:379
Json::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition: json_valueiterator.cpp:109
Json::Value::isObject
bool isObject() const
Definition: json_value.cpp:1069
std::string
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1049
Json::Value::isValidIndex
bool isValidIndex(UInt index) const
Return true if index < size().
Definition: json_value.cpp:862
cstring
Json::Value::allocated_
int allocated_
Definition: json_value.h:370
Json::Value::type_
ValueType type_
Definition: json_value.h:369
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
Json::Value::CZString::index
int index() const
Definition: json_value.cpp:145
Json::booleanValue
@ booleanValue
bool value
Definition: json_value.h:43
Json::StaticString::StaticString
constexpr StaticString(const char *czstring)
Definition: json_value.h:65
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition: json_valueiterator.cpp:167
Json::UInt
unsigned int UInt
Definition: json_forwards.h:28
Json::Value::get
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:853
vector
Json::ValueIteratorBase::ValueIteratorBase
ValueIteratorBase()
Definition: json_valueiterator.cpp:34
Json::Value::swap
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:349
Json::Value::isDouble
bool isDouble() const
Definition: json_value.cpp:1035
Json::ValueIteratorBase::operator==
bool operator==(const SelfType &other) const
Definition: json_value.h:436
Json::realValue
@ realValue
double value
Definition: json_value.h:41
Json::Value::CZString::c_str
const char * c_str() const
Definition: json_value.cpp:152
Json::ValueIteratorBase::index
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
Definition: json_valueiterator.cpp:133
Json::Value::isNull
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:998
Json::Value::CZString::duplicate
@ duplicate
Definition: json_value.h:165
Json::Value::toStyledString
std::string toStyledString() const
Definition: json_value.cpp:1081
Json::Value::end
const_iterator end() const
Definition: json_value.cpp:1107
Json::ValueIteratorBase::decrement
void decrement()
Definition: json_valueiterator.cpp:61
Json::ValueIteratorBase::SelfType
ValueIteratorBase SelfType
Definition: json_value.h:430
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition: json_valueiterator.cpp:97
Json::StaticString::c_str
constexpr const char * c_str() const
Definition: json_value.h:75
Json::ValueIteratorBase::key
Value key() const
Return either the index or the member name of the referenced value as a Value.
Definition: json_valueiterator.cpp:116
Json::ValueIteratorBase::current_
Value::ObjectValues::iterator current_
Definition: json_value.h:469
Json::ValueAllocator
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:408
Json::Value::asBool
bool asBool() const
Definition: json_value.cpp:626
Json::ValueAllocator::~ValueAllocator
virtual ~ValueAllocator()=default
Json::Value::ValueHolder::map_
ObjectValues * map_
Definition: json_value.h:367
Json::uintValue
@ uintValue
unsigned integer value
Definition: json_value.h:40
Json::Value::operator<
friend bool operator<(const Value &, const Value &)
Definition: json_value.cpp:379
Json::ValueIteratorBase::isNull_
bool isNull_
Definition: json_value.h:471
Json::Value::maxInt
static const Int maxInt
Definition: json_value.h:155
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
Json::Value::resolveReference
Value & resolveReference(const char *key, bool isStatic)
Definition: json_value.cpp:830
Json::Value::CZString::cstr_
const char * cstr_
Definition: json_value.h:179
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
Json::ValueIterator::operator++
SelfType & operator++()
Definition: json_value.h:571
Json::ValueIterator::operator*
reference operator*() const
Definition: json_value.h:577
Json::ValueIteratorBase::operator!=
bool operator!=(const SelfType &other) const
Definition: json_value.h:441
Json::Value::value_
union Json::Value::ValueHolder value_
Json::Value::isConvertibleTo
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:659
Json::Value::isArrayOrNull
bool isArrayOrNull() const
Definition: json_value.cpp:1062
Json::ValueConstIterator
const iterator for object and array value.
Definition: json_value.h:477
std::string::c_str
T c_str(T... args)
Json::ValueIteratorBase
base class for Value iterators.
Definition: json_value.h:425
Json::operator>
bool operator>(const Value &x, const Value &y)
Definition: json_value.h:390
Json::Value::ValueHolder::real_
double real_
Definition: json_value.h:364
Json::operator==
bool operator==(const Value &x, const Value &y)
Definition: json_value.cpp:428
Json::ValueAllocator::releaseStringValue
virtual void releaseStringValue(char *value)=0
Json::Value::size
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:720
Json::Value::minInt
static const Int minInt
Definition: json_value.h:154
Json::StaticString::str_
const char * str_
Definition: json_value.h:81
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:961
Json::operator<=
bool operator<=(const Value &x, const Value &y)
Definition: json_value.h:384
Json::stringValue
@ stringValue
UTF-8 string value.
Definition: json_value.h:42
Json::ValueType
ValueType
Type of the value held by a Value object.
Definition: json_value.h:36
Json::Value::asCString
const char * asCString() const
Definition: json_value.cpp:474
Json::ValueIterator::operator--
SelfType & operator--()
Definition: json_value.h:565
Json::ValueConstIterator::operator*
reference operator*() const
Definition: json_value.h:521
map
Json::operator!=
bool operator!=(StaticString x, StaticString y)
Definition: json_value.h:89
Json::Int
int Int
Definition: json_forwards.h:27
Json::ValueIterator::ValueIterator
ValueIterator()=default
Json::Value::CZString::operator<
bool operator<(const CZString &other) const
Definition: json_value.cpp:126
Json::Value::UInt
Json::UInt UInt
Definition: json_value.h:149
Json::Value::CZString::CZString
CZString(int index)
Definition: json_value.cpp:97
Json::Value::isArray
bool isArray() const
Definition: json_value.cpp:1056
Json::Value::CZString::operator==
bool operator==(const CZString &other) const
Definition: json_value.cpp:135
Json::ValueAllocator::duplicateStringValue
virtual char * duplicateStringValue(const char *value, unsigned int length=unknown)=0
Json::Value::getMemberNames
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:979
Json::ValueIteratorBase::computeDistance
difference_type computeDistance(const SelfType &other) const
Definition: json_valueiterator.cpp:68
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition: json_valueiterator.cpp:199
Json::Value::CZString::~CZString
~CZString()
Definition: json_value.cpp:119
Json::Value::operator=
Value & operator=(Value const &other)
Definition: json_value.cpp:324
Json::ValueAllocator::unknown
@ unknown
Definition: json_value.h:411
Json::Value::maxUInt
static const UInt maxUInt
Definition: json_value.h:156
Json::Value::removeMember
Value removeMember(const char *key)
Remove and return the named member.
Definition: json_value.cpp:936
Json::Value::isNumeric
bool isNumeric() const
Definition: json_value.cpp:1042
Json::ValueAllocator::releaseMemberName
virtual void releaseMemberName(char *memberName)=0
Json::Value::CZString::DuplicationPolicy
DuplicationPolicy
Definition: json_value.h:162
Json::Value::clear
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:768
Json::Value::CZString
Definition: json_value.h:159
Json::Value::CZString::noDuplication
@ noDuplication
Definition: json_value.h:164
Json::Value::CZString::index_
int index_
Definition: json_value.h:180
Json::ValueConstIterator::operator--
SelfType & operator--()
Definition: json_value.h:509
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:62
Json::intValue
@ intValue
signed integer value
Definition: json_value.h:39
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:555
Json::Value::isUInt
bool isUInt() const
Definition: json_value.cpp:1019
Json::Value::operator==
friend bool operator==(const Value &, const Value &)
Definition: json_value.cpp:428
Json::Value::isIntegral
bool isIntegral() const
Definition: json_value.cpp:1026
Json::Value::CZString::duplicateOnCopy
@ duplicateOnCopy
Definition: json_value.h:166
Json::nullValue
@ nullValue
'null' value
Definition: json_value.h:38
Json::operator>=
bool operator>=(const Value &x, const Value &y)
Definition: json_value.h:396
Json::Value::operator[]
Value & operator[](UInt index)
Access an array element (zero based index ).
Definition: json_value.cpp:785
Json::Value::ValueHolder::bool_
bool bool_
Definition: json_value.h:365
Json::Value::ValueHolder::string_
char * string_
Definition: json_value.h:366
Json::Value::Value
Value(ValueType type=nullValue)
Create a default Value of the given type.
Definition: json_value.cpp:175
Json::Value::isBool
bool isBool() const
Definition: json_value.cpp:1005
Json::Value::~Value
~Value()
Definition: json_value.cpp:295
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:516
Json::Value::ValueHolder
Definition: json_value.h:360
Json::Value::CZString::operator=
CZString & operator=(const CZString &other)=delete
Json::Value::begin
const_iterator begin() const
Definition: json_value.cpp:1089
Json::Value::asDouble
double asDouble() const
Definition: json_value.cpp:594
Json::Value::type
ValueType type() const
Definition: json_value.cpp:363
Json::ValueIteratorBase::increment
void increment()
Definition: json_valueiterator.cpp:54
Json::Value::CZString::isStaticString
bool isStaticString() const
Definition: json_value.cpp:158
Json::ValueIteratorBase::difference_type
int difference_type
Definition: json_value.h:429
Json::Value::ValueHolder::uint_
UInt uint_
Definition: json_value.h:363
Json::Value::isObjectOrNull
bool isObjectOrNull() const
Definition: json_value.cpp:1075
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::ValueIteratorBase::deref
Value & deref() const
Definition: json_valueiterator.cpp:47
Json::Value::ValueHolder::int_
Int int_
Definition: json_value.h:362
Json::ValueAllocator::makeMemberName
virtual char * makeMemberName(const char *memberName)=0
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()=default
Json::ValueIteratorBase::memberName
const char * memberName() const
Return the member name of the referenced Value. "" if it is not an objectValue.
Definition: json_valueiterator.cpp:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482
Json::Value::ArrayIndex
UInt ArrayIndex
Definition: json_value.h:151
string