Skip to content

Commit 59da644

Browse files
Add more specific error message when attempting to parse empty input (#4180)
1 parent cdb2906 commit 59da644

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

docs/mkdocs/docs/home/exceptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ This error indicates a syntax error while deserializing a JSON text. The error m
139139
No input:
140140

141141
```
142-
[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal
142+
[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON
143143
```
144144

145145
Control character was not escaped:

include/nlohmann/detail/input/parser.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,25 @@ class parser
341341
m_lexer.get_token_string(),
342342
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
343343
}
344+
case token_type::end_of_input:
345+
{
346+
if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
347+
{
348+
return sax->parse_error(m_lexer.get_position(),
349+
m_lexer.get_token_string(),
350+
parse_error::create(101, m_lexer.get_position(),
351+
"attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
352+
}
344353

354+
return sax->parse_error(m_lexer.get_position(),
355+
m_lexer.get_token_string(),
356+
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
357+
}
345358
case token_type::uninitialized:
346359
case token_type::end_array:
347360
case token_type::end_object:
348361
case token_type::name_separator:
349362
case token_type::value_separator:
350-
case token_type::end_of_input:
351363
case token_type::literal_or_value:
352364
default: // the last token was unexpected
353365
{

single_include/nlohmann/json.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12441,13 +12441,25 @@ class parser
1244112441
m_lexer.get_token_string(),
1244212442
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
1244312443
}
12444+
case token_type::end_of_input:
12445+
{
12446+
if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
12447+
{
12448+
return sax->parse_error(m_lexer.get_position(),
12449+
m_lexer.get_token_string(),
12450+
parse_error::create(101, m_lexer.get_position(),
12451+
"attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
12452+
}
1244412453

12454+
return sax->parse_error(m_lexer.get_position(),
12455+
m_lexer.get_token_string(),
12456+
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
12457+
}
1244512458
case token_type::uninitialized:
1244612459
case token_type::end_array:
1244712460
case token_type::end_object:
1244812461
case token_type::name_separator:
1244912462
case token_type::value_separator:
12450-
case token_type::end_of_input:
1245112463
case token_type::literal_or_value:
1245212464
default: // the last token was unexpected
1245312465
{

tests/src/unit-diagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TEST_CASE("Better diagnostics")
7474
SECTION("Parse error")
7575
{
7676
json _;
77-
CHECK_THROWS_WITH_AS(_ = json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error);
77+
CHECK_THROWS_WITH_AS(_ = json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error);
7878
}
7979

8080
SECTION("Wrong type in update()")

tests/src/unit-regression1.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ TEST_CASE("regression tests 1")
722722
{
723723
std::ifstream f("file_not_found.json");
724724
json _;
725-
CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
725+
CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
726726
}
727727

728728
SECTION("issue #367 - calling stream at EOF")
@@ -736,7 +736,7 @@ TEST_CASE("regression tests 1")
736736
// ss is not at EOF; this yielded an error before the fix
737737
// (threw basic_string::append). No, it should just throw
738738
// a parse error because of the EOF.
739-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
739+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
740740
}
741741

742742
SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads")
@@ -745,7 +745,7 @@ TEST_CASE("regression tests 1")
745745
{
746746
std::stringstream ss;
747747
json j;
748-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
748+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
749749
}
750750

751751
SECTION("(whitespace)")
@@ -765,7 +765,7 @@ TEST_CASE("regression tests 1")
765765
CHECK_NOTHROW(ss >> j);
766766
CHECK(j == 111);
767767

768-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
768+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
769769
}
770770

771771
SECTION("one value + whitespace")
@@ -788,7 +788,7 @@ TEST_CASE("regression tests 1")
788788
CHECK_NOTHROW(ss >> j);
789789
CHECK(j == 333);
790790

791-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
791+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
792792
}
793793

794794
SECTION("three values")
@@ -803,7 +803,7 @@ TEST_CASE("regression tests 1")
803803
CHECK_NOTHROW(ss >> j);
804804
CHECK(j == 333);
805805

806-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
806+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
807807
}
808808

809809
SECTION("literals without whitespace")
@@ -820,7 +820,7 @@ TEST_CASE("regression tests 1")
820820
CHECK_NOTHROW(ss >> j);
821821
CHECK(j == "");
822822

823-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
823+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
824824
}
825825

826826
SECTION("example from #529")
@@ -833,7 +833,7 @@ TEST_CASE("regression tests 1")
833833
CHECK_NOTHROW(ss >> j);
834834
CHECK(j == json({{"three", 3}}));
835835

836-
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
836+
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
837837
}
838838

839839
SECTION("second example from #529")

0 commit comments

Comments
 (0)