mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 13:10:33 +00:00
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>
84 lines
3.3 KiB
C++
84 lines
3.3 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
|
|
|
|
#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.
|
|
|
|
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/
|
|
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
|
|
inline nlohmann::json operator""_json(const char* s, std::size_t n)
|
|
#else
|
|
// GCC 4.8 requires a space between "" and suffix
|
|
inline nlohmann::json operator"" _json(const char* s, std::size_t n)
|
|
#endif
|
|
{
|
|
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/
|
|
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
|
|
inline nlohmann::json::json_pointer operator""_json_pointer(const char* s, std::size_t n)
|
|
#else
|
|
// GCC 4.8 requires a space between "" and suffix
|
|
inline nlohmann::json::json_pointer operator"" _json_pointer(const char* s, std::size_t n)
|
|
#endif
|
|
{
|
|
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
|
|
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
|
|
using nlohmann::literals::json_literals::operator""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
|
|
using nlohmann::literals::json_literals::operator""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
|
|
#else
|
|
// GCC 4.8 requires a space between "" and suffix
|
|
using nlohmann::literals::json_literals::operator"" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
|
|
using nlohmann::literals::json_literals::operator"" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
|
|
#endif
|
|
#endif
|
|
|
|
#endif // INCLUDE_NLOHMANN_JSON_LITERALS_HPP_
|