1c5f01b2fSopenharmony_ci# JSON_ASSERT 2c5f01b2fSopenharmony_ci 3c5f01b2fSopenharmony_ci```cpp 4c5f01b2fSopenharmony_ci#define JSON_ASSERT(x) /* value */ 5c5f01b2fSopenharmony_ci``` 6c5f01b2fSopenharmony_ci 7c5f01b2fSopenharmony_ciThis macro controls which code is executed for [runtime assertions](../../features/assertions.md) of the library. 8c5f01b2fSopenharmony_ci 9c5f01b2fSopenharmony_ci## Parameters 10c5f01b2fSopenharmony_ci 11c5f01b2fSopenharmony_ci`x` (in) 12c5f01b2fSopenharmony_ci: expression of scalar type 13c5f01b2fSopenharmony_ci 14c5f01b2fSopenharmony_ci## Default definition 15c5f01b2fSopenharmony_ci 16c5f01b2fSopenharmony_ciThe default value is [`#!cpp assert(x)`](https://en.cppreference.com/w/cpp/error/assert). 17c5f01b2fSopenharmony_ci 18c5f01b2fSopenharmony_ci```cpp 19c5f01b2fSopenharmony_ci#define JSON_ASSERT(x) assert(x) 20c5f01b2fSopenharmony_ci``` 21c5f01b2fSopenharmony_ci 22c5f01b2fSopenharmony_ciTherefore, assertions can be switched off by defining `NDEBUG`. 23c5f01b2fSopenharmony_ci 24c5f01b2fSopenharmony_ci## Notes 25c5f01b2fSopenharmony_ci 26c5f01b2fSopenharmony_ci- The library uses numerous assertions to guarantee invariants and to abort in case of otherwise undefined behavior 27c5f01b2fSopenharmony_ci (e.g., when calling [operator[]](../basic_json/operator%5B%5D.md) with a missing object key on a `const` object). See 28c5f01b2fSopenharmony_ci page [runtime assertions](../../features/assertions.md) for more information. 29c5f01b2fSopenharmony_ci- Defining the macro to code that does not call `std::abort` may leave the library in an undefined state. 30c5f01b2fSopenharmony_ci- The macro is undefined outside the library. 31c5f01b2fSopenharmony_ci 32c5f01b2fSopenharmony_ci## Examples 33c5f01b2fSopenharmony_ci 34c5f01b2fSopenharmony_ci??? example "Example 1: default behavior" 35c5f01b2fSopenharmony_ci 36c5f01b2fSopenharmony_ci The following code will trigger an assertion at runtime: 37c5f01b2fSopenharmony_ci 38c5f01b2fSopenharmony_ci ```cpp 39c5f01b2fSopenharmony_ci #include <nlohmann/json.hpp> 40c5f01b2fSopenharmony_ci 41c5f01b2fSopenharmony_ci using json = nlohmann::json; 42c5f01b2fSopenharmony_ci 43c5f01b2fSopenharmony_ci int main() 44c5f01b2fSopenharmony_ci { 45c5f01b2fSopenharmony_ci const json j = {{"key", "value"}}; 46c5f01b2fSopenharmony_ci auto v = j["missing"]; 47c5f01b2fSopenharmony_ci } 48c5f01b2fSopenharmony_ci ``` 49c5f01b2fSopenharmony_ci 50c5f01b2fSopenharmony_ci Output: 51c5f01b2fSopenharmony_ci 52c5f01b2fSopenharmony_ci ``` 53c5f01b2fSopenharmony_ci Assertion failed: (m_value.object->find(key) != m_value.object->end()), function operator[], file json.hpp, line 2144. 54c5f01b2fSopenharmony_ci ``` 55c5f01b2fSopenharmony_ci 56c5f01b2fSopenharmony_ci??? example "Example 2: user-defined behavior" 57c5f01b2fSopenharmony_ci 58c5f01b2fSopenharmony_ci The assertion reporting can be changed by defining `JSON_ASSERT(x)` differently. 59c5f01b2fSopenharmony_ci 60c5f01b2fSopenharmony_ci ```cpp 61c5f01b2fSopenharmony_ci #include <cstdio> 62c5f01b2fSopenharmony_ci #include <cstdlib> 63c5f01b2fSopenharmony_ci #define JSON_ASSERT(x) if(!(x)){fprintf(stderr, "assertion error in %s\n", __FUNCTION__); std::abort();} 64c5f01b2fSopenharmony_ci 65c5f01b2fSopenharmony_ci #include <nlohmann/json.hpp> 66c5f01b2fSopenharmony_ci 67c5f01b2fSopenharmony_ci using json = nlohmann::json; 68c5f01b2fSopenharmony_ci 69c5f01b2fSopenharmony_ci int main() 70c5f01b2fSopenharmony_ci { 71c5f01b2fSopenharmony_ci const json j = {{"key", "value"}}; 72c5f01b2fSopenharmony_ci auto v = j["missing"]; 73c5f01b2fSopenharmony_ci } 74c5f01b2fSopenharmony_ci ``` 75c5f01b2fSopenharmony_ci 76c5f01b2fSopenharmony_ci Output: 77c5f01b2fSopenharmony_ci 78c5f01b2fSopenharmony_ci ``` 79c5f01b2fSopenharmony_ci assertion error in operator[] 80c5f01b2fSopenharmony_ci ``` 81c5f01b2fSopenharmony_ci 82c5f01b2fSopenharmony_ci## Version history 83c5f01b2fSopenharmony_ci 84c5f01b2fSopenharmony_ci- Added in version 3.9.0. 85