diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index 02f1b40bb..bd0979524 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -198,7 +198,7 @@ class parse_error : public exception { std::string w = exception::name("parse_error", id_) + "parse error" + position_string(pos) + ": " + exception::diagnostics(context) + what_arg; - return parse_error(id_, pos.chars_read_total, w.c_str()); + return {id_, pos.chars_read_total, w.c_str()}; } template @@ -207,7 +207,7 @@ class parse_error : public exception std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + ": " + exception::diagnostics(context) + what_arg; - return parse_error(id_, byte_, w.c_str()); + return {id_, byte_, w.c_str()}; } /*! @@ -276,7 +276,7 @@ class invalid_iterator : public exception static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg; - return invalid_iterator(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -331,7 +331,7 @@ class type_error : public exception static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; - return type_error(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -379,7 +379,7 @@ class out_of_range : public exception static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; - return out_of_range(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -418,7 +418,7 @@ class other_error : public exception static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; - return other_error(id_, w.c_str()); + return {id_, w.c_str()}; } private: diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index fabb3d3b5..786507c41 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -9,6 +9,8 @@ #include // snprintf #include // numeric_limits #include // string, char_traits +#include // setfill, setw +#include // stringstream #include // is_same #include // move @@ -499,10 +501,9 @@ class serializer { case error_handler_t::strict: { - std::string sn(9, '\0'); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); + std::stringstream ss; + ss << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << byte; + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + ss.str(), BasicJsonType())); } case error_handler_t::ignore: @@ -594,10 +595,9 @@ class serializer { case error_handler_t::strict: { - std::string sn(9, '\0'); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); + std::stringstream ss; + ss << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << s.back(); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + ss.str(), BasicJsonType())); } case error_handler_t::ignore: diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index bc6797dc9..0a1bafbb0 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1073,64 +1073,34 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } /// constructor for strings - json_value(const string_t& value) - { - string = create(value); - } + json_value(const string_t& value) : string(create(value)) {} /// constructor for rvalue strings - json_value(string_t&& value) - { - string = create(std::move(value)); - } + json_value(string_t&& value) : string(create(std::move(value))) {} /// constructor for objects - json_value(const object_t& value) - { - object = create(value); - } + json_value(const object_t& value) : object(create(value)) {} /// constructor for rvalue objects - json_value(object_t&& value) - { - object = create(std::move(value)); - } + json_value(object_t&& value) : object(create(std::move(value))) {} /// constructor for arrays - json_value(const array_t& value) - { - array = create(value); - } + json_value(const array_t& value) : array(create(value)) {} /// constructor for rvalue arrays - json_value(array_t&& value) - { - array = create(std::move(value)); - } + json_value(array_t&& value) : array(create(std::move(value))) {} /// constructor for binary arrays - json_value(const typename binary_t::container_type& value) - { - binary = create(value); - } + json_value(const typename binary_t::container_type& value) : binary(create(value)) {} /// constructor for rvalue binary arrays - json_value(typename binary_t::container_type&& value) - { - binary = create(std::move(value)); - } + json_value(typename binary_t::container_type&& value) : binary(create(std::move(value))) {} /// constructor for binary arrays (internal type) - json_value(const binary_t& value) - { - binary = create(value); - } + json_value(const binary_t& value) : binary(create(value)) {} /// constructor for rvalue binary arrays (internal type) - json_value(binary_t&& value) - { - binary = create(std::move(value)); - } + json_value(binary_t&& value) : binary(create(std::move(value))) {} void destroy(value_t t) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 87475ab31..52a1c85c6 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2914,7 +2914,7 @@ class parse_error : public exception { std::string w = exception::name("parse_error", id_) + "parse error" + position_string(pos) + ": " + exception::diagnostics(context) + what_arg; - return parse_error(id_, pos.chars_read_total, w.c_str()); + return {id_, pos.chars_read_total, w.c_str()}; } template @@ -2923,7 +2923,7 @@ class parse_error : public exception std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + ": " + exception::diagnostics(context) + what_arg; - return parse_error(id_, byte_, w.c_str()); + return {id_, byte_, w.c_str()}; } /*! @@ -2992,7 +2992,7 @@ class invalid_iterator : public exception static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg; - return invalid_iterator(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -3047,7 +3047,7 @@ class type_error : public exception static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; - return type_error(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -3095,7 +3095,7 @@ class out_of_range : public exception static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; - return out_of_range(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -3134,7 +3134,7 @@ class other_error : public exception static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; - return other_error(id_, w.c_str()); + return {id_, w.c_str()}; } private: @@ -15315,6 +15315,8 @@ class binary_writer #include // snprintf #include // numeric_limits #include // string, char_traits +#include // setfill, setw +#include // stringstream #include // is_same #include // move @@ -16923,10 +16925,9 @@ class serializer { case error_handler_t::strict: { - std::string sn(9, '\0'); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); + std::stringstream ss; + ss << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << byte; + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + ss.str(), BasicJsonType())); } case error_handler_t::ignore: @@ -17018,10 +17019,9 @@ class serializer { case error_handler_t::strict: { - std::string sn(9, '\0'); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); + std::stringstream ss; + ss << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << s.back(); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + ss.str(), BasicJsonType())); } case error_handler_t::ignore: @@ -18574,64 +18574,34 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } /// constructor for strings - json_value(const string_t& value) - { - string = create(value); - } + json_value(const string_t& value) : string(create(value)) {} /// constructor for rvalue strings - json_value(string_t&& value) - { - string = create(std::move(value)); - } + json_value(string_t&& value) : string(create(std::move(value))) {} /// constructor for objects - json_value(const object_t& value) - { - object = create(value); - } + json_value(const object_t& value) : object(create(value)) {} /// constructor for rvalue objects - json_value(object_t&& value) - { - object = create(std::move(value)); - } + json_value(object_t&& value) : object(create(std::move(value))) {} /// constructor for arrays - json_value(const array_t& value) - { - array = create(value); - } + json_value(const array_t& value) : array(create(value)) {} /// constructor for rvalue arrays - json_value(array_t&& value) - { - array = create(std::move(value)); - } + json_value(array_t&& value) : array(create(std::move(value))) {} /// constructor for binary arrays - json_value(const typename binary_t::container_type& value) - { - binary = create(value); - } + json_value(const typename binary_t::container_type& value) : binary(create(value)) {} /// constructor for rvalue binary arrays - json_value(typename binary_t::container_type&& value) - { - binary = create(std::move(value)); - } + json_value(typename binary_t::container_type&& value) : binary(create(std::move(value))) {} /// constructor for binary arrays (internal type) - json_value(const binary_t& value) - { - binary = create(value); - } + json_value(const binary_t& value) : binary(create(value)) {} /// constructor for rvalue binary arrays (internal type) - json_value(binary_t&& value) - { - binary = create(std::move(value)); - } + json_value(binary_t&& value) : binary(create(std::move(value))) {} void destroy(value_t t) {