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 : public std::exception {
41 public:
42  Exception(std::string const& msg);
43  virtual ~Exception() throw();
44  virtual char const* what() const throw();
45 protected:
46  std::string const msg_;
47 };
48 
56 public:
57  RuntimeError(std::string const& msg);
58 };
59 
66 class JSON_API LogicError : public Exception {
67 public:
68  LogicError(std::string const& msg);
69 };
70 
72 void throwRuntimeError(std::string const& msg);
74 void throwLogicError(std::string const& msg);
75 
78 enum ValueType {
79  nullValue = 0,
87 };
88 
95 };
96 
97 //# ifdef JSON_USE_CPPTL
98 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
99 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
100 //# endif
101 
117 public:
118  explicit StaticString(const char* czstring) : c_str_(czstring) {}
119 
120  operator const char*() const { return c_str_; }
121 
122  const char* c_str() const { return c_str_; }
123 
124 private:
125  const char* c_str_;
126 };
127 
163  friend class ValueIteratorBase;
164 public:
165  typedef std::vector<std::string> Members;
168  typedef Json::UInt UInt;
169  typedef Json::Int Int;
170 #if defined(JSON_HAS_INT64)
173 #endif // defined(JSON_HAS_INT64)
177 
178  static const Value& nullRef;
179 #if !defined(__ARMEL__)
180  static const Value null;
182 #endif
183  static const LargestInt minLargestInt;
186  static const LargestInt maxLargestInt;
188  static const LargestUInt maxLargestUInt;
189 
191  static const Int minInt;
193  static const Int maxInt;
195  static const UInt maxUInt;
196 
197 #if defined(JSON_HAS_INT64)
198  static const Int64 minInt64;
201  static const Int64 maxInt64;
203  static const UInt64 maxUInt64;
204 #endif // defined(JSON_HAS_INT64)
205 
206 private:
207 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
208  class CZString {
209  public:
210  enum DuplicationPolicy {
211  noDuplication = 0,
212  duplicate,
213  duplicateOnCopy
214  };
215  CZString(ArrayIndex index);
216  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
217  CZString(CZString const& other);
218  ~CZString();
219  CZString& operator=(CZString other);
220  bool operator<(CZString const& other) const;
221  bool operator==(CZString const& other) const;
222  ArrayIndex index() const;
223  //const char* c_str() const; ///< \deprecated
224  char const* data() const;
225  unsigned length() const;
226  bool isStaticString() const;
227 
228  private:
229  void swap(CZString& other);
230 
231  struct StringStorage {
232  unsigned policy_: 2;
233  unsigned length_: 30; // 1GB max
234  };
235 
236  char const* cstr_; // actually, a prefixed string, unless policy is noDup
237  union {
238  ArrayIndex index_;
239  StringStorage storage_;
240  };
241  };
242 
243 public:
244 #ifndef JSON_USE_CPPTL_SMALLMAP
245  typedef std::map<CZString, Value> ObjectValues;
246 #else
247  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
248 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
249 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
250 
251 public:
267  Value(ValueType type = nullValue);
268  Value(Int value);
269  Value(UInt value);
270 #if defined(JSON_HAS_INT64)
271  Value(Int64 value);
272  Value(UInt64 value);
273 #endif // if defined(JSON_HAS_INT64)
274  Value(double value);
275  Value(const char* value);
276  Value(const char* begin, const char* end);
277 
292  Value(const StaticString& value);
293  Value(const std::string& value);
294 #ifdef JSON_USE_CPPTL
295  Value(const CppTL::ConstString& value);
296 #endif
297  Value(bool value);
299  Value(const Value& other);
300  ~Value();
301 
304  Value &operator=(const Value &other);
306  void swap(Value& other);
308  void swapPayload(Value& other);
309 
310  ValueType type() const;
311 
313  bool operator<(const Value& other) const;
314  bool operator<=(const Value& other) const;
315  bool operator>=(const Value& other) const;
316  bool operator>(const Value& other) const;
317  bool operator==(const Value& other) const;
318  bool operator!=(const Value& other) const;
319  int compare(const Value& other) const;
320 
321  const char* asCString() const;
322  std::string asString() const;
323 
326  bool getString(
327  char const** begin, char const** end) const;
328 #ifdef JSON_USE_CPPTL
329  CppTL::ConstString asConstString() const;
330 #endif
331  Int asInt() const;
332  UInt asUInt() const;
333 #if defined(JSON_HAS_INT64)
334  Int64 asInt64() const;
335  UInt64 asUInt64() const;
336 #endif // if defined(JSON_HAS_INT64)
337  LargestInt asLargestInt() const;
338  LargestUInt asLargestUInt() const;
339  float asFloat() const;
340  double asDouble() const;
341  bool asBool() const;
342 
343  bool isNull() const;
344  bool isBool() const;
345  bool isInt() const;
346  bool isInt64() const;
347  bool isUInt() const;
348  bool isUInt64() const;
349  bool isIntegral() const;
350  bool isDouble() const;
351  bool isNumeric() const;
352  bool isString() const;
353  bool isArray() const;
354  bool isObject() const;
355 
356  bool isConvertibleTo(ValueType other) const;
357 
359  ArrayIndex size() const;
360 
363  bool empty() const;
364 
366  bool operator!() const;
367 
371  void clear();
372 
378  void resize(ArrayIndex size);
379 
386  Value& operator[](ArrayIndex index);
387 
394  Value& operator[](int index);
395 
399  const Value& operator[](ArrayIndex index) const;
400 
404  const Value& operator[](int index) const;
405 
409  Value get(ArrayIndex index, const Value& defaultValue) const;
411  bool isValidIndex(ArrayIndex index) const;
415  Value& append(const Value& value);
416 
420  Value& operator[](const char* key);
423  const Value& operator[](const char* key) const;
426  Value& operator[](const std::string& key);
430  const Value& operator[](const std::string& key) const;
443  Value& operator[](const StaticString& key);
444 #ifdef JSON_USE_CPPTL
445  Value& operator[](const CppTL::ConstString& key);
449  const Value& operator[](const CppTL::ConstString& key) const;
450 #endif
451  Value get(const char* key, const Value& defaultValue) const;
457  Value get(const char* begin, const char* end, const Value& defaultValue) const;
461  Value get(const std::string& key, const Value& defaultValue) const;
462 #ifdef JSON_USE_CPPTL
463  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
466 #endif
467  Value const* find(char const* begin, char const* end) const;
474  Value const* demand(char const* begin, char const* end);
482  Value removeMember(const char* key);
486  Value removeMember(const std::string& key);
489  bool removeMember(const char* key, Value* removed);
496  bool removeMember(std::string const& key, Value* removed);
498  bool removeMember(const char* begin, const char* end, Value* removed);
505  bool removeIndex(ArrayIndex i, Value* removed);
506 
509  bool isMember(const char* key) const;
512  bool isMember(const std::string& key) const;
514  bool isMember(const char* begin, const char* end) const;
515 #ifdef JSON_USE_CPPTL
516  bool isMember(const CppTL::ConstString& key) const;
518 #endif
519 
525  Members getMemberNames() const;
526 
527  //# ifdef JSON_USE_CPPTL
528  // EnumMemberNames enumMemberNames() const;
529  // EnumValues enumValues() const;
530  //# endif
531 
533  JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
534  void setComment(const char* comment, CommentPlacement placement);
536  void setComment(const char* comment, size_t len, CommentPlacement placement);
538  void setComment(const std::string& comment, CommentPlacement placement);
539  bool hasComment(CommentPlacement placement) const;
541  std::string getComment(CommentPlacement placement) const;
542 
543  std::string toStyledString() const;
544 
545  const_iterator begin() const;
546  const_iterator end() const;
547 
548  iterator begin();
549  iterator end();
550 
551 private:
552  void initBasic(ValueType type, bool allocated = false);
553 
554  Value& resolveReference(const char* key);
555  Value& resolveReference(const char* key, const char* end);
556 
557  struct CommentInfo {
558  CommentInfo();
559  ~CommentInfo();
560 
561  void setComment(const char* text, size_t len);
562 
563  char* comment_;
564  };
565 
566  // struct MemberNamesTransform
567  //{
568  // typedef const char *result_type;
569  // const char *operator()( const CZString &name ) const
570  // {
571  // return name.c_str();
572  // }
573  //};
574 
575  union ValueHolder {
576  LargestInt int_;
577  LargestUInt uint_;
578  double real_;
579  bool bool_;
580  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
581  ObjectValues* map_;
582  } value_;
583  ValueType type_ : 8;
584  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
585  // If not allocated_, string_ must be null-terminated.
586  CommentInfo* comments_;
587 };
588 
593 public:
594  friend class Path;
595 
596  PathArgument();
597  PathArgument(ArrayIndex index);
598  PathArgument(const char* key);
599  PathArgument(const std::string& key);
600 
601 private:
602  enum Kind {
603  kindNone = 0,
604  kindIndex,
605  kindKey
606  };
607  std::string key_;
608  ArrayIndex index_;
609  Kind kind_;
610 };
611 
623 class JSON_API Path {
624 public:
625  Path(const std::string& path,
626  const PathArgument& a1 = PathArgument(),
627  const PathArgument& a2 = PathArgument(),
628  const PathArgument& a3 = PathArgument(),
629  const PathArgument& a4 = PathArgument(),
630  const PathArgument& a5 = PathArgument());
631 
632  const Value& resolve(const Value& root) const;
633  Value resolve(const Value& root, const Value& defaultValue) const;
636  Value& make(Value& root) const;
637 
638 private:
639  typedef std::vector<const PathArgument*> InArgs;
640  typedef std::vector<PathArgument> Args;
641 
642  void makePath(const std::string& path, const InArgs& in);
643  void addPathInArg(const std::string& path,
644  const InArgs& in,
645  InArgs::const_iterator& itInArg,
646  PathArgument::Kind kind);
647  void invalidPath(const std::string& path, int location);
648 
649  Args args_;
650 };
651 
656 public:
657  typedef std::bidirectional_iterator_tag iterator_category;
658  typedef unsigned int size_t;
659  typedef int difference_type;
661 
662  bool operator==(const SelfType& other) const { return isEqual(other); }
663 
664  bool operator!=(const SelfType& other) const { return !isEqual(other); }
665 
666  difference_type operator-(const SelfType& other) const {
667  return other.computeDistance(*this);
668  }
669 
672  Value key() const;
673 
675  UInt index() const;
676 
680  std::string name() const;
681 
685  JSONCPP_DEPRECATED("Use `key = name();` instead.")
686  char const* memberName() const;
690  char const* memberName(char const** end) const;
691 
692 protected:
693  Value& deref() const;
694 
695  void increment();
696 
697  void decrement();
698 
699  difference_type computeDistance(const SelfType& other) const;
700 
701  bool isEqual(const SelfType& other) const;
702 
703  void copy(const SelfType& other);
704 
705 private:
706  Value::ObjectValues::iterator current_;
707  // Indicates that iterator is for a null value.
708  bool isNull_;
709 
710 public:
711  // For some reason, BORLAND needs these at the end, rather
712  // than earlier. No idea why.
714  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
715 };
716 
721  friend class Value;
722 
723 public:
724  typedef const Value value_type;
725  //typedef unsigned int size_t;
726  //typedef int difference_type;
727  typedef const Value& reference;
728  typedef const Value* pointer;
730 
732 
733 private:
736  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
737 public:
738  SelfType& operator=(const ValueIteratorBase& other);
739 
740  SelfType operator++(int) {
741  SelfType temp(*this);
742  ++*this;
743  return temp;
744  }
745 
746  SelfType operator--(int) {
747  SelfType temp(*this);
748  --*this;
749  return temp;
750  }
751 
752  SelfType& operator--() {
753  decrement();
754  return *this;
755  }
756 
757  SelfType& operator++() {
758  increment();
759  return *this;
760  }
761 
762  reference operator*() const { return deref(); }
763 
764  pointer operator->() const { return &deref(); }
765 };
766 
770  friend class Value;
771 
772 public:
773  typedef Value value_type;
774  typedef unsigned int size_t;
775  typedef int difference_type;
776  typedef Value& reference;
777  typedef Value* pointer;
779 
780  ValueIterator();
781  ValueIterator(const ValueConstIterator& other);
782  ValueIterator(const ValueIterator& other);
783 
784 private:
787  explicit ValueIterator(const Value::ObjectValues::iterator& current);
788 public:
789  SelfType& operator=(const SelfType& other);
790 
791  SelfType operator++(int) {
792  SelfType temp(*this);
793  ++*this;
794  return temp;
795  }
796 
797  SelfType operator--(int) {
798  SelfType temp(*this);
799  --*this;
800  return temp;
801  }
802 
803  SelfType& operator--() {
804  decrement();
805  return *this;
806  }
807 
808  SelfType& operator++() {
809  increment();
810  return *this;
811  }
812 
813  reference operator*() const { return deref(); }
814 
815  pointer operator->() const { return &deref(); }
816 };
817 
818 } // namespace Json
819 
820 
821 namespace std {
823 template<>
824 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
825 }
826 
827 
828 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
829 #pragma warning(pop)
830 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
831 
832 #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:815
#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:201
unsigned int ArrayIndex
Definition: forwards.h:23
reference operator*() const
Definition: value.h:762
std::vector< std::string > Members
Definition: value.h:165
base class for Value iterators.
Definition: value.h:655
array value (ordered list)
Definition: value.h:85
unsigned __int64 UInt64
Definition: config.h:98
unsigned integer value
Definition: value.h:81
void throwLogicError(std::string const &msg)
used internally
Definition: json_value.cpp:175
Json::ArrayIndex ArrayIndex
Definition: value.h:176
Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Definition: value.h:66
const Value value_type
Definition: value.h:724
object value (collection of name/value pairs).
Definition: value.h:86
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition: value.h:193
STL namespace.
Lightweight wrapper to tag static string.
Definition: value.h:116
static const UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition: value.h:195
Json::LargestUInt LargestUInt
Definition: value.h:175
difference_type computeDistance(const SelfType &other) const
bool operator!=(const SelfType &other) const
Definition: value.h:664
const iterator for object and array value.
Definition: value.h:720
unsigned int size_t
Definition: value.h:774
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:592
SelfType & operator--()
Definition: value.h:752
&#39;null&#39; value
Definition: value.h:79
CommentPlacement
Definition: value.h:89
SelfType & operator--()
Definition: value.h:803
Value value_type
Definition: value.h:773
StaticString(const char *czstring)
Definition: value.h:118
ValueConstIterator SelfType
Definition: value.h:729
static const Value & nullRef
Definition: value.h:178
UInt64 LargestUInt
Definition: config.h:104
ValueConstIterator const_iterator
Definition: value.h:167
JSON (JavaScript Object Notation).
Definition: config.h:87
ValueIteratorBase SelfType
Definition: value.h:660
Json::Int64 Int64
Definition: value.h:172
ValueIterator SelfType
Definition: value.h:778
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:469
Experimental and untested: represents a "path" to access a node.
Definition: value.h:623
SelfType operator--(int)
Definition: value.h:746
Json::LargestInt LargestInt
Definition: value.h:174
const char * c_str() const
Definition: value.h:122
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition: value.h:203
double value
Definition: value.h:82
void throwRuntimeError(std::string const &msg)
used internally
Definition: json_value.cpp:171
SelfType operator--(int)
Definition: value.h:797
Json::UInt UInt
Definition: value.h:168
SelfType & operator++()
Definition: value.h:808
Json::UInt64 UInt64
Definition: value.h:171
Json::Int Int
Definition: value.h:169
Value * pointer
Definition: value.h:777
Represents a JSON value.
Definition: value.h:162
std::bidirectional_iterator_tag iterator_category
Definition: value.h:657
ValueIterator iterator
Definition: value.h:166
const Value * pointer
Definition: value.h:728
difference_type operator-(const SelfType &other) const
Definition: value.h:666
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:191
reference operator*() const
Definition: value.h:813
Exceptions which the user cannot easily avoid.
Definition: value.h:55
const Value & reference
Definition: value.h:727
a comment on the line after a value (only make sense for
Definition: value.h:92
unsigned int UInt
Definition: config.h:89
Iterator for object and array value.
Definition: value.h:769
SelfType & operator++()
Definition: value.h:757
__int64 Int64
Definition: config.h:97
SelfType operator++(int)
Definition: value.h:740
ValueType
Type of the value held by a Value object.
Definition: value.h:78
bool value
Definition: value.h:84
signed integer value
Definition: value.h:80
SelfType operator++(int)
Definition: value.h:791
unsigned int size_t
Definition: value.h:658
int Int
Definition: config.h:88
a comment placed on the line before a value
Definition: value.h:90
UTF-8 string value.
Definition: value.h:83
std::string const msg_
Definition: value.h:46
a comment just after a value on the same line
Definition: value.h:91
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: value.h:824
Base class for all exceptions we throw.
Definition: value.h:40
bool operator==(const SelfType &other) const
Definition: value.h:662
pointer operator->() const
Definition: value.h:764
Value & reference
Definition: value.h:776
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:186
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:188