mirror of
https://github.com/nlohmann/json.git
synced 2026-10-16 19:18:12 +00:00
Changes promised in the replies to the review of #5620: - read() reuses the owned copy: a copied input (an rvalue byte container, a stream, FILE*, wide characters, an iterator range) is appended to the cleared owned buffer instead of a fresh std::string being move-assigned over it. A moved-in std::string still replaces it. - The error replay runs the library parser with a SAX handler that stores nothing and throws the reported exception, instead of BasicJsonType::parse(), which built a DOM up to the error. - Pointers to and arrays of wide characters, and FILE*, take the input adapter path (converted to UTF-8 into an owned buffer) instead of failing a static_assert. Wide pointers are NUL-terminated. - Streams are read in blocks through rdbuf() into the buffer, instead of character by character through the input adapter; eofbit is set at the end, as by the library's stream adapter. - tape/tape_size/tape_cap are now nodes/node_count/node_capacity. - The builder's members get descriptive, m_-prefixed names. - The shallow frame stack is a std::array (in a wrapper with a user-provided constructor: -Weffc++ wants the member initialized, and value-initializing the 64 frames costs up to 30 % on small documents). - The value dispatch macro has no NEXT parameter; the switch is left with break, and the macro is undefined after its last use. - literal() and digits_exceed() deduce the length from the string literal; each literal has one helper; the two `if (!is_float)` blocks are merged. - arena_ensure() sits right before the writes; the decode arena is a std::vector<char>. - The builder records the deepest nesting, and materialize() sizes its stack from it once (growing it only if a subtree is deeper). - Comment fixes (hex4, slow_string, the NUL rule, put_utf8, 0 - m, the arena resize, grow()) and formatting (macro backslashes, digit cases, one power of ten per line, value_t assertion next to is_container()). - build_with's template parameters follow the builder's order. Greg's idea of keeping the source offset in `off` and the arena offset in `next` for escaped strings was measured and not adopted: string access got 7-18 % slower with a select, and 3.5-18 % slower with a branch-free field pick (Apple M-series, clang -O3), parsing was unchanged. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
161 lines
6.1 KiB
C++
161 lines
6.1 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm> // max
|
|
#include <cstddef> // size_t
|
|
#include <istream> // istream, streambuf
|
|
#include <string> // basic_string, char_traits, string
|
|
#include <type_traits> // decay, integral_constant, is_array, is_const, is_integral, is_lvalue_reference, is_pointer, is_same, remove_cv, remove_extent, remove_pointer, remove_reference
|
|
#include <utility> // forward
|
|
|
|
#include <nlohmann/json.hpp>
|
|
#include <nlohmann/detail/view/macro_scope.hpp>
|
|
|
|
#if NLOHMANN_VIEW_HAS_CPP_17
|
|
#include <string_view> // string_view
|
|
#endif
|
|
|
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
namespace detail
|
|
{
|
|
namespace view
|
|
{
|
|
|
|
/// how a document takes its input
|
|
enum class input_kind
|
|
{
|
|
move_string, ///< non-const rvalue std::string: owned without a copy
|
|
c_string, ///< pointer to single-byte characters (NUL-terminated): borrowed
|
|
char_array, ///< array of single-byte characters (e.g. a string literal): borrowed
|
|
borrow_range, ///< lvalue contiguous byte container, or std::string_view: borrowed
|
|
copy_range, ///< rvalue contiguous byte container (a const rvalue std::string too): copied
|
|
wide_c_string, ///< pointer to wide characters (NUL-terminated): converted to UTF-8 into a buffer
|
|
adapter, ///< streams, FILE*, wide strings and arrays, and the rest of what the library's input adapter reads: read into a buffer
|
|
};
|
|
|
|
/// a single-byte character type (char, signed/unsigned char, char8_t, std::uint8_t, ...)
|
|
template<typename T>
|
|
struct is_byte_char : std::integral_constant < bool, std::is_integral<T>::value && sizeof(T) == 1 > {}; // NOLINT(bugprone-sizeof-expression): T is any pointee or element type
|
|
|
|
/// a character type the library's input adapter converts to UTF-8
|
|
template<typename T>
|
|
struct is_wide_char : std::integral_constant < bool, std::is_same<T, wchar_t>::value || std::is_same<T, char16_t>::value
|
|
|| std::is_same<T, char32_t>::value > {};
|
|
|
|
template<typename InputType>
|
|
struct classify_input
|
|
{
|
|
using R = typename std::remove_reference<InputType>::type;
|
|
using D = typename std::decay<InputType>::type;
|
|
using element = typename std::remove_cv<typename std::remove_extent<R>::type>::type;
|
|
using pointee = typename std::remove_cv<typename std::remove_pointer<D>::type>::type;
|
|
static constexpr bool is_rvalue = !std::is_lvalue_reference<InputType>::value;
|
|
static constexpr bool is_bytes = is_contiguous_byte_container<D>::value;
|
|
#if NLOHMANN_VIEW_HAS_CPP_17
|
|
static constexpr bool is_string_view = std::is_same<D, std::string_view>::value;
|
|
#else
|
|
static constexpr bool is_string_view = false;
|
|
#endif
|
|
// NOLINTBEGIN(readability-avoid-nested-conditional-operator): a constant expression of C++11
|
|
// (arrays and pointers of other types, such as wide-character arrays and
|
|
// FILE*, go to the library's input adapter)
|
|
static constexpr input_kind value =
|
|
(std::is_array<R>::value && is_byte_char<element>::value) ? input_kind::char_array
|
|
: std::is_array<R>::value ? input_kind::adapter
|
|
: (std::is_pointer<D>::value && is_byte_char<pointee>::value) ? input_kind::c_string
|
|
: (std::is_pointer<D>::value && is_wide_char<pointee>::value) ? input_kind::wide_c_string
|
|
: std::is_pointer<D>::value ? input_kind::adapter
|
|
: (is_rvalue && !std::is_const<R>::value && std::is_same<D, std::string>::value) ? input_kind::move_string
|
|
: (is_bytes && (!is_rvalue || is_string_view)) ? input_kind::borrow_range
|
|
: is_bytes ? input_kind::copy_range
|
|
: input_kind::adapter;
|
|
// NOLINTEND(readability-avoid-nested-conditional-operator)
|
|
};
|
|
|
|
/// an integer type other than bool: a length passed where a flag is expected
|
|
template<typename T>
|
|
struct is_integer_not_bool : std::is_integral<T> {};
|
|
|
|
template<>
|
|
struct is_integer_not_bool<bool> : std::false_type {};
|
|
|
|
/// std::basic_string guarantees a NUL at data()[size()] (the parser's sentinel)
|
|
template<typename T>
|
|
struct is_std_string : std::false_type {};
|
|
|
|
template<typename Traits, typename Alloc>
|
|
struct is_std_string<std::basic_string<char, Traits, Alloc>> : std::true_type {};
|
|
|
|
/// append what a json input adapter yields (UTF-16/32 inputs arrive as UTF-8)
|
|
template<typename Adapter>
|
|
void append_adapter(std::string& buf, Adapter ia)
|
|
{
|
|
for (;;)
|
|
{
|
|
const auto ch = ia.get_character();
|
|
if (ch == std::char_traits<char>::eof())
|
|
{
|
|
break;
|
|
}
|
|
buf.push_back(static_cast<char>(ch));
|
|
}
|
|
}
|
|
|
|
/// an input adapter made by the document for an input that the library's
|
|
/// input_adapter() does not take as one argument (an iterator range, a wide C
|
|
/// string), to be appended with append_adapter()
|
|
template<typename Adapter>
|
|
struct adapter_input
|
|
{
|
|
Adapter ia;
|
|
};
|
|
|
|
template<typename T>
|
|
struct is_adapter_input : std::false_type {};
|
|
|
|
template<typename Adapter>
|
|
struct is_adapter_input<adapter_input<Adapter>> : std::true_type {};
|
|
|
|
template<typename Adapter>
|
|
adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& ia)
|
|
{
|
|
return {std::forward<Adapter>(ia)};
|
|
}
|
|
|
|
/// append the rest of a stream: its characters need no conversion, so they are
|
|
/// read in blocks from the stream buffer, straight into buf (reusing its
|
|
/// capacity). As the library's stream adapter, this reads the stream buffer
|
|
/// directly and sets eofbit at the end.
|
|
inline void append_stream(std::string& buf, std::istream& is)
|
|
{
|
|
std::streambuf* const sb = is.rdbuf();
|
|
if (sb == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
for (;;)
|
|
{
|
|
const std::size_t used = buf.size();
|
|
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
|
buf.resize(used + room);
|
|
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
|
|
buf.resize(used + static_cast<std::size_t>(got > 0 ? got : 0));
|
|
if (got <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
is.clear(is.rdstate() | std::ios::eofbit);
|
|
}
|
|
|
|
} // namespace view
|
|
} // namespace detail
|
|
NLOHMANN_JSON_NAMESPACE_END
|