• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Supported Macros
2
3Some aspects of the library can be configured by defining preprocessor macros before including the `json.hpp` header.
4
5## `JSON_ASSERT(x)`
6
7The default value is `#!cpp assert(x)`.
8
9## `JSON_CATCH_USER(exception)`
10
11This macro overrides `#!cpp catch` calls inside the library. The argument is the type of the exception to catch. As of version 3.8.0, the library only catches `std::out_of_range` exceptions internally to rethrow them as [`json::out_of_range`](../home/exceptions.md#out-of-range) exceptions. The macro is always followed by a scope.
12
13See [Switch off exceptions](../home/exceptions.md#switch-off-exceptions) for an example.
14
15## `JSON_DIAGNOSTICS`
16
17This macro enables extended diagnostics for exception messages. Possible values are `1` to enable or `0` to disable (default).
18
19When enabled, exception messages contain a [JSON Pointer](json_pointer.md) to the JSON value that triggered the exception, see [Extended diagnostic messages](../home/exceptions.md#extended-diagnostic-messages) for an example. Note that enabling this macro increases the size of every JSON value by one pointer and adds some runtime overhead.
20
21The diagnostics messages can also be controlled with the CMake option `JSON_Diagnostics` (`OFF` by default) which sets `JSON_DIAGNOSTICS` accordingly.
22
23## `JSON_HAS_CPP_11`, `JSON_HAS_CPP_14`, `JSON_HAS_CPP_17`, `JSON_HAS_CPP_20`
24
25The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view` support for C++17). For these new features, the library implements some preprocessor checks to determine the C++ standard. By defining any of these symbols, the internal check is overridden and the provided C++ version is unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be detected incorrectly.
26
27## `JSON_NOEXCEPTION`
28
29Exceptions can be switched off by defining the symbol `JSON_NOEXCEPTION`.
30When defining `JSON_NOEXCEPTION`, `#!cpp try` is replaced by `#!cpp if (true)`,
31`#!cpp catch` is replaced by `#!cpp if (false)`, and `#!cpp throw` is replaced by `#!cpp std::abort()`.
32
33The same effect is achieved by setting the compiler flag `-fno-exceptions`.
34
35Note the explanatory [`what()`](https://en.cppreference.com/w/cpp/error/exception/what) string of exceptions is not available for MSVC if exceptions are disabled, see [#2824](https://github.com/nlohmann/json/discussions/2824).
36
37## `JSON_NO_IO`
38
39When defined, headers `<cstdio>`, `<ios>`, `<iosfwd>`, `<istream>`, and `<ostream>` are not included and parse functions relying on these headers are excluded. This is relevant for environment where these I/O functions are disallowed for security reasons (e.g., Intel Software Guard Extensions (SGX)).
40
41## `JSON_SKIP_UNSUPPORTED_COMPILER_CHECK`
42
43When defined, the library will not create a compile error when a known unsupported compiler is detected. This allows to use the library with compilers that do not fully support C++11 and may only work if unsupported features are not used.
44
45## `JSON_THROW_USER(exception)`
46
47This macro overrides `#!cpp throw` calls inside the library. The argument is the exception to be thrown. Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.
48
49See [Switch off exceptions](../home/exceptions.md#switch-off-exceptions) for an example.
50
51## `JSON_TRY_USER`
52
53This macro overrides `#!cpp try` calls inside the library. It has no arguments and is always followed by a scope.
54
55See [Switch off exceptions](../home/exceptions.md#switch-off-exceptions) for an example.
56
57## `JSON_USE_IMPLICIT_CONVERSIONS`
58
59When defined to `0`, implicit conversions are switched off. By default, implicit conversions are switched on.
60
61??? example
62
63    This is an example for an implicit conversion:
64
65    ```cpp
66    json j = "Hello, world!";
67    std::string s = j;
68    ```
69
70    When `JSON_USE_IMPLICIT_CONVERSIONS` is defined to `0`, the code above does no longer compile. Instead, it must be written like this:
71
72    ```cpp
73    json j = "Hello, world!";
74    auto s = j.get<std::string>();
75    ```
76
77Implicit conversions can also be controlled with the CMake option `JSON_ImplicitConversions` (`ON` by default) which sets `JSON_USE_IMPLICIT_CONVERSIONS` accordingly.
78
79## `NLOHMANN_DEFINE_TYPE_INTRUSIVE(type, member...)`
80
81This macro can be used to simplify the serialization/deserialization of types if (1) want to use a JSON object as serialization and (2) want to use the member variable names as object keys in that object.
82
83The macro is to be defined inside of the class/struct to create code for. Unlike [`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE`](#nlohmann_define_type_non_intrusivetype-member), it can access private members.
84The first parameter is the name of the class/struct, and all remaining parameters name the members.
85
86See [Simplify your life with macros](arbitrary_types.md#simplify-your-life-with-macros) for an example.
87
88## `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(type, member...)`
89
90This macro can be used to simplify the serialization/deserialization of types if (1) want to use a JSON object as serialization and (2) want to use the member variable names as object keys in that object.
91
92The macro is to be defined inside of the namespace of the class/struct to create code for. Private members cannot be accessed. Use [`NLOHMANN_DEFINE_TYPE_INTRUSIVE`](#nlohmann_define_type_intrusivetype-member) in these scenarios.
93The first parameter is the name of the class/struct, and all remaining parameters name the members.
94
95See [Simplify your life with macros](arbitrary_types.md#simplify-your-life-with-macros) for an example.
96
97## `NLOHMANN_JSON_SERIALIZE_ENUM(type, ...)`
98
99This macro simplifies the serialization/deserialization of enum types. See [Specializing enum conversion](enum_conversion.md) for more information.
100