From 312a998873bf86e9371377317ad82aa0e624f1b3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Jan 2021 13:26:44 +0100 Subject: [PATCH 1/6] :children_crossing: add GDB pretty printer --- README.md | 1 + third_party/gdb_pretty_printer/README.md | 77 +++++++++++++++++++ .../gdb_pretty_printer/nlohmann-json.py | 28 +++++++ 3 files changed, 106 insertions(+) create mode 100644 third_party/gdb_pretty_printer/README.md create mode 100644 third_party/gdb_pretty_printer/nlohmann-json.py diff --git a/README.md b/README.md index 19d8f722c..eb56d6c6f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/third_party/gdb_pretty_printer/README.md b/third_party/gdb_pretty_printer/README.md new file mode 100644 index 000000000..f5f61927b --- /dev/null +++ b/third_party/gdb_pretty_printer/README.md @@ -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. diff --git a/third_party/gdb_pretty_printer/nlohmann-json.py b/third_party/gdb_pretty_printer/nlohmann-json.py new file mode 100644 index 000000000..5bac45f61 --- /dev/null +++ b/third_party/gdb_pretty_printer/nlohmann-json.py @@ -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) From ffdeb77468040576f7fcd1a0ba932725295a22ea Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 27 Jan 2021 12:54:46 +0100 Subject: [PATCH 2/6] :rotating_light: fix warnings #2615 --- .../nlohmann/detail/conversions/to_chars.hpp | 24 ++-- .../nlohmann/detail/input/input_adapters.hpp | 6 +- include/nlohmann/detail/input/parser.hpp | 96 ++++++------- .../detail/iterators/iteration_proxy.hpp | 2 +- single_include/nlohmann/json.hpp | 128 +++++++++--------- 5 files changed, 124 insertions(+), 132 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_chars.hpp b/include/nlohmann/detail/conversions/to_chars.hpp index c632ff2be..49ed0f913 100644 --- a/include/nlohmann/detail/conversions/to_chars.hpp +++ b/include/nlohmann/detail/conversions/to_chars.hpp @@ -490,51 +490,49 @@ inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10) return 10; } // LCOV_EXCL_STOP - else if (n >= 100000000) + if (n >= 100000000) { pow10 = 100000000; return 9; } - else if (n >= 10000000) + if (n >= 10000000) { pow10 = 10000000; return 8; } - else if (n >= 1000000) + if (n >= 1000000) { pow10 = 1000000; return 7; } - else if (n >= 100000) + if (n >= 100000) { pow10 = 100000; return 6; } - else if (n >= 10000) + if (n >= 10000) { pow10 = 10000; return 5; } - else if (n >= 1000) + if (n >= 1000) { pow10 = 1000; return 4; } - else if (n >= 100) + if (n >= 100) { pow10 = 100; return 3; } - else if (n >= 10) + if (n >= 10) { pow10 = 10; return 2; } - else - { - pow10 = 1; - return 1; - } + + pow10 = 1; + return 1; } inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta, diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index a78a6ec96..c437564fd 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -135,10 +135,8 @@ class iterator_input_adapter std::advance(current, 1); return result; } - else - { - return std::char_traits::eof(); - } + + return std::char_traits::eof(); } private: diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index ffe483aa1..74283cd12 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -393,62 +393,62 @@ class parser parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"))); } - else // object + + // states.back() is false -> object + + // comma -> next value + if (get_token() == token_type::value_separator) { - // comma -> next value - if (get_token() == token_type::value_separator) + // parse key + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); - } - - if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) - { - return false; - } - - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); - } - - // parse values - get_token(); - continue; + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::value_string, "object key"))); } - // closing } - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { - if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) - { - return false; - } - - // We are done with this object. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - JSON_ASSERT(!states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; + return false; } - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + // parse separator (:) + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::name_separator, "object separator"))); + } + + // parse values + get_token(); + continue; } + + // closing } + if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + { + if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) + { + return false; + } + + // We are done with this object. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + JSON_ASSERT(!states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; + } + + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_object, "object"))); } } diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp index 74b4eb347..3e181d5d9 100644 --- a/include/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp @@ -39,7 +39,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str = ""; + const string_type empty_str; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 492118a5f..03579b189 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3944,7 +3944,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str = ""; + const string_type empty_str; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} @@ -4947,10 +4947,8 @@ class iterator_input_adapter std::advance(current, 1); return result; } - else - { - return std::char_traits::eof(); - } + + return std::char_traits::eof(); } private: @@ -10618,62 +10616,62 @@ class parser parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"))); } - else // object + + // states.back() is false -> object + + // comma -> next value + if (get_token() == token_type::value_separator) { - // comma -> next value - if (get_token() == token_type::value_separator) + // parse key + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); - } - - if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) - { - return false; - } - - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); - } - - // parse values - get_token(); - continue; + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::value_string, "object key"))); } - // closing } - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { - if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) - { - return false; - } - - // We are done with this object. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - JSON_ASSERT(!states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; + return false; } - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + // parse separator (:) + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::name_separator, "object separator"))); + } + + // parse values + get_token(); + continue; } + + // closing } + if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + { + if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) + { + return false; + } + + // We are done with this object. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + JSON_ASSERT(!states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; + } + + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_object, "object"))); } } @@ -14948,51 +14946,49 @@ inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10) return 10; } // LCOV_EXCL_STOP - else if (n >= 100000000) + if (n >= 100000000) { pow10 = 100000000; return 9; } - else if (n >= 10000000) + if (n >= 10000000) { pow10 = 10000000; return 8; } - else if (n >= 1000000) + if (n >= 1000000) { pow10 = 1000000; return 7; } - else if (n >= 100000) + if (n >= 100000) { pow10 = 100000; return 6; } - else if (n >= 10000) + if (n >= 10000) { pow10 = 10000; return 5; } - else if (n >= 1000) + if (n >= 1000) { pow10 = 1000; return 4; } - else if (n >= 100) + if (n >= 100) { pow10 = 100; return 3; } - else if (n >= 10) + if (n >= 10) { pow10 = 10; return 2; } - else - { - pow10 = 1; - return 1; - } + + pow10 = 1; + return 1; } inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta, From 5b0c804630cc6dd0a40dd2b4cbc7dd1026f6326a Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 27 Jan 2021 14:22:58 +0100 Subject: [PATCH 3/6] :bug: properly assign two labels to one test case #2596 --- test/cmake_fetch_content/CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/cmake_fetch_content/CMakeLists.txt b/test/cmake_fetch_content/CMakeLists.txt index f850b8bd7..7df00865e 100644 --- a/test/cmake_fetch_content/CMakeLists.txt +++ b/test/cmake_fetch_content/CMakeLists.txt @@ -11,12 +11,10 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.11.0") ) set_tests_properties(cmake_fetch_content_configure PROPERTIES FIXTURES_SETUP cmake_fetch_content - LABELS git_required - LABELS not_reproducible + LABELS "git_required;not_reproducible" ) set_tests_properties(cmake_fetch_content_build PROPERTIES FIXTURES_REQUIRED cmake_fetch_content - LABELS git_required - LABELS not_reproducible + LABELS "git_required;not_reproducible" ) endif() From 9f6b78ee5a9e4e2e7d55dd65f6f3ba127e33f4d1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 27 Jan 2021 14:34:10 +0100 Subject: [PATCH 4/6] :memo: add documentation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index eb56d6c6f..076737245 100644 --- a/README.md +++ b/README.md @@ -1638,4 +1638,6 @@ In case you have downloaded the library rather than checked out the code via Git Some tests change the installed files and hence make the whole process not reproducible. Please execute `ctest -LE not_reproducible` to skip these tests. See [issue #2324](https://github.com/nlohmann/json/issues/2324) for more information. +Note you need to call `cmake -LE "not_reproducible|git_required"` to exclude both labels. See [issue #2596](https://github.com/nlohmann/json/issues/2596) for more information. + As Intel compilers use unsafe floating point optimization by default, the unit tests may fail. Use flag [`/fp:precise`](https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-model-fp.html) then. From 6d4eed5aeb59a3f40831edacf8693e53a543e832 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 30 Jan 2021 12:51:54 +0100 Subject: [PATCH 5/6] :rotating_light: fix warning --- include/nlohmann/detail/iterators/iteration_proxy.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp index 3e181d5d9..1b47faeb3 100644 --- a/include/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp @@ -39,7 +39,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str; + const string_type empty_str{}; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 03579b189..30dd96f43 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3944,7 +3944,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str; + const string_type empty_str{}; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} From 567e2e3412afea14b3fb7535c0c75179235c0832 Mon Sep 17 00:00:00 2001 From: "William A. Wieselquist" Date: Sat, 6 Feb 2021 14:48:12 -0500 Subject: [PATCH 6/6] Fix missing 1.78 in example in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 076737245..5d354f1ed 100644 --- a/README.md +++ b/README.md @@ -577,7 +577,7 @@ j[1] = 42; bool foo = j.at(2); // comparison -j == "[\"foo\", 42, true]"_json; // true +j == "[\"foo\", 42, true, 1.78]"_json; // true // other stuff j.size(); // 3 entries