mirror of
https://github.com/nlohmann/json.git
synced 2026-10-01 14:35:18 +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>
2.7 KiB
2.7 KiB
JSON_USE_GLOBAL_UDLS
#define JSON_USE_GLOBAL_UDLS /* value */
When defined to 1, the user-defined string literals (UDLs) are placed into the global namespace instead of
nlohmann::literals::json_literals.
Default definition
The default value is 1.
#define JSON_USE_GLOBAL_UDLS 1
When the macro is not defined, the library behaves as if it were defined to its default value.
Notes
!!! info "Future behavior change"
The user-defined string literals will be removed from the global namespace in the next major release of the library.
To prepare existing code, define `JSON_USE_GLOBAL_UDLS` to `0` and bring the string literals into scope where
needed. Refer to any of the [string literals](#see-also) for details.
!!! hint "CMake option"
The placement of user-defined string literals can also be controlled with the CMake option
[`JSON_GlobalUDLs`](../../integration/cmake.md#json_globaludls) (`ON` by default) which defines
`JSON_USE_GLOBAL_UDLS` accordingly.
!!! info "Leaving out the literals"
If [`JSON_NO_AUTOMATIC_UDLS`](json_no_automatic_udls.md) is defined, the literals are only declared where
`<nlohmann/json_literals.hpp>` is included; this macro then applies to that header.
Examples
??? example "Example 1: Default behavior"
The code below shows the default behavior using the `_json` UDL.
```cpp
#include <nlohmann/json.hpp>
#include <iostream>
int main()
{
auto j = "42"_json;
std::cout << j << std::endl;
}
```
Output:
```json
42
```
??? example "Example 2: Namespaced UDLs"
The code below shows how UDLs need to be brought into scope before using `_json` when `JSON_USE_GLOBAL_UDLS` is
defined to `0`.
```cpp
#define JSON_USE_GLOBAL_UDLS 0
#include <nlohmann/json.hpp>
#include <iostream>
int main()
{
// auto j = "42"_json; // This line would fail to compile,
// because the UDLs are not in the global namespace
// Bring the UDLs into scope
using namespace nlohmann::json_literals;
auto j = "42"_json;
std::cout << j << std::endl;
}
```
Output:
```json
42
```
See also
operator""_jsonoperator""_json_pointerJSON_NO_AUTOMATIC_UDLS- do not include the user-defined string literals automatically- :simple-cmake: JSON_GlobalUDLs - CMake option to control the macro
Version history
- Added in version 3.11.0.