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 Value const& nullSingleton();
194 
196  static const LargestInt minLargestInt;
198  static const LargestInt maxLargestInt;
200  static const LargestUInt maxLargestUInt;
201 
203  static const Int minInt;
205  static const Int maxInt;
207  static const UInt maxUInt;
208 
209 #if defined(JSON_HAS_INT64)
210  static const Int64 minInt64;
213  static const Int64 maxInt64;
215  static const UInt64 maxUInt64;
216 #endif // defined(JSON_HAS_INT64)
217 
218 private:
219 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
220  class CZString {
221  public:
222  enum DuplicationPolicy {
223  noDuplication = 0,
224  duplicate,
225  duplicateOnCopy
226  };
227  CZString(ArrayIndex index);
228  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
229  CZString(CZString const& other);
230 #if JSON_HAS_RVALUE_REFERENCES
231  CZString(CZString&& other);
232 #endif
233  ~CZString();
234  CZString& operator=(CZString other);
235  bool operator<(CZString const& other) const;
236  bool operator==(CZString const& other) const;
237  ArrayIndex index() const;
238  //const char* c_str() const; ///< \deprecated
239  char const* data() const;
240  unsigned length() const;
241  bool isStaticString() const;
242 
243  private:
244  void swap(CZString& other);
245 
246  struct StringStorage {
247  unsigned policy_: 2;
248  unsigned length_: 30; // 1GB max
249  };
250 
251  char const* cstr_; // actually, a prefixed string, unless policy is noDup
252  union {
253  ArrayIndex index_;
254  StringStorage storage_;
255  };
256  };
257 
258 public:
259 #ifndef JSON_USE_CPPTL_SMALLMAP
260  typedef std::map<CZString, Value> ObjectValues;
261 #else
262  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
263 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
264 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
265 
266 public:
282  Value(ValueType type = nullValue);
283  Value(Int value);
284  Value(UInt value);
285 #if defined(JSON_HAS_INT64)
286  Value(Int64 value);
287  Value(UInt64 value);
288 #endif // if defined(JSON_HAS_INT64)
289  Value(double value);
290  Value(const char* value);
291  Value(const char* begin, const char* end);
292 
307  Value(const StaticString& value);
308  Value(const JSONCPP_STRING& value);
309 #ifdef JSON_USE_CPPTL
310  Value(const CppTL::ConstString& value);
311 #endif
312  Value(bool value);
314  Value(const Value& other);
315 #if JSON_HAS_RVALUE_REFERENCES
316  Value(Value&& other);
318 #endif
319  ~Value();
320 
323  Value& operator=(Value other);
325  void swap(Value& other);
327  void swapPayload(Value& other);
328 
329  ValueType type() const;
330 
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  bool operator==(const Value& other) const;
337  bool operator!=(const Value& other) const;
338  int compare(const Value& other) const;
339 
340  const char* asCString() const;
341 #if JSONCPP_USING_SECURE_MEMORY
342  unsigned getCStringLength() const; //Allows you to understand the length of the CString
343 #endif
344  JSONCPP_STRING asString() const;
345 
348  bool getString(
349  char const** begin, char const** end) const;
350 #ifdef JSON_USE_CPPTL
351  CppTL::ConstString asConstString() const;
352 #endif
353  Int asInt() const;
354  UInt asUInt() const;
355 #if defined(JSON_HAS_INT64)
356  Int64 asInt64() const;
357  UInt64 asUInt64() const;
358 #endif // if defined(JSON_HAS_INT64)
359  LargestInt asLargestInt() const;
360  LargestUInt asLargestUInt() const;
361  float asFloat() const;
362  double asDouble() const;
363  bool asBool() const;
364 
365  bool isNull() const;
366  bool isBool() const;
367  bool isInt() const;
368  bool isInt64() const;
369  bool isUInt() const;
370  bool isUInt64() const;
371  bool isIntegral() const;
372  bool isDouble() const;
373  bool isNumeric() const;
374  bool isString() const;
375  bool isArray() const;
376  bool isObject() const;
377 
378  bool isConvertibleTo(ValueType other) const;
379 
381  ArrayIndex size() const;
382 
385  bool empty() const;
386 
388  bool operator!() const;
389 
393  void clear();
394 
400  void resize(ArrayIndex size);
401 
408  Value& operator[](ArrayIndex index);
409 
416  Value& operator[](int index);
417 
421  const Value& operator[](ArrayIndex index) const;
422 
426  const Value& operator[](int index) const;
427 
431  Value get(ArrayIndex index, const Value& defaultValue) const;
433  bool isValidIndex(ArrayIndex index) const;
437  Value& append(const Value& value);
438 
442  Value& operator[](const char* key);
445  const Value& operator[](const char* key) const;
448  Value& operator[](const JSONCPP_STRING& key);
452  const Value& operator[](const JSONCPP_STRING& key) const;
465  Value& operator[](const StaticString& key);
466 #ifdef JSON_USE_CPPTL
467  Value& operator[](const CppTL::ConstString& key);
471  const Value& operator[](const CppTL::ConstString& key) const;
472 #endif
473  Value get(const char* key, const Value& defaultValue) const;
479  Value get(const char* begin, const char* end, const Value& defaultValue) const;
483  Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
484 #ifdef JSON_USE_CPPTL
485  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
488 #endif
489  Value const* find(char const* begin, char const* end) const;
496  Value const* demand(char const* begin, char const* end);
504  Value removeMember(const char* key);
508  Value removeMember(const JSONCPP_STRING& key);
511  bool removeMember(const char* key, Value* removed);
518  bool removeMember(JSONCPP_STRING const& key, Value* removed);
520  bool removeMember(const char* begin, const char* end, Value* removed);
527  bool removeIndex(ArrayIndex i, Value* removed);
528 
531  bool isMember(const char* key) const;
534  bool isMember(const JSONCPP_STRING& key) const;
536  bool isMember(const char* begin, const char* end) const;
537 #ifdef JSON_USE_CPPTL
538  bool isMember(const CppTL::ConstString& key) const;
540 #endif
541 
547  Members getMemberNames() const;
548 
549  //# ifdef JSON_USE_CPPTL
550  // EnumMemberNames enumMemberNames() const;
551  // EnumValues enumValues() const;
552  //# endif
553 
555  JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
556  void setComment(const char* comment, CommentPlacement placement);
558  void setComment(const char* comment, size_t len, CommentPlacement placement);
560  void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
561  bool hasComment(CommentPlacement placement) const;
563  JSONCPP_STRING getComment(CommentPlacement placement) const;
564 
565  JSONCPP_STRING toStyledString() const;
566 
567  const_iterator begin() const;
568  const_iterator end() const;
569 
570  iterator begin();
571  iterator end();
572 
573  // Accessors for the [start, limit) range of bytes within the JSON text from
574  // which this value was parsed, if any.
575  void setOffsetStart(ptrdiff_t start);
576  void setOffsetLimit(ptrdiff_t limit);
577  ptrdiff_t getOffsetStart() const;
578  ptrdiff_t getOffsetLimit() const;
579 
580 private:
581  void initBasic(ValueType type, bool allocated = false);
582 
583  Value& resolveReference(const char* key);
584  Value& resolveReference(const char* key, const char* end);
585 
586  struct CommentInfo {
587  CommentInfo();
588  ~CommentInfo();
589 
590  void setComment(const char* text, size_t len);
591 
592  char* comment_;
593  };
594 
595  // struct MemberNamesTransform
596  //{
597  // typedef const char *result_type;
598  // const char *operator()( const CZString &name ) const
599  // {
600  // return name.c_str();
601  // }
602  //};
603 
604  union ValueHolder {
605  LargestInt int_;
606  LargestUInt uint_;
607  double real_;
608  bool bool_;
609  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
610  ObjectValues* map_;
611  } value_;
612  ValueType type_ : 8;
613  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
614  // If not allocated_, string_ must be null-terminated.
615  CommentInfo* comments_;
616 
617  // [start, limit) byte offsets in the source JSON text from which this Value
618  // was extracted.
619  ptrdiff_t start_;
620  ptrdiff_t limit_;
621 };
622 
627 public:
628  friend class Path;
629 
630  PathArgument();
631  PathArgument(ArrayIndex index);
632  PathArgument(const char* key);
633  PathArgument(const JSONCPP_STRING& key);
634 
635 private:
636  enum Kind {
637  kindNone = 0,
638  kindIndex,
639  kindKey
640  };
641  JSONCPP_STRING key_;
642  ArrayIndex index_;
643  Kind kind_;
644 };
645 
657 class JSON_API Path {
658 public:
659  Path(const JSONCPP_STRING& path,
660  const PathArgument& a1 = PathArgument(),
661  const PathArgument& a2 = PathArgument(),
662  const PathArgument& a3 = PathArgument(),
663  const PathArgument& a4 = PathArgument(),
664  const PathArgument& a5 = PathArgument());
665 
666  const Value& resolve(const Value& root) const;
667  Value resolve(const Value& root, const Value& defaultValue) const;
670  Value& make(Value& root) const;
671 
672 private:
673  typedef std::vector<const PathArgument*> InArgs;
674  typedef std::vector<PathArgument> Args;
675 
676  void makePath(const JSONCPP_STRING& path, const InArgs& in);
677  void addPathInArg(const JSONCPP_STRING& path,
678  const InArgs& in,
679  InArgs::const_iterator& itInArg,
680  PathArgument::Kind kind);
681  void invalidPath(const JSONCPP_STRING& path, int location);
682 
683  Args args_;
684 };
685 
690 public:
691  typedef std::bidirectional_iterator_tag iterator_category;
692  typedef unsigned int size_t;
693  typedef int difference_type;
695 
696  bool operator==(const SelfType& other) const { return isEqual(other); }
697 
698  bool operator!=(const SelfType& other) const { return !isEqual(other); }
699 
700  difference_type operator-(const SelfType& other) const {
701  return other.computeDistance(*this);
702  }
703 
706  Value key() const;
707 
709  UInt index() const;
710 
714  JSONCPP_STRING name() const;
715 
719  JSONCPP_DEPRECATED("Use `key = name();` instead.")
720  char const* memberName() const;
724  char const* memberName(char const** end) const;
725 
726 protected:
727  Value& deref() const;
728 
729  void increment();
730 
731  void decrement();
732 
733  difference_type computeDistance(const SelfType& other) const;
734 
735  bool isEqual(const SelfType& other) const;
736 
737  void copy(const SelfType& other);
738 
739 private:
740  Value::ObjectValues::iterator current_;
741  // Indicates that iterator is for a null value.
742  bool isNull_;
743 
744 public:
745  // For some reason, BORLAND needs these at the end, rather
746  // than earlier. No idea why.
748  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
749 };
750 
755  friend class Value;
756 
757 public:
758  typedef const Value value_type;
759  //typedef unsigned int size_t;
760  //typedef int difference_type;
761  typedef const Value& reference;
762  typedef const Value* pointer;
764 
766  ValueConstIterator(ValueIterator const& other);
767 
768 private:
771  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
772 public:
773  SelfType& operator=(const ValueIteratorBase& other);
774 
775  SelfType operator++(int) {
776  SelfType temp(*this);
777  ++*this;
778  return temp;
779  }
780 
781  SelfType operator--(int) {
782  SelfType temp(*this);
783  --*this;
784  return temp;
785  }
786 
787  SelfType& operator--() {
788  decrement();
789  return *this;
790  }
791 
792  SelfType& operator++() {
793  increment();
794  return *this;
795  }
796 
797  reference operator*() const { return deref(); }
798 
799  pointer operator->() const { return &deref(); }
800 };
801 
805  friend class Value;
806 
807 public:
808  typedef Value value_type;
809  typedef unsigned int size_t;
810  typedef int difference_type;
811  typedef Value& reference;
812  typedef Value* pointer;
814 
815  ValueIterator();
816  explicit ValueIterator(const ValueConstIterator& other);
817  ValueIterator(const ValueIterator& other);
818 
819 private:
822  explicit ValueIterator(const Value::ObjectValues::iterator& current);
823 public:
824  SelfType& operator=(const SelfType& other);
825 
826  SelfType operator++(int) {
827  SelfType temp(*this);
828  ++*this;
829  return temp;
830  }
831 
832  SelfType operator--(int) {
833  SelfType temp(*this);
834  --*this;
835  return temp;
836  }
837 
838  SelfType& operator--() {
839  decrement();
840  return *this;
841  }
842 
843  SelfType& operator++() {
844  increment();
845  return *this;
846  }
847 
848  reference operator*() const { return deref(); }
849 
850  pointer operator->() const { return &deref(); }
851 };
852 
853 } // namespace Json
854 
855 
856 namespace std {
858 template<>
859 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
860 }
861 
862 
863 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
864 #pragma warning(pop)
865 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
866 
867 #endif // CPPTL_JSON_H_INCLUDED
#define JSONCPP_OVERRIDE
Definition: config.h:88
#define JSONCPP_DEPRECATED(message)
Definition: config.h:125
Int64 LargestInt
Definition: config.h:158
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:53
static const Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
Definition: value.h:213
unsigned int ArrayIndex
Definition: forwards.h:23
bool operator!=(const SelfType &other) const
Definition: value.h:698
base class for Value iterators.
Definition: value.h:689
array value (ordered list)
Definition: value.h:98
unsigned __int64 UInt64
Definition: config.h:153
reference operator*() const
Definition: value.h:797
unsigned integer value
Definition: value.h:94
#define JSONCPP_STRING
Definition: config.h:169
bool operator==(const SelfType &other) const
Definition: value.h:696
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:758
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:205
STL namespace.
static const Value & null
We regret this reference to a global instance; prefer the simpler Value().
Definition: value.h:191
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:207
pointer operator->() const
Definition: value.h:799
static const Value & nullRef
just a kludge for binary-compatibility; same as null
Definition: value.h:192
Json::LargestUInt LargestUInt
Definition: value.h:188
pointer operator->() const
Definition: value.h:850
const iterator for object and array value.
Definition: value.h:754
unsigned int size_t
Definition: value.h:809
#define JSONCPP_NORETURN
Definition: value.h:30
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:626
static const LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
Definition: value.h:196
SelfType & operator--()
Definition: value.h:787
&#39;null&#39; value
Definition: value.h:92
CommentPlacement
Definition: value.h:102
SelfType & operator--()
Definition: value.h:838
Value value_type
Definition: value.h:808
StaticString(const char *czstring)
Definition: value.h:131
ValueConstIterator SelfType
Definition: value.h:763
UInt64 LargestUInt
Definition: config.h:159
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:694
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:83
Json::Int64 Int64
Definition: value.h:185
ValueIterator SelfType
Definition: value.h:813
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:524
Experimental and untested: represents a "path" to access a node.
Definition: value.h:657
SelfType operator--(int)
Definition: value.h:781
Json::LargestInt LargestInt
Definition: value.h:187
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition: value.h:215
const char * c_str() const
Definition: value.h:135
double value
Definition: value.h:95
SelfType operator--(int)
Definition: value.h:832
Json::UInt UInt
Definition: value.h:181
SelfType & operator++()
Definition: value.h:843
Json::UInt64 UInt64
Definition: value.h:184
Json::Int Int
Definition: value.h:182
Value * pointer
Definition: value.h:812
Represents a JSON value.
Definition: value.h:175
std::bidirectional_iterator_tag iterator_category
Definition: value.h:691
reference operator*() const
Definition: value.h:848
ValueIterator iterator
Definition: value.h:179
const Value * pointer
Definition: value.h:762
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:203
Exceptions which the user cannot easily avoid.
Definition: value.h:68
const Value & reference
Definition: value.h:761
a comment on the line after a value (only make sense for
Definition: value.h:105
unsigned int UInt
Definition: config.h:144
Iterator for object and array value.
Definition: value.h:804
SelfType & operator++()
Definition: value.h:792
__int64 Int64
Definition: config.h:152
SelfType operator++(int)
Definition: value.h:775
difference_type operator-(const SelfType &other) const
Definition: value.h:700
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:826
unsigned int size_t
Definition: value.h:692
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:88
int Int
Definition: config.h:143
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
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: value.h:859
Base class for all exceptions we throw.
Definition: value.h:53
Value & reference
Definition: value.h:811
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:198
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:200