Merge branch 'develop' of https://github.com/nlohmann/json into diagnostics

 Conflicts:
	include/nlohmann/detail/input/parser.hpp
	single_include/nlohmann/json.hpp
This commit is contained in:
Niels Lohmann 2021-02-07 17:45:09 +01:00
commit 4917e7c259
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
9 changed files with 577 additions and 669 deletions

View File

@ -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
@ -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.
@ -1637,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.

View File

@ -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,

View File

@ -135,10 +135,8 @@ class iterator_input_adapter
std::advance(current, 1);
return result;
}
else
{
return std::char_traits<char_type>::eof();
}
return std::char_traits<char_type>::eof();
}
private:

View File

@ -386,8 +386,9 @@ class parser
m_lexer.get_token_string(),
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType()));
}
else // object
{
// states.back() is false -> object
// comma -> next value
if (get_token() == token_type::value_separator)
{
@ -397,12 +398,12 @@ class parser
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"), BasicJsonType()));
}
}
if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
{
return false;
}
// parse separator (:)
if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
@ -410,35 +411,34 @@ class parser
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"), BasicJsonType()));
}
// parse values
get_token();
continue;
}
// closing }
if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
// parse values
get_token();
continue;
}
// closing }
if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
{
if (JSON_HEDLEY_UNLIKELY(!sax->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 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"), BasicJsonType()));
}
}
}

View File

@ -39,7 +39,7 @@ template<typename IteratorType> 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) {}

File diff suppressed because it is too large Load Diff

View File

@ -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()

View 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.

View 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)