JsonCpp project page Classes Namespace 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  // Required for boost integration, e. g. BOOST_TEST
194  typedef std::string value_type;
195 
196  static const Value& null;
197  static const Value& nullRef;
198  static Value const& nullSingleton();
199 
201  static const LargestInt minLargestInt;
203  static const LargestInt maxLargestInt;
206 
208  static const Int minInt;
210  static const Int maxInt;
212  static const UInt maxUInt;
213 
214 #if defined(JSON_HAS_INT64)
215  static const Int64 minInt64;
218  static const Int64 maxInt64;
220  static const UInt64 maxUInt64;
221 #endif // defined(JSON_HAS_INT64)
222 
223 private:
224 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
225  class CZString {
226  public:
227  enum DuplicationPolicy {
228  noDuplication = 0,
229  duplicate,
230  duplicateOnCopy
231  };
232  CZString(ArrayIndex index);
233  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
234  CZString(CZString const& other);
235 #if JSON_HAS_RVALUE_REFERENCES
236  CZString(CZString&& other);
237 #endif
238  ~CZString();
239  CZString& operator=(const CZString& other);
240 
241 #if JSON_HAS_RVALUE_REFERENCES
242  CZString& operator=(CZString&& other);
243 #endif
244 
245  bool operator<(CZString const& other) const;
246  bool operator==(CZString const& other) const;
247  ArrayIndex index() const;
248  //const char* c_str() const; ///< \deprecated
249  char const* data() const;
250  unsigned length() const;
251  bool isStaticString() const;
252 
253  private:
254  void swap(CZString& other);
255 
256  struct StringStorage {
257  unsigned policy_: 2;
258  unsigned length_: 30; // 1GB max
259  };
260 
261  char const* cstr_; // actually, a prefixed string, unless policy is noDup
262  union {
263  ArrayIndex index_;
264  StringStorage storage_;
265  };
266  };
267 
268 public:
269 #ifndef JSON_USE_CPPTL_SMALLMAP
270  typedef std::map<CZString, Value> ObjectValues;
271 #else
272  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
273 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
274 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
275 
276 public:
292  Value(ValueType type = nullValue);
293  Value(Int value);
294  Value(UInt value);
295 #if defined(JSON_HAS_INT64)
296  Value(Int64 value);
297  Value(UInt64 value);
298 #endif // if defined(JSON_HAS_INT64)
299  Value(double value);
300  Value(const char* value);
301  Value(const char* begin, const char* end);
302 
317  Value(const StaticString& value);
318  Value(const JSONCPP_STRING& value);
319 #ifdef JSON_USE_CPPTL
320  Value(const CppTL::ConstString& value);
321 #endif
322  Value(bool value);
324  Value(const Value& other);
325 #if JSON_HAS_RVALUE_REFERENCES
326  Value(Value&& other);
328 #endif
329  ~Value();
330 
333  Value& operator=(Value other);
334 
336  void swap(Value& other);
338  void swapPayload(Value& other);
339 
341  void copy(const Value& other);
343  void copyPayload(const Value& other);
344 
345  ValueType type() const;
346 
348  bool operator<(const Value& other) const;
349  bool operator<=(const Value& other) const;
350  bool operator>=(const Value& other) const;
351  bool operator>(const Value& other) const;
352  bool operator==(const Value& other) const;
353  bool operator!=(const Value& other) const;
354  int compare(const Value& other) const;
355 
356  const char* asCString() const;
357 #if JSONCPP_USING_SECURE_MEMORY
358  unsigned getCStringLength() const; //Allows you to understand the length of the CString
359 #endif
360  JSONCPP_STRING asString() const;
361 
364  bool getString(
365  char const** begin, char const** end) const;
366 #ifdef JSON_USE_CPPTL
367  CppTL::ConstString asConstString() const;
368 #endif
369  Int asInt() const;
370  UInt asUInt() const;
371 #if defined(JSON_HAS_INT64)
372  Int64 asInt64() const;
373  UInt64 asUInt64() const;
374 #endif // if defined(JSON_HAS_INT64)
375  LargestInt asLargestInt() const;
376  LargestUInt asLargestUInt() const;
377  float asFloat() const;
378  double asDouble() const;
379  bool asBool() const;
380 
381  bool isNull() const;
382  bool isBool() const;
383  bool isInt() const;
384  bool isInt64() const;
385  bool isUInt() const;
386  bool isUInt64() const;
387  bool isIntegral() const;
388  bool isDouble() const;
389  bool isNumeric() const;
390  bool isString() const;
391  bool isArray() const;
392  bool isObject() const;
393 
394  bool isConvertibleTo(ValueType other) const;
395 
397  ArrayIndex size() const;
398 
401  bool empty() const;
402 
404  explicit operator bool() const;
405 
409  void clear();
410 
416  void resize(ArrayIndex size);
417 
424  Value& operator[](ArrayIndex index);
425 
432  Value& operator[](int index);
433 
437  const Value& operator[](ArrayIndex index) const;
438 
442  const Value& operator[](int index) const;
443 
447  Value get(ArrayIndex index, const Value& defaultValue) const;
449  bool isValidIndex(ArrayIndex index) const;
453  Value& append(const Value& value);
454 
455 #if JSON_HAS_RVALUE_REFERENCES
456  Value& append(Value&& value);
457 #endif
458 
462  Value& operator[](const char* key);
465  const Value& operator[](const char* key) const;
468  Value& operator[](const JSONCPP_STRING& key);
472  const Value& operator[](const JSONCPP_STRING& key) const;
485  Value& operator[](const StaticString& key);
486 #ifdef JSON_USE_CPPTL
487  Value& operator[](const CppTL::ConstString& key);
491  const Value& operator[](const CppTL::ConstString& key) const;
492 #endif
493  Value get(const char* key, const Value& defaultValue) const;
499  Value get(const char* begin, const char* end, const Value& defaultValue) const;
503  Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
504 #ifdef JSON_USE_CPPTL
505  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
508 #endif
509  Value const* find(char const* begin, char const* end) const;
516  Value const* demand(char const* begin, char const* end);
524  void removeMember(const char* key);
528  void removeMember(const JSONCPP_STRING& key);
531  bool removeMember(const char* key, Value* removed);
538  bool removeMember(JSONCPP_STRING const& key, Value* removed);
540  bool removeMember(const char* begin, const char* end, Value* removed);
547  bool removeIndex(ArrayIndex i, Value* removed);
548 
551  bool isMember(const char* key) const;
554  bool isMember(const JSONCPP_STRING& key) const;
556  bool isMember(const char* begin, const char* end) const;
557 #ifdef JSON_USE_CPPTL
558  bool isMember(const CppTL::ConstString& key) const;
560 #endif
561 
567  Members getMemberNames() const;
568 
569  //# ifdef JSON_USE_CPPTL
570  // EnumMemberNames enumMemberNames() const;
571  // EnumValues enumValues() const;
572  //# endif
573 
575  JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
576  void setComment(const char* comment, CommentPlacement placement);
578  void setComment(const char* comment, size_t len, CommentPlacement placement);
580  void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
581  bool hasComment(CommentPlacement placement) const;
583  JSONCPP_STRING getComment(CommentPlacement placement) const;
584 
585  JSONCPP_STRING toStyledString() const;
586 
587  const_iterator begin() const;
588  const_iterator end() const;
589 
590  iterator begin();
591  iterator end();
592 
593  // Accessors for the [start, limit) range of bytes within the JSON text from
594  // which this value was parsed, if any.
595  void setOffsetStart(ptrdiff_t start);
596  void setOffsetLimit(ptrdiff_t limit);
597  ptrdiff_t getOffsetStart() const;
598  ptrdiff_t getOffsetLimit() const;
599 
600 private:
601  void initBasic(ValueType type, bool allocated = false);
602 
603  Value& resolveReference(const char* key);
604  Value& resolveReference(const char* key, const char* end);
605 
606  struct CommentInfo {
607  CommentInfo();
608  ~CommentInfo();
609 
610  void setComment(const char* text, size_t len);
611 
612  char* comment_;
613  };
614 
615  // struct MemberNamesTransform
616  //{
617  // typedef const char *result_type;
618  // const char *operator()( const CZString &name ) const
619  // {
620  // return name.c_str();
621  // }
622  //};
623 
624  union ValueHolder {
625  LargestInt int_;
626  LargestUInt uint_;
627  double real_;
628  bool bool_;
629  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
630  ObjectValues* map_;
631  } value_;
632  ValueType type_ : 8;
633  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
634  // If not allocated_, string_ must be null-terminated.
635  CommentInfo* comments_;
636 
637  // [start, limit) byte offsets in the source JSON text from which this Value
638  // was extracted.
639  ptrdiff_t start_;
640  ptrdiff_t limit_;
641 };
642 
647 public:
648  friend class Path;
649 
650  PathArgument();
651  PathArgument(ArrayIndex index);
652  PathArgument(const char* key);
653  PathArgument(const JSONCPP_STRING& key);
654 
655 private:
656  enum Kind {
657  kindNone = 0,
658  kindIndex,
659  kindKey
660  };
661  JSONCPP_STRING key_;
662  ArrayIndex index_;
663  Kind kind_;
664 };
665 
677 class JSON_API Path {
678 public:
679  Path(const JSONCPP_STRING& path,
680  const PathArgument& a1 = PathArgument(),
681  const PathArgument& a2 = PathArgument(),
682  const PathArgument& a3 = PathArgument(),
683  const PathArgument& a4 = PathArgument(),
684  const PathArgument& a5 = PathArgument());
685 
686  const Value& resolve(const Value& root) const;
687  Value resolve(const Value& root, const Value& defaultValue) const;
690  Value& make(Value& root) const;
691 
692 private:
693  typedef std::vector<const PathArgument*> InArgs;
694  typedef std::vector<PathArgument> Args;
695 
696  void makePath(const JSONCPP_STRING& path, const InArgs& in);
697  void addPathInArg(const JSONCPP_STRING& path,
698  const InArgs& in,
699  InArgs::const_iterator& itInArg,
700  PathArgument::Kind kind);
701  void invalidPath(const JSONCPP_STRING& path, int location);
702 
703  Args args_;
704 };
705 
710 public:
711  typedef std::bidirectional_iterator_tag iterator_category;
712  typedef unsigned int size_t;
713  typedef int difference_type;
715 
716  bool operator==(const SelfType& other) const { return isEqual(other); }
717 
718  bool operator!=(const SelfType& other) const { return !isEqual(other); }
719 
720  difference_type operator-(const SelfType& other) const {
721  return other.computeDistance(*this);
722  }
723 
726  Value key() const;
727 
729  UInt index() const;
730 
734  JSONCPP_STRING name() const;
735 
739  JSONCPP_DEPRECATED("Use `key = name();` instead.")
740  char const* memberName() const;
744  char const* memberName(char const** end) const;
745 
746 protected:
747  Value& deref() const;
748 
749  void increment();
750 
751  void decrement();
752 
753  difference_type computeDistance(const SelfType& other) const;
754 
755  bool isEqual(const SelfType& other) const;
756 
757  void copy(const SelfType& other);
758 
759 private:
760  Value::ObjectValues::iterator current_;
761  // Indicates that iterator is for a null value.
762  bool isNull_;
763 
764 public:
765  // For some reason, BORLAND needs these at the end, rather
766  // than earlier. No idea why.
768  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
769 };
770 
775  friend class Value;
776 
777 public:
778  typedef const Value value_type;
779  //typedef unsigned int size_t;
780  //typedef int difference_type;
781  typedef const Value& reference;
782  typedef const Value* pointer;
784 
786  ValueConstIterator(ValueIterator const& other);
787 
788 private:
791  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
792 public:
793  SelfType& operator=(const ValueIteratorBase& other);
794 
796  SelfType temp(*this);
797  ++*this;
798  return temp;
799  }
800 
802  SelfType temp(*this);
803  --*this;
804  return temp;
805  }
806 
808  decrement();
809  return *this;
810  }
811 
813  increment();
814  return *this;
815  }
816 
817  reference operator*() const { return deref(); }
818 
819  pointer operator->() const { return &deref(); }
820 };
821 
825  friend class Value;
826 
827 public:
828  typedef Value value_type;
829  typedef unsigned int size_t;
830  typedef int difference_type;
831  typedef Value& reference;
832  typedef Value* pointer;
834 
835  ValueIterator();
836  explicit ValueIterator(const ValueConstIterator& other);
837  ValueIterator(const ValueIterator& other);
838 
839 private:
842  explicit ValueIterator(const Value::ObjectValues::iterator& current);
843 public:
844  SelfType& operator=(const SelfType& other);
845 
847  SelfType temp(*this);
848  ++*this;
849  return temp;
850  }
851 
853  SelfType temp(*this);
854  --*this;
855  return temp;
856  }
857 
859  decrement();
860  return *this;
861  }
862 
864  increment();
865  return *this;
866  }
867 
868  reference operator*() const { return deref(); }
869 
870  pointer operator->() const { return &deref(); }
871 };
872 
873 } // namespace Json
874 
875 
876 namespace std {
878 template<>
879 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
880 }
881 
882 #pragma pack(pop)
883 
884 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
885 #pragma warning(pop)
886 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
887 
888 #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 amalgamated 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:218
unsigned int ArrayIndex
Definition: forwards.h:23
bool operator!=(const SelfType &other) const
Definition: value.h:718
base class for Value iterators.
Definition: value.h:709
array value (ordered list)
Definition: value.h:100
unsigned __int64 UInt64
Definition: config.h:163
reference operator*() const
Definition: value.h:817
unsigned integer value
Definition: value.h:96
#define JSONCPP_STRING
Definition: config.h:179
bool operator==(const SelfType &other) const
Definition: value.h:716
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:778
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:210
STL namespace.
static const Value & null
We regret this reference to a global instance; prefer the simpler Value().
Definition: value.h:196
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:212
pointer operator->() const
Definition: value.h:819
static const Value & nullRef
just a kludge for binary-compatibility; same as null
Definition: value.h:197
Json::LargestUInt LargestUInt
Definition: value.h:190
pointer operator->() const
Definition: value.h:870
const iterator for object and array value.
Definition: value.h:774
unsigned int size_t
Definition: value.h:829
#define JSONCPP_NORETURN
Definition: value.h:30
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:646
static const LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
Definition: value.h:201
SelfType & operator--()
Definition: value.h:807
&#39;null&#39; value
Definition: value.h:94
CommentPlacement
Definition: value.h:104
SelfType & operator--()
Definition: value.h:858
Value value_type
Definition: value.h:828
StaticString(const char *czstring)
Definition: value.h:133
#define JSONCPP_NOEXCEPT
Definition: config.h:95
std::string value_type
Definition: value.h:194
ValueConstIterator SelfType
Definition: value.h:783
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:714
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:85
Json::Int64 Int64
Definition: value.h:187
ValueIterator SelfType
Definition: value.h:833
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:542
Experimental and untested: represents a "path" to access a node.
Definition: value.h:677
SelfType operator--(int)
Definition: value.h:801
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:220
const char * c_str() const
Definition: value.h:137
double value
Definition: value.h:97
SelfType operator--(int)
Definition: value.h:852
Json::UInt UInt
Definition: value.h:183
SelfType & operator++()
Definition: value.h:863
Json::UInt64 UInt64
Definition: value.h:186
Json::Int Int
Definition: value.h:184
Value * pointer
Definition: value.h:832
Represents a JSON value.
Definition: value.h:177
std::bidirectional_iterator_tag iterator_category
Definition: value.h:711
reference operator*() const
Definition: value.h:868
ValueIterator iterator
Definition: value.h:181
const Value * pointer
Definition: value.h:782
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:208
Exceptions which the user cannot easily avoid.
Definition: value.h:70
const Value & reference
Definition: value.h:781
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:824
SelfType & operator++()
Definition: value.h:812
__int64 Int64
Definition: config.h:162
SelfType operator++(int)
Definition: value.h:795
difference_type operator-(const SelfType &other) const
Definition: value.h:720
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:846
unsigned int size_t
Definition: value.h:712
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:879
Base class for all exceptions we throw.
Definition: value.h:55
Value & reference
Definition: value.h:831
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:203
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:205