From cbaf1033bec27dbcfbaf572269b8f8f7d4fc03b1 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 1 Aug 2022 22:42:35 +0200 Subject: [PATCH] Fix global UDLs (#3646) * Add ci_test_noglobaludls to CI * Really default JSON_GLOBAL_UDLS to 1 * Test global UDLs * Suppress warnings * Clarify documentation --- .github/workflows/ubuntu.yml | 2 +- cmake/ci.cmake | 3 ++- docs/mkdocs/docs/api/operator_literal_json.md | 7 ++++--- docs/mkdocs/docs/api/operator_literal_json_pointer.md | 7 ++++--- include/nlohmann/detail/macro_scope.hpp | 2 +- include/nlohmann/json.hpp | 3 ++- single_include/nlohmann/json.hpp | 5 +++-- tests/src/unit-class_parser.cpp | 4 +++- tests/src/unit-deserialization.cpp | 4 +++- tests/src/unit-element_access2.cpp | 4 +++- tests/src/unit-json_patch.cpp | 4 +++- tests/src/unit-json_pointer.cpp | 4 +++- tests/src/unit-merge_patch.cpp | 4 +++- tests/src/unit-msgpack.cpp | 4 +++- tests/src/unit-readme.cpp | 4 +++- tests/src/unit-regression1.cpp | 4 +++- tests/src/unit-regression2.cpp | 4 +++- tests/src/unit-udl.cpp | 10 ++++++++-- tests/src/unit-udt.cpp | 4 +++- 19 files changed, 58 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 1b477c9e51..7fd292f78f 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -62,7 +62,7 @@ jobs: container: ghcr.io/nlohmann/json-ci:v2.4.0 strategy: matrix: - target: [ci_test_diagnostics, ci_test_noexceptions, ci_test_noimplicitconversions, ci_test_legacycomparison] + target: [ci_test_diagnostics, ci_test_noexceptions, ci_test_noimplicitconversions, ci_test_legacycomparison, ci_test_noglobaludls] steps: - uses: actions/checkout@v3 - name: cmake diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 5ccd4c4a4d..22cf090935 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -524,7 +524,8 @@ add_custom_target(ci_test_legacycomparison add_custom_target(ci_test_noglobaludls COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja - -DJSON_BuildTests=ON -DJSON_FastTests=ON -DJSON_UseGlobalUDLs=OFF + -DJSON_BuildTests=ON -DJSON_FastTests=ON -DJSON_GlobalUDLs=OFF + -DCMAKE_CXX_FLAGS=-DJSON_TEST_NO_GLOBAL_UDLS -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_noglobaludls COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_noglobaludls COMMAND cd ${PROJECT_BINARY_DIR}/build_noglobaludls && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure diff --git a/docs/mkdocs/docs/api/operator_literal_json.md b/docs/mkdocs/docs/api/operator_literal_json.md index 330729de0b..6775a3e1bc 100644 --- a/docs/mkdocs/docs/api/operator_literal_json.md +++ b/docs/mkdocs/docs/api/operator_literal_json.md @@ -7,16 +7,17 @@ json operator "" _json(const char* s, std::size_t n); This operator implements a user-defined string literal for JSON objects. It can be used by adding `#!cpp _json` to a string literal and returns a [`json`](json.md) object if no parse error occurred. -Use any of the following lines to bring the operator into scope: +It is recommended to bring the operator into scope using any of the following lines: ```cpp +using nlohmann::literals::operator "" _json; using namespace nlohmann::literals; using namespace nlohmann::json_literals; using namespace nlohmann::literals::json_literals; using namespace nlohmann; ``` +This is suggested to ease migration to the next major version release of the library. +See ['JSON_USE_GLOBAL_UDLS`](macros/json_use_global_udls.md#notes) for details. -Alternatively, define [`JSON_USE_GLOBAL_UDLS`](macros/json_use_global_udls.md) to make them available in the global -namespace. ## Parameters `s` (in) diff --git a/docs/mkdocs/docs/api/operator_literal_json_pointer.md b/docs/mkdocs/docs/api/operator_literal_json_pointer.md index 7c788db2c8..9b79697dc1 100644 --- a/docs/mkdocs/docs/api/operator_literal_json_pointer.md +++ b/docs/mkdocs/docs/api/operator_literal_json_pointer.md @@ -7,16 +7,17 @@ json_pointer operator "" _json_pointer(const char* s, std::size_t n); This operator implements a user-defined string literal for JSON Pointers. It can be used by adding `#!cpp _json_pointer` to a string literal and returns a [`json_pointer`](json_pointer/index.md) object if no parse error occurred. -Use any of the following lines to bring the operator into scope: +It is recommended to bring the operator into scope using any of the following lines: ```cpp +using nlohmann::literals::operator "" _json_pointer; using namespace nlohmann::literals; using namespace nlohmann::json_literals; using namespace nlohmann::literals::json_literals; using namespace nlohmann; ``` +This is suggested to ease migration to the next major version release of the library. +See ['JSON_USE_GLOBAL_UDLS`](macros/json_use_global_udls.md#notes) for details. -Alternatively, define [`JSON_USE_GLOBAL_UDLS`](macros/json_use_global_udls.md) to make them available in the global -namespace. ## Parameters `s` (in) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index cd01039e45..2075ecf226 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -464,5 +464,5 @@ #endif #ifndef JSON_USE_GLOBAL_UDLS - #define JSON_USE_GLOBAL_UDLS 0 + #define JSON_USE_GLOBAL_UDLS 1 #endif diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 742b283fce..4239bfe3b6 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5154,7 +5154,8 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC } // namespace std #if JSON_USE_GLOBAL_UDLS - using namespace nlohmann::literals::json_literals; // NOLINT(build/namespaces_literals) + 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 #include diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8a8a0a0c7e..368467b168 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2793,7 +2793,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #ifndef JSON_USE_GLOBAL_UDLS - #define JSON_USE_GLOBAL_UDLS 0 + #define JSON_USE_GLOBAL_UDLS 1 #endif #if JSON_HAS_THREE_WAY_COMPARISON @@ -24123,7 +24123,8 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC } // namespace std #if JSON_USE_GLOBAL_UDLS - using namespace nlohmann::literals::json_literals; // NOLINT(build/namespaces_literals) + 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 // #include diff --git a/tests/src/unit-class_parser.cpp b/tests/src/unit-class_parser.cpp index df821beb0f..e5b7f5db8b 100644 --- a/tests/src/unit-class_parser.cpp +++ b/tests/src/unit-class_parser.cpp @@ -11,7 +11,9 @@ #define JSON_TESTS_PRIVATE #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include diff --git a/tests/src/unit-deserialization.cpp b/tests/src/unit-deserialization.cpp index f4a76b3036..d42a6997a9 100644 --- a/tests/src/unit-deserialization.cpp +++ b/tests/src/unit-deserialization.cpp @@ -11,7 +11,9 @@ #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include diff --git a/tests/src/unit-element_access2.cpp b/tests/src/unit-element_access2.cpp index ab59461ebb..918c263d2a 100644 --- a/tests/src/unit-element_access2.cpp +++ b/tests/src/unit-element_access2.cpp @@ -10,7 +10,9 @@ #include "doctest_compatibility.h" #include -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_json) { diff --git a/tests/src/unit-json_patch.cpp b/tests/src/unit-json_patch.cpp index 0b39d00ae6..1f6716bde6 100644 --- a/tests/src/unit-json_patch.cpp +++ b/tests/src/unit-json_patch.cpp @@ -10,7 +10,9 @@ #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include "make_test_data_available.hpp" diff --git a/tests/src/unit-json_pointer.cpp b/tests/src/unit-json_pointer.cpp index 445b8f0261..4f457d0ee5 100644 --- a/tests/src/unit-json_pointer.cpp +++ b/tests/src/unit-json_pointer.cpp @@ -11,7 +11,9 @@ #define JSON_TESTS_PRIVATE #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include diff --git a/tests/src/unit-merge_patch.cpp b/tests/src/unit-merge_patch.cpp index 47bab58f75..af56b0dece 100644 --- a/tests/src/unit-merge_patch.cpp +++ b/tests/src/unit-merge_patch.cpp @@ -10,7 +10,9 @@ #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif TEST_CASE("JSON Merge Patch") { diff --git a/tests/src/unit-msgpack.cpp b/tests/src/unit-msgpack.cpp index 5c720f6ac9..f901903157 100644 --- a/tests/src/unit-msgpack.cpp +++ b/tests/src/unit-msgpack.cpp @@ -10,7 +10,9 @@ #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include diff --git a/tests/src/unit-readme.cpp b/tests/src/unit-readme.cpp index 71ffffff64..108cdd0391 100644 --- a/tests/src/unit-readme.cpp +++ b/tests/src/unit-readme.cpp @@ -10,7 +10,9 @@ #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include diff --git a/tests/src/unit-regression1.cpp b/tests/src/unit-regression1.cpp index 77a194db6d..c7720b4f14 100644 --- a/tests/src/unit-regression1.cpp +++ b/tests/src/unit-regression1.cpp @@ -14,7 +14,9 @@ #define JSON_TESTS_PRIVATE #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 5ba3d310f6..3f82301ccd 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -22,7 +22,9 @@ #include using json = nlohmann::json; using ordered_json = nlohmann::ordered_json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include diff --git a/tests/src/unit-udl.cpp b/tests/src/unit-udl.cpp index 19d901f2b7..526e05b27a 100644 --- a/tests/src/unit-udl.cpp +++ b/tests/src/unit-udl.cpp @@ -8,8 +8,6 @@ #include "doctest_compatibility.h" -#undef JSON_USE_GLOBAL_UDLS -#define JSON_USE_GLOBAL_UDLS 0 #include TEST_CASE("user-defined string literals") @@ -48,4 +46,12 @@ TEST_CASE("user-defined string literals") CHECK(R"({"foo": "bar", "baz": 42})"_json == j_expected); CHECK("/foo/bar"_json_pointer == ptr_expected); } + +#ifndef JSON_TEST_NO_GLOBAL_UDLS + SECTION("global namespace") + { + CHECK(R"({"foo": "bar", "baz": 42})"_json == j_expected); + CHECK("/foo/bar"_json_pointer == ptr_expected); + } +#endif } diff --git a/tests/src/unit-udt.cpp b/tests/src/unit-udt.cpp index 7952442cf1..2cecca3d9b 100644 --- a/tests/src/unit-udt.cpp +++ b/tests/src/unit-udt.cpp @@ -14,7 +14,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept") #include using nlohmann::json; -using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#ifdef JSON_TEST_NO_GLOBAL_UDLS + using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) +#endif #include #include