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 : public std::exception {
41 public:
42  Exception(std::string const& msg);
43  virtual ~Exception() throw();
44  virtual char const* what() const throw();
45 protected:
46  std::string const msg_;
47 };
48 
56 public:
57  RuntimeError(std::string const& msg);
58 };
59 
66 class JSON_API LogicError : public Exception {
67 public:
68  LogicError(std::string const& msg);
69 };
70 
72 void throwRuntimeError(std::string const& msg);
74 void throwLogicError(std::string const& msg);
75 
78 enum ValueType {
79  nullValue = 0,
87 };
88 
95 };
96 
97 //# ifdef JSON_USE_CPPTL
98 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
99 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
100 //# endif
101 
117 public:
118  explicit StaticString(const char* czstring) : c_str_(czstring) {}
119 
120  operator const char*() const { return c_str_; }
121 
122  const char* c_str() const { return c_str_; }
123 
124 private:
125  const char* c_str_;
126 };
127 
163  friend class ValueIteratorBase;
164 public:
165  typedef std::vector<std::string> Members;
168  typedef Json::UInt UInt;
169  typedef Json::Int Int;
170 #if defined(JSON_HAS_INT64)
173 #endif // defined(JSON_HAS_INT64)
177 
178  static const Value& null;
179  static const Value& nullRef;
180  static const LargestInt minLargestInt;
183  static const LargestInt maxLargestInt;
186 
188  static const Int minInt;
190  static const Int maxInt;
192  static const UInt maxUInt;
193 
194 #if defined(JSON_HAS_INT64)
195  static const Int64 minInt64;
198  static const Int64 maxInt64;
200  static const UInt64 maxUInt64;
201 #endif // defined(JSON_HAS_INT64)
202 
203 private:
204 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
205  class CZString {
206  public:
207  enum DuplicationPolicy {
208  noDuplication = 0,
209  duplicate,
210  duplicateOnCopy
211  };
212  CZString(ArrayIndex index);
213  CZString(char const* str, unsigned length, DuplicationPolicy allocate);
214  CZString(CZString const& other);
215  ~CZString();
216  CZString& operator=(CZString other);
217  bool operator<(CZString const& other) const;
218  bool operator==(CZString const& other) const;
219  ArrayIndex index() const;
220  //const char* c_str() const; ///< \deprecated
221  char const* data() const;
222  unsigned length() const;
223  bool isStaticString() const;
224 
225  private:
226  void swap(CZString& other);
227 
228  struct StringStorage {
229  unsigned policy_: 2;
230  unsigned length_: 30; // 1GB max
231  };
232 
233  char const* cstr_; // actually, a prefixed string, unless policy is noDup
234  union {
235  ArrayIndex index_;
236  StringStorage storage_;
237  };
238  };
239 
240 public:
241 #ifndef JSON_USE_CPPTL_SMALLMAP
242  typedef std::map<CZString, Value> ObjectValues;
243 #else
244  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
245 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
246 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
247 
248 public:
264  Value(ValueType type = nullValue);
265  Value(Int value);
266  Value(UInt value);
267 #if defined(JSON_HAS_INT64)
268  Value(Int64 value);
269  Value(UInt64 value);
270 #endif // if defined(JSON_HAS_INT64)
271  Value(double value);
272  Value(const char* value);
273  Value(const char* begin, const char* end);
274 
289  Value(const StaticString& value);
290  Value(const std::string& value);
291 #ifdef JSON_USE_CPPTL
292  Value(const CppTL::ConstString& value);
293 #endif
294  Value(bool value);
296  Value(const Value& other);
297  ~Value();
298 
301  Value& operator=(Value other);
303  void swap(Value& other);
305  void swapPayload(Value& other);
306 
307  ValueType type() const;
308 
310  bool operator<(const Value& other) const;
311  bool operator<=(const Value& other) const;
312  bool operator>=(const Value& other) const;
313  bool operator>(const Value& other) const;
314  bool operator==(const Value& other) const;
315  bool operator!=(const Value& other) const;
316  int compare(const Value& other) const;
317 
318  const char* asCString() const;
319  std::string asString() const;
320 
323  bool getString(
324  char const** begin, char const** end) const;
325 #ifdef JSON_USE_CPPTL
326  CppTL::ConstString asConstString() const;
327 #endif
328  Int asInt() const;
329  UInt asUInt() const;
330 #if defined(JSON_HAS_INT64)
331  Int64 asInt64() const;
332  UInt64 asUInt64() const;
333 #endif // if defined(JSON_HAS_INT64)
334  LargestInt asLargestInt() const;
335  LargestUInt asLargestUInt() const;
336  float asFloat() const;
337  double asDouble() const;
338  bool asBool() const;
339 
340  bool isNull() const;
341  bool isBool() const;
342  bool isInt() const;
343  bool isInt64() const;
344  bool isUInt() const;
345  bool isUInt64() const;
346  bool isIntegral() const;
347  bool isDouble() const;
348  bool isNumeric() const;
349  bool isString() const;
350  bool isArray() const;
351  bool isObject() const;
352 
353  bool isConvertibleTo(ValueType other) const;
354 
356  ArrayIndex size() const;
357 
360  bool empty() const;
361 
363  bool operator!() const;
364 
368  void clear();
369 
375  void resize(ArrayIndex size);
376 
383  Value& operator[](ArrayIndex index);
384 
391  Value& operator[](int index);
392 
396  const Value& operator[](ArrayIndex index) const;
397 
401  const Value& operator[](int index) const;
402 
406  Value get(ArrayIndex index, const Value& defaultValue) const;
408  bool isValidIndex(ArrayIndex index) const;
412  Value& append(const Value& value);
413 
417  Value& operator[](const char* key);
420  const Value& operator[](const char* key) const;
423  Value& operator[](const std::string& key);
427  const Value& operator[](const std::string& key) const;
440  Value& operator[](const StaticString& key);
441 #ifdef JSON_USE_CPPTL
442  Value& operator[](const CppTL::ConstString& key);
446  const Value& operator[](const CppTL::ConstString& key) const;
447 #endif
448  Value get(const char* key, const Value& defaultValue) const;
454  Value get(const char* begin, const char* end, const Value& defaultValue) const;
458  Value get(const std::string& key, const Value& defaultValue) const;
459 #ifdef JSON_USE_CPPTL
460  Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
463 #endif
464  Value const* find(char const* begin, char const* end) const;
471  Value const* demand(char const* begin, char const* end);
479  Value removeMember(const char* key);
483  Value removeMember(const std::string& key);
486  bool removeMember(const char* key, Value* removed);
493  bool removeMember(std::string const& key, Value* removed);
495  bool removeMember(const char* begin, const char* end, Value* removed);
502  bool removeIndex(ArrayIndex i, Value* removed);
503 
506  bool isMember(const char* key) const;
509  bool isMember(const std::string& key) const;
511  bool isMember(const char* begin, const char* end) const;
512 #ifdef JSON_USE_CPPTL
513  bool isMember(const CppTL::ConstString& key) const;
515 #endif
516 
522  Members getMemberNames() const;
523 
524  //# ifdef JSON_USE_CPPTL
525  // EnumMemberNames enumMemberNames() const;
526  // EnumValues enumValues() const;
527  //# endif
528 
530  JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
531  void setComment(const char* comment, CommentPlacement placement);
533  void setComment(const char* comment, size_t len, CommentPlacement placement);
535  void setComment(const std::string& comment, CommentPlacement placement);
536  bool hasComment(CommentPlacement placement) const;
538  std::string getComment(CommentPlacement placement) const;
539 
540  std::string toStyledString() const;
541 
542  const_iterator begin() const;
543  const_iterator end() const;
544 
545  iterator begin();
546  iterator end();
547 
548  // Accessors for the [start, limit) range of bytes within the JSON text from
549  // which this value was parsed, if any.
550  void setOffsetStart(size_t start);
551  void setOffsetLimit(size_t limit);
552  size_t getOffsetStart() const;
553  size_t getOffsetLimit() const;
554 
555 private:
556  void initBasic(ValueType type, bool allocated = false);
557 
558  Value& resolveReference(const char* key);
559  Value& resolveReference(const char* key, const char* end);
560 
561  struct CommentInfo {
562  CommentInfo();
563  ~CommentInfo();
564 
565  void setComment(const char* text, size_t len);
566 
567  char* comment_;
568  };
569 
570  // struct MemberNamesTransform
571  //{
572  // typedef const char *result_type;
573  // const char *operator()( const CZString &name ) const
574  // {
575  // return name.c_str();
576  // }
577  //};
578 
579  union ValueHolder {
580  LargestInt int_;
581  LargestUInt uint_;
582  double real_;
583  bool bool_;
584  char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
585  ObjectValues* map_;
586  } value_;
587  ValueType type_ : 8;
588  unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
589  // If not allocated_, string_ must be null-terminated.
590  CommentInfo* comments_;
591 
592  // [start, limit) byte offsets in the source JSON text from which this Value
593  // was extracted.
594  size_t start_;
595  size_t limit_;
596 };
597 
602 public:
603  friend class Path;
604 
605  PathArgument();
606  PathArgument(ArrayIndex index);
607  PathArgument(const char* key);
608  PathArgument(const std::string& key);
609 
610 private:
611  enum Kind {
612  kindNone = 0,
613  kindIndex,
614  kindKey
615  };
616  std::string key_;
617  ArrayIndex index_;
618  Kind kind_;
619 };
620 
632 class JSON_API Path {
633 public:
634  Path(const std::string& path,
635  const PathArgument& a1 = PathArgument(),
636  const PathArgument& a2 = PathArgument(),
637  const PathArgument& a3 = PathArgument(),
638  const PathArgument& a4 = PathArgument(),
639  const PathArgument& a5 = PathArgument());
640 
641  const Value& resolve(const Value& root) const;
642  Value resolve(const Value& root, const Value& defaultValue) const;
645  Value& make(Value& root) const;
646 
647 private:
648  typedef std::vector<const PathArgument*> InArgs;
649  typedef std::vector<PathArgument> Args;
650 
651  void makePath(const std::string& path, const InArgs& in);
652  void addPathInArg(const std::string& path,
653  const InArgs& in,
654  InArgs::const_iterator& itInArg,
655  PathArgument::Kind kind);
656  void invalidPath(const std::string& path, int location);
657 
658  Args args_;
659 };
660 
665 public:
666  typedef std::bidirectional_iterator_tag iterator_category;
667  typedef unsigned int size_t;
668  typedef int difference_type;
670 
671  bool operator==(const SelfType& other) const { return isEqual(other); }
672 
673  bool operator!=(const SelfType& other) const { return !isEqual(other); }
674 
675  difference_type operator-(const SelfType& other) const {
676  return other.computeDistance(*this);
677  }
678 
681  Value key() const;
682 
684  UInt index() const;
685 
689  std::string name() const;
690 
694  JSONCPP_DEPRECATED("Use `key = name();` instead.")
695  char const* memberName() const;
699  char const* memberName(char const** end) const;
700 
701 protected:
702  Value& deref() const;
703 
704  void increment();
705 
706  void decrement();
707 
708  difference_type computeDistance(const SelfType& other) const;
709 
710  bool isEqual(const SelfType& other) const;
711 
712  void copy(const SelfType& other);
713 
714 private:
715  Value::ObjectValues::iterator current_;
716  // Indicates that iterator is for a null value.
717  bool isNull_;
718 
719 public:
720  // For some reason, BORLAND needs these at the end, rather
721  // than earlier. No idea why.
723  explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
724 };
725 
729 class JSON_API ValueConstIterator : public ValueIteratorBase {
730  friend class Value;
731 
732 public:
733  typedef const Value value_type;
734  //typedef unsigned int size_t;
735  //typedef int difference_type;
736  typedef const Value& reference;
737  typedef const Value* pointer;
739 
741 
742 private:
745  explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
746 public:
747  SelfType& operator=(const ValueIteratorBase& other);
748 
749  SelfType operator++(int) {
750  SelfType temp(*this);
751  ++*this;
752  return temp;
753  }
754 
755  SelfType operator--(int) {
756  SelfType temp(*this);
757  --*this;
758  return temp;
759  }
760 
761  SelfType& operator--() {
762  decrement();
763  return *this;
764  }
765 
766  SelfType& operator++() {
767  increment();
768  return *this;
769  }
770 
771  reference operator*() const { return deref(); }
772 
773  pointer operator->() const { return &deref(); }
774 };
775 
779  friend class Value;
780 
781 public:
782  typedef Value value_type;
783  typedef unsigned int size_t;
784  typedef int difference_type;
785  typedef Value& reference;
786  typedef Value* pointer;
788 
789  ValueIterator();
790  ValueIterator(const ValueConstIterator& other);
791  ValueIterator(const ValueIterator& other);
792 
793 private:
796  explicit ValueIterator(const Value::ObjectValues::iterator& current);
797 public:
798  SelfType& operator=(const SelfType& other);
799 
801  SelfType temp(*this);
802  ++*this;
803  return temp;
804  }
805 
807  SelfType temp(*this);
808  --*this;
809  return temp;
810  }
811 
813  decrement();
814  return *this;
815  }
816 
818  increment();
819  return *this;
820  }
821 
822  reference operator*() const { return deref(); }
823 
824  pointer operator->() const { return &deref(); }
825 };
826 
827 } // namespace Json
828 
829 
830 namespace std {
832 template<>
833 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
834 }
835 
836 
837 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
838 #pragma warning(pop)
839 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
840 
841 #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:824
#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:198
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:178
reference operator*() const
Definition: value.h:771
std::vector< std::string > Members
Definition: value.h:165
base class for Value iterators.
Definition: value.h:664
array value (ordered list)
Definition: value.h:85
unsigned __int64 UInt64
Definition: config.h:98
unsigned integer value
Definition: value.h:81
void throwLogicError(std::string const &msg)
used internally
Definition: json_value.cpp:174
Json::ArrayIndex ArrayIndex
Definition: value.h:176
Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Definition: value.h:66
const Value value_type
Definition: value.h:733
object value (collection of name/value pairs).
Definition: value.h:86
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition: value.h:190
Lightweight wrapper to tag static string.
Definition: value.h:116
static const UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition: value.h:192
Json::LargestUInt LargestUInt
Definition: value.h:175
difference_type computeDistance(const SelfType &other) const
bool operator!=(const SelfType &other) const
Definition: value.h:673
const iterator for object and array value.
Definition: value.h:729
unsigned int size_t
Definition: value.h:783
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:601
SelfType & operator--()
Definition: value.h:761
'null' value
Definition: value.h:79
CommentPlacement
Definition: value.h:89
SelfType & operator--()
Definition: value.h:812
Value value_type
Definition: value.h:782
StaticString(const char *czstring)
Definition: value.h:118
ValueConstIterator SelfType
Definition: value.h:738
static const Value & nullRef
just a kludge for binary-compatibility; same as null
Definition: value.h:179
UInt64 LargestUInt
Definition: config.h:104
ValueConstIterator const_iterator
Definition: value.h:167
ValueIteratorBase SelfType
Definition: value.h:669
Json::Int64 Int64
Definition: value.h:172
ValueIterator SelfType
Definition: value.h:787
void swap(Value &other)
Swap everything.
Definition: json_value.cpp:467
Experimental and untested: represents a "path" to access a node.
Definition: value.h:632
SelfType operator--(int)
Definition: value.h:755
Json::LargestInt LargestInt
Definition: value.h:174
const char * c_str() const
Definition: value.h:122
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition: value.h:200
double value
Definition: value.h:82
void throwRuntimeError(std::string const &msg)
used internally
Definition: json_value.cpp:170
SelfType operator--(int)
Definition: value.h:806
Json::UInt UInt
Definition: value.h:168
SelfType & operator++()
Definition: value.h:817
Json::UInt64 UInt64
Definition: value.h:171
Json::Int Int
Definition: value.h:169
Value * pointer
Definition: value.h:786
Represents a JSON value.
Definition: value.h:162
std::bidirectional_iterator_tag iterator_category
Definition: value.h:666
ValueIterator iterator
Definition: value.h:166
const Value * pointer
Definition: value.h:737
difference_type operator-(const SelfType &other) const
Definition: value.h:675
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition: value.h:188
reference operator*() const
Definition: value.h:822
Exceptions which the user cannot easily avoid.
Definition: value.h:55
const Value & reference
Definition: value.h:736
a comment on the line after a value (only make sense for
Definition: value.h:92
unsigned int UInt
Definition: config.h:89
Iterator for object and array value.
Definition: value.h:778
SelfType & operator++()
Definition: value.h:766
__int64 Int64
Definition: config.h:97
SelfType operator++(int)
Definition: value.h:749
ValueType
Type of the value held by a Value object.
Definition: value.h:78
bool value
Definition: value.h:84
signed integer value
Definition: value.h:80
SelfType operator++(int)
Definition: value.h:800
unsigned int size_t
Definition: value.h:667
int Int
Definition: config.h:88
a comment placed on the line before a value
Definition: value.h:90
UTF-8 string value.
Definition: value.h:83
std::string const msg_
Definition: value.h:46
a comment just after a value on the same line
Definition: value.h:91
Base class for all exceptions we throw.
Definition: value.h:40
bool operator==(const SelfType &other) const
Definition: value.h:671
pointer operator->() const
Definition: value.h:773
Value & reference
Definition: value.h:785
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition: value.h:183
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition: value.h:185