Skip to content

Commit

Permalink
Fix global UDLs (#3646)
Browse files Browse the repository at this point in the history
* Add ci_test_noglobaludls to CI

* Really default JSON_GLOBAL_UDLS to 1

* Test global UDLs

* Suppress warnings

* Clarify documentation
  • Loading branch information
falbrechtskirchinger committed Aug 1, 2022
1 parent 817a4a2 commit cbaf103
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmake/ci.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions docs/mkdocs/docs/api/operator_literal_json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions docs/mkdocs/docs/api/operator_literal_json_pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,5 +464,5 @@
#endif

#ifndef JSON_USE_GLOBAL_UDLS
#define JSON_USE_GLOBAL_UDLS 0
#define JSON_USE_GLOBAL_UDLS 1
#endif
3 changes: 2 additions & 1 deletion include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nlohmann/detail/macro_unscope.hpp>
Expand Down
5 changes: 3 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <nlohmann/detail/macro_unscope.hpp>
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-class_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp>
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 <valarray>

Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

#include <nlohmann/json.hpp>
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 <iostream>
#include <iterator>
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-element_access2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
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)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-json_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include <nlohmann/json.hpp>
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 <fstream>
#include "make_test_data_available.hpp"
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-json_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp>
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 <sstream>

Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-merge_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include <nlohmann/json.hpp>
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")
{
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-msgpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include <nlohmann/json.hpp>
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 <fstream>
#include <sstream>
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-readme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include <nlohmann/json.hpp>
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 <deque>
#include <forward_list>
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-regression1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp>
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 <fstream>
#include <sstream>
Expand Down
4 changes: 3 additions & 1 deletion tests/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <nlohmann/json.hpp>
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 <cstdio>
#include <list>
Expand Down
10 changes: 8 additions & 2 deletions tests/src/unit-udl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

#include "doctest_compatibility.h"

#undef JSON_USE_GLOBAL_UDLS
#define JSON_USE_GLOBAL_UDLS 0
#include <nlohmann/json.hpp>

TEST_CASE("user-defined string literals")
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion tests/src/unit-udt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept")

#include <nlohmann/json.hpp>
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 <map>
#include <memory>
Expand Down

0 comments on commit cbaf103

Please sign in to comment.