Go to the documentation of this file.
6 #ifndef CPPTL_JSON_H_INCLUDED
7 #define CPPTL_JSON_H_INCLUDED
9 #if !defined(JSON_IS_AMALGAMATION)
11 #endif // if !defined(JSON_IS_AMALGAMATION)
16 #if !defined(JSONCPP_NORETURN)
17 #if defined(_MSC_VER) && _MSC_VER == 1800
18 #define JSONCPP_NORETURN __declspec(noreturn)
20 #define JSONCPP_NORETURN [[noreturn]]
30 #ifndef JSON_USE_CPPTL_SMALLMAP
33 #include <cpptl/smallmap.h>
36 #include <cpptl/forwards.h>
41 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
43 #pragma warning(disable : 4251)
44 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
52 #if JSON_USE_EXCEPTION
146 operator const char*()
const {
return c_str_; }
148 const char*
c_str()
const {
return c_str_; }
197 #if defined(JSON_HAS_INT64)
200 #endif // defined(JSON_HAS_INT64)
215 static Value const& nullSingleton();
232 #if defined(JSON_HAS_INT64)
239 #endif // defined(JSON_HAS_INT64)
240 static constexpr
UInt defaultRealPrecision = 17;
245 static constexpr
double maxUInt64AsDouble = 18446744073709551615.0;
254 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
257 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
259 CZString(
char const* str,
unsigned length, DuplicationPolicy allocate);
260 CZString(CZString
const& other);
261 CZString(CZString&& other);
263 CZString& operator=(
const CZString& other);
264 CZString& operator=(CZString&& other);
266 bool operator<(CZString
const& other)
const;
270 char const* data()
const;
271 unsigned length()
const;
272 bool isStaticString()
const;
275 void swap(CZString& other);
277 struct StringStorage {
278 unsigned policy_ : 2;
279 unsigned length_ : 30;
285 StringStorage storage_;
290 #ifndef JSON_USE_CPPTL_SMALLMAP
291 typedef std::map<CZString, Value> ObjectValues;
293 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
294 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
295 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
317 #if defined(JSON_HAS_INT64)
320 #endif // if defined(JSON_HAS_INT64)
322 Value(
const char* value);
323 Value(
const char* begin,
const char* end);
343 #ifdef JSON_USE_CPPTL
344 Value(
const CppTL::ConstString& value);
359 void swapPayload(
Value& other);
362 void copy(
const Value& other);
364 void copyPayload(
const Value& other);
369 bool operator<(
const Value& other)
const;
370 bool operator<=(
const Value& other)
const;
371 bool operator>=(
const Value& other)
const;
372 bool operator>(
const Value& other)
const;
375 int compare(
const Value& other)
const;
377 const char* asCString()
const;
378 #if JSONCPP_USING_SECURE_MEMORY
379 unsigned getCStringLength()
const;
386 bool getString(
char const** begin,
char const** end)
const;
387 #ifdef JSON_USE_CPPTL
388 CppTL::ConstString asConstString()
const;
392 #if defined(JSON_HAS_INT64)
393 Int64 asInt64()
const;
395 #endif // if defined(JSON_HAS_INT64)
398 float asFloat()
const;
399 double asDouble()
const;
405 bool isInt64()
const;
407 bool isUInt64()
const;
408 bool isIntegral()
const;
409 bool isDouble()
const;
410 bool isNumeric()
const;
411 bool isString()
const;
412 bool isArray()
const;
413 bool isObject()
const;
416 template <
typename T> T as()
const =
delete;
417 template <
typename T>
bool is()
const =
delete;
419 bool isConvertibleTo(
ValueType other)
const;
450 Value& operator[](
int index);
458 const Value& operator[](
int index)
const;
477 Value& operator[](
const char* key);
480 const Value& operator[](
const char* key)
const;
501 #ifdef JSON_USE_CPPTL
502 Value& operator[](
const CppTL::ConstString& key);
506 const Value& operator[](
const CppTL::ConstString& key)
const;
508 Value get(
const char* key,
const Value& defaultValue)
const;
514 Value get(
const char* begin,
const char* end,
515 const Value& defaultValue)
const;
520 #ifdef JSON_USE_CPPTL
521 Value get(
const CppTL::ConstString& key,
const Value& defaultValue)
const;
525 Value const* find(
char const* begin,
char const* end)
const;
532 Value* demand(
char const* begin,
char const* end);
538 void removeMember(
const char* key);
541 void removeMember(
const String& key);
544 bool removeMember(
const char* key,
Value* removed);
551 bool removeMember(
String const& key,
Value* removed);
553 bool removeMember(
const char* begin,
const char* end,
Value* removed);
564 bool isMember(
const char* key)
const;
567 bool isMember(
const String& key)
const;
569 bool isMember(
const char* begin,
const char* end)
const;
570 #ifdef JSON_USE_CPPTL
571 bool isMember(
const CppTL::ConstString& key)
const;
580 Members getMemberNames()
const;
590 setComment(
String(comment, strlen(comment)), placement);
594 setComment(
String(comment, len), placement);
602 String toStyledString()
const;
604 const_iterator begin()
const;
605 const_iterator end()
const;
612 void setOffsetStart(ptrdiff_t start);
613 void setOffsetLimit(ptrdiff_t limit);
614 ptrdiff_t getOffsetStart()
const;
615 ptrdiff_t getOffsetLimit()
const;
619 bits_.value_type_ = static_cast<unsigned char>(v);
621 bool isAllocated()
const {
return bits_.allocated_; }
622 void setIsAllocated(
bool v) { bits_.allocated_ = v; }
624 void initBasic(
ValueType type,
bool allocated =
false);
625 void dupPayload(
const Value& other);
626 void releasePayload();
627 void dupMeta(
const Value& other);
629 Value& resolveReference(
const char* key);
630 Value& resolveReference(
const char* key,
const char* end);
659 Comments() =
default;
660 Comments(
const Comments& that);
661 Comments(Comments&& that);
662 Comments& operator=(
const Comments& that);
663 Comments& operator=(Comments&& that);
669 using Array = std::array<String, numberOfCommentPlacement>;
670 std::unique_ptr<Array> ptr_;
680 template <>
inline bool Value::as<bool>()
const {
return asBool(); }
681 template <>
inline bool Value::is<bool>()
const {
return isBool(); }
683 template <>
inline Int Value::as<Int>()
const {
return asInt(); }
684 template <>
inline bool Value::is<Int>()
const {
return isInt(); }
686 template <>
inline UInt Value::as<UInt>()
const {
return asUInt(); }
687 template <>
inline bool Value::is<UInt>()
const {
return isUInt(); }
689 #if defined(JSON_HAS_INT64)
690 template <>
inline Int64 Value::as<Int64>()
const {
return asInt64(); }
691 template <>
inline bool Value::is<Int64>()
const {
return isInt64(); }
694 template <>
inline bool Value::is<UInt64>()
const {
return isUInt64(); }
697 template <>
inline double Value::as<double>()
const {
return asDouble(); }
698 template <>
inline bool Value::is<double>()
const {
return isDouble(); }
701 template <>
inline bool Value::is<String>()
const {
return isString(); }
705 template <>
inline float Value::as<float>()
const {
return asFloat(); }
706 template <>
inline const char* Value::as<const char*>()
const {
709 #ifdef JSON_USE_CPPTL
710 template <>
inline CppTL::ConstString Value::as<CppTL::ConstString>()
const {
711 return asConstString();
728 enum Kind { kindNone = 0, kindIndex, kindKey };
731 Kind kind_{kindNone};
753 const Value& resolve(
const Value& root)
const;
760 typedef std::vector<const PathArgument*> InArgs;
761 typedef std::vector<PathArgument> Args;
763 void makePath(
const String& path,
const InArgs& in);
764 void addPathInArg(
const String& path,
const InArgs& in,
765 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
766 static void invalidPath(
const String& path,
int location);
807 char const* memberName() const;
811 char const* memberName(
char const** end) const;
820 const
Value& deref() const;
827 difference_type computeDistance(const SelfType& other) const;
829 bool isEqual(const SelfType& other) const;
831 void copy(const SelfType& other);
834 Value::ObjectValues::iterator current_;
842 explicit ValueIteratorBase(
const Value::ObjectValues::iterator& current);
917 explicit ValueIterator(
const Value::ObjectValues::iterator& current);
959 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
961 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
963 #endif // CPPTL_JSON_H_INCLUDED
bool operator!=(const SelfType &other) const
pointer operator->() const
ValueType
Type of the value held by a Value object.
#define JSONCPP_OP_EXPLICIT
we set max number of significant digits in string
a comment just after a value on the same line
const iterator for object and array value.
const char * asCString() const
Embedded zeroes could cause you trouble!
bool operator==(const SelfType &other) const
a comment placed on the line before a value
Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Iterator for object and array value.
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
difference_type computeDistance(const SelfType &other) const
Experimental and untested: represents a "path" to access a node.
a comment on the line after a value (only make sense for
we set max number of digits after "." in string
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
array value (ordered list)
PrecisionType
Type of precision for formatting of real values.
ValueIteratorBase SelfType
ValueConstIterator SelfType
void swap(Value &other)
Swap everything.
Base class for all exceptions we throw.
StaticString(const char *czstring)
Experimental and untested: represents an element of the "path" to access a node.
#define JSONCPP_DEPRECATED(message)
std::bidirectional_iterator_tag iterator_category
Json::LargestInt LargestInt
void swap(Value &a, Value &b)
JSON (JavaScript Object Notation).
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
String asString() const
Embedded zeroes are possible.
const typedef Value value_type
ValueConstIterator const_iterator
base class for Value iterators.
object value (collection of name/value pairs).
const char * c_str() const
static const Value & nullRef
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Json::LargestUInt LargestUInt
Json::ArrayIndex ArrayIndex
const typedef Value & reference
void setComment(const char *comment, size_t len, CommentPlacement placement)
Comments must be //... or /* ... */.
reference operator*() const
const typedef Value * pointer
difference_type operator-(const SelfType &other) const
Lightweight wrapper to tag static string.
std::vector< String > Members
Exceptions which the user cannot easily avoid.