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 //Conditional NORETURN attribute on the throw functions would:
26 // a) suppress false positives from static code analysis
27 // b) possibly improve optimization opportunities.
28 #if !defined(JSONCPP_NORETURN)
29 # if defined(_MSC_VER)
30 # define JSONCPP_NORETURN __declspec(noreturn)
31 # elif defined(__GNUC__)
32 # define JSONCPP_NORETURN __attribute__ ((__noreturn__))
33 # else
34 # define JSONCPP_NORETURN
35 # endif
36 #endif
37 
38 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
39 // be used by...
40 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
41 #pragma warning(push)
42 #pragma warning(disable : 4251)
43 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
44 
47 namespace Json {
48 
53 class JSON_API Exception : public std::exception {
54 public:
55  Exception(JSONCPP_STRING const& msg);
56  ~Exception() throw() JSONCPP_OVERRIDE;
57  char const* what() const throw() JSONCPP_OVERRIDE;
58 protected:
60 };
61 
69 public:
70  RuntimeError(JSONCPP_STRING const& msg);
71 };
72 
79 class JSON_API LogicError : public Exception {
80 public:
81  LogicError(JSONCPP_STRING const& msg);
82 };
83 
85 JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
87 JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
88 
91 enum ValueType {
92  nullValue = 0,
100 };
101 
108 };
109 
110 //# ifdef JSON_USE_CPPTL
111 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
112 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
113 //# endif
114 
130 public:
131  explicit StaticString(const char* czstring) : c_str_(czstring) {}
132 
133  operator const char*() const { return c_str_; }
134 
135  const char* c_str() const { return c_str_; }
136 
137 private:
138  const char* c_str_;
139 };
140 
176  friend class ValueIteratorBase;
177 public:
178  typedef std::vector<JSONCPP_STRING> Members;
181  typedef Json::UInt UInt;
182  typedef Json::Int Int;
183 #if defined(JSON_HAS_INT64)
186 #endif // defined(JSON_HAS_INT64)
190 
191  static const Value& null;
192  static const Value& nullRef;
193  static const LargestInt minLargestInt;
196  static const LargestInt maxLargestInt;
198  static const LargestUInt maxLargestUInt;
199 
201  static const Int minInt;
203  static const Int maxInt;
205  static const UInt maxUInt;
206 
207 #if defined(JSON_HAS_INT64)
208  static const Int64 minInt64;
211  static const Int64 maxInt64;
213  static const UInt64 maxUInt64;
214 #endif // defined(JSON_HAS_INT64)
215 
216 private:
217 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
218  class CZString {
219  public:
220  enum DuplicationPolicy {
221  noDuplication = 0,
222  duplicate,
223  duplicateOnCopy
224  };
225  CZString(ArrayIndex index);
226  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
227  CZString(CZString const& other);
228 #if JSON_HAS_RVALUE_REFERENCES
229  CZString(CZString&& other);
230 #endif
231  ~CZString();
232  CZString& operator=(CZString other);
233  bool operator<(CZString const& other) const;
234  bool operator==(CZString const& other) const;
235  ArrayIndex index() const;
236  //const char* c_str() const; ///< \deprecated
237  char const* data() const;
238  unsigned length() const;
239  bool isStaticString() const;
240 
241  private:
242  void swap(CZString& other);
243 
244  struct StringStorage {
245  unsigned policy_: 2;
246  unsigned length_: 30; // 1GB max
247  };
248 
249  char const* cstr_; // actually, a prefixed string, unless policy is noDup
250  union {
251  ArrayIndex index_;
252  StringStorage storage_;
253  };
254  };
255 
256 public:
257 #ifndef JSON_USE_CPPTL_SMALLMAP
258  typedef std::map<CZString, Value> ObjectValues;
259 #else
260  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
261 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
262 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
263 
264 public:
280  Value(ValueType type = nullValue);
281  Value(Int value);
282  Value(UInt value);
283 #if defined(JSON_HAS_INT64)
284  Value(Int64 value);
285  Value(UInt64 value);
286 #endif // if defined(JSON_HAS_INT64)
287  Value(double value);
288  Value(const char* value);
289  Value(const char* begin, const char* end);
290 
305  Value(const StaticString& value);
306  Value(const JSONCPP_STRING& value);
307 #ifdef JSON_USE_CPPTL
308  Value(const CppTL::ConstString& value);
309 #endif
310  Value(bool value);
312  Value(const Value& other);
313 #if JSON_HAS_RVALUE_REFERENCES
314  Value(Value&& other);
316 #endif
317  ~Value();
318 
321  Value& operator=(Value other);
323  void swap(Value& other);
325  void swapPayload(Value& other);
326 
327  ValueType type() const;
328 
330  bool operator<(const Value& other) const;
331  bool operator<=(const Value& other) const;
332  bool operator>=(const Value& other) const;
333  bool operator>(const Value& other) const;
334  bool operator==(const Value& other) const;
335  bool operator!=(const Value& other) const;
336  int compare(const Value& other) const;
337 
338  const char* asCString() const;
339 #if JSONCPP_USING_SECURE_MEMORY
340  unsigned getCStringLength() const; //Allows you to understand the length of the CString
341 #endif
342  JSONCPP_STRING asString() const;
343 
346  bool getString(
347  char const** begin, char const** end) const;
348 #ifdef JSON_USE_CPPTL
349  CppTL::ConstString asConstString() const;
350 #endif
351  Int asInt() const;
352  UInt asUInt() const;
353 #if defined(JSON_HAS_INT64)
354  Int64 asInt64() const;
355  UInt64 asUInt64() const;
356 #endif // if defined(JSON_HAS_INT64)
357  LargestInt asLargestInt() const;
358  LargestUInt asLargestUInt() const;
359  float asFloat() const;
360  double asDouble() const;
361  bool asBool() const;
362 
363  bool isNull() const;
364  bool isBool() const;
365  bool isInt() const;
366  bool isInt64() const;
367  bool isUInt() const;
368  bool isUInt64() const;
369  bool isIntegral() const;
370  bool isDouble() const;
371  bool isNumeric() const;
372  bool isString() const;
373  bool isArray() const;
374  bool isObject() const;
375 
376  bool isConvertibleTo(ValueType other) const;
377 
379  ArrayIndex size() const;
380 
383  bool empty() const;
384 
386  bool operator!() const;
387 
391  void clear();
392 
398  void resize(ArrayIndex size);
399 
406  Value& operator[](ArrayIndex index);
407 
414  Value& operator[](int index);
415 
419  const Value& operator[](ArrayIndex index) const;
420 
424  const Value& operator[](int index) const;
425 
429  Value get(ArrayIndex index, const Value& defaultValue) const;
431  bool isValidIndex(ArrayIndex index) const;
435  Value& append(const Value& value);
436 
440  Value& operator[](const char* key);
443  const Value& operator[](const char* key) const;
446  Value& operator[](const JSONCPP_STRING& key);
450  const Value& operator[](const JSONCPP_STRING& key) const;
463  Value& operator[](const StaticString& key);
464 #ifdef JSON_USE_CPPTL
465  Value& operator[](const CppTL::ConstString& key);
469  const Value& operator[](const CppTL::ConstString& key) const;
470 #endif
471  Value get(const char* key, const Value& defaultValue) const;
477  Value get(const char* begin, const char* end, const Value& defaultValue) const;
481  Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
482 #ifdef JSON_USE_CPPTL
483  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
486 #endif
487  Value const* find(char const* begin, char const* end) const;
494  Value const* demand(char const* begin, char const* end);
502  Value removeMember(const char* key);
506  Value removeMember(const JSONCPP_STRING& key);
509  bool removeMember(const char* key, Value* removed);
516  bool removeMember(JSONCPP_STRING const& key, Value* removed);
518  bool removeMember(const char* begin, const char* end, Value* removed);
525  bool removeIndex(ArrayIndex i, Value* removed);
526 
529  bool isMember(const char* key) const;
532  bool isMember(const JSONCPP_STRING& key) const;
534  bool isMember(const char* begin, const char* end) const;
535 #ifdef JSON_USE_CPPTL
536  bool isMember(const CppTL::ConstString& key) const;
538 #endif
539 
545  Members getMemberNames() const;
546 
547  //# ifdef JSON_USE_CPPTL
548  // EnumMemberNames enumMemberNames() const;
549  // EnumValues enumValues() const;
550  //# endif
551 
553  JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
554  void setComment(const char* comment, CommentPlacement placement);
556  void setComment(const char* comment, size_t len, CommentPlacement placement);
558  void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
559  bool hasComment(CommentPlacement placement) const;
561  JSONCPP_STRING getComment(CommentPlacement placement) const;
562 
563  JSONCPP_STRING toStyledString() const;
564 
565  const_iterator begin() const;
566  const_iterator end() const;
567 
568  iterator begin();
569  iterator end();
570 
571  // Accessors for the [start, limit) range of bytes within the JSON text from
572  // which this value was parsed, if any.
573  void setOffsetStart(ptrdiff_t start);
574  void setOffsetLimit(ptrdiff_t limit);
575  ptrdiff_t getOffsetStart() const;
576  ptrdiff_t getOffsetLimit() const;
577 
578 private:
579  void initBasic(ValueType type, bool allocated = false);
580 
581  Value& resolveReference(const char* key);
582  Value& resolveReference(const char* key, const char* end);
583 
584  struct CommentInfo {
585  CommentInfo();
586  ~CommentInfo();
587 
588  void setComment(const char* text, size_t len);
589 
590  char* comment_;
591  };
592 
593  // struct MemberNamesTransform
594  //{
595  // typedef const char *result_type;
596  // const char *operator()( const CZString &name ) const
597  // {
598  // return name.c_str();
599  // }
600  //};
601 
602  union ValueHolder {
603  LargestInt int_;
604  LargestUInt uint_;
605  double real_;
606  bool bool_;
607  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
608  ObjectValues* map_;
609  } value_;
610  ValueType type_ : 8;
611  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
612  // If not allocated_, string_ must be null-terminated.
613  CommentInfo* comments_;
614 
615  // [start, limit) byte offsets in the source JSON text from which this Value
616  // was extracted.
617  ptrdiff_t start_;
618  ptrdiff_t limit_;
619 };
620 
625 public:
626  friend class Path;
627 
628  PathArgument();
629  PathArgument(ArrayIndex index);
630  PathArgument(const char* key);
631  PathArgument(const JSONCPP_STRING& key);
632 
633 private:
634  enum Kind {
635  kindNone = 0,
636  kindIndex,
637  kindKey
638  };
639  JSONCPP_STRING key_;
640  ArrayIndex index_;
641  Kind kind_;
642 };
643 
655 class JSON_API Path {
656 public:
657  Path(const JSONCPP_STRING& path,
658  const PathArgument& a1 = PathArgument(),
659  const PathArgument& a2 = PathArgument(),
660  const PathArgument& a3 = PathArgument(),
661  const PathArgument& a4 = PathArgument(),
662  const PathArgument& a5 = PathArgument());
663 
664  const Value& resolve(const Value& root) const;
665  Value resolve(const Value& root, const Value& defaultValue) const;
668  Value& make(Value& root) const;
669 
670 private:
671  typedef std::vector<const PathArgument*> InArgs;
672  typedef std::vector<PathArgument> Args;
673 
674  void makePath(const JSONCPP_STRING& path, const InArgs& in);
675  void addPathInArg(const JSONCPP_STRING& path,
676  const InArgs& in,
677  InArgs::const_iterator& itInArg,
678  PathArgument::Kind kind);
679  void invalidPath(const JSONCPP_STRING& path, int location);
680 
681  Args args_;
682 };
683 
688 public:
689  typedef std::bidirectional_iterator_tag iterator_category;
690  typedef unsigned int size_t;
691  typedef int difference_type;
693 
694  bool operator==(const SelfType& other) const { return isEqual(other); }
695 
696  bool operator!=(const SelfType& other) const { return !isEqual(other); }
697 
698  difference_type operator-(const SelfType& other) const {
699  return other.computeDistance(*this);
700  }
701 
704  Value key() const;
705 
707  UInt index() const;
708 
712  JSONCPP_STRING name() const;
713 
717  JSONCPP_DEPRECATED("Use `key = name();` instead.")
718  char const* memberName() const;
722  char const* memberName(char const** end) const;
723 
724 protected:
725  Value& deref() const;
726 
727  void increment();
728 
729  void decrement();
730 
731  difference_type computeDistance(const SelfType& other) const;
732 
733  bool isEqual(const SelfType& other) const;
734 
735  void copy(const SelfType& other);
736 
737 private:
738  Value::ObjectValues::iterator current_;
739  // Indicates that iterator is for a null value.
740  bool isNull_;
741 
742 public:
743  // For some reason, BORLAND needs these at the end, rather
744  // than earlier. No idea why.
746  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
747 };
748 
752 class JSON_API ValueConstIterator : public ValueIteratorBase {
753  friend class Value;
754 
755 public:
756  typedef const Value value_type;
757  //typedef unsigned int size_t;
758  //typedef int difference_type;
759  typedef const Value& reference;
760  typedef const Value* pointer;
762 
764  ValueConstIterator(ValueIterator const& other);
765 
766 private:
769  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
770 public:
771  SelfType& operator=(const ValueIteratorBase& 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 
803  friend class Value;
804 
805 public:
806  typedef Value value_type;
807  typedef unsigned int size_t;
808  typedef int difference_type;
809  typedef Value& reference;
810  typedef Value* pointer;
812 
813  ValueIterator();
814  explicit ValueIterator(const ValueConstIterator& other);
815  ValueIterator(const ValueIterator& other);
816 
817 private:
820  explicit ValueIterator(const Value::ObjectValues::iterator& current);
821 public:
822  SelfType& operator=(const SelfType& other);
823 
824  SelfType operator++(int) {
825  SelfType temp(*this);
826  ++*this;
827  return temp;
828  }
829 
830  SelfType operator--(int) {
831  SelfType temp(*this);
832  --*this;
833  return temp;
834  }
835 
836  SelfType& operator--() {
837  decrement();
838  return *this;
839  }
840 
841  SelfType& operator++() {
842  increment();
843  return *this;
844  }
845 
846  reference operator*() const { return deref(); }
847 
848  pointer operator->() const { return &deref(); }
849 };
850 
851 } // namespace Json
852 
853 
854 namespace std {
856 template<>
857 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
858 }
859 
860 
861 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
862 #pragma warning(pop)
863 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
864 
865 #endif // CPPTL_JSON_H_INCLUDED
#define JSONCPP_OVERRIDE
Definition: config.h:81
#define JSONCPP_DEPRECATED(message)
Definition: config.h:121
Int64 LargestInt
Definition: config.h:154
pointer operator->() const
Definition: value.h:848
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:53
static const Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
Definition: value.h:211
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:191
reference operator*() const
Definition: value.h:795
base class for Value iterators.
Definition: value.h:687
array value (ordered list)
Definition: value.h:98
unsigned __int64 UInt64
Definition: config.h:149
unsigned integer value
Definition: value.h:94
#define JSONCPP_STRING
Definition: config.h:165
Json::ArrayIndex ArrayIndex
Definition: value.h:189
Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Definition: value.h:79
const Value value_type
Definition: value.h:756
object value (collection of name/value pairs).
Definition: value.h:99
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition: value.h:203
STL namespace.
Lightweight wrapper to tag static string.
Definition: value.h:129
static const UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition: value.h:205
Json::LargestUInt LargestUInt
Definition: value.h:188
difference_type computeDistance(const SelfType &other) const
bool operator!=(const SelfType &other) const
Definition: value.h:696
const iterator for object and array value.
Definition: value.h:752
unsigned int size_t
Definition: value.h:807
#define JSONCPP_NORETURN
Definition: value.h:30
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:624
SelfType & operator--()
Definition: value.h:785
'null' value
Definition: value.h:92
CommentPlacement
Definition: value.h:102
SelfType & operator--()
Definition: value.h:836
Value value_type
Definition: value.h:806
StaticString(const char *czstring)
Definition: value.h:131
ValueConstIterator SelfType
Definition: value.h:761
static const Value & nullRef
just a kludge for binary-compatibility; same as null
Definition: value.h:192
UInt64 LargestUInt
Definition: config.h:155
ValueConstIterator const_iterator
Definition: value.h:180
std::string msg_
Definition: value.h:59
JSON (JavaScript Object Notation).
Definition: allocator.h:12
ValueIteratorBase SelfType
Definition: value.h:692
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:83
Json::Int64 Int64
Definition: value.h:185
ValueIterator SelfType
Definition: value.h:811
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:512
Experimental and untested: represents a "path" to access a node.
Definition: value.h:655
SelfType operator--(int)
Definition: value.h:779
Json::LargestInt LargestInt
Definition: value.h:187
const char * c_str() const
Definition: value.h:135
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition: value.h:213
double value
Definition: value.h:95
SelfType operator--(int)
Definition: value.h:830
Json::UInt UInt
Definition: value.h:181
SelfType & operator++()
Definition: value.h:841
Json::UInt64 UInt64
Definition: value.h:184
Json::Int Int
Definition: value.h:182
Value * pointer
Definition: value.h:810
Represents a JSON value.
Definition: value.h:175
std::bidirectional_iterator_tag iterator_category
Definition: value.h:689
ValueIterator iterator
Definition: value.h:179
const Value * pointer
Definition: value.h:760
difference_type operator-(const SelfType &other) const
Definition: value.h:698
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:201
reference operator*() const
Definition: value.h:846
Exceptions which the user cannot easily avoid.
Definition: value.h:68
const Value & reference
Definition: value.h:759
a comment on the line after a value (only make sense for
Definition: value.h:105
unsigned int UInt
Definition: config.h:140
Iterator for object and array value.
Definition: value.h:802
SelfType & operator++()
Definition: value.h:790
__int64 Int64
Definition: config.h:148
SelfType operator++(int)
Definition: value.h:773
ValueType
used internally
Definition: value.h:91
std::vector< std::string > Members
Definition: value.h:178
bool value
Definition: value.h:97
signed integer value
Definition: value.h:93
SelfType operator++(int)
Definition: value.h:824
unsigned int size_t
Definition: value.h:690
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:88
int Int
Definition: config.h:139
a comment placed on the line before a value
Definition: value.h:103
UTF-8 string value.
Definition: value.h:96
a comment just after a value on the same line
Definition: value.h:104
Base class for all exceptions we throw.
Definition: value.h:53
bool operator==(const SelfType &other) const
Definition: value.h:694
pointer operator->() const
Definition: value.h:797
Value & reference
Definition: value.h:809
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:196
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:198