• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# JSON_DIAGNOSTICS
2
3```cpp
4#define JSON_DIAGNOSTICS /* value */
5```
6
7This macro enables [extended diagnostics for exception messages](../../home/exceptions.md#extended-diagnostic-messages).
8Possible values are `1` to enable or `0` to disable (default).
9
10When enabled, exception messages contain a [JSON Pointer](../json_pointer/json_pointer.md) to the JSON value that
11triggered the exception. Note that enabling this macro increases the size of every JSON value by one pointer and adds
12some  runtime overhead.
13
14## Default definition
15
16The default value is `0` (extended diagnostics are switched off).
17
18```cpp
19#define JSON_DIAGNOSTICS 0
20```
21
22When the macro is not defined, the library will define it to its default value.
23
24## Notes
25
26!!! note "ABI compatibility"
27
28    As of version 3.11.0, this macro is no longer required to be defined consistently throughout a codebase to avoid
29    One Definition Rule (ODR) violations, as the value of this macro is encoded in the namespace, resulting in distinct
30    symbol names.
31
32    This allows different parts of a codebase to use different versions or configurations of this library without
33    causing improper behavior.
34
35    Where possible, it is still recommended that all code define this the same way for maximum interoperability.
36
37!!! hint "CMake option"
38
39    Diagnostic messages can also be controlled with the CMake option
40    [`JSON_Diagnostics`](../../integration/cmake.md#json_diagnostics) (`OFF` by default)
41    which defines `JSON_DIAGNOSTICS` accordingly.
42
43## Examples
44
45??? example "Example 1: default behavior"
46
47    ```cpp
48    --8<-- "examples/diagnostics_standard.cpp"
49    ```
50
51    Output:
52
53    ```
54    --8<-- "examples/diagnostics_standard.output"
55    ```
56
57    This exception can be hard to debug if storing the value `#!c "12"` and accessing it is further apart.
58
59??? example "Example 2: extended diagnostic messages"
60
61    ```cpp
62    --8<-- "examples/diagnostics_extended.cpp"
63    ```
64
65    Output:
66
67    ```
68    --8<-- "examples/diagnostics_extended.output"
69    ```
70
71    Now the exception message contains a JSON Pointer `/address/housenumber` that indicates which value has the wrong type.
72
73## Version history
74
75- Added in version 3.10.0.
76- As of version 3.11.0 the definition is allowed to vary between translation units.
77