JsonCpp project page JsonCpp home page

value.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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 
45 #pragma pack(push, 8)
46 
49 namespace Json {
50 
55 class JSON_API Exception : public std::exception {
56 public:
57  Exception(JSONCPP_STRING const& msg);
59  char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
60 protected:
62 };
63 
71 public:
72  RuntimeError(JSONCPP_STRING const& msg);
73 };
74 
81 class JSON_API LogicError : public Exception {
82 public:
83  LogicError(JSONCPP_STRING const& msg);
84 };
85 
87 JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
89 JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
90 
93 enum ValueType {
94  nullValue = 0,
102 };
103 
110 };
111 
112 //# ifdef JSON_USE_CPPTL
113 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
114 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
115 //# endif
116 
132 public:
133  explicit StaticString(const char* czstring) : c_str_(czstring) {}
134 
135  operator const char*() const { return c_str_; }
136 
137  const char* c_str() const { return c_str_; }
138 
139 private:
140  const char* c_str_;
141 };
142 
178  friend class ValueIteratorBase;
179 public:
180  typedef std::vector<JSONCPP_STRING> Members;
183  typedef Json::UInt UInt;
184  typedef Json::Int Int;
185 #if defined(JSON_HAS_INT64)
188 #endif // defined(JSON_HAS_INT64)
192 
193  static const Value& null;
194  static const Value& nullRef;
195  static Value const& nullSingleton();
196 
198  static const LargestInt minLargestInt;
200  static const LargestInt maxLargestInt;
202  static const LargestUInt maxLargestUInt;
203 
205  static const Int minInt;
207  static const Int maxInt;
209  static const UInt maxUInt;
210 
211 #if defined(JSON_HAS_INT64)
212  static const Int64 minInt64;
215  static const Int64 maxInt64;
217  static const UInt64 maxUInt64;
218 #endif // defined(JSON_HAS_INT64)
219 
220 private:
221 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
222  class CZString {
223  public:
224  enum DuplicationPolicy {
225  noDuplication = 0,
226  duplicate,
227  duplicateOnCopy
228  };
229  CZString(ArrayIndex index);
230  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
231  CZString(CZString const& other);
232 #if JSON_HAS_RVALUE_REFERENCES
233  CZString(CZString&& other);
234 #endif
235  ~CZString();
236  CZString& operator=(const CZString& other);
237 
238 #if JSON_HAS_RVALUE_REFERENCES
239  CZString& operator=(CZString&& other);
240 #endif
241 
242  bool operator<(CZString const& other) const;
243  bool operator==(CZString const& other) const;
244  ArrayIndex index() const;
245  //const char* c_str() const; ///< \deprecated
246  char const* data() const;
247  unsigned length() const;
248  bool isStaticString() const;
249 
250  private:
251  void swap(CZString& other);
252 
253  struct StringStorage {
254  unsigned policy_: 2;
255  unsigned length_: 30; // 1GB max
256  };
257 
258  char const* cstr_; // actually, a prefixed string, unless policy is noDup
259  union {
260  ArrayIndex index_;
261  StringStorage storage_;
262  };
263  };
264 
265 public:
266 #ifndef JSON_USE_CPPTL_SMALLMAP
267  typedef std::map<CZString, Value> ObjectValues;
268 #else
269  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
270 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
271 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
272 
273 public:
289  Value(ValueType type = nullValue);
290  Value(Int value);
291  Value(UInt value);
292 #if defined(JSON_HAS_INT64)
293  Value(Int64 value);
294  Value(UInt64 value);
295 #endif // if defined(JSON_HAS_INT64)
296  Value(double value);
297  Value(const char* value);
298  Value(const char* begin, const char* end);
299 
314  Value(const StaticString& value);
315  Value(const JSONCPP_STRING& value);
316 #ifdef JSON_USE_CPPTL
317  Value(const CppTL::ConstString& value);
318 #endif
319  Value(bool value);
321  Value(const Value& other);
322 #if JSON_HAS_RVALUE_REFERENCES
323  Value(Value&& other);
325 #endif
326  ~Value();
327 
330  Value& operator=(Value other);
331 
333  void swap(Value& other);
335  void swapPayload(Value& other);
336 
338  void copy(const Value& other);
340  void copyPayload(const Value& other);
341 
342  ValueType type() const;
343 
345  bool operator<(const Value& other) const;
346  bool operator<=(const Value& other) const;
347  bool operator>=(const Value& other) const;
348  bool operator>(const Value& other) const;
349  bool operator==(const Value& other) const;
350  bool operator!=(const Value& other) const;
351  int compare(const Value& other) const;
352 
353  const char* asCString() const;
354 #if JSONCPP_USING_SECURE_MEMORY
355  unsigned getCStringLength() const; //Allows you to understand the length of the CString
356 #endif
357  JSONCPP_STRING asString() const;
358 
361  bool getString(
362  char const** begin, char const** end) const;
363 #ifdef JSON_USE_CPPTL
364  CppTL::ConstString asConstString() const;
365 #endif
366  Int asInt() const;
367  UInt asUInt() const;
368 #if defined(JSON_HAS_INT64)
369  Int64 asInt64() const;
370  UInt64 asUInt64() const;
371 #endif // if defined(JSON_HAS_INT64)
372  LargestInt asLargestInt() const;
373  LargestUInt asLargestUInt() const;
374  float asFloat() const;
375  double asDouble() const;
376  bool asBool() const;
377 
378  bool isNull() const;
379  bool isBool() const;
380  bool isInt() const;
381  bool isInt64() const;
382  bool isUInt() const;
383  bool isUInt64() const;
384  bool isIntegral() const;
385  bool isDouble() const;
386  bool isNumeric() const;
387  bool isString() const;
388  bool isArray() const;
389  bool isObject() const;
390 
391  bool isConvertibleTo(ValueType other) const;
392 
394  ArrayIndex size() const;
395 
398  bool empty() const;
399 
401  bool operator!() const;
402 
406  void clear();
407 
413  void resize(ArrayIndex size);
414 
421  Value& operator[](ArrayIndex index);
422 
429  Value& operator[](int index);
430 
434  const Value& operator[](ArrayIndex index) const;
435 
439  const Value& operator[](int index) const;
440 
444  Value get(ArrayIndex index, const Value& defaultValue) const;
446  bool isValidIndex(ArrayIndex index) const;
450  Value& append(const Value& value);
451 
452 #if JSON_HAS_RVALUE_REFERENCES
453  Value& append(Value&& value);
454 #endif
455 
459  Value& operator[](const char* key);
462  const Value& operator[](const char* key) const;
465  Value& operator[](const JSONCPP_STRING& key);
469  const Value& operator[](const JSONCPP_STRING& key) const;
482  Value& operator[](const StaticString& key);
483 #ifdef JSON_USE_CPPTL
484  Value& operator[](const CppTL::ConstString& key);
488  const Value& operator[](const CppTL::ConstString& key) const;
489 #endif
490  Value get(const char* key, const Value& defaultValue) const;
496  Value get(const char* begin, const char* end, const Value& defaultValue) const;
500  Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
501 #ifdef JSON_USE_CPPTL
502  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
505 #endif
506  Value const* find(char const* begin, char const* end) const;
513  Value const* demand(char const* begin, char const* end);
522  Value removeMember(const char* key);
527  Value removeMember(const JSONCPP_STRING& key);
530  bool removeMember(const char* key, Value* removed);
537  bool removeMember(JSONCPP_STRING const& key, Value* removed);
539  bool removeMember(const char* begin, const char* end, Value* removed);
546  bool removeIndex(ArrayIndex i, Value* removed);
547 
550  bool isMember(const char* key) const;
553  bool isMember(const JSONCPP_STRING& key) const;
555  bool isMember(const char* begin, const char* end) const;
556 #ifdef JSON_USE_CPPTL
557  bool isMember(const CppTL::ConstString& key) const;
559 #endif
560 
566  Members getMemberNames() const;
567 
568  //# ifdef JSON_USE_CPPTL
569  // EnumMemberNames enumMemberNames() const;
570  // EnumValues enumValues() const;
571  //# endif
572 
574  JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
575  void setComment(const char* comment, CommentPlacement placement);
577  void setComment(const char* comment, size_t len, CommentPlacement placement);
579  void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
580  bool hasComment(CommentPlacement placement) const;
582  JSONCPP_STRING getComment(CommentPlacement placement) const;
583 
584  JSONCPP_STRING toStyledString() const;
585 
586  const_iterator begin() const;
587  const_iterator end() const;
588 
589  iterator begin();
590  iterator end();
591 
592  // Accessors for the [start, limit) range of bytes within the JSON text from
593  // which this value was parsed, if any.
594  void setOffsetStart(ptrdiff_t start);
595  void setOffsetLimit(ptrdiff_t limit);
596  ptrdiff_t getOffsetStart() const;
597  ptrdiff_t getOffsetLimit() const;
598 
599 private:
600  void initBasic(ValueType type, bool allocated = false);
601 
602  Value& resolveReference(const char* key);
603  Value& resolveReference(const char* key, const char* end);
604 
605  struct CommentInfo {
606  CommentInfo();
607  ~CommentInfo();
608 
609  void setComment(const char* text, size_t len);
610 
611  char* comment_;
612  };
613 
614  // struct MemberNamesTransform
615  //{
616  // typedef const char *result_type;
617  // const char *operator()( const CZString &name ) const
618  // {
619  // return name.c_str();
620  // }
621  //};
622 
623  union ValueHolder {
624  LargestInt int_;
625  LargestUInt uint_;
626  double real_;
627  bool bool_;
628  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
629  ObjectValues* map_;
630  } value_;
631  ValueType type_ : 8;
632  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
633  // If not allocated_, string_ must be null-terminated.
634  CommentInfo* comments_;
635 
636  // [start, limit) byte offsets in the source JSON text from which this Value
637  // was extracted.
638  ptrdiff_t start_;
639  ptrdiff_t limit_;
640 };
641 
646 public:
647  friend class Path;
648 
649  PathArgument();
650  PathArgument(ArrayIndex index);
651  PathArgument(const char* key);
652  PathArgument(const JSONCPP_STRING& key);
653 
654 private:
655  enum Kind {
656  kindNone = 0,
657  kindIndex,
658  kindKey
659  };
660  JSONCPP_STRING key_;
661  ArrayIndex index_;
662  Kind kind_;
663 };
664 
676 class JSON_API Path {
677 public:
678  Path(const JSONCPP_STRING& path,
679  const PathArgument& a1 = PathArgument(),
680  const PathArgument& a2 = PathArgument(),
681  const PathArgument& a3 = PathArgument(),
682  const PathArgument& a4 = PathArgument(),
683  const PathArgument& a5 = PathArgument());
684 
685  const Value& resolve(const Value& root) const;
686  Value resolve(const Value& root, const Value& defaultValue) const;
689  Value& make(Value& root) const;
690 
691 private:
692  typedef std::vector<const PathArgument*> InArgs;
693  typedef std::vector<PathArgument> Args;
694 
695  void makePath(const JSONCPP_STRING& path, const InArgs& in);
696  void addPathInArg(const JSONCPP_STRING& path,
697  const InArgs& in,
698  InArgs::const_iterator& itInArg,
699  PathArgument::Kind kind);
700  void invalidPath(const JSONCPP_STRING& path, int location);
701 
702  Args args_;
703 };
704 
709 public:
710  typedef std::bidirectional_iterator_tag iterator_category;
711  typedef unsigned int size_t;
712  typedef int difference_type;
714 
715  bool operator==(const SelfType& other) const { return isEqual(other); }
716 
717  bool operator!=(const SelfType& other) const { return !isEqual(other); }
718 
719  difference_type operator-(const SelfType& other) const {
720  return other.computeDistance(*this);
721  }
722 
725  Value key() const;
726 
728  UInt index() const;
729 
733  JSONCPP_STRING name() const;
734 
738  JSONCPP_DEPRECATED("Use `key = name();` instead.")
739  char const* memberName() const;
743  char const* memberName(char const** end) const;
744 
745 protected:
746  Value& deref() const;
747 
748  void increment();
749 
750  void decrement();
751 
752  difference_type computeDistance(const SelfType& other) const;
753 
754  bool isEqual(const SelfType& other) const;
755 
756  void copy(const SelfType& other);
757 
758 private:
759  Value::ObjectValues::iterator current_;
760  // Indicates that iterator is for a null value.
761  bool isNull_;
762 
763 public:
764  // For some reason, BORLAND needs these at the end, rather
765  // than earlier. No idea why.
767  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
768 };
769 
774  friend class Value;
775 
776 public:
777  typedef const Value value_type;
778  //typedef unsigned int size_t;
779  //typedef int difference_type;
780  typedef const Value& reference;
781  typedef const Value* pointer;
783 
785  ValueConstIterator(ValueIterator const& other);
786 
787 private:
790  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
791 public:
792  SelfType& operator=(const ValueIteratorBase& other);
793 
794  SelfType operator++(int) {
795  SelfType temp(*this);
796  ++*this;
797  return temp;
798  }
799 
800  SelfType operator--(int) {
801  SelfType temp(*this);
802  --*this;
803  return temp;
804  }
805 
806  SelfType& operator--() {
807  decrement();
808  return *this;
809  }
810 
811  SelfType& operator++() {
812  increment();
813  return *this;
814  }
815 
816  reference operator*() const { return deref(); }
817 
818  pointer operator->() const { return &deref(); }
819 };
820 
824  friend class Value;
825 
826 public:
827  typedef Value value_type;
828  typedef unsigned int size_t;
829  typedef int difference_type;
830  typedef Value& reference;
831  typedef Value* pointer;
833 
834  ValueIterator();
835  explicit ValueIterator(const ValueConstIterator& other);
836  ValueIterator(const ValueIterator& other);
837 
838 private:
841  explicit ValueIterator(const Value::ObjectValues::iterator& current);
842 public:
843  SelfType& operator=(const SelfType& other);
844 
845  SelfType operator++(int) {
846  SelfType temp(*this);
847  ++*this;
848  return temp;
849  }
850 
851  SelfType operator--(int) {
852  SelfType temp(*this);
853  --*this;
854  return temp;
855  }
856 
857  SelfType& operator--() {
858  decrement();
859  return *this;
860  }
861 
862  SelfType& operator++() {
863  increment();
864  return *this;
865  }
866 
867  reference operator*() const { return deref(); }
868 
869  pointer operator->() const { return &deref(); }
870 };
871 
872 } // namespace Json
873 
874 
875 namespace std {
877 template<>
878 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
879 }
880 
881 #pragma pack(pop)
882 
883 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
884 #pragma warning(pop)
885 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
886 
887 #endif // CPPTL_JSON_H_INCLUDED
#define JSONCPP_OVERRIDE
Definition: config.h:94
#define JSONCPP_DEPRECATED(message)
Definition: config.h:135
Int64 LargestInt
Definition: config.h:168
difference_type computeDistance(const SelfType &other) const
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:54
static const Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
Definition: value.h:215
unsigned int ArrayIndex
Definition: forwards.h:23
bool operator!=(const SelfType &other) const
Definition: value.h:717
base class for Value iterators.
Definition: value.h:708
array value (ordered list)
Definition: value.h:100
unsigned __int64 UInt64
Definition: config.h:163
reference operator*() const
Definition: value.h:816
unsigned integer value
Definition: value.h:96
#define JSONCPP_STRING
Definition: config.h:179
bool operator==(const SelfType &other) const
Definition: value.h:715
Json::ArrayIndex ArrayIndex
Definition: value.h:191
Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Definition: value.h:81
const Value value_type
Definition: value.h:777
object value (collection of name/value pairs).
Definition: value.h:101
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition: value.h:207
STL namespace.
static const Value & null
We regret this reference to a global instance; prefer the simpler Value().
Definition: value.h:193
Lightweight wrapper to tag static string.
Definition: value.h:131
static const UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition: value.h:209
pointer operator->() const
Definition: value.h:818
static const Value & nullRef
just a kludge for binary-compatibility; same as null
Definition: value.h:194
Json::LargestUInt LargestUInt
Definition: value.h:190
pointer operator->() const
Definition: value.h:869
const iterator for object and array value.
Definition: value.h:773
unsigned int size_t
Definition: value.h:828
#define JSONCPP_NORETURN
Definition: value.h:30
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:645
static const LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
Definition: value.h:198
SelfType & operator--()
Definition: value.h:806
&#39;null&#39; value
Definition: value.h:94
CommentPlacement
Definition: value.h:104
SelfType & operator--()
Definition: value.h:857
Value value_type
Definition: value.h:827
StaticString(const char *czstring)
Definition: value.h:133
#define JSONCPP_NOEXCEPT
Definition: config.h:95
ValueConstIterator SelfType
Definition: value.h:782
UInt64 LargestUInt
Definition: config.h:169
ValueConstIterator const_iterator
Definition: value.h:182
std::string msg_
Definition: value.h:61
JSON (JavaScript Object Notation).
Definition: allocator.h:14
ValueIteratorBase SelfType
Definition: value.h:713
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:85
Json::Int64 Int64
Definition: value.h:187
ValueIterator SelfType
Definition: value.h:832
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:542
Experimental and untested: represents a "path" to access a node.
Definition: value.h:676
SelfType operator--(int)
Definition: value.h:800
Json::LargestInt LargestInt
Definition: value.h:189
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition: value.h:217
const char * c_str() const
Definition: value.h:137
double value
Definition: value.h:97
SelfType operator--(int)
Definition: value.h:851
Json::UInt UInt
Definition: value.h:183
SelfType & operator++()
Definition: value.h:862
Json::UInt64 UInt64
Definition: value.h:186
Json::Int Int
Definition: value.h:184
Value * pointer
Definition: value.h:831
Represents a JSON value.
Definition: value.h:177
std::bidirectional_iterator_tag iterator_category
Definition: value.h:710
reference operator*() const
Definition: value.h:867
ValueIterator iterator
Definition: value.h:181
const Value * pointer
Definition: value.h:781
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:205
Exceptions which the user cannot easily avoid.
Definition: value.h:70
const Value & reference
Definition: value.h:780
a comment on the line after a value (only make sense for
Definition: value.h:107
unsigned int UInt
Definition: config.h:154
Iterator for object and array value.
Definition: value.h:823
SelfType & operator++()
Definition: value.h:811
__int64 Int64
Definition: config.h:162
SelfType operator++(int)
Definition: value.h:794
difference_type operator-(const SelfType &other) const
Definition: value.h:719
ValueType
used internally
Definition: value.h:93
std::vector< std::string > Members
Definition: value.h:180
bool value
Definition: value.h:99
signed integer value
Definition: value.h:95
SelfType operator++(int)
Definition: value.h:845
unsigned int size_t
Definition: value.h:711
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:90
int Int
Definition: config.h:153
a comment placed on the line before a value
Definition: value.h:105
UTF-8 string value.
Definition: value.h:98
a comment just after a value on the same line
Definition: value.h:106
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: value.h:878
Base class for all exceptions we throw.
Definition: value.h:55
Value & reference
Definition: value.h:830
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:200
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:202