JsonCpp project page JsonCpp home page

value.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5 
6 #ifndef CPPTL_JSON_H_INCLUDED
7 #define CPPTL_JSON_H_INCLUDED
8 
9 #if !defined(JSON_IS_AMALGAMATION)
10 #include "forwards.h"
11 #endif // if !defined(JSON_IS_AMALGAMATION)
12 #include <string>
13 #include <vector>
14 #include <exception>
15 
16 #ifndef JSON_USE_CPPTL_SMALLMAP
17 #include <map>
18 #else
19 #include <cpptl/smallmap.h>
20 #endif
21 #ifdef JSON_USE_CPPTL
22 #include <cpptl/forwards.h>
23 #endif
24 
25 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
26 // be used by...
27 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
28 #pragma warning(push)
29 #pragma warning(disable : 4251)
30 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
31 
34 namespace Json {
35 
40 class JSON_API Exception;
47 class JSON_API RuntimeError;
54 class JSON_API LogicError;
55 
57 void throwRuntimeError(std::string const& msg);
59 void throwLogicError(std::string const& msg);
60 
63 enum ValueType {
64  nullValue = 0,
72 };
73 
80 };
81 
82 //# ifdef JSON_USE_CPPTL
83 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
84 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
85 //# endif
86 
102 public:
103  explicit StaticString(const char* czstring) : c_str_(czstring) {}
104 
105  operator const char*() const { return c_str_; }
106 
107  const char* c_str() const { return c_str_; }
108 
109 private:
110  const char* c_str_;
111 };
112 
148  friend class ValueIteratorBase;
149 public:
150  typedef std::vector<std::string> Members;
153  typedef Json::UInt UInt;
154  typedef Json::Int Int;
155 #if defined(JSON_HAS_INT64)
158 #endif // defined(JSON_HAS_INT64)
162 
163  static const Value& null;
164  static const Value& nullRef;
165  static const LargestInt minLargestInt;
168  static const LargestInt maxLargestInt;
170  static const LargestUInt maxLargestUInt;
171 
173  static const Int minInt;
175  static const Int maxInt;
177  static const UInt maxUInt;
178 
179 #if defined(JSON_HAS_INT64)
180  static const Int64 minInt64;
183  static const Int64 maxInt64;
185  static const UInt64 maxUInt64;
186 #endif // defined(JSON_HAS_INT64)
187 
188 private:
189 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
190  class CZString {
191  public:
192  enum DuplicationPolicy {
193  noDuplication = 0,
194  duplicate,
195  duplicateOnCopy
196  };
197  CZString(ArrayIndex index);
198  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
199  CZString(CZString const& other);
200  ~CZString();
201  CZString& operator=(CZString other);
202  bool operator<(CZString const& other) const;
203  bool operator==(CZString const& other) const;
204  ArrayIndex index() const;
205  //const char* c_str() const; ///< \deprecated
206  char const* data() const;
207  unsigned length() const;
208  bool isStaticString() const;
209 
210  private:
211  void swap(CZString& other);
212 
213  struct StringStorage {
214  DuplicationPolicy policy_: 2;
215  unsigned length_: 30; // 1GB max
216  };
217 
218  char const* cstr_; // actually, a prefixed string, unless policy is noDup
219  union {
220  ArrayIndex index_;
221  StringStorage storage_;
222  };
223  };
224 
225 public:
226 #ifndef JSON_USE_CPPTL_SMALLMAP
227  typedef std::map<CZString, Value> ObjectValues;
228 #else
229  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
230 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
231 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
232 
233 public:
249  Value(ValueType type = nullValue);
250  Value(Int value);
251  Value(UInt value);
252 #if defined(JSON_HAS_INT64)
253  Value(Int64 value);
254  Value(UInt64 value);
255 #endif // if defined(JSON_HAS_INT64)
256  Value(double value);
257  Value(const char* value);
258  Value(const char* beginValue, const char* endValue);
259 
274  Value(const StaticString& value);
275  Value(const std::string& value);
276 #ifdef JSON_USE_CPPTL
277  Value(const CppTL::ConstString& value);
278 #endif
279  Value(bool value);
281  Value(const Value& other);
282  ~Value();
283 
286  Value& operator=(Value other);
288  void swap(Value& other);
290  void swapPayload(Value& other);
291 
292  ValueType type() const;
293 
295  bool operator<(const Value& other) const;
296  bool operator<=(const Value& other) const;
297  bool operator>=(const Value& other) const;
298  bool operator>(const Value& other) const;
299  bool operator==(const Value& other) const;
300  bool operator!=(const Value& other) const;
301  int compare(const Value& other) const;
302 
303  const char* asCString() const;
304  std::string asString() const;
305 
308  bool getString(
309  char const** str, char const** end) const;
310 #ifdef JSON_USE_CPPTL
311  CppTL::ConstString asConstString() const;
312 #endif
313  Int asInt() const;
314  UInt asUInt() const;
315 #if defined(JSON_HAS_INT64)
316  Int64 asInt64() const;
317  UInt64 asUInt64() const;
318 #endif // if defined(JSON_HAS_INT64)
319  LargestInt asLargestInt() const;
320  LargestUInt asLargestUInt() const;
321  float asFloat() const;
322  double asDouble() const;
323  bool asBool() const;
324 
325  bool isNull() const;
326  bool isBool() const;
327  bool isInt() const;
328  bool isInt64() const;
329  bool isUInt() const;
330  bool isUInt64() const;
331  bool isIntegral() const;
332  bool isDouble() const;
333  bool isNumeric() const;
334  bool isString() const;
335  bool isArray() const;
336  bool isObject() const;
337 
338  bool isConvertibleTo(ValueType other) const;
339 
341  ArrayIndex size() const;
342 
345  bool empty() const;
346 
348  bool operator!() const;
349 
353  void clear();
354 
360  void resize(ArrayIndex size);
361 
368  Value& operator[](ArrayIndex index);
369 
376  Value& operator[](int index);
377 
381  const Value& operator[](ArrayIndex index) const;
382 
386  const Value& operator[](int index) const;
387 
391  Value get(ArrayIndex index, const Value& defaultValue) const;
393  bool isValidIndex(ArrayIndex index) const;
397  Value& append(const Value& value);
398 
402  Value& operator[](const char* key);
405  const Value& operator[](const char* key) const;
408  Value& operator[](const std::string& key);
412  const Value& operator[](const std::string& key) const;
425  Value& operator[](const StaticString& key);
426 #ifdef JSON_USE_CPPTL
427  Value& operator[](const CppTL::ConstString& key);
431  const Value& operator[](const CppTL::ConstString& key) const;
432 #endif
433  Value get(const char* key, const Value& defaultValue) const;
439  Value get(const char* key, const char* end, const Value& defaultValue) const;
443  Value get(const std::string& key, const Value& defaultValue) const;
444 #ifdef JSON_USE_CPPTL
445  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
448 #endif
449  Value const* find(char const* key, char const* end) const;
456  Value const* demand(char const* key, char const* end);
464  Value removeMember(const char* key);
468  Value removeMember(const std::string& key);
471  bool removeMember(const char* key, Value* removed);
478  bool removeMember(std::string const& key, Value* removed);
480  bool removeMember(const char* key, const char* end, Value* removed);
487  bool removeIndex(ArrayIndex i, Value* removed);
488 
491  bool isMember(const char* key) const;
494  bool isMember(const std::string& key) const;
496  bool isMember(const char* key, const char* end) const;
497 #ifdef JSON_USE_CPPTL
498  bool isMember(const CppTL::ConstString& key) const;
500 #endif
501 
507  Members getMemberNames() const;
508 
509  //# ifdef JSON_USE_CPPTL
510  // EnumMemberNames enumMemberNames() const;
511  // EnumValues enumValues() const;
512  //# endif
513 
515  void setComment(const char* comment, CommentPlacement placement);
517  void setComment(const char* comment, size_t len, CommentPlacement placement);
519  void setComment(const std::string& comment, CommentPlacement placement);
520  bool hasComment(CommentPlacement placement) const;
522  std::string getComment(CommentPlacement placement) const;
523 
524  std::string toStyledString() const;
525 
526  const_iterator begin() const;
527  const_iterator end() const;
528 
529  iterator begin();
530  iterator end();
531 
532  // Accessors for the [start, limit) range of bytes within the JSON text from
533  // which this value was parsed, if any.
534  void setOffsetStart(size_t start);
535  void setOffsetLimit(size_t limit);
536  size_t getOffsetStart() const;
537  size_t getOffsetLimit() const;
538 
539 private:
540  void initBasic(ValueType type, bool allocated = false);
541 
542  Value& resolveReference(const char* key);
543  Value& resolveReference(const char* key, const char* end);
544 
545  struct CommentInfo {
546  CommentInfo();
547  ~CommentInfo();
548 
549  void setComment(const char* text, size_t len);
550 
551  char* comment_;
552  };
553 
554  // struct MemberNamesTransform
555  //{
556  // typedef const char *result_type;
557  // const char *operator()( const CZString &name ) const
558  // {
559  // return name.c_str();
560  // }
561  //};
562 
563  union ValueHolder {
564  LargestInt int_;
565  LargestUInt uint_;
566  double real_;
567  bool bool_;
568  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
569  ObjectValues* map_;
570  } value_;
571  ValueType type_ : 8;
572  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
573  // If not allocated_, string_ must be null-terminated.
574  CommentInfo* comments_;
575 
576  // [start, limit) byte offsets in the source JSON text from which this Value
577  // was extracted.
578  size_t start_;
579  size_t limit_;
580 };
581 
586 public:
587  friend class Path;
588 
589  PathArgument();
590  PathArgument(ArrayIndex index);
591  PathArgument(const char* key);
592  PathArgument(const std::string& key);
593 
594 private:
595  enum Kind {
596  kindNone = 0,
597  kindIndex,
598  kindKey
599  };
600  std::string key_;
601  ArrayIndex index_;
602  Kind kind_;
603 };
604 
616 class JSON_API Path {
617 public:
618  Path(const std::string& path,
619  const PathArgument& a1 = PathArgument(),
620  const PathArgument& a2 = PathArgument(),
621  const PathArgument& a3 = PathArgument(),
622  const PathArgument& a4 = PathArgument(),
623  const PathArgument& a5 = PathArgument());
624 
625  const Value& resolve(const Value& root) const;
626  Value resolve(const Value& root, const Value& defaultValue) const;
629  Value& make(Value& root) const;
630 
631 private:
632  typedef std::vector<const PathArgument*> InArgs;
633  typedef std::vector<PathArgument> Args;
634 
635  void makePath(const std::string& path, const InArgs& in);
636  void addPathInArg(const std::string& path,
637  const InArgs& in,
638  InArgs::const_iterator& itInArg,
639  PathArgument::Kind kind);
640  void invalidPath(const std::string& path, int location);
641 
642  Args args_;
643 };
644 
649 public:
650  typedef std::bidirectional_iterator_tag iterator_category;
651  typedef unsigned int size_t;
652  typedef int difference_type;
654 
656  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
657 
658  bool operator==(const SelfType& other) const { return isEqual(other); }
659 
660  bool operator!=(const SelfType& other) const { return !isEqual(other); }
661 
662  difference_type operator-(const SelfType& other) const {
663  return other.computeDistance(*this);
664  }
665 
668  Value key() const;
669 
671  UInt index() const;
672 
676  std::string name() const;
677 
681  JSONCPP_DEPRECATED("Use `key = name();` instead.")
682  char const* memberName() const;
686  char const* memberName(char const** end) const;
687 
688 protected:
689  Value& deref() const;
690 
691  void increment();
692 
693  void decrement();
694 
695  difference_type computeDistance(const SelfType& other) const;
696 
697  bool isEqual(const SelfType& other) const;
698 
699  void copy(const SelfType& other);
700 
701 private:
702  Value::ObjectValues::iterator current_;
703  // Indicates that iterator is for a null value.
704  bool isNull_;
705 };
706 
711  friend class Value;
712 
713 public:
714  typedef const Value value_type;
715  //typedef unsigned int size_t;
716  //typedef int difference_type;
717  typedef const Value& reference;
718  typedef const Value* pointer;
720 
722 
723 private:
726  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
727 public:
728  SelfType& operator=(const ValueIteratorBase& other);
729 
730  SelfType operator++(int) {
731  SelfType temp(*this);
732  ++*this;
733  return temp;
734  }
735 
736  SelfType operator--(int) {
737  SelfType temp(*this);
738  --*this;
739  return temp;
740  }
741 
742  SelfType& operator--() {
743  decrement();
744  return *this;
745  }
746 
747  SelfType& operator++() {
748  increment();
749  return *this;
750  }
751 
752  reference operator*() const { return deref(); }
753 
754  pointer operator->() const { return &deref(); }
755 };
756 
760  friend class Value;
761 
762 public:
763  typedef Value value_type;
764  typedef unsigned int size_t;
765  typedef int difference_type;
766  typedef Value& reference;
767  typedef Value* pointer;
769 
770  ValueIterator();
771  ValueIterator(const ValueConstIterator& other);
772  ValueIterator(const ValueIterator& other);
773 
774 private:
777  explicit ValueIterator(const Value::ObjectValues::iterator& current);
778 public:
779  SelfType& operator=(const SelfType& other);
780 
781  SelfType operator++(int) {
782  SelfType temp(*this);
783  ++*this;
784  return temp;
785  }
786 
787  SelfType operator--(int) {
788  SelfType temp(*this);
789  --*this;
790  return temp;
791  }
792 
793  SelfType& operator--() {
794  decrement();
795  return *this;
796  }
797 
798  SelfType& operator++() {
799  increment();
800  return *this;
801  }
802 
803  reference operator*() const { return deref(); }
804 
805  pointer operator->() const { return &deref(); }
806 };
807 
808 } // namespace Json
809 
810 
811 namespace std {
813 template<>
814 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
815 }
816 
817 
818 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
819 #pragma warning(pop)
820 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
821 
822 #endif // CPPTL_JSON_H_INCLUDED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:84
Int64 LargestInt
Definition: config.h:103
pointer operator->() const
Definition: value.h:805
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:51
static const Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
Definition: value.h:183
unsigned int ArrayIndex
Definition: forwards.h:23
static const Value & null
We regret this reference to a global instance; prefer the simpler Value().
Definition: value.h:163
reference operator*() const
Definition: value.h:752
std::vector< std::string > Members
Definition: value.h:150
base class for Value iterators.
Definition: value.h:648
array value (ordered list)
Definition: value.h:70
unsigned __int64 UInt64
Definition: config.h:98
unsigned integer value
Definition: value.h:66
void throwLogicError(std::string const &msg)
used internally
Definition: json_value.cpp:191
Json::ArrayIndex ArrayIndex
Definition: value.h:161
const Value value_type
Definition: value.h:714
object value (collection of name/value pairs).
Definition: value.h:71
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition: value.h:175
STL namespace.
Lightweight wrapper to tag static string.
Definition: value.h:101
static const UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition: value.h:177
Json::LargestUInt LargestUInt
Definition: value.h:160
difference_type computeDistance(const SelfType &other) const
bool operator!=(const SelfType &other) const
Definition: value.h:660
const iterator for object and array value.
Definition: value.h:710
unsigned int size_t
Definition: value.h:764
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:585
SelfType & operator--()
Definition: value.h:742
'null' value
Definition: value.h:64
CommentPlacement
Definition: value.h:74
SelfType & operator--()
Definition: value.h:793
Value value_type
Definition: value.h:763
StaticString(const char *czstring)
Definition: value.h:103
ValueConstIterator SelfType
Definition: value.h:719
static const Value & nullRef
just a kludge for binary-compatibility; same as null
Definition: value.h:164
UInt64 LargestUInt
Definition: config.h:104
ValueConstIterator const_iterator
Definition: value.h:152
JSON (JavaScript Object Notation).
Definition: config.h:87
ValueIteratorBase SelfType
Definition: value.h:653
Json::Int64 Int64
Definition: value.h:157
ValueIterator SelfType
Definition: value.h:768
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:484
Experimental and untested: represents a "path" to access a node.
Definition: value.h:616
SelfType operator--(int)
Definition: value.h:736
Json::LargestInt LargestInt
Definition: value.h:159
const char * c_str() const
Definition: value.h:107
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition: value.h:185
double value
Definition: value.h:67
void throwRuntimeError(std::string const &msg)
used internally
Definition: json_value.cpp:187
SelfType operator--(int)
Definition: value.h:787
Json::UInt UInt
Definition: value.h:153
SelfType & operator++()
Definition: value.h:798
Json::UInt64 UInt64
Definition: value.h:156
Json::Int Int
Definition: value.h:154
Value * pointer
Definition: value.h:767
Represents a JSON value.
Definition: value.h:147
std::bidirectional_iterator_tag iterator_category
Definition: value.h:650
ValueIterator iterator
Definition: value.h:151
const Value * pointer
Definition: value.h:718
difference_type operator-(const SelfType &other) const
Definition: value.h:662
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:173
reference operator*() const
Definition: value.h:803
const Value & reference
Definition: value.h:717
a comment on the line after a value (only make sense for
Definition: value.h:77
unsigned int UInt
Definition: config.h:89
Iterator for object and array value.
Definition: value.h:759
SelfType & operator++()
Definition: value.h:747
__int64 Int64
Definition: config.h:97
SelfType operator++(int)
Definition: value.h:730
ValueType
Type of the value held by a Value object.
Definition: value.h:63
bool value
Definition: value.h:69
signed integer value
Definition: value.h:65
SelfType operator++(int)
Definition: value.h:781
unsigned int size_t
Definition: value.h:651
int Int
Definition: config.h:88
a comment placed on the line before a value
Definition: value.h:75
UTF-8 string value.
Definition: value.h:68
a comment just after a value on the same line
Definition: value.h:76
bool operator==(const SelfType &other) const
Definition: value.h:658
pointer operator->() const
Definition: value.h:754
Value & reference
Definition: value.h:766
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:168
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:170