Skip to content

Commit

Permalink
update exceptions.hpp to use the diagnostic positions
Browse files Browse the repository at this point in the history
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
  • Loading branch information
hnampally committed Jan 5, 2025
1 parent 05f8bb5 commit 2cc096a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
27 changes: 27 additions & 0 deletions docs/examples/diagnostic_positions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@

using json = nlohmann::json;

/* Demonstration of type error exception with diagnostic postions support enabled */
void type_error_exception()
{
//Invalid json string - housenumber type must be int instead of string
std::string json_invalid_string = R"(
{
"address": {
"street": "Fake Street",
"housenumber": "1"
}
}
)";
json j = json::parse(json_invalid_string);
try
{
int housenumber = j["address"]["housenumber"];

Check notice on line 23 in docs/examples/diagnostic_positions.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

docs/examples/diagnostic_positions.cpp#L23

Variable 'housenumber' is assigned a value that is never used.
}
catch (const json::exception& e)
{
std::cout << e.what() << '\n';
}

}

int main()
{
std::string json_string = R"(
Expand Down Expand Up @@ -48,4 +72,7 @@ int main()
std::cout << "1" << "\n";
std::cout << "Parsed string: \n";
std::cout << json_string.substr(j["address"]["housenumber"].start_pos(), j["address"]["housenumber"].end_pos() - j["address"]["housenumber"].start_pos()) << "\n\n";

type_error_exception();

}
1 change: 1 addition & 0 deletions docs/examples/diagnostic_positions.output
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Original string:
Parsed string:
1

[json.exception.type_error.302] (byte 92-95) type must be number, but is string
13 changes: 10 additions & 3 deletions docs/examples/diagnostics_extended.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#include <iostream>

# define JSON_DIAGNOSTICS 1
# define JSON_DIAGNOSTIC_POSITIONS 1
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
json j;
j["address"]["street"] = "Fake Street";
j["address"]["housenumber"] = "12";
std::string json_string = R"(
{
"address": {
"street": "Fake Street",
"housenumber": "12"
}
}
)";
json j = json::parse(json_string);

try
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/diagnostics_extended.output
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[json.exception.type_error.302] (/address/housenumber) type must be number, but is string
[json.exception.type_error.302] (/address/housenumber, byte 92-96) type must be number, but is string
7 changes: 6 additions & 1 deletion include/nlohmann/detail/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class exception : public std::exception
}
break;
}

case value_t::null: // LCOV_EXCL_LINE
case value_t::string: // LCOV_EXCL_LINE
case value_t::boolean: // LCOV_EXCL_LINE
Expand All @@ -131,6 +130,12 @@ class exception : public std::exception
{
return concat(a, '/', detail::escape(b));
});
#if JSON_DIAGNOSTIC_POSITIONS
str += ", byte " + std::to_string(leaf_element->start_pos()) + "-" + std::to_string(leaf_element->end_pos());
#endif
return concat('(', str, ") ");
#elif JSON_DIAGNOSTIC_POSITIONS
auto str = "byte " + std::to_string(leaf_element->start_pos()) + "-" + std::to_string(leaf_element->end_pos());
return concat('(', str, ") ");
#else
static_cast<void>(leaf_element);
Expand Down
7 changes: 6 additions & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4510,7 +4510,6 @@ class exception : public std::exception
}
break;
}

case value_t::null: // LCOV_EXCL_LINE
case value_t::string: // LCOV_EXCL_LINE
case value_t::boolean: // LCOV_EXCL_LINE
Expand All @@ -4534,6 +4533,12 @@ class exception : public std::exception
{
return concat(a, '/', detail::escape(b));
});
#if JSON_DIAGNOSTIC_POSITIONS
str += ", byte " + std::to_string(leaf_element->start_pos()) + "-" + std::to_string(leaf_element->end_pos());
#endif
return concat('(', str, ") ");
#elif JSON_DIAGNOSTIC_POSITIONS
auto str = "byte " + std::to_string(leaf_element->start_pos()) + "-" + std::to_string(leaf_element->end_pos());
return concat('(', str, ") ");
#else
static_cast<void>(leaf_element);
Expand Down

0 comments on commit 2cc096a

Please sign in to comment.