mirror of
https://github.com/nlohmann/json.git
synced 2026-07-03 09:14:18 +00:00
Overwork project infrastructure (#5218)
* 📝 overwork project infrastructure Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 fix GCC16 issue Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 fix GCC16 issue Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 only build module for GCC Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 fix build Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 📝 fix documentation Closes #5012: fix the error_handler_t::ignore wording Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 📝 fix documentation Closes #4354: fix "Custom data source" example Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
|
||||
# `import std;` (used by main.cpp) needs CMake's experimental C++ import-std
|
||||
# support. The accepted token is CMake-version-specific; this value matches the
|
||||
# CMake version pinned in the ci_module_cpp20 CI jobs (4.3.x). If that pin is
|
||||
# bumped, this token must be updated to match (configuration fails loudly
|
||||
# otherwise).
|
||||
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "451f2fe2-a8a2-47c3-bc32-94786d8fc91b")
|
||||
|
||||
project(json_test CXX)
|
||||
|
||||
# import std; requires C++23 and the std library module
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_MODULE_STD ON)
|
||||
|
||||
set(NLOHMANN_JSON_BUILD_MODULES ON CACHE BOOL "Enable nlohmann.json module support")
|
||||
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/tests)
|
||||
|
||||
add_executable(json_test main.cpp)
|
||||
target_link_libraries(json_test
|
||||
PRIVATE
|
||||
target_link_libraries(json_test
|
||||
PRIVATE
|
||||
nlohmann_json_modules
|
||||
)
|
||||
|
||||
target_compile_definitions(json_test
|
||||
PRIVATE NLOHMANN_JSON_BUILD_MODULES
|
||||
)
|
||||
|
||||
target_compile_features(json_test PUBLIC cxx_std_20)
|
||||
target_compile_features(json_test PRIVATE cxx_std_23)
|
||||
|
||||
@@ -6,9 +6,54 @@
|
||||
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import std;
|
||||
import nlohmann.json;
|
||||
|
||||
using namespace nlohmann::literals;
|
||||
|
||||
// Exercise the surface exported by the nlohmann.json module so that a missing
|
||||
// or broken export is caught at compile time.
|
||||
//
|
||||
// Standard-library facilities are pulled in via `import std;` rather than
|
||||
// textual `#include`s: mixing `import` with textual standard headers does not
|
||||
// compile under GCC's C++20 modules implementation, whereas `import std;` works
|
||||
// across GCC, Clang, and MSVC. This requires C++23 and CMake's (experimental)
|
||||
// import-std support; see CMakeLists.txt.
|
||||
int main()
|
||||
{
|
||||
nlohmann::json j;
|
||||
// basic_json / json: parsing and value access
|
||||
nlohmann::json j = nlohmann::json::parse(R"({"a": 1, "list": [1, 2, 3]})");
|
||||
const int a = j["a"].get<int>();
|
||||
|
||||
// json_pointer and operator""_json_pointer
|
||||
nlohmann::json_pointer<std::string> ptr = "/list/2"_json_pointer;
|
||||
const int last = j[ptr].get<int>();
|
||||
|
||||
// operator""_json literal
|
||||
const nlohmann::json lit = R"([1, 2, 3])"_json;
|
||||
|
||||
// ordered_json
|
||||
nlohmann::ordered_json oj;
|
||||
oj["b"] = 2;
|
||||
const int b = oj["b"].get<int>();
|
||||
|
||||
// ordered_map alias
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
m["x"] = 1;
|
||||
|
||||
// adl_serializer (reference the exported template)
|
||||
using serializer = nlohmann::adl_serializer<int, void>;
|
||||
static_cast<void>(sizeof(serializer));
|
||||
|
||||
// to_string
|
||||
const std::string dumped = nlohmann::to_string(j);
|
||||
|
||||
// operator<< (hidden friend, reached via ADL through the module)
|
||||
std::ostringstream os;
|
||||
os << j << oj << lit;
|
||||
|
||||
// use every result so the references cannot be optimized away
|
||||
return (a == 1 && last == 3 && b == 2 && lit.size() == 3
|
||||
&& m.size() == 1 && !dumped.empty() && !os.str().empty())
|
||||
? 0 : 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user