mirror of
https://github.com/nlohmann/json
synced 2024-11-26 05:40:07 +00:00
🚸 add GDB pretty printer
This commit is contained in:
parent
92fa1d9582
commit
312a998873
@ -1528,6 +1528,7 @@ I deeply appreciate the help of the following people.
|
||||
- [KonanM](https://github.com/KonanM) proposed an implementation for the `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE`/`NLOHMANN_DEFINE_TYPE_INTRUSIVE` macros.
|
||||
- [Guillaume Racicot](https://github.com/gracicot) implemented `string_view` support and allowed C++20 support.
|
||||
- [Alex Reinking](https://github.com/alexreinking) improved CMake support for `FetchContent`.
|
||||
- [Hannes Domani](https://github.com/ssbssa) provided a GDB pretty printer.
|
||||
|
||||
Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone.
|
||||
|
||||
|
77
third_party/gdb_pretty_printer/README.md
vendored
Normal file
77
third_party/gdb_pretty_printer/README.md
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
# GDB Pretty Printer
|
||||
|
||||
File [nlohmann-json.py](nlohmann-json.py) contains a pretty printer for GDB for JSON values of this library. It was originally published as [Gist](https://gist.github.com/ssbssa/60da5339c6e6036b2afce17de06050ea#file-nlohmann-json-py) by [Hannes Domani](https://github.com/ssbssa).
|
||||
|
||||
## How to use
|
||||
|
||||
- Add line
|
||||
|
||||
```
|
||||
source /path/to/nlohmann-json.py
|
||||
```
|
||||
|
||||
to `~/.gdbinit`. Note you must replace `/path/to` with whatever path you stored file `nlohmann-json.py`.
|
||||
- In GDB, debug as usual. When you want to pretty-print a JSON value `var`, type
|
||||
|
||||
```
|
||||
p -pretty on -array on -- var
|
||||
```
|
||||
|
||||
The result should look like
|
||||
|
||||
```
|
||||
$1 = std::map with 5 elements = {
|
||||
["Baptiste"] = std::map with 1 element = {
|
||||
["first"] = "second"
|
||||
},
|
||||
["Emmanuel"] = std::vector of length 3, capacity 3 = {
|
||||
3,
|
||||
"25",
|
||||
0.5
|
||||
},
|
||||
["Jean"] = 0.7,
|
||||
["Zorg"] = std::map with 8 elements = {
|
||||
["array"] = std::vector of length 3, capacity 3 = {
|
||||
1,
|
||||
0,
|
||||
2
|
||||
},
|
||||
["awesome_str"] = "bleh",
|
||||
["bool"] = true,
|
||||
["flex"] = 0.2,
|
||||
["float"] = 5.22,
|
||||
["int"] = 5,
|
||||
["nested"] = std::map with 1 element = {
|
||||
["bar"] = "barz"
|
||||
},
|
||||
["trap "] = "you fell"
|
||||
},
|
||||
["empty"] = nlohmann::detail::value_t::null
|
||||
}
|
||||
```
|
||||
|
||||
Tested with GDB 9.2. See [#1952](https://github.com/nlohmann/json/issues/1952) for more information. Please post questions there.
|
||||
|
||||
## Copyright
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (C) 2020 [Hannes Domani](https://github.com/ssbssa)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
28
third_party/gdb_pretty_printer/nlohmann-json.py
vendored
Normal file
28
third_party/gdb_pretty_printer/nlohmann-json.py
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
import gdb
|
||||
import re
|
||||
|
||||
class JsonValuePrinter:
|
||||
"Print a json-value"
|
||||
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
def to_string(self):
|
||||
if self.val.type.strip_typedefs().code == gdb.TYPE_CODE_FLT:
|
||||
return ("%.6f" % float(self.val)).rstrip("0")
|
||||
return self.val
|
||||
|
||||
def json_lookup_function(val):
|
||||
if re.search("^nlohmann::basic_json<.*>$", val.type.strip_typedefs().name):
|
||||
t = str(val['m_type'])
|
||||
if t.startswith("nlohmann::detail::value_t::"):
|
||||
try:
|
||||
union_val = val['m_value'][t[27:]]
|
||||
if union_val.type.code == gdb.TYPE_CODE_PTR:
|
||||
return gdb.default_visualizer(union_val.dereference())
|
||||
else:
|
||||
return JsonValuePrinter(union_val)
|
||||
except:
|
||||
return JsonValuePrinter(val['m_type'])
|
||||
|
||||
gdb.pretty_printers.append(json_lookup_function)
|
Loading…
Reference in New Issue
Block a user