1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-28 12:02:00 +03:00

Merge branch 'develop' of https://github.com/nlohmann/json into issue2179

 Conflicts:
	single_include/nlohmann/json.hpp
This commit is contained in:
Niels Lohmann
2020-07-11 13:20:16 +02:00
44 changed files with 1769 additions and 868 deletions

View File

@ -35,7 +35,6 @@ SOFTWARE.
#define NLOHMANN_JSON_VERSION_PATCH 0
#include <algorithm> // all_of, find, for_each
#include <cassert> // assert
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <functional> // hash, less
#include <initializer_list> // initializer_list
@ -197,10 +196,12 @@ class basic_json
static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
InputAdapterType adapter,
detail::parser_callback_t<basic_json>cb = nullptr,
bool allow_exceptions = true
const bool allow_exceptions = true,
const bool ignore_comments = false
)
{
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter), std::move(cb), allow_exceptions);
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
std::move(cb), allow_exceptions, ignore_comments);
}
using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
@ -923,7 +924,7 @@ class basic_json
};
std::unique_ptr<T, decltype(deleter)> object(AllocatorTraits::allocate(alloc, 1), deleter);
AllocatorTraits::construct(alloc, object.get(), std::forward<Args>(args)...);
assert(object != nullptr);
JSON_ASSERT(object != nullptr);
return object.release();
}
@ -1218,10 +1219,10 @@ class basic_json
*/
void assert_invariant() const noexcept
{
assert(m_type != value_t::object or m_value.object != nullptr);
assert(m_type != value_t::array or m_value.array != nullptr);
assert(m_type != value_t::string or m_value.string != nullptr);
assert(m_type != value_t::binary or m_value.binary != nullptr);
JSON_ASSERT(m_type != value_t::object or m_value.object != nullptr);
JSON_ASSERT(m_type != value_t::array or m_value.array != nullptr);
JSON_ASSERT(m_type != value_t::string or m_value.string != nullptr);
JSON_ASSERT(m_type != value_t::binary or m_value.binary != nullptr);
}
public:
@ -1514,7 +1515,7 @@ class basic_json
m_type = value_t::discarded;
break;
default: // LCOV_EXCL_LINE
assert(false); // LCOV_EXCL_LINE
JSON_ASSERT(false); // LCOV_EXCL_LINE
}
assert_invariant();
}
@ -1914,8 +1915,8 @@ class basic_json
std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int>::type = 0>
basic_json(InputIT first, InputIT last)
{
assert(first.m_object != nullptr);
assert(last.m_object != nullptr);
JSON_ASSERT(first.m_object != nullptr);
JSON_ASSERT(last.m_object != nullptr);
// make sure iterator fits the current value
if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
@ -2237,7 +2238,8 @@ class basic_json
@param[in] error_handler how to react on decoding errors; there are three
possible values: `strict` (throws and exception in case a decoding error
occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD),
and `ignore` (ignore invalid UTF-8 sequences during serialization).
and `ignore` (ignore invalid UTF-8 sequences during serialization; all
bytes are copied to the output unchanged).
@return string containing the serialization of the JSON value
@ -3018,6 +3020,18 @@ class basic_json
return v;
}
// specialization to allow to call get_to with a basic_json value
// see https://github.com/nlohmann/json/issues/2175
template<typename ValueType,
detail::enable_if_t <
detail::is_basic_json<ValueType>::value,
int> = 0>
ValueType & get_to(ValueType& v) const
{
v = *this;
return v;
}
template <
typename T, std::size_t N,
typename Array = T (&)[N],
@ -3619,7 +3633,7 @@ class basic_json
// const operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
assert(m_value.object->find(key) != m_value.object->end());
JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
return m_value.object->find(key)->second;
}
@ -3711,7 +3725,7 @@ class basic_json
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
assert(m_value.object->find(key) != m_value.object->end());
JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
return m_value.object->find(key)->second;
}
@ -5479,7 +5493,7 @@ class basic_json
iterator insert_iterator(const_iterator pos, Args&& ... args)
{
iterator result(this);
assert(m_value.array != nullptr);
JSON_ASSERT(m_value.array != nullptr);
auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
@ -5866,6 +5880,34 @@ class basic_json
/*!
@brief exchanges the values
Exchanges the contents of the JSON value from @a left with those of @a right. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated. implemented as a friend function callable via ADL.
@param[in,out] left JSON value to exchange the contents with
@param[in,out] right JSON value to exchange the contents with
@complexity Constant.
@liveexample{The example below shows how JSON values can be swapped with
`swap()`.,swap__reference}
@since version 1.0.0
*/
friend void swap(reference left, reference right) noexcept (
std::is_nothrow_move_constructible<value_t>::value and
std::is_nothrow_move_assignable<value_t>::value and
std::is_nothrow_move_constructible<json_value>::value and
std::is_nothrow_move_assignable<json_value>::value
)
{
left.swap(right);
}
/*!
@brief exchanges the values
Exchanges the contents of a JSON array with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
@ -6564,6 +6606,9 @@ class basic_json
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@param[in] ignore_comments whether comments should be ignored and treated
like whitespace (true) or yield a parse error (true); (optional, false by
default)
@return deserialized JSON value; in case of a parse error and
@a allow_exceptions set to `false`, the return value will be
@ -6592,16 +6637,18 @@ class basic_json
@liveexample{The example below demonstrates the `parse()` function reading
from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
@since version 2.0.3 (contiguous containers)
@since version 2.0.3 (contiguous containers); version 3.9.0 allowed to
ignore comments.
*/
template<typename InputType>
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json parse(InputType&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
const bool allow_exceptions = true,
const bool ignore_comments = false)
{
basic_json result;
parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions).parse(true, result);
parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
return result;
}
@ -6618,6 +6665,9 @@ class basic_json
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@param[in] ignore_comments whether comments should be ignored and treated
like whitespace (true) or yield a parse error (true); (optional, false by
default)
@return deserialized JSON value; in case of a parse error and
@a allow_exceptions set to `false`, the return value will be
@ -6633,10 +6683,11 @@ class basic_json
static basic_json parse(IteratorType first,
IteratorType last,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
const bool allow_exceptions = true,
const bool ignore_comments = false)
{
basic_json result;
parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions).parse(true, result);
parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
return result;
}
@ -6644,10 +6695,11 @@ class basic_json
JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
static basic_json parse(detail::span_input_adapter&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
const bool allow_exceptions = true,
const bool ignore_comments = false)
{
basic_json result;
parser(i.get(), cb, allow_exceptions).parse(true, result);
parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
return result;
}
@ -6667,6 +6719,9 @@ class basic_json
iterators.
@param[in] i input to read from
@param[in] ignore_comments whether comments should be ignored and treated
like whitespace (true) or yield a parse error (true); (optional, false by
default)
@return Whether the input read from @a i is valid JSON.
@ -6679,22 +6734,25 @@ class basic_json
from a string.,accept__string}
*/
template<typename InputType>
static bool accept(InputType&& i)
static bool accept(InputType&& i,
const bool ignore_comments = false)
{
return parser(detail::input_adapter(std::forward<InputType>(i))).accept(true);
return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
}
template<typename IteratorType>
static bool accept(IteratorType first, IteratorType last)
static bool accept(IteratorType first, IteratorType last,
const bool ignore_comments = false)
{
return parser(detail::input_adapter(std::move(first), std::move(last))).accept(true);
return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
}
JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
static bool accept(detail::span_input_adapter&& i)
static bool accept(detail::span_input_adapter&& i,
const bool ignore_comments = false)
{
return parser(i.get()).accept(true);
return parser(i.get(), nullptr, false, ignore_comments).accept(true);
}
/*!
@ -6714,6 +6772,9 @@ class basic_json
@param[in,out] sax SAX event listener
@param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON)
@param[in] strict whether the input has to be consumed completely
@param[in] ignore_comments whether comments should be ignored and treated
like whitespace (true) or yield a parse error (true); (optional, false by
default); only applies to the JSON file format.
@return return value of the last processed SAX event
@ -6738,11 +6799,12 @@ class basic_json
JSON_HEDLEY_NON_NULL(2)
static bool sax_parse(InputType&& i, SAX* sax,
input_format_t format = input_format_t::json,
const bool strict = true)
const bool strict = true,
const bool ignore_comments = false)
{
auto ia = detail::input_adapter(std::forward<InputType>(i));
return format == input_format_t::json
? parser(std::move(ia)).sax_parse(sax, strict)
? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}
@ -6750,11 +6812,12 @@ class basic_json
JSON_HEDLEY_NON_NULL(3)
static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
input_format_t format = input_format_t::json,
const bool strict = true)
const bool strict = true,
const bool ignore_comments = false)
{
auto ia = detail::input_adapter(std::move(first), std::move(last));
return format == input_format_t::json
? parser(std::move(ia)).sax_parse(sax, strict)
? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}
@ -6763,11 +6826,12 @@ class basic_json
JSON_HEDLEY_NON_NULL(2)
static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
input_format_t format = input_format_t::json,
const bool strict = true)
const bool strict = true,
const bool ignore_comments = false)
{
auto ia = i.get();
return format == input_format_t::json
? parser(std::move(ia)).sax_parse(sax, strict)
? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}
@ -7041,7 +7105,8 @@ class basic_json
number_unsigned | 256..65535 | uint 16 | 0xCD
number_unsigned | 65536..4294967295 | uint 32 | 0xCE
number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
number_float | *any value* | float 64 | 0xCB
number_float | *any value representable by a float* | float 32 | 0xCA
number_float | *any value NOT representable by a float* | float 64 | 0xCB
string | *length*: 0..31 | fixstr | 0xA0..0xBF
string | *length*: 32..255 | str 8 | 0xD9
string | *length*: 256..65535 | str 16 | 0xDA
@ -7065,9 +7130,6 @@ class basic_json
- arrays with more than 4294967295 elements
- objects with more than 4294967295 elements
@note The following MessagePack types are not used in the conversion:
- float 32 (0xCA)
@note Any MessagePack output created @ref to_msgpack can be successfully
parsed by @ref from_msgpack.
@ -8182,7 +8244,7 @@ class basic_json
else
{
const auto idx = json_pointer::array_index(last_path);
if (JSON_HEDLEY_UNLIKELY(static_cast<size_type>(idx) > parent.size()))
if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
{
// avoid undefined behavior
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
@ -8196,7 +8258,7 @@ class basic_json
// if there exists a parent it cannot be primitive
default: // LCOV_EXCL_LINE
assert(false); // LCOV_EXCL_LINE
JSON_ASSERT(false); // LCOV_EXCL_LINE
}
};
@ -8225,7 +8287,7 @@ class basic_json
else if (parent.is_array())
{
// note erase performs range check
parent.erase(static_cast<size_type>(json_pointer::array_index(last_path)));
parent.erase(json_pointer::array_index(last_path));
}
};
@ -8660,6 +8722,9 @@ struct less<::nlohmann::detail::value_t>
}
};
// C++20 prohibit function specialization in the std namespace.
#ifndef JSON_HAS_CPP_20
/*!
@brief exchanges the values of two JSON objects
@ -8674,6 +8739,8 @@ inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcep
j1.swap(j2);
}
#endif
} // namespace std
/*!