Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some cleanup for extended diagnostic branch #1

Open
wants to merge 5 commits into
base: diagnostics
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <valarray> // valarray

#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/diagnostics_t.hpp>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/cpp_future.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
Expand All @@ -28,7 +27,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
{
JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j));
}
n = nullptr;
}
Expand Down Expand Up @@ -59,7 +58,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
}

default:
JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
}
}

Expand All @@ -68,7 +67,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
{
JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j));
}
b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
}
Expand All @@ -78,7 +77,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
}
s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
Expand All @@ -94,7 +93,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
}

s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
Expand Down Expand Up @@ -134,7 +133,7 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
}
l.clear();
std::transform(j.rbegin(), j.rend(),
Expand All @@ -151,7 +150,7 @@ void from_json(const BasicJsonType& j, std::valarray<T>& l)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
}
l.resize(j.size());
std::transform(j.begin(), j.end(), std::begin(l),
Expand Down Expand Up @@ -242,7 +241,7 @@ void())
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
}

from_json_array_impl(j, arr, priority_tag<3> {});
Expand All @@ -253,7 +252,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
{
JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j));
}

bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
Expand All @@ -265,7 +264,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
{
JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j));
}

ConstructibleObjectType ret;
Expand Down Expand Up @@ -319,7 +318,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
}

default:
JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
}
}

Expand Down Expand Up @@ -348,14 +347,14 @@ void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>&
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
}
m.clear();
for (const auto& p : j)
{
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
}
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
}
Expand All @@ -368,14 +367,14 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
}
m.clear();
for (const auto& p : j)
{
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
{
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t<BasicJsonType>(j)));
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
}
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
}
Expand Down
23 changes: 23 additions & 0 deletions include/nlohmann/detail/diagnostics_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <nlohmann/detail/value_t.hpp>
#include <nlohmann/detail/string_escape.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>

namespace nlohmann
{
Expand Down Expand Up @@ -82,5 +83,27 @@ class diagnostics_t
const BasicJsonType* m_j = nullptr;
};

template <typename DiagnosticsContext>
std::string diagonstics_message(const DiagnosticsContext&) = delete;

template <typename BasicJsonType>
std::string diagnostics_message(const diagnostics_t<BasicJsonType>& context)
{
return context.diagnostics();
}

template <typename BasicJsonType>
typename std::enable_if<is_basic_json<BasicJsonType>::value, std::string>::type
diagnostics_message(const BasicJsonType& context)
{
return diagnostics_message(diagnostics_t<BasicJsonType>(context));
}

inline
std::string diagnostics_message(std::nullptr_t)
{
return {};
}

} // namespace detail
} // namespace nlohmann
36 changes: 18 additions & 18 deletions include/nlohmann/detail/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,20 @@ class parse_error : public exception
@param[in] what_arg the explanatory string
@return parse_error object
*/
template<typename BasicJsonType>
static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t<BasicJsonType>& diagnostics)
template<typename DiagnosticsContext = std::nullptr_t>
static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const DiagnosticsContext& context = nullptr)
{
std::string w = exception::name("parse_error", id_) + "parse error" +
position_string(pos) + ": " + diagnostics.diagnostics() + what_arg;
position_string(pos) + ": " + diagnostics_message(context) + what_arg;
return parse_error(id_, pos.chars_read_total, w.c_str());
}

template<typename BasicJsonType>
static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t<BasicJsonType>& diagnostics)
template<typename DiagnosticsContext = std::nullptr_t>
static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const DiagnosticsContext& context = nullptr)
{
std::string w = exception::name("parse_error", id_) + "parse error" +
(byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
": " + diagnostics.diagnostics() + what_arg;
": " + diagnostics_message(context) + what_arg;
return parse_error(id_, byte_, w.c_str());
}

Expand Down Expand Up @@ -207,10 +207,10 @@ caught.,invalid_iterator}
class invalid_iterator : public exception
{
public:
template<typename BasicJsonType>
static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t<BasicJsonType>& diagnostics)
template<typename DiagnosticsContext = std::nullptr_t>
static invalid_iterator create(int id_, const std::string& what_arg, const DiagnosticsContext& context = nullptr)
{
std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg;
std::string w = exception::name("invalid_iterator", id_) + diagnostics_message(context) + what_arg;
return invalid_iterator(id_, w.c_str());
}

Expand Down Expand Up @@ -262,10 +262,10 @@ caught.,type_error}
class type_error : public exception
{
public:
template<typename BasicJsonType>
static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t<BasicJsonType>& diagnostics)
template<typename DiagnosticsContext = std::nullptr_t>
static type_error create(int id_, const std::string& what_arg, const DiagnosticsContext& context = nullptr)
{
std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg;
std::string w = exception::name("type_error", id_) + diagnostics_message(context) + what_arg;
return type_error(id_, w.c_str());
}

Expand Down Expand Up @@ -310,10 +310,10 @@ caught.,out_of_range}
class out_of_range : public exception
{
public:
template<typename BasicJsonType>
static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t<BasicJsonType>& diagnostics)
template<typename DiagnosticsContext = std::nullptr_t>
static out_of_range create(int id_, const std::string& what_arg, const DiagnosticsContext& context = nullptr)
{
std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg;
std::string w = exception::name("out_of_range", id_) + diagnostics_message(context) + what_arg;
return out_of_range(id_, w.c_str());
}

Expand Down Expand Up @@ -349,10 +349,10 @@ caught.,other_error}
class other_error : public exception
{
public:
template<typename BasicJsonType>
static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t<BasicJsonType>& diagnostics)
template<typename DiagnosticsContext = std::nullptr_t>
static other_error create(int id_, const std::string& what_arg, const DiagnosticsContext& context = nullptr)
{
std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg;
std::string w = exception::name("other_error", id_) + diagnostics_message(context) + what_arg;
return other_error(id_, w.c_str());
}

Expand Down
Loading