From 432d78195de421ebf5c638d3e427188473a213c8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 20 Jul 2022 17:02:51 +0200 Subject: [PATCH] :ok_hand: apply review comment --- docs/mkdocs/docs/api/basic_json/accept.md | 4 ++++ docs/mkdocs/docs/api/basic_json/parse.md | 10 +++++++++- docs/mkdocs/docs/home/exceptions.md | 2 +- include/nlohmann/detail/input/input_adapters.hpp | 5 ++--- single_include/nlohmann/json.hpp | 5 ++--- tests/src/unit-deserialization.cpp | 5 +++-- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/mkdocs/docs/api/basic_json/accept.md b/docs/mkdocs/docs/api/basic_json/accept.md index bbbac053a..1e7f00209 100644 --- a/docs/mkdocs/docs/api/basic_json/accept.md +++ b/docs/mkdocs/docs/api/basic_json/accept.md @@ -64,6 +64,10 @@ Whether the input is valid JSON. Strong guarantee: if an exception is thrown, there are no changes in the JSON value. +## Exceptions + +Throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if passed `#!cpp FILE` pointer is `#!cpp nullptr`. + ## Complexity Linear in the length of the input. The parser is a predictive LL(1) parser. diff --git a/docs/mkdocs/docs/api/basic_json/parse.md b/docs/mkdocs/docs/api/basic_json/parse.md index 0bfae1916..9d7566b25 100644 --- a/docs/mkdocs/docs/api/basic_json/parse.md +++ b/docs/mkdocs/docs/api/basic_json/parse.md @@ -28,7 +28,7 @@ static basic_json parse(IteratorType first, IteratorType last, : A compatible input, for instance: - an `std::istream` object - - a `FILE` pointer (will throw [json.exception.parse_error.116](../../home/exceptions.md#jsonexceptionparse_error116) if passed pointer is `#!cpp nullptr`) + - a `FILE` pointer - a C-style array of characters - a pointer to a null-terminated string of single byte characters - a `std::string` @@ -71,6 +71,14 @@ Deserialized JSON value; in case of a parse error and `allow_exceptions` set to Strong guarantee: if an exception is thrown, there are no changes in the JSON value. +## Exceptions + +- Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token. +- Throws [`parse_error.102`](../../home/exceptions.md#jsonexceptionparse_error102) if to_unicode fails or surrogate + error. +- Throws [`parse_error.103`](../../home/exceptions.md#jsonexceptionparse_error103) if to_unicode fails. +- Throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if passed `#!cpp FILE` pointer is `#!cpp nullptr`. + ## Complexity Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the parser diff --git a/docs/mkdocs/docs/home/exceptions.md b/docs/mkdocs/docs/home/exceptions.md index 8efad510e..7e27d51da 100644 --- a/docs/mkdocs/docs/home/exceptions.md +++ b/docs/mkdocs/docs/home/exceptions.md @@ -361,7 +361,7 @@ A `#!cpp FILE*` pointer passed to the [parse](../api/basic_json/parse.md) functi !!! failure "Example message" ``` - [json.exception.parse_error.116] parse error: input file is invalid: No such file or directory + [json.exception.parse_error.116] parse error: input file is invalid ``` ## Iterator errors diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index a3a31d173..805a965d0 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -9,9 +9,8 @@ #pragma once #include // array -#include // errno #include // size_t -#include // strlen, strerror +#include // strlen #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -55,7 +54,7 @@ class file_input_adapter { if (m_file == nullptr) { - JSON_THROW(parse_error::create(116, 0, detail::concat("input file is invalid: ", reinterpret_cast(std::strerror(errno))), nullptr)); + JSON_THROW(parse_error::create(116, 0, "input file is invalid", nullptr)); } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1ffefec60..044189df4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5893,9 +5893,8 @@ std::size_t hash(const BasicJsonType& j) #include // array -#include // errno #include // size_t -#include // strlen, strerror +#include // strlen #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -5942,7 +5941,7 @@ class file_input_adapter { if (m_file == nullptr) { - JSON_THROW(parse_error::create(116, 0, detail::concat("input file is invalid: ", reinterpret_cast(std::strerror(errno))), nullptr)); + JSON_THROW(parse_error::create(116, 0, "input file is invalid", nullptr)); } } diff --git a/tests/src/unit-deserialization.cpp b/tests/src/unit-deserialization.cpp index a6a8fd542..06ae503e1 100644 --- a/tests/src/unit-deserialization.cpp +++ b/tests/src/unit-deserialization.cpp @@ -335,9 +335,10 @@ TEST_CASE("deserialization") SECTION("FILE*") { - std::FILE* f = std::fopen("nonexisting_file", "r"); // NOTLINT(cppcoreguidelines-owning-memory) + std::FILE* f = std::fopen("nonexisting_file", "r"); // NOLINT(cppcoreguidelines-owning-memory) json _; - CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.116] parse error: input file is invalid: No such file or directory", json::parse_error&); + CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.116] parse error: input file is invalid", json::parse_error&); + CHECK_THROWS_WITH_AS(_ = json::accept(f), "[json.exception.parse_error.116] parse error: input file is invalid", json::parse_error&); } }