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& nullRef;
164 #if !defined(__ARMEL__)
165  static const Value null;
167 #endif
168  static const LargestInt minLargestInt;
171  static const LargestInt maxLargestInt;
173  static const LargestUInt maxLargestUInt;
174 
176  static const Int minInt;
178  static const Int maxInt;
180  static const UInt maxUInt;
181 
182 #if defined(JSON_HAS_INT64)
183  static const Int64 minInt64;
186  static const Int64 maxInt64;
188  static const UInt64 maxUInt64;
189 #endif // defined(JSON_HAS_INT64)
190 
191 private:
192 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
193  class CZString {
194  public:
195  enum DuplicationPolicy {
196  noDuplication = 0,
197  duplicate,
198  duplicateOnCopy
199  };
200  CZString(ArrayIndex index);
201  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
202  CZString(CZString const& other);
203  ~CZString();
204  CZString& operator=(CZString other);
205  bool operator<(CZString const& other) const;
206  bool operator==(CZString const& other) const;
207  ArrayIndex index() const;
208  //const char* c_str() const; ///< \deprecated
209  char const* data() const;
210  unsigned length() const;
211  bool isStaticString() const;
212 
213  private:
214  void swap(CZString& other);
215 
216  struct StringStorage {
217  DuplicationPolicy policy_: 2;
218  unsigned length_: 30; // 1GB max
219  };
220 
221  char const* cstr_; // actually, a prefixed string, unless policy is noDup
222  union {
223  ArrayIndex index_;
224  StringStorage storage_;
225  };
226  };
227 
228 public:
229 #ifndef JSON_USE_CPPTL_SMALLMAP
230  typedef std::map<CZString, Value> ObjectValues;
231 #else
232  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
233 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
234 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
235 
236 public:
252  Value(ValueType type = nullValue);
253  Value(Int value);
254  Value(UInt value);
255 #if defined(JSON_HAS_INT64)
256  Value(Int64 value);
257  Value(UInt64 value);
258 #endif // if defined(JSON_HAS_INT64)
259  Value(double value);
260  Value(const char* value);
261  Value(const char* beginValue, const char* endValue);
262 
277  Value(const StaticString& value);
278  Value(const std::string& value);
279 #ifdef JSON_USE_CPPTL
280  Value(const CppTL::ConstString& value);
281 #endif
282  Value(bool value);
284  Value(const Value& other);
285  ~Value();
286 
289  Value &operator=(const Value &other);
291  void swap(Value& other);
293  void swapPayload(Value& other);
294 
295  ValueType type() const;
296 
298  bool operator<(const Value& other) const;
299  bool operator<=(const Value& other) const;
300  bool operator>=(const Value& other) const;
301  bool operator>(const Value& other) const;
302  bool operator==(const Value& other) const;
303  bool operator!=(const Value& other) const;
304  int compare(const Value& other) const;
305 
306  const char* asCString() const;
307  std::string asString() const;
308 
311  bool getString(
312  char const** str, char const** end) const;
313 #ifdef JSON_USE_CPPTL
314  CppTL::ConstString asConstString() const;
315 #endif
316  Int asInt() const;
317  UInt asUInt() const;
318 #if defined(JSON_HAS_INT64)
319  Int64 asInt64() const;
320  UInt64 asUInt64() const;
321 #endif // if defined(JSON_HAS_INT64)
322  LargestInt asLargestInt() const;
323  LargestUInt asLargestUInt() const;
324  float asFloat() const;
325  double asDouble() const;
326  bool asBool() const;
327 
328  bool isNull() const;
329  bool isBool() const;
330  bool isInt() const;
331  bool isInt64() const;
332  bool isUInt() const;
333  bool isUInt64() const;
334  bool isIntegral() const;
335  bool isDouble() const;
336  bool isNumeric() const;
337  bool isString() const;
338  bool isArray() const;
339  bool isObject() const;
340 
341  bool isConvertibleTo(ValueType other) const;
342 
344  ArrayIndex size() const;
345 
348  bool empty() const;
349 
351  bool operator!() const;
352 
356  void clear();
357 
363  void resize(ArrayIndex size);
364 
371  Value& operator[](ArrayIndex index);
372 
379  Value& operator[](int index);
380 
384  const Value& operator[](ArrayIndex index) const;
385 
389  const Value& operator[](int index) const;
390 
394  Value get(ArrayIndex index, const Value& defaultValue) const;
396  bool isValidIndex(ArrayIndex index) const;
400  Value& append(const Value& value);
401 
405  Value& operator[](const char* key);
408  const Value& operator[](const char* key) const;
411  Value& operator[](const std::string& key);
415  const Value& operator[](const std::string& key) const;
428  Value& operator[](const StaticString& key);
429 #ifdef JSON_USE_CPPTL
430  Value& operator[](const CppTL::ConstString& key);
434  const Value& operator[](const CppTL::ConstString& key) const;
435 #endif
436  Value get(const char* key, const Value& defaultValue) const;
442  Value get(const char* key, const char* end, const Value& defaultValue) const;
446  Value get(const std::string& key, const Value& defaultValue) const;
447 #ifdef JSON_USE_CPPTL
448  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
451 #endif
452  Value const* find(char const* key, char const* end) const;
459  Value const* demand(char const* key, char const* end);
467  Value removeMember(const char* key);
471  Value removeMember(const std::string& key);
474  bool removeMember(const char* key, Value* removed);
481  bool removeMember(std::string const& key, Value* removed);
483  bool removeMember(const char* key, const char* end, Value* removed);
490  bool removeIndex(ArrayIndex i, Value* removed);
491 
494  bool isMember(const char* key) const;
497  bool isMember(const std::string& key) const;
499  bool isMember(const char* key, const char* end) const;
500 #ifdef JSON_USE_CPPTL
501  bool isMember(const CppTL::ConstString& key) const;
503 #endif
504 
510  Members getMemberNames() const;
511 
512  //# ifdef JSON_USE_CPPTL
513  // EnumMemberNames enumMemberNames() const;
514  // EnumValues enumValues() const;
515  //# endif
516 
518  JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
519  void setComment(const char* comment, CommentPlacement placement);
521  void setComment(const char* comment, size_t len, CommentPlacement placement);
523  void setComment(const std::string& comment, CommentPlacement placement);
524  bool hasComment(CommentPlacement placement) const;
526  std::string getComment(CommentPlacement placement) const;
527 
528  std::string toStyledString() const;
529 
530  const_iterator begin() const;
531  const_iterator end() const;
532 
533  iterator begin();
534  iterator end();
535 
536 private:
537  void initBasic(ValueType type, bool allocated = false);
538 
539  Value& resolveReference(const char* key);
540  Value& resolveReference(const char* key, const char* end);
541 
542  struct CommentInfo {
543  CommentInfo();
544  ~CommentInfo();
545 
546  void setComment(const char* text, size_t len);
547 
548  char* comment_;
549  };
550 
551  // struct MemberNamesTransform
552  //{
553  // typedef const char *result_type;
554  // const char *operator()( const CZString &name ) const
555  // {
556  // return name.c_str();
557  // }
558  //};
559 
560  union ValueHolder {
561  LargestInt int_;
562  LargestUInt uint_;
563  double real_;
564  bool bool_;
565  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
566  ObjectValues* map_;
567  } value_;
568  ValueType type_ : 8;
569  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
570  // If not allocated_, string_ must be null-terminated.
571  CommentInfo* comments_;
572 };
573 
578 public:
579  friend class Path;
580 
581  PathArgument();
582  PathArgument(ArrayIndex index);
583  PathArgument(const char* key);
584  PathArgument(const std::string& key);
585 
586 private:
587  enum Kind {
588  kindNone = 0,
589  kindIndex,
590  kindKey
591  };
592  std::string key_;
593  ArrayIndex index_;
594  Kind kind_;
595 };
596 
608 class JSON_API Path {
609 public:
610  Path(const std::string& path,
611  const PathArgument& a1 = PathArgument(),
612  const PathArgument& a2 = PathArgument(),
613  const PathArgument& a3 = PathArgument(),
614  const PathArgument& a4 = PathArgument(),
615  const PathArgument& a5 = PathArgument());
616 
617  const Value& resolve(const Value& root) const;
618  Value resolve(const Value& root, const Value& defaultValue) const;
621  Value& make(Value& root) const;
622 
623 private:
624  typedef std::vector<const PathArgument*> InArgs;
625  typedef std::vector<PathArgument> Args;
626 
627  void makePath(const std::string& path, const InArgs& in);
628  void addPathInArg(const std::string& path,
629  const InArgs& in,
630  InArgs::const_iterator& itInArg,
631  PathArgument::Kind kind);
632  void invalidPath(const std::string& path, int location);
633 
634  Args args_;
635 };
636 
641 public:
642  typedef std::bidirectional_iterator_tag iterator_category;
643  typedef unsigned int size_t;
644  typedef int difference_type;
646 
648  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
649 
650  bool operator==(const SelfType& other) const { return isEqual(other); }
651 
652  bool operator!=(const SelfType& other) const { return !isEqual(other); }
653 
654  difference_type operator-(const SelfType& other) const {
655  return other.computeDistance(*this);
656  }
657 
660  Value key() const;
661 
663  UInt index() const;
664 
668  std::string name() const;
669 
673  JSONCPP_DEPRECATED("Use `key = name();` instead.")
674  char const* memberName() const;
678  char const* memberName(char const** end) const;
679 
680 protected:
681  Value& deref() const;
682 
683  void increment();
684 
685  void decrement();
686 
687  difference_type computeDistance(const SelfType& other) const;
688 
689  bool isEqual(const SelfType& other) const;
690 
691  void copy(const SelfType& other);
692 
693 private:
694  Value::ObjectValues::iterator current_;
695  // Indicates that iterator is for a null value.
696  bool isNull_;
697 };
698 
703  friend class Value;
704 
705 public:
706  typedef const Value value_type;
707  //typedef unsigned int size_t;
708  //typedef int difference_type;
709  typedef const Value& reference;
710  typedef const Value* pointer;
712 
714 
715 private:
718  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
719 public:
720  SelfType& operator=(const ValueIteratorBase& other);
721 
722  SelfType operator++(int) {
723  SelfType temp(*this);
724  ++*this;
725  return temp;
726  }
727 
728  SelfType operator--(int) {
729  SelfType temp(*this);
730  --*this;
731  return temp;
732  }
733 
734  SelfType& operator--() {
735  decrement();
736  return *this;
737  }
738 
739  SelfType& operator++() {
740  increment();
741  return *this;
742  }
743 
744  reference operator*() const { return deref(); }
745 
746  pointer operator->() const { return &deref(); }
747 };
748 
752  friend class Value;
753 
754 public:
755  typedef Value value_type;
756  typedef unsigned int size_t;
757  typedef int difference_type;
758  typedef Value& reference;
759  typedef Value* pointer;
761 
762  ValueIterator();
763  ValueIterator(const ValueConstIterator& other);
764  ValueIterator(const ValueIterator& other);
765 
766 private:
769  explicit ValueIterator(const Value::ObjectValues::iterator& current);
770 public:
771  SelfType& operator=(const SelfType& other);
772 
773  SelfType operator++(int) {
774  SelfType temp(*this);
775  ++*this;
776  return temp;
777  }
778 
779  SelfType operator--(int) {
780  SelfType temp(*this);
781  --*this;
782  return temp;
783  }
784 
785  SelfType& operator--() {
786  decrement();
787  return *this;
788  }
789 
790  SelfType& operator++() {
791  increment();
792  return *this;
793  }
794 
795  reference operator*() const { return deref(); }
796 
797  pointer operator->() const { return &deref(); }
798 };
799 
800 } // namespace Json
801 
802 
803 namespace std {
805 template<>
806 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
807 }
808 
809 
810 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
811 #pragma warning(pop)
812 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
813 
814 #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:797
#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:186
unsigned int ArrayIndex
Definition: forwards.h:23
reference operator*() const
Definition: value.h:744
std::vector< std::string > Members
Definition: value.h:150
base class for Value iterators.
Definition: value.h:640
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:192
Json::ArrayIndex ArrayIndex
Definition: value.h:161
const Value value_type
Definition: value.h:706
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:178
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:180
Json::LargestUInt LargestUInt
Definition: value.h:160
difference_type computeDistance(const SelfType &other) const
bool operator!=(const SelfType &other) const
Definition: value.h:652
const iterator for object and array value.
Definition: value.h:702
unsigned int size_t
Definition: value.h:756
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:577
SelfType & operator--()
Definition: value.h:734
'null' value
Definition: value.h:64
CommentPlacement
Definition: value.h:74
SelfType & operator--()
Definition: value.h:785
Value value_type
Definition: value.h:755
StaticString(const char *czstring)
Definition: value.h:103
ValueConstIterator SelfType
Definition: value.h:711
static const Value & nullRef
Definition: value.h:163
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:645
Json::Int64 Int64
Definition: value.h:157
ValueIterator SelfType
Definition: value.h:760
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:486
Experimental and untested: represents a "path" to access a node.
Definition: value.h:608
SelfType operator--(int)
Definition: value.h:728
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:188
double value
Definition: value.h:67
void throwRuntimeError(std::string const &msg)
used internally
Definition: json_value.cpp:188
SelfType operator--(int)
Definition: value.h:779
Json::UInt UInt
Definition: value.h:153
SelfType & operator++()
Definition: value.h:790
Json::UInt64 UInt64
Definition: value.h:156
Json::Int Int
Definition: value.h:154
Value * pointer
Definition: value.h:759
Represents a JSON value.
Definition: value.h:147
std::bidirectional_iterator_tag iterator_category
Definition: value.h:642
ValueIterator iterator
Definition: value.h:151
const Value * pointer
Definition: value.h:710
difference_type operator-(const SelfType &other) const
Definition: value.h:654
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:176
reference operator*() const
Definition: value.h:795
const Value & reference
Definition: value.h:709
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:751
SelfType & operator++()
Definition: value.h:739
__int64 Int64
Definition: config.h:97
SelfType operator++(int)
Definition: value.h:722
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:773
unsigned int size_t
Definition: value.h:643
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:650
pointer operator->() const
Definition: value.h:746
Value & reference
Definition: value.h:758
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:171
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:173