nlohmann_json.natvis had no entries for the view and document types, so a debugger showed the raw node index. Add visualizers for basic_json_document<*,*>, basic_json_view<*,*> and detail::view::node. A document is a flat index into the source text, so the strings of the direct children can be read through document_data::base, while the children of nested containers are shown as nodes (kind, size, numbers; strings only by length). Containers with edits (node_flags::moved) are not expanded. The types live in the same inline namespaces as basic_json and view_mode is covered by a wildcard, so the ABI tags do not multiply with it; the file grows from 862 KB to 1.7 MB. Also correct the ABI tag count (11) in the --all size note. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
4.0 KiB
Debugging
This page collects the library's built-in debugger integrations and other debugging-related features. They are not covered by any single page elsewhere in the docs, so they are collected here.
Visual Studio (natvis)
The repository ships nlohmann_json.natvis
at its root, a Natvis
file that gives json/ordered_json values a friendly, key/value debugger view instead of showing raw internal
fields, when debugging with the MSVC debug engine (cppvsdbg) in Visual Studio or VS Code.
The file also has visualizers for json_document and json_view (and their ordered_json/editable variants). They
show the value of the document or view, and its elements as an expandable list. Because a document is only a flat index
into the source text, the strings of the direct elements are shown, but the elements of nested arrays and objects are
shown as index nodes (their kind, size, and numbers; strings only by length). Arrays and objects that have been edited
in a json_editable_document are not expanded. The visualizers read the internal layout of the MSVC STL (std::array,
std::unique_ptr).
The checked-in visualizers cover namespace variants with zero, one, or two ABI tags. Configurations with three or more ABI tags are not covered and may show raw internal fields instead. To generate a Natvis file for such a configuration:
-
Check out the release tag of the library version you use.
-
List the ABI tags and the macros that enable them:
make natvis NATVIS_ARGS="--list-tags" -
Generate a Natvis file with the tags of your configuration, for example for
JSON_DIAGNOSTICS,JSON_DIAGNOSTIC_POSITIONS, andJSON_NO_SIMD:make natvis NATVIS_ARGS="--abi-tags _diag,_dp,_ns --output my_nlohmann_json.natvis" -
Use the generated file instead of
nlohmann_json.natvis.
--abi-tags can be repeated for several configurations, and --all generates every combination (about 60 MB for 11
ABI tags, doubling with each further tag). See
tools/generate_natvis for all options.
Debug engines that wrap LLDB instead of the MSVC debug engine (for example, codelldb in VS Code) only have
partial/experimental Natvis support, and commonly fall back to showing raw internal fields even with the
.natvis file present. Switching to cppvsdbg where available, or checking your debug extension's own Natvis
support/version, are the next things to try if this happens. There is currently no bundled LLDB-native
pretty-printer script in this repository.
GDB
The repository ships a GDB Python pretty printer
under tools/gdb_pretty_printer, with its own usage instructions in that directory's README.md.
Extended exception diagnostics
Defining JSON_DIAGNOSTICS before including the library augments
type_error/out_of_range-style exceptions with a JSON Pointer to the offending value, which can help pinpoint
where in a large document a runtime error occurred. This only applies to exceptions thrown after a value
exists (e.g. during element access); parse errors, which happen before any value exists to point at, are not
covered by this mechanism — see Parsing and exceptions for how parse
errors report their own location instead.
See also
JSON_DIAGNOSTICS- extended exception messages with the path of the failing valueJSON_DIAGNOSTIC_POSITIONS- record byte positions of values for error messages- Exceptions - all exceptions and their error codes
exception- base class of all exceptions thrown by the library