| 1 | # GDB Pretty Printer |
| 2 | |
| 3 | File [nlohmann-json.py](nlohmann-json.py) contains a pretty printer for GDB for JSON values of this library. It was |
| 4 | originally published as [Gist](https://gist.github.com/ssbssa/60da5339c6e6036b2afce17de06050ea#file-nlohmann-json-py) by [Hannes Domani](https://github.com/ssbssa). |
| 5 | |
| 6 | ## How to use |
| 7 | |
| 8 | - Add line |
| 9 | |
| 10 | ``` |
| 11 | source /path/to/nlohmann-json.py |
| 12 | ``` |
| 13 | |
| 14 | to `~/.gdbinit`. Note you must replace `/path/to` with whatever path you stored file `nlohmann-json.py`. |
| 15 | - In GDB, debug as usual. When you want to pretty-print a JSON value `var`, type |
| 16 | |
| 17 | ``` |
| 18 | p -pretty on -array on -- var |
| 19 | ``` |
| 20 | |
| 21 | The result should look like |
| 22 | |
| 23 | ``` |
| 24 | $1 = std::map with 5 elements = { |
| 25 | ["Baptiste"] = std::map with 1 element = { |
| 26 | ["first"] = "second" |
| 27 | }, |
| 28 | ["Emmanuel"] = std::vector of length 3, capacity 3 = { |
| 29 | 3, |
| 30 | "25", |
| 31 | 0.5 |
| 32 | }, |
| 33 | ["Jean"] = 0.7, |
| 34 | ["Zorg"] = std::map with 8 elements = { |
| 35 | ["array"] = std::vector of length 3, capacity 3 = { |
| 36 | 1, |
| 37 | 0, |
| 38 | 2 |
| 39 | }, |
| 40 | ["awesome_str"] = "bleh", |
| 41 | ["bool"] = true, |
| 42 | ["flex"] = 0.2, |
| 43 | ["float"] = 5.22, |
| 44 | ["int"] = 5, |
| 45 | ["nested"] = std::map with 1 element = { |
| 46 | ["bar"] = "barz" |
| 47 | }, |
| 48 | ["trap "] = "you fell" |
| 49 | }, |
| 50 | ["empty"] = nlohmann::detail::value_t::null |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | Requires Python 3.9+. Last tested with GDB 12.1. |
| 55 | See [#1952](https://github.com/nlohmann/json/issues/1952) for more information. Please post questions there. |
| 56 | |
| 57 | ## Copyright |
| 58 | |
| 59 | MIT License |
| 60 | |
| 61 | Copyright (C) 2020 [Hannes Domani](https://github.com/ssbssa) |
| 62 | |
| 63 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 64 | of this software and associated documentation files (the "Software"), to deal |
| 65 | in the Software without restriction, including without limitation the rights |
| 66 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 67 | copies of the Software, and to permit persons to whom the Software is |
| 68 | furnished to do so, subject to the following conditions: |
| 69 | |
| 70 | The above copyright notice and this permission notice shall be included in all |
| 71 | copies or substantial portions of the Software. |
| 72 | |
| 73 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 74 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 75 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 76 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 77 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 78 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 79 | SOFTWARE. |