Add json_document and json_view: node index, parser, and document (#5620)
* Add json_document and json_view: node index, parser, and document Add json_document and json_view, a read-only, zero-copy index of a JSON text, as the first public slice of the zero-copy view (#5295). A parse produces a flat array of 16-byte nodes in document order, one per value and one per object key. Strings stay in the source text; escaped strings are decoded into an arena. Integers are converted while their digits are in the cache; floats keep only their digit layout and are converted on read. Containers store the size of their subtree, so a reader can step over one in constant time. A document makes a handful of allocations, however many values it has. The parser accepts exactly what json::parse accepts, with every combination of ignore_comments and ignore_trailing_commas, with and without a trailing NUL, and under JSON_STRICT_NUL_HANDLING. It is portable C++11 and does not depend on byte order. basic_json_document adds parse, parse_copy, accept, read (reuses a document's memory), root, is_discarded, source, owns_source, node_count, memory_usage, and shrink_to_fit. basic_json_view adds type, the is_* queries, operator bool, size, empty, materialize, and source_offset. A parse error throws the same exception basic_json::parse would throw for the same input, message and position included. detail::abi_config keeps JSON_STRICT_NUL_HANDLING and JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON readable after json.hpp undefines them, in the ABI namespace so they always match the basic_json in use. A NUL byte that ends a // comment is the end of the input, as in parse() since #5696. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix document_data for old Clang (3.4-3.6) Drop the empty braced NSDMIs of the std::string members: old Clang rejects the defaulted constructor when it is used by a member initializer before the end of the class definition. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Exclude libstdc++'s string_view comparison from the sign-change check basic_string_view::_S_compare stores the difference of two lengths in a signed difference_type on purpose, which -fsanitize=integer reports for every comparison with a shorter view (test-json_view_cpp17). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Reject integer lengths in json_document::parse parse(ptr, len) compiled: len converted to allow_exceptions, and ptr was read as a C string, past the end of a buffer without a terminating NUL. Delete the overloads of parse, parse_copy, accept, and read that take an integer other than bool where the flags are expected. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Copy const rvalue strings and document the accepted inputs json_document::parse(std::move(const_string)) failed to compile with "no matching read_kind": only a non-const rvalue std::string can be moved from. Treat a const rvalue as a copied byte container. parse() does not accept everything BasicJsonType::parse() does: a FILE* and pointers to or arrays of wide characters are rejected at compile time. List the supported inputs instead. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove the conversion to bool from json_view explicit operator bool meant "refers to a value", which silently differs from what a basic_json converts to. Use !v.is_discarded() instead. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Delete json_document::root() on temporary documents auto v = json_document::parse(text).root() compiled and left the view dangling. Delete the overload for rvalue documents, take a named document in the tests, and document the lifetime rule. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Store node integers by halves on big-endian targets The non-little-endian path of the node writer wrote the integer's native word over len and next, so len got the high half there. Compose and split the value explicitly (len is the low half, next the high half); the little-endian path stays a plain memcpy. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Check the node array size for overflow reserve(n) and the growth of the index computed n * sizeof(node) without a check, which wraps around on 32-bit targets for inputs of about 1 GiB and allocates a too small array. Throw std::bad_alloc for a count beyond the address space and clamp the growth step to it. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * State the exact input size limit of json_document The check rejects inputs of 0xFFFFFFF0 bytes or more, but the exception message and the documentation said 4 GiB. Name the limit once (max_input_size), and state 4 GiB minus 16 bytes in the message and the documentation. Test the limit with a container that only claims the size. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove the unused legacy_discarded_value_comparison from abi_config Nothing reads it: the view does not compare values. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Align the banner comment of json_view.hpp Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fuzz json_document with exact-size buffers and parse options The fuzzer only parsed a std::string with default options. Also parse an exact-size byte vector, which has no NUL after its last byte and takes the bounds-checked path, and derive ignore_comments and ignore_trailing_commas from the first input byte, comparing against json::parse and json::accept with the same options. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Give document_data a user-provided constructor Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix clang-tidy findings in unit-json_view_builder.cpp Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Annotate intentional patterns in unit-json_view.cpp for clang-tidy Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Suppress Flawfinder's read() finding on the deleted overload Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Check the steps of the deep-nesting test separately Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Make max_input_size a function (GCC -Wunused-const-variable) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Make json_view.hpp pass include-what-you-use ci_single_binaries runs IWYU with --error on every header. For json_view.hpp it suggested adding <array> (std::array is used), the headers that json.hpp already provides (abi_config, abi_macros, input_adapters, json_pointer, cpp_future, string_concat, value_t, json_fwd), and <version> for std::nullptr_t, and removing <cstddef>. - include <array> - keep <cstddef> (nullptr_t, size_t; IWYU attributes them to <version> and <cstring>) - tell IWYU not to suggest the headers that json.hpp provides: the amalgamated json_view.hpp only includes json.hpp, so including them here would duplicate their definitions - export json.hpp, keep macro_unscope.hpp, and drop the unused forward declaration of the document class Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Read the node array base after emit() in the view builder's open() emit() moves the node array when it grows, and the subtraction read base in the same expression, so the order was unspecified. MSVC Release builds without forced inlining read the old base; the container index then pointed outside the array and close() wrote out of bounds. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Silence a clang-tidy use-after-move finding in the json_view test const_text is const, so std::move does not move from it; the second call is part of the const rvalue test. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Regenerate the amalgamated headers Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Address the review of the view builder and document 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> * Apply the review's wording fixes to the json_view documentation - Copying is disabled because of its cost, not because of two documents borrowing the same buffer; the unique_ptr follows from move-only. - Inputs: streams, FILE*, and wide characters (strings, arrays, pointers) are all supported and owned; point to parse_copy() for owned copies. - The error replay only runs for failing input, builds no values, and is skipped with allow_exceptions = false. - read() writes copied inputs into the previous owned copy. - Views can dangle like std::string_view, and why there is no shared/weak pointer design. - The 4 GiB limit (16-byte nodes, 16-byte scanner steps), streams being read to the end first, which macros apply, why materialize() has no source positions, and wording nits (accept() vs basic_json::accept(), is_binary, is_discarded, see-also lists, size table, "back into"). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Say that json_view converts integers while parsing Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Address the review: null inline nodes, trait spelling, and max() in json_view - inline_nodes is null when the document has no inline nodes, instead of pointing past the end of the allocation; shrink_to_fit() no longer passes it to memcpy() for an empty node array - is_byte_char and is_wide_char use conjunction/disjunction, which astyle leaves unspaced - append_stream() uses (std::max) for the count read Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Include materialize.hpp in the view builder test The test calls detail::view::materialize(), which only the single header pulled in; the multiple-header build (JSON_TEST_USING_MULTIPLE_HEADERS) failed with "'materialize' is not a member of detail::view". Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Rename emit() in the view builder for Qt compatibility Qt defines emit as an empty macro, so a Qt header included before json_view.hpp broke the declaration of the builder's emit(), see issue #5865. It is now called push_node(). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Do not copy an empty run into an unallocated arena memcpy() must not be passed a null pointer even for zero bytes, which arena_run() did for an empty run while the arena was still empty (ci_test_clang_sanitizer: "null pointer passed as argument 1" in builder.hpp). Only the slow path can see such a run. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Exclude the view builder test from Infer's reports Infer's std::unique_ptr model confuses the values that the test's build() helper returns by value and reports USE_AFTER_DELETE for one of them after the destructor of an unrelated temporary (ci_infer, 12 reports, all in tests/src/unit-json_view_builder.cpp). AddressSanitizer is clean on the same tests. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Skip the deleted-overload checks of json_view on MSVC 2015 MSVC 2015 does not treat selecting a deleted function in decltype as a substitution failure, so is_detected reports the deleted parse/parse_copy/accept/read(ptr, len) overloads as callable and the static_asserts in "integer arguments do not compile" fail (AppVeyor VS2015 jobs). Calling them still fails to compile there. Guard the negative checks as unit-delete_deprecated_functions.cpp does. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
a4974e72c1
commit
122d718330
146 files changed
+10787
-17
No files matched your search
@@ -0,0 +1,33 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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 <nlohmann/detail/abi_macros.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
@brief the configuration macros that json_view.hpp reads
|
||||
|
||||
json.hpp undefines these macros at its end (see macro_unscope.hpp), so code
|
||||
that builds on the library after it (json_view.hpp) reads them here. A macro
|
||||
is added when the view starts to depend on it. Like the
|
||||
macros, they are part of the ABI namespace, so they always match the
|
||||
basic_json they are used with.
|
||||
*/
|
||||
struct abi_config
|
||||
{
|
||||
/// JSON_STRICT_NUL_HANDLING: a null byte is an error, not the end of input
|
||||
static constexpr bool strict_nul_handling = JSON_STRICT_NUL_HANDLING != 0;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,165 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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 <array> // array
|
||||
#include <cstddef> // size_t
|
||||
#include <cstring> // memcpy
|
||||
#include <limits> // numeric_limits
|
||||
#include <new> // bad_alloc, operator new, placement new
|
||||
#include <string> // string
|
||||
#include <vector> // vector
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
#include <nlohmann/detail/view/node.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
/// storage of a parsed document; heap-allocated (header and an initial node
|
||||
/// array in one block) so that views survive moves of the owning document
|
||||
struct document_data
|
||||
{
|
||||
const char* src = nullptr;
|
||||
std::size_t size = 0;
|
||||
node* nodes = nullptr; ///< the node index (see node.hpp)
|
||||
std::size_t node_count = 0;
|
||||
std::size_t node_capacity = 0;
|
||||
node* inline_nodes = nullptr; ///< node array allocated together with this header
|
||||
std::size_t inline_capacity = 0;
|
||||
std::size_t max_depth = 0; ///< deepest nesting of arrays and objects (the root container is depth 1)
|
||||
std::vector<char> arena; ///< decoded strings that contained escapes (no terminator: they are handed out with their length)
|
||||
std::string owned; ///< owned copy of the input, if any (a std::string, so that its NUL serves as the parser's sentinel)
|
||||
std::array<const char*, 4> base = {{nullptr, nullptr, nullptr, nullptr}}; ///< string bases: source, arena (indexed by flags & node_flags::storage)
|
||||
bool discarded = true;
|
||||
|
||||
/// One allocation for the header and room for `count` nodes, so that a
|
||||
/// small document needs one allocation instead of two. Large documents get
|
||||
/// a separate node array instead (so it can be trimmed). When a document
|
||||
/// outgrows the inline array, that array stays unused: at most 256 nodes
|
||||
/// (4 KiB), and only for documents whose estimate was too small.
|
||||
static document_data* create(std::size_t count)
|
||||
{
|
||||
count = count <= 256 ? count : 0;
|
||||
void* mem = ::operator new (sizeof(document_data) + (count * sizeof(node)));
|
||||
auto* d = new (mem) document_data(); // NOLINT(cppcoreguidelines-owning-memory): owned by the returned pointer, freed by deleter
|
||||
// (aligned: sizeof is a multiple of the alignment; through void*, as GCC's -Wcast-align wants)
|
||||
// (null without inline nodes, not a pointer past the end of the allocation)
|
||||
d->inline_nodes = count == 0 ? nullptr : static_cast<node*>(static_cast<void*>(static_cast<char*>(mem) + sizeof(document_data))); // NOLINT(bugprone-casting-through-void)
|
||||
d->inline_capacity = count;
|
||||
d->nodes = d->inline_nodes;
|
||||
d->node_capacity = count;
|
||||
return d;
|
||||
}
|
||||
|
||||
struct deleter
|
||||
{
|
||||
void operator()(document_data* d) const noexcept
|
||||
{
|
||||
d->~document_data();
|
||||
::operator delete (d);
|
||||
}
|
||||
};
|
||||
|
||||
/// user-provided so that the class-type members can be initialized in the
|
||||
/// member initialization list (-Weffc++ asks for it, and old GCC rejects a
|
||||
/// defaulted constructor whose exception specification differs from the
|
||||
/// implicit one); they cannot take default member initializers, which old
|
||||
/// Clang (3.4-3.6) rejects. The other members have default member
|
||||
/// initializers. noexcept: create() constructs into raw memory and could not
|
||||
/// release it if this threw (the std::vector and std::string default
|
||||
/// constructors do not allocate).
|
||||
document_data() noexcept
|
||||
: arena() // NOLINT(readability-redundant-member-init)
|
||||
, owned() // NOLINT(readability-redundant-member-init)
|
||||
{}
|
||||
document_data(const document_data&) = delete;
|
||||
document_data(document_data&&) = delete;
|
||||
document_data& operator=(const document_data&) = delete;
|
||||
document_data& operator=(document_data&&) = delete;
|
||||
~document_data()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
void release() noexcept
|
||||
{
|
||||
if (nodes != inline_nodes)
|
||||
{
|
||||
::operator delete (nodes);
|
||||
}
|
||||
nodes = inline_nodes;
|
||||
node_capacity = inline_capacity;
|
||||
}
|
||||
|
||||
/// the largest node count whose size in bytes fits a std::size_t
|
||||
static constexpr std::size_t max_nodes() noexcept
|
||||
{
|
||||
return (std::numeric_limits<std::size_t>::max)() / sizeof(node);
|
||||
}
|
||||
|
||||
[[noreturn]] NLOHMANN_VIEW_NOINLINE static void throw_bad_alloc()
|
||||
{
|
||||
NLOHMANN_VIEW_THROW(std::bad_alloc());
|
||||
}
|
||||
|
||||
/// make room for n nodes; keeps the first node_count nodes (throws
|
||||
/// std::bad_alloc for a count that does not fit the address space,
|
||||
/// instead of wrapping around in n * sizeof(node))
|
||||
void reserve(std::size_t n)
|
||||
{
|
||||
if (n <= node_capacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (NLOHMANN_VIEW_UNLIKELY(n > max_nodes()))
|
||||
{
|
||||
throw_bad_alloc();
|
||||
}
|
||||
node* fresh = static_cast<node*>(::operator new (n * sizeof(node)));
|
||||
if (node_count != 0)
|
||||
{
|
||||
std::memcpy(fresh, nodes, node_count * sizeof(node));
|
||||
}
|
||||
release();
|
||||
nodes = fresh;
|
||||
node_capacity = n;
|
||||
}
|
||||
|
||||
const char* str(const node& n) const noexcept
|
||||
{
|
||||
return base[n.flags & node_flags::storage] + n.off;
|
||||
}
|
||||
|
||||
/// the node after n's subtree (containers span `next` nodes, scalars one)
|
||||
static NLOHMANN_VIEW_ALWAYS_INLINE const node* after(const node* n) noexcept
|
||||
{
|
||||
return n + (is_container(*n) ? n->next : 1u);
|
||||
}
|
||||
|
||||
/// first element (array) or first key (object) of a container
|
||||
static NLOHMANN_VIEW_ALWAYS_INLINE const node* first_child(const node* n) noexcept
|
||||
{
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
/// end of the elements of a container
|
||||
static NLOHMANN_VIEW_ALWAYS_INLINE const node* child_end(const node* n) noexcept
|
||||
{
|
||||
return n + n->next;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,167 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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> // min
|
||||
#include <cstddef> // size_t
|
||||
#include <string> // string
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/builder.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
// Exceptions are thrown out of line, so that the accessors that may throw stay
|
||||
// small enough to be inlined.
|
||||
|
||||
[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(int id, const char* prefix, const char* type)
|
||||
{
|
||||
NLOHMANN_VIEW_THROW(type_error::create(id, concat(prefix, type), nullptr));
|
||||
}
|
||||
|
||||
[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_out_of_range(int id, const std::string& msg)
|
||||
{
|
||||
NLOHMANN_VIEW_THROW(out_of_range::create(id, msg, nullptr));
|
||||
}
|
||||
|
||||
[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_iterator(int id, const char* msg)
|
||||
{
|
||||
NLOHMANN_VIEW_THROW(invalid_iterator::create(id, msg, nullptr));
|
||||
}
|
||||
|
||||
/// SAX handler of the error replay: accepts every event without storing it,
|
||||
/// and throws the exception the parser reports
|
||||
template<typename BasicJsonType>
|
||||
struct throwing_sax
|
||||
{
|
||||
using number_integer_t = typename BasicJsonType::number_integer_t;
|
||||
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
|
||||
using number_float_t = typename BasicJsonType::number_float_t;
|
||||
using string_t = typename BasicJsonType::string_t;
|
||||
using binary_t = typename BasicJsonType::binary_t;
|
||||
|
||||
bool null()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool boolean(bool /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool number_integer(number_integer_t /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool number_unsigned(number_unsigned_t /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool string(string_t& /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool binary(binary_t& /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start_object(std::size_t /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool key(string_t& /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end_object()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start_array(std::size_t /*unused*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end_array()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// (a template, as in json_sax_dom_parser: the parser passes the concrete
|
||||
/// exception type, which is thrown as is)
|
||||
template<class Exception>
|
||||
[[noreturn]] bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const Exception& ex)
|
||||
{
|
||||
NLOHMANN_VIEW_THROW(ex);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
@brief throw the exception BasicJsonType::parse would throw for this input
|
||||
|
||||
The view accepts exactly the inputs parse() accepts, so on a failure the
|
||||
library parser is run on the same bytes: it throws the exception parse() would
|
||||
throw, with the same message, position, and "last read" token. The replay
|
||||
uses a SAX handler that stores nothing, so it only lexes and parses up to the
|
||||
error, without building values. The error path is cold, so this costs nothing
|
||||
on valid input. Should parse() accept the input nevertheless (a bug), the
|
||||
view's own failure is reported.
|
||||
*/
|
||||
template<typename BasicJsonType>
|
||||
[[noreturn]] NLOHMANN_VIEW_NOINLINE void throw_parse_failure(const parse_failure& f, const char* src, std::size_t size,
|
||||
bool ignore_comments, bool ignore_trailing_commas)
|
||||
{
|
||||
if (f.code == error_code::input_too_large)
|
||||
{
|
||||
// (the limit is detail::view::max_input_size(): 4 GiB minus 16 bytes)
|
||||
NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4294967280 bytes or more is not supported by json_document", nullptr));
|
||||
}
|
||||
throwing_sax<BasicJsonType> sax;
|
||||
const bool accepted = BasicJsonType::sax_parse(src, src + size, &sax, input_format_t::json, true, ignore_comments, ignore_trailing_commas);
|
||||
// LCOV_EXCL_START (only if parse() accepts what the view rejects: a bug)
|
||||
static_cast<void>(accepted);
|
||||
|
||||
position_t pos;
|
||||
const std::size_t off = (std::min)(f.offset, size);
|
||||
pos.chars_read_total = off + 1;
|
||||
std::size_t line_start = 0;
|
||||
for (std::size_t i = 0; i < off; ++i)
|
||||
{
|
||||
if (src[i] == '\n')
|
||||
{
|
||||
++pos.lines_read;
|
||||
line_start = i + 1;
|
||||
}
|
||||
}
|
||||
pos.chars_read_current_line = off + 1 - line_start;
|
||||
NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr));
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,159 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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 : conjunction<std::is_integral<T>, std::integral_constant<bool, 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 : disjunction<std::is_same<T, wchar_t>, std::is_same<T, char16_t>, std::is_same<T, char32_t>> {};
|
||||
|
||||
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>((std::max)(got, std::streamsize{0})));
|
||||
if (got <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
is.clear(is.rdstate() | std::ios::eofbit);
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,71 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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
|
||||
|
||||
// Macros of json_view.hpp and its detail headers. json.hpp undefines its own
|
||||
// macros at its end (macro_unscope.hpp), so the view defines the few it needs
|
||||
// under its own prefix; json_view.hpp undefines them all at its end
|
||||
// (detail/view/macro_unscope.hpp). Configuration that json.hpp undefines is
|
||||
// read from detail::abi_config instead.
|
||||
|
||||
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
|
||||
#define NLOHMANN_VIEW_HAS_CPP_17 1
|
||||
#else
|
||||
#define NLOHMANN_VIEW_HAS_CPP_17 0
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#define NLOHMANN_VIEW_LIKELY(x) __builtin_expect(!!(x), 1)
|
||||
#define NLOHMANN_VIEW_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
||||
#define NLOHMANN_VIEW_ALWAYS_INLINE inline __attribute__((always_inline))
|
||||
#define NLOHMANN_VIEW_NOINLINE __attribute__((noinline))
|
||||
#elif defined(_MSC_VER)
|
||||
#define NLOHMANN_VIEW_LIKELY(x) (x)
|
||||
#define NLOHMANN_VIEW_UNLIKELY(x) (x)
|
||||
#define NLOHMANN_VIEW_ALWAYS_INLINE __forceinline
|
||||
#define NLOHMANN_VIEW_NOINLINE __declspec(noinline)
|
||||
#else
|
||||
#define NLOHMANN_VIEW_LIKELY(x) (x)
|
||||
#define NLOHMANN_VIEW_UNLIKELY(x) (x)
|
||||
#define NLOHMANN_VIEW_ALWAYS_INLINE inline
|
||||
#define NLOHMANN_VIEW_NOINLINE
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#define NLOHMANN_VIEW_NODISCARD __attribute__((warn_unused_result))
|
||||
#elif defined(_MSC_VER)
|
||||
#define NLOHMANN_VIEW_NODISCARD _Check_return_
|
||||
#else
|
||||
#define NLOHMANN_VIEW_NODISCARD
|
||||
#endif
|
||||
|
||||
// exceptions as in json.hpp (JSON_NOEXCEPTION, JSON_THROW_USER)
|
||||
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
|
||||
#define NLOHMANN_VIEW_THROW(exception) throw exception
|
||||
#else
|
||||
#include <cstdlib>
|
||||
// (the exception is built first, so that the arguments of the throwing
|
||||
// helpers count as used; the program ends anyway)
|
||||
#define NLOHMANN_VIEW_THROW(exception) (static_cast<void>(exception), std::abort())
|
||||
#endif
|
||||
#if defined(JSON_THROW_USER)
|
||||
#undef NLOHMANN_VIEW_THROW
|
||||
#define NLOHMANN_VIEW_THROW JSON_THROW_USER
|
||||
#endif
|
||||
|
||||
// the parser stores a node's first word at once where the layout of `node` is
|
||||
// known to be little-endian (MSVC targets are); elsewhere field by field
|
||||
#if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(_MSC_VER)
|
||||
#define NLOHMANN_VIEW_LITTLE_ENDIAN 1
|
||||
#else
|
||||
#define NLOHMANN_VIEW_LITTLE_ENDIAN 0
|
||||
#endif
|
||||
|
||||
/// sixteen checks at fixed offsets 0..15
|
||||
#define NLOHMANN_VIEW_REPEAT16(X) X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7) X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15)
|
||||
@@ -0,0 +1,21 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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
|
||||
|
||||
// undefine the macros of detail/view/macro_scope.hpp (at the end of json_view.hpp)
|
||||
|
||||
#undef NLOHMANN_VIEW_HAS_CPP_17
|
||||
#undef NLOHMANN_VIEW_LIKELY
|
||||
#undef NLOHMANN_VIEW_UNLIKELY
|
||||
#undef NLOHMANN_VIEW_ALWAYS_INLINE
|
||||
#undef NLOHMANN_VIEW_NOINLINE
|
||||
#undef NLOHMANN_VIEW_NODISCARD
|
||||
#undef NLOHMANN_VIEW_THROW
|
||||
#undef NLOHMANN_VIEW_LITTLE_ENDIAN
|
||||
#undef NLOHMANN_VIEW_REPEAT16
|
||||
@@ -0,0 +1,144 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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 <cstdint> // int64_t, uint8_t
|
||||
#include <string> // string
|
||||
#include <vector> // vector
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/document_data.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
#include <nlohmann/detail/view/node.hpp>
|
||||
#include <nlohmann/detail/view/number.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
/*!
|
||||
@brief the basic_json value of the subtree at n
|
||||
|
||||
The subtree is replayed into the SAX handler that parse() uses to build its
|
||||
values, so the result is the value parse() would produce: duplicate keys keep
|
||||
the last value, and with JSON_DIAGNOSTICS the parent pointers are set. It is
|
||||
iterative, so the nesting depth is limited by memory only, as for parse().
|
||||
Without a lexer the handler records no source positions
|
||||
(JSON_DIAGNOSTIC_POSITIONS).
|
||||
*/
|
||||
template<typename BasicJsonType>
|
||||
BasicJsonType materialize(const document_data& d, const node* n)
|
||||
{
|
||||
using string_t = typename BasicJsonType::string_t;
|
||||
using sax_t = json_sax_dom_parser<BasicJsonType, iterator_input_adapter<const char*>>;
|
||||
|
||||
BasicJsonType result;
|
||||
sax_t sax(result, true);
|
||||
const string_t no_token{};
|
||||
// the ends of the open containers, and whether they are objects; sized
|
||||
// once from the document's deepest nesting (for a subtree an upper bound)
|
||||
struct open_container
|
||||
{
|
||||
const node* end;
|
||||
bool is_object;
|
||||
};
|
||||
std::vector<open_container> open((std::max)(d.max_depth, std::size_t{1}));
|
||||
std::size_t depth = 0;
|
||||
for (;;)
|
||||
{
|
||||
switch (static_cast<value_t>(n->kind))
|
||||
{
|
||||
case value_t::object:
|
||||
case value_t::array:
|
||||
{
|
||||
const bool object = n->kind == static_cast<std::uint8_t>(value_t::object);
|
||||
if (object)
|
||||
{
|
||||
sax.start_object(n->len);
|
||||
}
|
||||
else
|
||||
{
|
||||
sax.start_array(n->len);
|
||||
}
|
||||
if (NLOHMANN_VIEW_UNLIKELY(depth == open.size()))
|
||||
{
|
||||
open.resize(depth * 2); // (deeper than the document's recorded nesting)
|
||||
}
|
||||
open[depth++] = open_container{document_data::child_end(n), object};
|
||||
n = document_data::first_child(n);
|
||||
break;
|
||||
}
|
||||
case value_t::string:
|
||||
{
|
||||
string_t s(d.str(*n), n->len);
|
||||
sax.string(s);
|
||||
++n;
|
||||
break;
|
||||
}
|
||||
case value_t::number_integer:
|
||||
sax.number_integer(static_cast<typename BasicJsonType::number_integer_t>(static_cast<std::int64_t>(integer_bits(*n))));
|
||||
++n;
|
||||
break;
|
||||
case value_t::number_unsigned:
|
||||
sax.number_unsigned(static_cast<typename BasicJsonType::number_unsigned_t>(integer_bits(*n)));
|
||||
++n;
|
||||
break;
|
||||
case value_t::number_float:
|
||||
sax.number_float(float_value<typename BasicJsonType::number_float_t>(d.str(*n), *n), no_token);
|
||||
++n;
|
||||
break;
|
||||
case value_t::boolean:
|
||||
sax.boolean((n->flags & node_flags::is_true) != 0);
|
||||
++n;
|
||||
break;
|
||||
case value_t::null:
|
||||
case value_t::binary:
|
||||
case value_t::discarded:
|
||||
default:
|
||||
sax.null();
|
||||
++n;
|
||||
break;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
if (depth == 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (n != open[depth - 1].end)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (open[depth - 1].is_object)
|
||||
{
|
||||
sax.end_object();
|
||||
}
|
||||
else
|
||||
{
|
||||
sax.end_array();
|
||||
}
|
||||
--depth;
|
||||
}
|
||||
if (open[depth - 1].is_object)
|
||||
{
|
||||
// the key of the next member
|
||||
string_t key(d.str(*n), n->len);
|
||||
sax.key(key);
|
||||
++n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,115 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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 <cstddef> // size_t
|
||||
#include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
|
||||
#include <cstring> // memcpy
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
/// The largest input a document accepts, in bytes. Offsets and node counts are
|
||||
/// 32 bits wide; the limit keeps 16 bytes (the width of the scanner's steps)
|
||||
/// below 2^32, so that a position one step past the end of the text fits.
|
||||
constexpr std::size_t max_input_size() noexcept
|
||||
{
|
||||
return 0xFFFFFFEFu;
|
||||
}
|
||||
|
||||
/// node flags
|
||||
struct node_flags
|
||||
{
|
||||
static constexpr std::uint8_t escaped = 1; ///< string payload lives in the decode arena, not the source
|
||||
static constexpr std::uint8_t storage = 3; ///< mask: where a string or number token lives (index into document_data::base)
|
||||
static constexpr std::uint8_t is_true = 4; ///< boolean value
|
||||
};
|
||||
|
||||
/// One entry of the flat index, in document order. An object's members are
|
||||
/// stored as key node followed by the value's subtree. Integers keep their
|
||||
/// converted 64-bit value in the len/next bytes (the node after a scalar is
|
||||
/// always the next one, and the token length follows from `extra`).
|
||||
struct node
|
||||
{
|
||||
std::uint8_t kind; ///< value_t
|
||||
std::uint8_t flags; ///< node_flags
|
||||
std::uint16_t extra; ///< numbers: integer digits (low byte) and fraction digits (high byte), 255 = "many"; otherwise 0
|
||||
std::uint32_t off; ///< source offset (string content, number token, literal, bracket); arena offset if node_flags::escaped
|
||||
std::uint32_t len; ///< string: decoded bytes; float: token bytes; array/object: element count
|
||||
std::uint32_t next; ///< array/object: number of nodes of the subtree (its extent in the enclosing sequence)
|
||||
};
|
||||
static_assert(sizeof(node) == 16, "node must stay 16 bytes");
|
||||
|
||||
// the node kinds are value_t values; the test below (object and array are 1
|
||||
// and 2) and those of the number kinds depend on this numbering
|
||||
static_assert(static_cast<std::uint8_t>(value_t::null) == 0 && static_cast<std::uint8_t>(value_t::object) == 1
|
||||
&& static_cast<std::uint8_t>(value_t::array) == 2 && static_cast<std::uint8_t>(value_t::string) == 3
|
||||
&& static_cast<std::uint8_t>(value_t::boolean) == 4 && static_cast<std::uint8_t>(value_t::number_integer) == 5
|
||||
&& static_cast<std::uint8_t>(value_t::number_unsigned) == 6 && static_cast<std::uint8_t>(value_t::number_float) == 7,
|
||||
"the node format depends on the numbering of value_t");
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE bool is_container(const node& n) noexcept
|
||||
{
|
||||
return static_cast<unsigned>(n.kind) - 1u <= 1u;
|
||||
}
|
||||
|
||||
/// the converted value of an integer node: len is its low half, next its high
|
||||
/// half (on little-endian targets the two words are the value in memory)
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE std::uint64_t integer_bits(const node& n) noexcept
|
||||
{
|
||||
#if NLOHMANN_VIEW_LITTLE_ENDIAN
|
||||
std::uint64_t v = 0;
|
||||
std::memcpy(&v, reinterpret_cast<const unsigned char*>(&n) + 8, 8); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
return v;
|
||||
#else
|
||||
return static_cast<std::uint64_t>(n.len) | (static_cast<std::uint64_t>(n.next) << 32);
|
||||
#endif
|
||||
}
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE void set_integer_bits(node& n, std::uint64_t v) noexcept
|
||||
{
|
||||
#if NLOHMANN_VIEW_LITTLE_ENDIAN
|
||||
std::memcpy(reinterpret_cast<unsigned char*>(&n) + 8, &v, 8); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
#else
|
||||
n.len = static_cast<std::uint32_t>(v);
|
||||
n.next = static_cast<std::uint32_t>(v >> 32);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// token length of a number node
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE std::uint32_t number_length(const node& n) noexcept
|
||||
{
|
||||
return n.kind == static_cast<std::uint8_t>(value_t::number_float) ? n.len
|
||||
: (n.extra & 0xFFu) + (n.kind == static_cast<std::uint8_t>(value_t::number_integer) ? 1u : 0u);
|
||||
}
|
||||
|
||||
/// estimated number of nodes for an input of `size` bytes (one node per ~12
|
||||
/// bytes covers typical documents without regrowth)
|
||||
inline std::size_t estimate_nodes(std::size_t size) noexcept
|
||||
{
|
||||
return (size / 12) + 16;
|
||||
}
|
||||
|
||||
/// estimated number of nodes for the input [src, src + size): pretty-printed
|
||||
/// input (whitespace after the first byte) needs about a node per 12 bytes,
|
||||
/// minified input up to one per 4 (yyjson tells the two apart the same way)
|
||||
inline std::size_t estimate_nodes(const char* src, std::size_t size) noexcept
|
||||
{
|
||||
return size >= 2 && (src[1] == ' ' || src[1] == '\n' || src[1] == '\r' || src[1] == '\t') ? estimate_nodes(size) : (size / 4) + 16;
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,67 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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 <cstddef> // size_t
|
||||
#include <string> // string
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
#include <nlohmann/detail/view/node.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
/*!
|
||||
@brief the value of the float token of a node, as parse() converts it
|
||||
|
||||
Uses the lexer's conversion (detail::convert_float), so that the values are
|
||||
bit-identical to parse(): float and double are converted without allocation
|
||||
and independent of the locale. The digit layout recorded while parsing locates
|
||||
the decimal point and the exponent without scanning the token.
|
||||
*/
|
||||
template<typename FloatType>
|
||||
NLOHMANN_VIEW_NOINLINE FloatType float_value(const char* first, const node& n)
|
||||
{
|
||||
const char* const last = first + n.len;
|
||||
const std::size_t neg = first[0] == '-' ? 1 : 0;
|
||||
const std::size_t int_digits = n.extra & 0xFFu;
|
||||
const std::size_t frac_digits = n.extra >> 8u;
|
||||
std::size_t dot = std::string::npos;
|
||||
std::size_t mantissa_end = n.len;
|
||||
if (int_digits != 255 && frac_digits != 255)
|
||||
{
|
||||
dot = frac_digits != 0 ? neg + int_digits : std::string::npos;
|
||||
mantissa_end = neg + int_digits + (frac_digits != 0 ? 1 + frac_digits : 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// more digits than the layout records: locate them
|
||||
for (std::size_t i = 0; i < n.len; ++i)
|
||||
{
|
||||
if (first[i] == '.')
|
||||
{
|
||||
dot = i;
|
||||
}
|
||||
else if (first[i] == 'e' || first[i] == 'E')
|
||||
{
|
||||
mantissa_end = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return convert_float<FloatType>(first, last, dot, mantissa_end);
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,206 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.12.0
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-FileCopyrightText: 2020 YaoYuan <https://github.com/ibireme/yyjson>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array> // array
|
||||
#include <cstddef> // size_t
|
||||
#include <cstdint> // uint8_t, uint16_t, uint64_t
|
||||
#include <cstring> // memcpy
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
|
||||
// Scanning primitives of the view's parser. The unrolled checks at fixed
|
||||
// offsets follow yyjson (https://github.com/ibireme/yyjson, MIT license): the
|
||||
// loads do not depend on each other, so the CPU can run ahead. Words are read
|
||||
// with read_eight_bytes(), so nothing here depends on the byte order.
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
/// 1 for bytes that may appear verbatim in a string: 0x20..0x7F except '"' and '\\'
|
||||
inline const std::uint8_t* string_plain() noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> table =
|
||||
{
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00..0x1F
|
||||
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20..0x3F ('"')
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 0x40..0x5F ('\\')
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60..0x7F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80..0x9F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xA0..0xBF
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xC0..0xDF
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xE0..0xFF
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE bool is_digit(unsigned char c) noexcept
|
||||
{
|
||||
return static_cast<unsigned char>(c - '0') <= 9;
|
||||
}
|
||||
|
||||
/// two bytes as they are in memory (only compared with byte-symmetric patterns)
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE std::uint16_t load16(const unsigned char* p) noexcept
|
||||
{
|
||||
std::uint16_t w = 0;
|
||||
std::memcpy(&w, p, 2);
|
||||
return w;
|
||||
}
|
||||
|
||||
/// Advance over plain string bytes and well-formed UTF-8. Stops at a quote,
|
||||
/// a backslash, a control character, ill-formed UTF-8, or the end. The first
|
||||
/// 16 bytes are checked one by one, so that the position advances by
|
||||
/// constants in predicted branches (most strings are short); longer runs
|
||||
/// continue eight bytes at a time.
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE const unsigned char* scan_string_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint8_t* plain = string_plain();
|
||||
for (;;)
|
||||
{
|
||||
if (e - p >= 16)
|
||||
{
|
||||
#define NLOHMANN_VIEW_STEP(i) if (NLOHMANN_VIEW_LIKELY(plain[p[i]] != 0)) {} else { p += (i); goto stop; }
|
||||
NLOHMANN_VIEW_REPEAT16(NLOHMANN_VIEW_STEP)
|
||||
#undef NLOHMANN_VIEW_STEP
|
||||
p += 16;
|
||||
while (e - p >= 8)
|
||||
{
|
||||
const std::uint64_t special = swar_string_special(read_eight_bytes(p));
|
||||
if (special != 0)
|
||||
{
|
||||
p += count_trailing_zeros(special) / 8;
|
||||
goto stop;
|
||||
}
|
||||
p += 8;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
while (p != e && plain[*p] != 0)
|
||||
{
|
||||
++p;
|
||||
}
|
||||
if (p == e)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
stop:
|
||||
if (*p < 0x80)
|
||||
{
|
||||
return p; // quote, backslash, or control character
|
||||
}
|
||||
// non-ASCII: a run of well-formed sequences (the library's check, so
|
||||
// that exactly what json::parse accepts is accepted)
|
||||
do
|
||||
{
|
||||
const std::size_t n = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (n == 0)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
p += n;
|
||||
}
|
||||
while (p != e && *p >= 0x80);
|
||||
}
|
||||
}
|
||||
|
||||
/// advance over ASCII digits
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE const unsigned char* skip_digits(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
while (e - p >= 16)
|
||||
{
|
||||
#define NLOHMANN_VIEW_STEP(i) if (NLOHMANN_VIEW_LIKELY(is_digit(p[i]))) {} else { return p + (i); }
|
||||
NLOHMANN_VIEW_REPEAT16(NLOHMANN_VIEW_STEP)
|
||||
#undef NLOHMANN_VIEW_STEP
|
||||
p += 16;
|
||||
}
|
||||
while (p != e && is_digit(*p))
|
||||
{
|
||||
++p;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/// powers of ten up to 10^19 as integers
|
||||
inline std::uint64_t int_pow10(unsigned k) noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 20> table =
|
||||
{
|
||||
{
|
||||
1u,
|
||||
10u,
|
||||
100u,
|
||||
1000u,
|
||||
10000u,
|
||||
100000u,
|
||||
1000000u,
|
||||
10000000u,
|
||||
100000000u,
|
||||
1000000000u,
|
||||
10000000000u,
|
||||
100000000000u,
|
||||
1000000000000u,
|
||||
10000000000000u,
|
||||
100000000000000u,
|
||||
1000000000000000u,
|
||||
10000000000000000u,
|
||||
100000000000000000u,
|
||||
1000000000000000000u,
|
||||
10000000000000000000u
|
||||
}
|
||||
};
|
||||
return table[k];
|
||||
}
|
||||
|
||||
/// value of 0 < k < 8 digits at p in one step if [p, p + 8) lies below
|
||||
/// limit, else one digit at a time (whole blocks of eight digits are read by
|
||||
/// parse_upto19() directly)
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE std::uint64_t parse_upto8(const unsigned char* p, unsigned k, const unsigned char* limit) noexcept
|
||||
{
|
||||
if (NLOHMANN_VIEW_LIKELY(limit - p >= 8))
|
||||
{
|
||||
// move the k digits to the top and pad the vacated low bytes with '0'
|
||||
const unsigned shift = 8 * (8 - k);
|
||||
return parse_eight_digits((read_eight_bytes(p) << shift) | (0x3030303030303030u >> (8 * k)));
|
||||
}
|
||||
std::uint64_t v = 0;
|
||||
for (unsigned i = 0; i < k; ++i)
|
||||
{
|
||||
v = (v * 10) + static_cast<std::uint64_t>(p[i] - '0');
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/// value of k <= 19 digits at p
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE std::uint64_t parse_upto19(const unsigned char* p, unsigned k, const unsigned char* limit) noexcept
|
||||
{
|
||||
std::uint64_t w = 0;
|
||||
while (k >= 8)
|
||||
{
|
||||
// (eight digits of the token: they lie below limit)
|
||||
w = (w * 100000000u) + parse_eight_digits(read_eight_bytes(p));
|
||||
p += 8;
|
||||
k -= 8;
|
||||
}
|
||||
if (k != 0)
|
||||
{
|
||||
w = (w * int_pow10(k)) + parse_upto8(p, k, limit);
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -0,0 +1,113 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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> // min
|
||||
#include <cstddef> // size_t
|
||||
#include <cstring> // memcmp, strlen
|
||||
#include <string> // basic_string
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
|
||||
#if NLOHMANN_VIEW_HAS_CPP_17
|
||||
#include <string_view> // string_view
|
||||
#endif
|
||||
#ifndef JSON_NO_IO
|
||||
#include <ostream> // ostream
|
||||
#endif
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
namespace view
|
||||
{
|
||||
|
||||
#if NLOHMANN_VIEW_HAS_CPP_17
|
||||
using string_ref = std::string_view;
|
||||
#else
|
||||
/// minimal C++11 stand-in for std::string_view
|
||||
class string_ref
|
||||
{
|
||||
public:
|
||||
using size_type = std::size_t;
|
||||
using const_iterator = const char*;
|
||||
|
||||
string_ref() noexcept = default;
|
||||
// s must be null-terminated, as for std::string_view(const char*)
|
||||
// flawfinder: ignore
|
||||
string_ref(const char* s) : m_data(s), m_size(std::strlen(s)) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
|
||||
string_ref(const char* s, std::size_t n) noexcept : m_data(s), m_size(n) {}
|
||||
template<typename Traits, typename Alloc>
|
||||
string_ref(const std::basic_string<char, Traits, Alloc>& s) noexcept : m_data(s.data()), m_size(s.size()) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
|
||||
|
||||
const char* data() const noexcept
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
std::size_t size() const noexcept
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
std::size_t length() const noexcept
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
bool empty() const noexcept
|
||||
{
|
||||
return m_size == 0;
|
||||
}
|
||||
const char* begin() const noexcept
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
const char* end() const noexcept
|
||||
{
|
||||
return m_data + m_size;
|
||||
}
|
||||
char operator[](std::size_t i) const noexcept
|
||||
{
|
||||
return m_data[i];
|
||||
}
|
||||
|
||||
template<typename Traits, typename Alloc>
|
||||
explicit operator std::basic_string<char, Traits, Alloc>() const
|
||||
{
|
||||
return std::basic_string<char, Traits, Alloc>(m_data, m_size);
|
||||
}
|
||||
|
||||
friend bool operator==(string_ref a, string_ref b) noexcept
|
||||
{
|
||||
return a.m_size == b.m_size && (a.m_size == 0 || std::memcmp(a.m_data, b.m_data, a.m_size) == 0);
|
||||
}
|
||||
friend bool operator!=(string_ref a, string_ref b) noexcept
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
friend bool operator<(string_ref a, string_ref b) noexcept
|
||||
{
|
||||
const int c = std::memcmp(a.m_data, b.m_data, (std::min)(a.m_size, b.m_size));
|
||||
return c != 0 ? c < 0 : a.m_size < b.m_size;
|
||||
}
|
||||
#ifndef JSON_NO_IO
|
||||
friend std::ostream& operator<<(std::ostream& o, string_ref s)
|
||||
{
|
||||
return o.write(s.m_data, static_cast<std::streamsize>(s.m_size));
|
||||
}
|
||||
#endif
|
||||
|
||||
private:
|
||||
const char* m_data = "";
|
||||
std::size_t m_size = 0;
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace view
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -50,6 +50,7 @@
|
||||
// tests/iwyu_consumer checks this.
|
||||
#include <nlohmann/adl_serializer.hpp> // IWYU pragma: export
|
||||
#include <nlohmann/byte_container_with_subtype.hpp>
|
||||
#include <nlohmann/detail/abi_config.hpp>
|
||||
#include <nlohmann/detail/abi_macros.hpp> // IWYU pragma: export
|
||||
#include <nlohmann/detail/conversions/from_json.hpp> // IWYU pragma: keep
|
||||
#include <nlohmann/detail/conversions/to_json.hpp> // IWYU pragma: keep
|
||||
|
||||
@@ -0,0 +1,685 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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
|
||||
|
||||
/****************************************************************************\
|
||||
* Zero-copy, read-only view of a parsed JSON text. *
|
||||
* *
|
||||
* json_document::parse() builds a flat index of the values of a JSON text *
|
||||
* (16 bytes per value) instead of a tree of basic_json values. Strings and *
|
||||
* numbers stay in the source text; only strings with escapes are decoded, *
|
||||
* into one buffer. json_view is a handle to one value of the document, with *
|
||||
* the read-only part of the basic_json interface; materialize() turns a *
|
||||
* subtree into the basic_json value that parse() would produce. *
|
||||
* *
|
||||
* The source text must outlive a document that borrows it (lvalue byte *
|
||||
* containers, C strings); rvalue strings, streams, and other inputs are *
|
||||
* owned by the document. *
|
||||
\****************************************************************************/
|
||||
|
||||
#ifndef INCLUDE_NLOHMANN_JSON_VIEW_HPP_
|
||||
#define INCLUDE_NLOHMANN_JSON_VIEW_HPP_
|
||||
|
||||
#include <array> // array
|
||||
#include <cstddef> // nullptr_t, size_t // IWYU pragma: keep
|
||||
#include <cstring> // memcpy, strlen
|
||||
#include <istream> // istream
|
||||
#include <iterator> // distance, input_iterator_tag, iterator_traits
|
||||
#include <memory> // unique_ptr
|
||||
#include <string> // string
|
||||
#include <type_traits> // decay, enable_if, integral_constant, is_base_of, is_integral, is_same, remove_cv, remove_extent
|
||||
#include <utility> // forward, move
|
||||
#include <vector> // vector
|
||||
|
||||
#include <nlohmann/json.hpp> // IWYU pragma: export
|
||||
|
||||
// json.hpp provides the library's types and macros used below (it includes
|
||||
// the headers that define them); json_view.hpp must not include them again,
|
||||
// because the amalgamated json_view.hpp only includes json.hpp
|
||||
// IWYU pragma: no_include <version>
|
||||
// IWYU pragma: no_include "nlohmann/detail/abi_config.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/detail/abi_macros.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/detail/input/input_adapters.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/detail/json_pointer.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/detail/meta/cpp_future.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/detail/string_concat.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/detail/value_t.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/json.hpp"
|
||||
// IWYU pragma: no_include "nlohmann/json_fwd.hpp"
|
||||
|
||||
// the view builds on internals of the library: both must be the same version
|
||||
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
|
||||
#error "json_view.hpp requires json.hpp of the same version (3.12.0)"
|
||||
#endif
|
||||
|
||||
#include <nlohmann/detail/view/builder.hpp>
|
||||
#include <nlohmann/detail/view/document_data.hpp>
|
||||
#include <nlohmann/detail/view/errors.hpp>
|
||||
#include <nlohmann/detail/view/input.hpp>
|
||||
#include <nlohmann/detail/view/macro_scope.hpp>
|
||||
#include <nlohmann/detail/view/materialize.hpp>
|
||||
#include <nlohmann/detail/view/node.hpp>
|
||||
#include <nlohmann/detail/view/string_ref.hpp>
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
|
||||
/*!
|
||||
@brief read-only handle to one value of a basic_json_document
|
||||
|
||||
Trivially copyable (two pointers). Valid as long as the document is alive and
|
||||
has not been re-parsed, and as long as a borrowed source text is alive.
|
||||
*/
|
||||
template<typename BasicJsonType>
|
||||
class basic_json_view
|
||||
{
|
||||
using node = detail::view::node;
|
||||
using document_data = detail::view::document_data;
|
||||
|
||||
public:
|
||||
using value_t = detail::value_t;
|
||||
using string_t = typename BasicJsonType::string_t;
|
||||
using number_integer_t = typename BasicJsonType::number_integer_t;
|
||||
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
|
||||
using number_float_t = typename BasicJsonType::number_float_t;
|
||||
using json_pointer = typename BasicJsonType::json_pointer;
|
||||
using size_type = std::size_t;
|
||||
/// std::string_view from C++17 on
|
||||
using string_view_t = detail::view::string_ref;
|
||||
|
||||
/// an invalid view (type() == value_t::discarded)
|
||||
basic_json_view() noexcept = default;
|
||||
|
||||
//////////
|
||||
// type //
|
||||
//////////
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE value_t type() const noexcept
|
||||
{
|
||||
return m_node != nullptr ? static_cast<value_t>(m_node->kind) : value_t::discarded;
|
||||
}
|
||||
|
||||
bool is_null() const noexcept
|
||||
{
|
||||
return type() == value_t::null;
|
||||
}
|
||||
|
||||
bool is_boolean() const noexcept
|
||||
{
|
||||
return type() == value_t::boolean;
|
||||
}
|
||||
|
||||
bool is_number() const noexcept
|
||||
{
|
||||
return is_number_integer() || is_number_float();
|
||||
}
|
||||
|
||||
bool is_number_integer() const noexcept
|
||||
{
|
||||
return type() == value_t::number_integer || type() == value_t::number_unsigned;
|
||||
}
|
||||
|
||||
bool is_number_unsigned() const noexcept
|
||||
{
|
||||
return type() == value_t::number_unsigned;
|
||||
}
|
||||
|
||||
bool is_number_float() const noexcept
|
||||
{
|
||||
return type() == value_t::number_float;
|
||||
}
|
||||
|
||||
bool is_string() const noexcept
|
||||
{
|
||||
return type() == value_t::string;
|
||||
}
|
||||
|
||||
bool is_array() const noexcept
|
||||
{
|
||||
return type() == value_t::array;
|
||||
}
|
||||
|
||||
bool is_object() const noexcept
|
||||
{
|
||||
return type() == value_t::object;
|
||||
}
|
||||
|
||||
/// always false: JSON text has no binary values
|
||||
bool is_binary() const noexcept
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_primitive() const noexcept
|
||||
{
|
||||
return is_null() || is_string() || is_boolean() || is_number();
|
||||
}
|
||||
|
||||
bool is_structured() const noexcept
|
||||
{
|
||||
return is_array() || is_object();
|
||||
}
|
||||
|
||||
/// the root of a failed parse with allow_exceptions == false, or a
|
||||
/// default-constructed view
|
||||
bool is_discarded() const noexcept
|
||||
{
|
||||
return type() == value_t::discarded;
|
||||
}
|
||||
|
||||
//////////////
|
||||
// capacity //
|
||||
//////////////
|
||||
|
||||
/// the number of elements (arrays, objects), 0 for null and discarded,
|
||||
/// 1 otherwise, as basic_json::size()
|
||||
size_type size() const noexcept
|
||||
{
|
||||
switch (type())
|
||||
{
|
||||
case value_t::null:
|
||||
case value_t::discarded:
|
||||
return 0;
|
||||
case value_t::array:
|
||||
case value_t::object:
|
||||
return m_node->len;
|
||||
case value_t::string:
|
||||
case value_t::boolean:
|
||||
case value_t::number_integer:
|
||||
case value_t::number_unsigned:
|
||||
case value_t::number_float:
|
||||
case value_t::binary:
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// as basic_json::empty()
|
||||
bool empty() const noexcept
|
||||
{
|
||||
switch (type())
|
||||
{
|
||||
case value_t::null:
|
||||
case value_t::discarded:
|
||||
return true;
|
||||
case value_t::array:
|
||||
case value_t::object:
|
||||
return m_node->len == 0;
|
||||
case value_t::string:
|
||||
case value_t::boolean:
|
||||
case value_t::number_integer:
|
||||
case value_t::number_unsigned:
|
||||
case value_t::number_float:
|
||||
case value_t::binary:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////
|
||||
// materialize //
|
||||
/////////////////
|
||||
|
||||
/// the basic_json value of this subtree, as parse() would produce it
|
||||
/// (a discarded value for a discarded view)
|
||||
BasicJsonType materialize() const
|
||||
{
|
||||
if (m_node == nullptr)
|
||||
{
|
||||
return BasicJsonType(value_t::discarded);
|
||||
}
|
||||
return detail::view::materialize<BasicJsonType>(*m_doc, m_node);
|
||||
}
|
||||
|
||||
/// byte offset of this value in the source text (for strings: of the
|
||||
/// first byte after the opening quote); static_cast<std::size_t>(-1) for
|
||||
/// a discarded view and for strings with escapes, which are decoded
|
||||
std::size_t source_offset() const noexcept
|
||||
{
|
||||
return m_node != nullptr && (m_node->flags & detail::view::node_flags::storage) == 0
|
||||
? m_node->off : static_cast<std::size_t>(-1);
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename> friend class basic_json_document;
|
||||
|
||||
basic_json_view(const document_data* d, const node* n) noexcept
|
||||
: m_doc(d), m_node(n)
|
||||
{}
|
||||
|
||||
const document_data* m_doc = nullptr;
|
||||
const node* m_node = nullptr;
|
||||
};
|
||||
|
||||
/*!
|
||||
@brief a parsed JSON text: owns the node index (and, optionally, the text)
|
||||
|
||||
Borrowed parses keep a pointer to the caller's text, which must outlive the
|
||||
document. Owned parses (parse_copy, rvalue std::string, streams, and inputs
|
||||
that are not contiguous byte ranges) keep their own copy.
|
||||
*/
|
||||
template<typename BasicJsonType>
|
||||
class basic_json_document
|
||||
{
|
||||
using document_data = detail::view::document_data;
|
||||
|
||||
static_assert(sizeof(typename BasicJsonType::number_integer_t) == 8 && sizeof(typename BasicJsonType::number_unsigned_t) == 8,
|
||||
"json_view supports 64-bit integer types only");
|
||||
|
||||
public:
|
||||
using view_type = basic_json_view<BasicJsonType>;
|
||||
using value_t = detail::value_t;
|
||||
|
||||
/// an empty (discarded) document
|
||||
basic_json_document() = default;
|
||||
basic_json_document(basic_json_document&&) noexcept = default;
|
||||
basic_json_document& operator=(basic_json_document&&) noexcept = default;
|
||||
basic_json_document(const basic_json_document&) = delete;
|
||||
basic_json_document& operator=(const basic_json_document&) = delete;
|
||||
~basic_json_document() = default;
|
||||
|
||||
/////////////
|
||||
// parsing //
|
||||
/////////////
|
||||
|
||||
/// parse a JSON text; contiguous byte inputs are borrowed, everything else
|
||||
/// (and rvalue std::string) is owned
|
||||
template<typename InputType>
|
||||
NLOHMANN_VIEW_NODISCARD
|
||||
static basic_json_document parse(InputType&& input,
|
||||
const bool allow_exceptions = true,
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read(std::forward<InputType>(input), allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
/// parse(ptr, len) does not compile: len would convert to allow_exceptions
|
||||
/// and ptr be read as a C string (as for the overloads of parse_copy,
|
||||
/// accept, and read below)
|
||||
template<typename InputType, typename IntegerType, typename... Flags>
|
||||
static typename std::enable_if<detail::view::is_integer_not_bool<IntegerType>::value, basic_json_document>::type
|
||||
parse(InputType&& input, IntegerType value, Flags&&... flags) = delete;
|
||||
|
||||
/// parse [first, last)
|
||||
template<typename IteratorType, typename std::enable_if<
|
||||
std::is_base_of<std::input_iterator_tag, typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
|
||||
NLOHMANN_VIEW_NODISCARD
|
||||
static basic_json_document parse(IteratorType first, IteratorType last,
|
||||
const bool allow_exceptions = true,
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read_range(first, last, allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
/// parse a copy of the input; the document does not depend on it afterwards
|
||||
template<typename InputType>
|
||||
NLOHMANN_VIEW_NODISCARD
|
||||
static basic_json_document parse_copy(InputType&& input,
|
||||
const bool allow_exceptions = true,
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
std::string buf;
|
||||
append_input(buf, std::forward<InputType>(input));
|
||||
d.build_owned(std::move(buf), allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
template<typename InputType, typename IntegerType, typename... Flags>
|
||||
static typename std::enable_if<detail::view::is_integer_not_bool<IntegerType>::value, basic_json_document>::type
|
||||
parse_copy(InputType&& input, IntegerType value, Flags&&... flags) = delete;
|
||||
|
||||
/// check whether the input is valid JSON (the result of basic_json::accept)
|
||||
template<typename InputType>
|
||||
static bool accept(InputType&& input, const bool ignore_comments = false, const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read(std::forward<InputType>(input), false, ignore_comments, ignore_trailing_commas);
|
||||
return !d.is_discarded();
|
||||
}
|
||||
|
||||
template<typename InputType, typename IntegerType, typename... Flags>
|
||||
static typename std::enable_if<detail::view::is_integer_not_bool<IntegerType>::value, bool>::type
|
||||
accept(InputType&& input, IntegerType value, Flags&&... flags) = delete;
|
||||
|
||||
/// parse into this document, reusing its memory
|
||||
template<typename InputType>
|
||||
// flawfinder: ignore (a member function, not POSIX read())
|
||||
void read(InputType&& input,
|
||||
const bool allow_exceptions = true,
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
read_kind(std::forward<InputType>(input), allow_exceptions, ignore_comments, ignore_trailing_commas,
|
||||
std::integral_constant<detail::view::input_kind, detail::view::classify_input<InputType>::value> {});
|
||||
}
|
||||
|
||||
template<typename InputType, typename IntegerType, typename... Flags>
|
||||
typename std::enable_if<detail::view::is_integer_not_bool<IntegerType>::value, void>::type
|
||||
// flawfinder: ignore (a member function, not POSIX read())
|
||||
read(InputType&& input, IntegerType value, Flags&&... flags) = delete;
|
||||
|
||||
////////////
|
||||
// access //
|
||||
////////////
|
||||
|
||||
/// the root value (discarded if parsing failed without exceptions)
|
||||
view_type root() const& noexcept
|
||||
{
|
||||
if (!m_data || m_data->discarded)
|
||||
{
|
||||
return view_type();
|
||||
}
|
||||
return view_type(m_data.get(), m_data->nodes);
|
||||
}
|
||||
|
||||
/// deleted: the view of a temporary document would dangle
|
||||
view_type root() const&& = delete;
|
||||
|
||||
bool is_discarded() const noexcept
|
||||
{
|
||||
return !m_data || m_data->discarded;
|
||||
}
|
||||
|
||||
/// the parsed text
|
||||
typename view_type::string_view_t source() const noexcept
|
||||
{
|
||||
return m_data ? typename view_type::string_view_t(m_data->src, m_data->size) : typename view_type::string_view_t();
|
||||
}
|
||||
|
||||
/// whether the document holds its own copy of the text
|
||||
bool owns_source() const noexcept
|
||||
{
|
||||
return m_data && !m_data->owned.empty() && m_data->src == m_data->owned.data();
|
||||
}
|
||||
|
||||
/// number of index nodes (values plus object keys)
|
||||
std::size_t node_count() const noexcept
|
||||
{
|
||||
return m_data ? m_data->node_count : 0;
|
||||
}
|
||||
|
||||
/// bytes held by the document (index, decoded strings, owned text)
|
||||
std::size_t memory_usage() const noexcept
|
||||
{
|
||||
if (!m_data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return sizeof(document_data) + (m_data->inline_capacity * sizeof(detail::view::node))
|
||||
+ (m_data->nodes != m_data->inline_nodes ? m_data->node_capacity * sizeof(detail::view::node) : 0)
|
||||
+ m_data->arena.capacity() + m_data->owned.capacity();
|
||||
}
|
||||
|
||||
/// release unused capacity of the index and the decoded strings; like
|
||||
/// std::vector::shrink_to_fit, this invalidates the views of the document
|
||||
/// (take new ones from root())
|
||||
void shrink_to_fit()
|
||||
{
|
||||
if (!m_data)
|
||||
{
|
||||
return;
|
||||
}
|
||||
using detail::view::node;
|
||||
document_data& d = *m_data;
|
||||
|
||||
// allocate everything first, so that an exception leaves the document
|
||||
// unchanged
|
||||
const bool shrink_arena = d.arena.capacity() > d.arena.size();
|
||||
std::vector<char> arena(shrink_arena ? d.arena : std::vector<char>());
|
||||
const bool shrink_nodes = d.nodes != d.inline_nodes && d.node_count != d.node_capacity;
|
||||
const bool into_header = d.node_count <= d.inline_capacity;
|
||||
node* fresh = (shrink_nodes && !into_header) ? static_cast<node*>(::operator new (d.node_count * sizeof(node))) : d.inline_nodes;
|
||||
|
||||
if (shrink_nodes)
|
||||
{
|
||||
if (d.node_count != 0)
|
||||
{
|
||||
std::memcpy(fresh, d.nodes, d.node_count * sizeof(node));
|
||||
}
|
||||
::operator delete (d.nodes);
|
||||
d.nodes = fresh;
|
||||
d.node_capacity = into_header ? d.inline_capacity : d.node_count;
|
||||
}
|
||||
if (shrink_arena)
|
||||
{
|
||||
d.arena.swap(arena);
|
||||
d.base[1] = d.arena.data();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
using input_kind = detail::view::input_kind;
|
||||
|
||||
/// create the storage (sized for the input) on first use
|
||||
void ensure_data(const char* src, std::size_t size)
|
||||
{
|
||||
if (!m_data)
|
||||
{
|
||||
m_data.reset(document_data::create(detail::view::estimate_nodes(src, size)));
|
||||
}
|
||||
}
|
||||
|
||||
/// parse a buffer the document takes ownership of (it replaces the
|
||||
/// previous owned copy)
|
||||
void build_owned(std::string&& buf, bool allow_exceptions, bool comments, bool trailing_commas)
|
||||
{
|
||||
ensure_data(buf.data(), buf.size());
|
||||
m_data->owned = std::move(buf);
|
||||
build(m_data->owned.data(), m_data->owned.size(), allow_exceptions, comments, trailing_commas, true, true);
|
||||
}
|
||||
|
||||
/// parse a copy of the input; the copy is appended to the cleared owned
|
||||
/// buffer, so that its capacity is reused
|
||||
template<typename T>
|
||||
void build_copy(T&& input, bool allow_exceptions, bool comments, bool trailing_commas)
|
||||
{
|
||||
if (!m_data)
|
||||
{
|
||||
std::string buf;
|
||||
append_input(buf, std::forward<T>(input));
|
||||
build_owned(std::move(buf), allow_exceptions, comments, trailing_commas);
|
||||
return;
|
||||
}
|
||||
document_data& d = *m_data;
|
||||
// (the previous text is gone from here on: should reading the input
|
||||
// throw, the document is discarded)
|
||||
d.discarded = true;
|
||||
d.src = "";
|
||||
d.size = 0;
|
||||
d.owned.clear();
|
||||
append_input(d.owned, std::forward<T>(input));
|
||||
build(d.owned.data(), d.owned.size(), allow_exceptions, comments, trailing_commas, true, true);
|
||||
}
|
||||
|
||||
/// sentinel: src[size] is readable and 0 (std::string, C strings)
|
||||
void build(const char* src, std::size_t size, bool allow_exceptions, bool comments, bool trailing_commas, bool owned, bool sentinel)
|
||||
{
|
||||
ensure_data(src, size);
|
||||
document_data& d = *m_data;
|
||||
if (!owned)
|
||||
{
|
||||
d.owned.clear();
|
||||
}
|
||||
d.src = src;
|
||||
d.size = size;
|
||||
d.node_count = 0;
|
||||
d.arena.clear();
|
||||
d.discarded = true;
|
||||
detail::view::parse_failure failure;
|
||||
bool ok = false;
|
||||
if (NLOHMANN_VIEW_UNLIKELY(size > detail::view::max_input_size()))
|
||||
{
|
||||
failure.code = detail::view::error_code::input_too_large;
|
||||
}
|
||||
else
|
||||
{
|
||||
ok = detail::view::build < typename BasicJsonType::number_float_t, !detail::abi_config::strict_nul_handling > (d, src, size, comments, trailing_commas, sentinel, failure);
|
||||
}
|
||||
if (NLOHMANN_VIEW_LIKELY(ok))
|
||||
{
|
||||
d.base[0] = d.src;
|
||||
d.base[1] = d.arena.data();
|
||||
d.discarded = false;
|
||||
return;
|
||||
}
|
||||
if (allow_exceptions)
|
||||
{
|
||||
detail::view::throw_parse_failure<BasicJsonType>(failure, src, size, comments, trailing_commas);
|
||||
}
|
||||
}
|
||||
|
||||
// --- input dispatch (see detail::view::input_kind) ---
|
||||
|
||||
void read_kind(std::string&& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::move_string> /*unused*/)
|
||||
{
|
||||
build_owned(std::move(s), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename CharT>
|
||||
void read_kind(CharT* s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::c_string> /*unused*/)
|
||||
{
|
||||
if (s == nullptr)
|
||||
{
|
||||
build("", 0, ae, c, tc, false, true);
|
||||
return;
|
||||
}
|
||||
const char* cs = reinterpret_cast<const char*>(s); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
// C strings are null-terminated, as for json::parse(const char*)
|
||||
// flawfinder: ignore
|
||||
build(cs, std::strlen(cs), ae, c, tc, false, true);
|
||||
}
|
||||
|
||||
template<typename Array>
|
||||
void read_kind(Array& a, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::char_array> /*unused*/)
|
||||
{
|
||||
using CharT = typename std::remove_cv<typename std::remove_extent<Array>::type>::type;
|
||||
const std::size_t n = std::extent<Array>::value;
|
||||
// a trailing NUL (string literals) is not part of the text, as for
|
||||
// parse(), and serves as sentinel
|
||||
const bool terminated = n > 0 && a[n - 1] == 0 && (!detail::abi_config::strict_nul_handling || std::is_same<CharT, char>::value);
|
||||
build(reinterpret_cast<const char*>(&a[0]), terminated ? n - 1 : n, ae, c, tc, false, terminated); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(const T& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::borrow_range> /*unused*/)
|
||||
{
|
||||
// std::basic_string guarantees data()[size()] == 0: use it as sentinel
|
||||
const auto size = static_cast<std::size_t>(s.size());
|
||||
build(size == 0 ? "" : reinterpret_cast<const char*>(s.data()), size, ae, c, tc, false, // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
detail::view::is_std_string<T>::value || size == 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(const T& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::copy_range> /*unused*/)
|
||||
{
|
||||
build_copy(s, ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename CharT>
|
||||
void read_kind(CharT* s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::wide_c_string> /*unused*/)
|
||||
{
|
||||
if (s == nullptr)
|
||||
{
|
||||
build("", 0, ae, c, tc, false, true);
|
||||
return;
|
||||
}
|
||||
// NUL-terminated, as for the single-byte C strings
|
||||
build_copy(detail::view::make_adapter_input(detail::input_adapter(s, s + std::char_traits<typename std::remove_cv<CharT>::type>::length(s))), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(T&& input, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::adapter> /*unused*/)
|
||||
{
|
||||
build_copy(std::forward<T>(input), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range(IteratorType first, IteratorType last, bool ae, bool c, bool tc)
|
||||
{
|
||||
using value_type = typename std::remove_cv<typename std::iterator_traits<IteratorType>::value_type>::type;
|
||||
// borrow the range where the library's input adapter scans it as one
|
||||
// block: pointers and, in C++20, contiguous iterators (std::vector,
|
||||
// std::string, ...) over single bytes
|
||||
read_range_impl(first, last, ae, c, tc, std::integral_constant < bool, std::is_integral<value_type>::value
|
||||
&& detail::iterator_input_adapter<IteratorType>::supports_bulk_scan > {});
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range_impl(IteratorType first, IteratorType last, bool ae, bool c, bool tc, std::true_type /*contiguous bytes*/)
|
||||
{
|
||||
const auto size = static_cast<std::size_t>(std::distance(first, last));
|
||||
build(size == 0 ? "" : reinterpret_cast<const char*>(&*first), size, ae, c, tc, false, size == 0); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range_impl(IteratorType first, IteratorType last, bool ae, bool c, bool tc, std::false_type /*other*/)
|
||||
{
|
||||
build_copy(detail::view::make_adapter_input(detail::input_adapter(first, last)), ae, c, tc);
|
||||
}
|
||||
|
||||
/// append the text of an input to buf: a contiguous byte input as one
|
||||
/// block, a stream in blocks, an input adapter (made here for any other
|
||||
/// input) character by character
|
||||
template<typename T>
|
||||
static void append_input(std::string& buf, T&& input)
|
||||
{
|
||||
using D = typename std::decay<T>::type;
|
||||
// NOLINTBEGIN(readability-avoid-nested-conditional-operator): a constant expression of C++11
|
||||
append_input_impl(buf, std::forward<T>(input), std::integral_constant < int, detail::is_contiguous_byte_container<D>::value ? 0
|
||||
: std::is_base_of<std::istream, D>::value ? 1
|
||||
: detail::view::is_adapter_input<D>::value ? 2 : 3 > {});
|
||||
// NOLINTEND(readability-avoid-nested-conditional-operator)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static void append_input_impl(std::string& buf, const T& s, std::integral_constant<int, 0> /*contiguous bytes*/)
|
||||
{
|
||||
buf.append(reinterpret_cast<const char*>(s.data()), static_cast<std::size_t>(s.size())); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
}
|
||||
|
||||
template<typename Stream>
|
||||
static void append_input_impl(std::string& buf, Stream&& is, std::integral_constant<int, 1> /*stream*/) // NOLINT(cppcoreguidelines-missing-std-forward): read in place, as by the library's stream adapter
|
||||
{
|
||||
detail::view::append_stream(buf, is);
|
||||
}
|
||||
|
||||
template<typename Adapter>
|
||||
static void append_input_impl(std::string& buf, detail::view::adapter_input<Adapter>&& input, std::integral_constant<int, 2> /*input adapter*/)
|
||||
{
|
||||
detail::view::append_adapter(buf, std::move(input.ia));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static void append_input_impl(std::string& buf, T&& input, std::integral_constant<int, 3> /*other*/)
|
||||
{
|
||||
detail::view::append_adapter(buf, detail::input_adapter(std::forward<T>(input)));
|
||||
}
|
||||
|
||||
std::unique_ptr<document_data, document_data::deleter> m_data{}; // NOLINT(readability-redundant-member-init)
|
||||
};
|
||||
|
||||
/// a parsed JSON text for json
|
||||
using json_document = basic_json_document<json>;
|
||||
/// a value of a json_document
|
||||
using json_view = basic_json_view<json>;
|
||||
/// a parsed JSON text for ordered_json
|
||||
using ordered_json_document = basic_json_document<ordered_json>;
|
||||
/// a value of an ordered_json_document
|
||||
using ordered_json_view = basic_json_view<ordered_json>;
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
|
||||
#include <nlohmann/detail/view/macro_unscope.hpp> // IWYU pragma: keep
|
||||
|
||||
#endif // INCLUDE_NLOHMANN_JSON_VIEW_HPP_
|
||||
Reference in new issue
Block a user