6#ifndef JSON_VALUE_H_INCLUDED
7#define JSON_VALUE_H_INCLUDED
9#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
64#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
66#pragma warning(disable : 4251 4275)
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)
252#if defined(JSON_HAS_INT64)
261 static constexpr UInt defaultRealPrecision = 17;
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;
333#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()) {}
396#if JSONCPP_USE_SECURE_MEMORY
397 unsigned getCStringLength()
const;
405#ifdef JSONCPP_HAS_STRING_VIEW
409 inline bool getString(std::string_view* str)
const {
414 *str = std::string_view(
begin,
static_cast<size_t>(
end -
begin));
420#if defined(JSON_HAS_INT64)
421 Int64 asInt64()
const;
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;
457 explicit operator
bool() const;
478 Value& operator[](
int index);
486 const
Value& operator[](
int index) const;
504#ifdef JSONCPP_HAS_STRING_VIEW
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();
523 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
550 inline Value get(std::string_view key,
const Value& defaultValue)
const {
551 return get(key.data(), key.data() + key.length(), defaultValue);
556 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>
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
642 inline bool isMember(std::string_view key)
const {
643 return isMember(key.data(), key.data() + key.length());
648 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;
677 String toStyledString()
const;
679 const_iterator begin()
const;
680 const_iterator end()
const;
688 const Value& front()
const;
698 const Value& back()
const;
707 void setOffsetStart(ptrdiff_t start);
708 void setOffsetLimit(ptrdiff_t limit);
709 ptrdiff_t getOffsetStart()
const;
710 ptrdiff_t getOffsetLimit()
const;
714 bits_.value_type_ =
static_cast<unsigned char>(v);
716 bool isAllocated()
const {
return bits_.allocated_; }
717 void setIsAllocated(
bool v) { bits_.allocated_ = v; }
719 void initBasic(ValueType type,
bool allocated =
false);
720 void dupPayload(
const Value& other);
721 void releasePayload();
722 void dupMeta(
const Value& other);
724 Value& resolveReference(
const char* key);
725 Value& resolveReference(
const char* key,
const char* end);
754 Comments() =
default;
755 Comments(
const Comments& that);
756 Comments(Comments&& that)
noexcept;
757 Comments& operator=(
const Comments& that);
758 Comments& operator=(Comments&& that)
noexcept;
764 using Array = std::array<String, numberOfCommentPlacement>;
765 std::unique_ptr<Array> ptr_;
784#if defined(JSON_HAS_INT64)
818 enum Kind { kindNone = 0, kindIndex, kindKey };
821 Kind kind_{kindNone};
850 using InArgs = std::vector<const PathArgument*>;
851 using Args = std::vector<PathArgument>;
853 void makePath(
const String& path,
const InArgs& in);
854 void addPathInArg(
const String& path,
const InArgs& in,
855 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
856 static void invalidPath(
const String& path,
int location);
901 char const*
memberName(
char const** end) const;
924 Value::ObjectValues::iterator current_;
1007 explicit ValueIterator(
const Value::ObjectValues::iterator& current);
1057#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
char const * what() const noexcept override
~Exception() noexcept override
LogicError(String const &msg)
Experimental and untested: represents an element of the "path" to access a node.
Path(const String &path, const PathArgument &a1=PathArgument(), const PathArgument &a2=PathArgument(), const PathArgument &a3=PathArgument(), const PathArgument &a4=PathArgument(), const PathArgument &a5=PathArgument())
Value & make(Value &root) const
Creates the "path" to access the specified node and returns a reference on the node.
const Value & resolve(const Value &root) const
RuntimeError(String const &msg)
Lightweight wrapper to tag static string.
const char * c_str() const
StaticString(const char *czstring)
const iterator for object and array value.
pointer operator->() const
ValueConstIterator SelfType
SelfType & operator=(const ValueIteratorBase &other)
reference operator*() const
const_iterator begin() const
Value get(ArrayIndex index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool empty() const
Return true if empty array, empty object, or null; otherwise, false.
Value(std::nullptr_t ptr)=delete
static constexpr LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Json::ArrayIndex ArrayIndex
ArrayIndex size() const
Number of values in array or object.
const char * asCString() const
Embedded zeroes could cause you trouble!
bool operator==(const Value &other) const
static constexpr Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
void copy(const Value &other)
copy everything.
static const Value & null
void setComment(const char *comment, size_t len, CommentPlacement placement)
Comments must be //... or /* ... */.
bool getString(char const **begin, char const **end) const
Get raw char* of string-value.
T as() const =delete
The as<T> and is<T> member function templates and specializations.
static constexpr double maxUInt64AsDouble
std::vector< String > Members
const_iterator end() const
bool operator<=(const Value &other) const
const Value & front() const
Returns a reference to the first element in the Value.
bool operator>(const Value &other) const
void clear()
Remove all object members and array elements.
String asString() const
Embedded zeroes are possible.
void swapPayload(Value &other)
Swap values but leave comments and source offsets in place.
CommentPlacement placement
Json::LargestInt LargestInt
Json::LargestUInt LargestUInt
ValueConstIterator const_iterator
void resize(ArrayIndex newSize)
Resize the array to newSize elements.
Value & operator[](ArrayIndex index)
Value & append(const Value &value)
Append value to array at the end.
bool operator!=(const Value &other) const
Value const * findValue(const String &key) const
Calls find and only returns a valid pointer if the type is found.
const Value & back() const
Returns a reference to the last element in the Value.
void swap(Value &other)
Swap everything.
bool operator<(const Value &other) const
Compare payload only, not comments etc.
static const Value & nullRef
void copyPayload(const Value &other)
copy values but leave comments and source offsets in place.
static constexpr Int maxInt
Maximum signed int value that can be stored in a Json::Value.
static constexpr LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
bool isValidIndex(ArrayIndex index) const
Return true if index < size().
friend class ValueIteratorBase
Value(ValueType type=nullValue)
Create a default Value of the given type.
static constexpr UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Value & operator=(const Value &other)
static constexpr Int minInt
Minimum signed int value that can be stored in a Json::Value.
bool insert(ArrayIndex index, const Value &newValue)
Insert value in array at specific index.
static constexpr Int64 minInt64
Minimum signed 64 bits int value that can be stored in a Json::Value.
int compare(const Value &other) const
static constexpr LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
bool isConvertibleTo(ValueType other) const
static Value const & nullSingleton()
Value const * find(char const *begin, char const *end) const
Most general and efficient version of isMember()const, get()const, and operator[]const.
bool operator>=(const Value &other) const
static constexpr UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
base class for Value iterators.
bool isEqual(const SelfType &other) const
bool operator==(const SelfType &other) const
char const * memberName(char const **end) const
Return the member name of the referenced Value, or NULL if it is not an objectValue.
void copy(const SelfType &other)
std::bidirectional_iterator_tag iterator_category
difference_type operator-(const SelfType &other) const
bool operator!=(const SelfType &other) const
const Value & deref() const
ValueIteratorBase SelfType
difference_type computeDistance(const SelfType &other) const
Iterator for object and array value.
reference operator*() const
ValueIterator(const ValueIterator &other)
pointer operator->() const
SelfType & operator=(const SelfType &other)
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
#define JSONCPP_DEPRECATED(message)
JSON (JavaScript Object Notation).
@ commentAfterOnSameLine
a comment just after a value on the same line
@ commentBefore
a comment placed on the line before a value
@ numberOfCommentPlacement
root value)
@ commentAfter
a comment on the line after a value (only make sense for
ValueType
Type of the value held by a Value object.
@ stringValue
UTF-8 string value.
@ arrayValue
array value (ordered list)
@ intValue
signed integer value
@ objectValue
object value (collection of name/value pairs).
@ uintValue
unsigned integer value
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
PrecisionType
Type of precision for formatting of real values.
@ decimalPlaces
we set max number of digits after "." in string
@ significantDigits
we set max number of significant digits in string
void swap(Value &a, Value &b)
#define JSONCPP_TEMPLATE_DELETE