mirror of
https://github.com/nlohmann/json.git
synced 2026-10-01 22:45:17 +00:00
* Add JSON_NO_UDLS to leave out the user-defined string literals The bodies of operator""_json and operator""_json_pointer call the parser, so every translation unit including the library instantiates it, even if it never parses anything. Defining JSON_NO_UDLS leaves the literals out entirely, which saves 15-35% compile time for such translation units (#5294). Nothing changes if the macro is not defined. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Mention JSON_NO_UDLS in the list of exported module symbols Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Move the user-defined string literals to <nlohmann/json_literals.hpp> Following the review in #5294, the literals now live in their own header instead of being removed entirely: <nlohmann/json.hpp> includes it at the end unless JSON_NO_AUTOMATIC_UDLS (renamed from JSON_NO_UDLS) is defined, so a project can opt out globally and include the header only where the literals are used. The header only uses public and standard macros, because the library's internal macros are undefined at the end of json.hpp and the amalgamation inlines macro_scope.hpp only once. For the same reason, the library no longer defines and undefines JSON_USE_GLOBAL_UDLS, so a user's definition is still visible to the header. The single-header copy is identical to the multi-header one, as it only includes <nlohmann/json.hpp>. The module always exports the literals. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix CI: include cycle, GCC 4.8 literal operator spacing, and global UDLs off in the JSON_NO_AUTOMATIC_UDLS test - Suppress clang-tidy misc-header-include-cycle on the intentional mutual include of json.hpp and json_literals.hpp. - Use operator"" _json with a space for GCC 4.8 in the test's detection aliases, as the header does. - Only test the global literal operators when JSON_USE_GLOBAL_UDLS is on. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Declare the literal operators through a local macro The GCC 4.8 spacing condition was repeated for both operator definitions and the global using-declarations. NLOHMANN_JSON_LITERAL_OPERATOR(suffix) now selects operator""##suffix or operator"" suffix in one place and is undefined at the end of json_literals.hpp. Suggested by gregmarr in review. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
79 lines
2.9 KiB
C++
79 lines
2.9 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
|
|
|
|
#ifndef INCLUDE_NLOHMANN_JSON_LITERALS_HPP_
|
|
#define INCLUDE_NLOHMANN_JSON_LITERALS_HPP_
|
|
|
|
#include <cstddef> // size_t
|
|
#include <string> // string
|
|
|
|
// NOLINTNEXTLINE(misc-header-include-cycle): json.hpp includes this header at its end
|
|
#include <nlohmann/json.hpp>
|
|
|
|
// This header is included at the end of <nlohmann/json.hpp> unless
|
|
// JSON_NO_AUTOMATIC_UDLS is defined, and can be included on its own after that.
|
|
// Either way, the library's internal macros are no longer defined here (and the
|
|
// amalgamation inlines macro_scope.hpp only once), so only standard and public
|
|
// macros may be used below.
|
|
|
|
// declares the literal operator for the given suffix; GCC 4.8 requires a space
|
|
// between "" and the suffix, which newer compilers deprecate (CWG 2521)
|
|
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
|
|
#define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator""##suffix
|
|
#else
|
|
#define NLOHMANN_JSON_LITERAL_OPERATOR(suffix) operator"" suffix
|
|
#endif
|
|
|
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
inline namespace literals
|
|
{
|
|
inline namespace json_literals
|
|
{
|
|
|
|
/// @brief user-defined string literal for JSON values
|
|
/// @sa https://json.nlohmann.me/api/operator_literal_json/
|
|
inline nlohmann::json NLOHMANN_JSON_LITERAL_OPERATOR(_json)(const char* s, std::size_t n)
|
|
{
|
|
return nlohmann::json::parse(s, s + n);
|
|
}
|
|
|
|
#if defined(__cpp_char8_t)
|
|
inline nlohmann::json operator""_json(const char8_t* s, std::size_t n)
|
|
{
|
|
return nlohmann::json::parse(reinterpret_cast<const char*>(s),
|
|
reinterpret_cast<const char*>(s) + n);
|
|
}
|
|
#endif
|
|
|
|
/// @brief user-defined string literal for JSON pointer
|
|
/// @sa https://json.nlohmann.me/api/operator_literal_json_pointer/
|
|
inline nlohmann::json::json_pointer NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer)(const char* s, std::size_t n)
|
|
{
|
|
return nlohmann::json::json_pointer(std::string(s, n));
|
|
}
|
|
|
|
#if defined(__cpp_char8_t)
|
|
inline nlohmann::json::json_pointer operator""_json_pointer(const char8_t* s, std::size_t n)
|
|
{
|
|
return nlohmann::json::json_pointer(std::string(reinterpret_cast<const char*>(s), n));
|
|
}
|
|
#endif
|
|
|
|
} // namespace json_literals
|
|
} // namespace literals
|
|
NLOHMANN_JSON_NAMESPACE_END
|
|
|
|
#if !defined(JSON_USE_GLOBAL_UDLS) || JSON_USE_GLOBAL_UDLS
|
|
using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json); // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
|
|
using nlohmann::literals::json_literals::NLOHMANN_JSON_LITERAL_OPERATOR(_json_pointer); //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
|
|
#endif
|
|
|
|
#undef NLOHMANN_JSON_LITERAL_OPERATOR
|
|
|
|
#endif // INCLUDE_NLOHMANN_JSON_LITERALS_HPP_
|