6 #ifndef JSON_VALUE_H_INCLUDED
7 #define JSON_VALUE_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]]
27 #if !defined(JSONCPP_TEMPLATE_DELETE)
28 #if defined(__clang__) && defined(__apple_build_version__)
29 #if __apple_build_version__ <= 8000042
30 #define JSONCPP_TEMPLATE_DELETE
32 #elif defined(__clang__)
33 #if __clang_major__ == 3 && __clang_minor__ <= 8
34 #define JSONCPP_TEMPLATE_DELETE
37 #if !defined(JSONCPP_TEMPLATE_DELETE)
38 #define JSONCPP_TEMPLATE_DELETE = delete
42 #ifndef JSONCPP_HAS_STRING_VIEW
43 #if __cplusplus >= 201703L
44 #define JSONCPP_HAS_STRING_VIEW 1
58 #ifdef JSONCPP_HAS_STRING_VIEW
59 #include <string_view>
64 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
66 #pragma warning(disable : 4251 4275)
67 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
76 #if JSON_USE_EXCEPTION
85 char const* what()
const noexcept
override;
165 operator const char*()
const {
return c_str_; }
167 const char*
c_str()
const {
return c_str_; }
209 friend struct ::ValueTest;
217 #if defined(JSON_HAS_INT64)
220 #endif // defined(JSON_HAS_INT64)
235 static Value const& nullSingleton();
252 #if defined(JSON_HAS_INT64)
259 #endif // defined(JSON_HAS_INT64)
260 static constexpr
UInt defaultRealPrecision = 17;
265 static constexpr
double maxUInt64AsDouble = 18446744073709551615.0;
274 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
277 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
279 CZString(
char const* str,
unsigned length, DuplicationPolicy allocate);
280 CZString(CZString
const& other);
281 CZString(CZString&& other) noexcept;
283 CZString& operator=(
const CZString& other);
284 CZString& operator=(CZString&& other) noexcept;
286 bool operator<(CZString
const& other)
const;
290 char const* data()
const;
291 unsigned length()
const;
292 bool isStaticString()
const;
295 void swap(CZString& other);
297 struct StringStorage {
298 unsigned policy_ : 2;
299 unsigned length_ : 30;
305 StringStorage storage_;
310 typedef std::map<CZString, Value> ObjectValues;
311 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
333 #if defined(JSON_HAS_INT64)
336 #endif // if defined(JSON_HAS_INT64)
338 Value(
const char* value);
339 Value(
const char* begin,
const char* end);
359 #ifdef JSONCPP_HAS_STRING_VIEW
360 inline Value(std::string_view value)
361 :
Value(value.data(), value.data() + value.length()) {}
364 Value(std::nullptr_t ptr) =
delete;
365 Value(
const Value& other);
366 Value(Value&& other) noexcept;
371 Value& operator=(const Value& other);
372 Value& operator=(Value&& other) noexcept;
375 void swap(Value& other);
377 void swapPayload(Value& other);
380 void copy(const Value& other);
382 void copyPayload(const Value& other);
387 bool operator<(const Value& other) const;
388 bool operator<=(const Value& other) const;
389 bool operator>=(const Value& other) const;
390 bool operator>(const Value& other) const;
391 bool operator==(const Value& other) const;
392 bool operator!=(const Value& other) const;
393 int compare(const Value& other) const;
395 const
char* asCString() const;
396 #if JSONCPP_USE_SECURE_MEMORY
397 unsigned getCStringLength()
const;
404 bool getString(
char const** begin,
char const** end)
const;
405 #ifdef JSONCPP_HAS_STRING_VIEW
409 inline bool getString(std::string_view* str)
const {
412 if (!getString(&begin, &end))
414 *str = std::string_view(begin, static_cast<size_t>(end - begin));
420 #if defined(JSON_HAS_INT64)
421 Int64 asInt64()
const;
423 #endif // if defined(JSON_HAS_INT64)
426 float asFloat()
const;
427 double asDouble()
const;
433 bool isInt64()
const;
435 bool isUInt64()
const;
436 bool isIntegral()
const;
437 bool isDouble()
const;
438 bool isNumeric()
const;
439 bool isString()
const;
440 bool isArray()
const;
441 bool isObject()
const;
447 bool isConvertibleTo(
ValueType other) const;
457 explicit operator
bool() const;
478 Value& operator[](
int index);
485 const Value& operator[](
ArrayIndex index) const;
486 const Value& operator[](
int index) const;
491 Value get(
ArrayIndex index, const Value& defaultValue) const;
497 Value& append(const Value& value);
498 Value& append(Value&& value);
501 bool insert(
ArrayIndex index, const Value& newValue);
502 bool insert(
ArrayIndex index, Value&& newValue);
504 #ifdef JSONCPP_HAS_STRING_VIEW
505 inline Value& operator[](std::string_view key) {
508 return resolveReference(key.data(), key.data() + key.length());
513 inline const Value& operator[](std::string_view key)
const {
514 Value
const* found = find(key.data(), key.data() + key.length());
516 return nullSingleton();
520 Value& operator[](
const char* key);
526 const Value& operator[](
const char* key)
const;
529 Value& operator[](
const String& key);
533 const Value& operator[](
const String& key)
const;
546 Value& operator[](
const StaticString& key);
547 #ifdef JSONCPP_HAS_STRING_VIEW
548 inline Value
get(std::string_view key,
const Value& defaultValue)
const {
551 return get(key.data(), key.data() + key.length(), defaultValue);
554 Value
get(
const char* key,
const Value& defaultValue)
const;
560 Value
get(
const String& key,
const Value& defaultValue)
const;
564 Value
get(
const char* begin,
const char* end,
565 const Value& defaultValue)
const;
569 Value
const* find(
char const* begin,
char const* end)
const;
572 Value
const* find(
const String& key)
const;
575 template <
typename T,
bool (T::*TMemFn)() const>
577 Value const* found = find(key);
578 if (!found || !(found->*TMemFn)())
589 Value const* findIntegral(
const String& key)
const;
599 Value* demand(
char const* begin,
char const* end);
605 #if JSONCPP_HAS_STRING_VIEW
606 inline void removeMember(std::string_view key) {
607 removeMember(key.data(), key.data() + key.length(),
nullptr);
610 void removeMember(
const char* key);
613 void removeMember(
const String& key);
620 #if JSONCPP_HAS_STRING_VIEW
621 inline bool removeMember(std::string_view key, Value* removed) {
622 return removeMember(key.data(), key.data() + key.length(), removed);
625 bool removeMember(
String const& key, Value* removed);
628 bool removeMember(
const char* key, Value* removed);
630 bool removeMember(
const char* begin,
const char* end, Value* removed);
637 bool removeIndex(
ArrayIndex index, Value* removed);
639 #ifdef JSONCPP_HAS_STRING_VIEW
640 inline bool isMember(std::string_view key)
const {
643 return isMember(key.data(), key.data() + key.length());
646 bool isMember(
const char* key)
const;
651 bool isMember(
const String& key)
const;
653 bool isMember(
const char* begin,
const char* end)
const;
660 Members getMemberNames()
const;
665 setComment(
String(comment, strlen(comment)), placement);
669 setComment(
String(comment, len), placement);
677 String toStyledString()
const;
679 const_iterator begin()
const;
680 const_iterator end()
const;
693 const Value& front()
const;
703 const Value& back()
const;
712 void setOffsetStart(ptrdiff_t start);
713 void setOffsetLimit(ptrdiff_t limit);
714 ptrdiff_t getOffsetStart()
const;
715 ptrdiff_t getOffsetLimit()
const;
719 bits_.value_type_ =
static_cast<unsigned char>(v);
721 bool isAllocated()
const {
return bits_.allocated_; }
722 void setIsAllocated(
bool v) { bits_.allocated_ = v; }
724 void initBasic(
ValueType type,
bool allocated =
false);
725 void dupPayload(
const Value& other);
726 void releasePayload();
727 void dupMeta(
const Value& other);
729 Value& resolveReference(
const char* key);
730 Value& resolveReference(
const char* key,
const char* end);
752 unsigned int value_type_ : 8;
754 unsigned int allocated_ : 1;
759 Comments() =
default;
760 Comments(
const Comments& that);
761 Comments(Comments&& that) noexcept;
762 Comments& operator=(
const Comments& that);
763 Comments& operator=(Comments&& that) noexcept;
769 using Array = std::array<String, numberOfCommentPlacement>;
770 std::unique_ptr<Array> ptr_;
780 template <>
inline bool Value::as<bool>()
const {
return asBool(); }
781 template <>
inline bool Value::is<bool>()
const {
return isBool(); }
783 template <>
inline Int Value::as<Int>()
const {
return asInt(); }
784 template <>
inline bool Value::is<Int>()
const {
return isInt(); }
786 template <>
inline UInt Value::as<UInt>()
const {
return asUInt(); }
787 template <>
inline bool Value::is<UInt>()
const {
return isUInt(); }
789 #if defined(JSON_HAS_INT64)
790 template <>
inline Int64 Value::as<Int64>()
const {
return asInt64(); }
791 template <>
inline bool Value::is<Int64>()
const {
return isInt64(); }
793 template <>
inline UInt64 Value::as<UInt64>()
const {
return asUInt64(); }
794 template <>
inline bool Value::is<UInt64>()
const {
return isUInt64(); }
797 template <>
inline double Value::as<double>()
const {
return asDouble(); }
798 template <>
inline bool Value::is<double>()
const {
return isDouble(); }
800 template <>
inline String Value::as<String>()
const {
return asString(); }
801 template <>
inline bool Value::is<String>()
const {
return isString(); }
805 template <>
inline float Value::as<float>()
const {
return asFloat(); }
806 template <>
inline const char* Value::as<const char*>()
const {
823 enum Kind { kindNone = 0, kindIndex, kindKey };
826 Kind kind_{kindNone};
848 const Value& resolve(
const Value& root)
const;
855 using InArgs = std::vector<const PathArgument*>;
856 using Args = std::vector<PathArgument>;
858 void makePath(
const String& path,
const InArgs& in);
859 void addPathInArg(
const String& path,
const InArgs& in,
860 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
861 static void invalidPath(
const String& path,
int location);
872 using size_t =
unsigned int;
902 char const* memberName() const;
906 char const* memberName(
char const** end) const;
915 const
Value& deref() const;
922 difference_type computeDistance(const SelfType& other) const;
924 bool isEqual(const SelfType& other) const;
926 void copy(const SelfType& other);
929 Value::ObjectValues::iterator current_;
937 explicit ValueIteratorBase(
const Value::ObjectValues::iterator& current);
999 using size_t =
unsigned int;
1012 explicit ValueIterator(
const Value::ObjectValues::iterator& current);
1086 return it_ == other.it_;
1089 return it_ != other.it_;
1121 return it_ == other.it_;
1124 return it_ != other.it_;
1139 : begin_(begin), end_(end) {}
1153 : begin_(begin), end_(end) {}
1187 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1188 #pragma warning(pop)
1189 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1191 #endif // JSON_H_INCLUDED
Proxy struct to enable range-based for loops over const object members.
#define JSONCPP_DEPRECATED(message)
pointer operator->() const
const Value & front() const
Returns a reference to the first element in the Value.
bool operator==(ValueMembersIterator const &other) const
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion...
reference operator*() const
std::forward_iterator_tag iterator_category
PrecisionType
Type of precision for formatting of real values.
base class for Value iterators.
bool operator==(ValueConstMembersIterator const &other) const
array value (ordered list)
#define JSONCPP_TEMPLATE_DELETE
ValueConstMembersIterator(ValueConstIterator const &iter)
std::forward_iterator_tag iterator_category
Json::LargestUInt LargestUInt
Proxy struct to enable range-based for loops over object members.
Iterator adapter for range-based for loops.
Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
void setComment(const char *comment, size_t len, CommentPlacement placement)
Comments must be //... or /* ... */.
object value (collection of name/value pairs).
Iterator adapter for range-based for loops.
std::bidirectional_iterator_tag iterator_category
ValueMembersIterator begin() const
ValueMembersView members()
Lightweight wrapper to tag static string.
std::basic_string< char, std::char_traits< char >, Allocator< char >> String
ValueMembersIterator end() const
difference_type computeDistance(const SelfType &other) const
ValueMembersView(ValueIterator begin, ValueIterator end)
bool operator!=(const SelfType &other) const
const iterator for object and array value.
Value const * findValue(const String &key) const
Calls find and only returns a valid pointer if the type is found.
bool operator!=(ValueConstMembersIterator const &other) const
ValueMembersIterator()=default
MemberProxy operator*() const
Experimental and untested: represents an element of the "path" to access a node.
ValueConstMembersIterator()=default
ValueMembersIterator operator++(int)
String name() const
Return the member name of the referenced Value, or "" if it is not an objectValue.
StaticString(const char *czstring)
bool operator!=(ValueMembersIterator const &other) const
Range-based for loop adapter for object members.
std::vector< String > Members
void swap(Value &a, Value &b)
ValueConstMembersIterator operator++(int)
Range-based for loop adapter for object members.
ValueMembersIterator(ValueIterator const &iter)
JSON (JavaScript Object Notation).
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
we set max number of digits after "." in string
void swap(Value &other)
Swap everything.
Experimental and untested: represents a "path" to access a node.
const char * c_str() const
ValueMembersIterator & operator++()
Json::LargestInt LargestInt
static const Value & nullRef
difference_type operator-(const SelfType &other) const
reference operator*() const
Exceptions which the user cannot easily avoid.
a comment on the line after a value (only make sense for
ValueConstMembersView(ValueConstIterator begin, ValueConstIterator end)
we set max number of significant digits in string
Iterator for object and array value.
ValueType
Type of the value held by a Value object.
ValueConstMembersIterator begin() const
Json::ArrayIndex ArrayIndex
ConstMemberProxy operator*() const
static const Value & null
a comment placed on the line before a value
a comment just after a value on the same line
const_iterator begin() const
Base class for all exceptions we throw.
ValueConstMembersIterator & operator++()
ValueConstMembersIterator end() const
bool operator==(const SelfType &other) const
pointer operator->() const
const Value & back() const
Returns a reference to the last element in the Value.
const_iterator end() const