#define JSON_ASSERT(x) /* value */
This macro controls which code is executed for runtime assertions of the library.
x
(in) : expression of scalar type
The default value is #!cpp assert(x)
.
#define JSON_ASSERT(x) assert(x)
Therefore, assertions can be switched off by defining NDEBUG
.
const
object). See page runtime assertions for more information.std::abort
may leave the library in an undefined state.??? example “Example 1: default behavior”
The following code will trigger an assertion at runtime: ```cpp #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { const json j = {{"key", "value"}}; auto v = j["missing"]; } ``` Output: ``` Assertion failed: (m_value.object->find(key) != m_value.object->end()), function operator[], file json.hpp, line 2144. ```
??? example “Example 2: user-defined behavior”
The assertion reporting can be changed by defining `JSON_ASSERT(x)` differently. ```cpp #include <cstdio> #include <cstdlib> #define JSON_ASSERT(x) if(!(x)){fprintf(stderr, "assertion error in %s\n", __FUNCTION__); std::abort();} #include <nlohmann/json.hpp> using json = nlohmann::json; int main() { const json j = {{"key", "value"}}; auto v = j["missing"]; } ``` Output: ``` assertion error in operator[] ```