diff --git a/include/json.hpp b/include/json.hpp index 259370e..9cbee67 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -2905,9 +2905,8 @@ struct next_is_optional_t struct va_arg_end {}; -class dumper +struct dumper { -public: template json::value _to_json(const char* key, const var_t& var, rest_t&&... rest) const { @@ -2923,68 +2922,66 @@ class dumper json::value _to_json(va_arg_end) const { return {}; } }; -class checker +struct checker { -public: template - bool _check_json(const json::value& in, std::string& error_key, const char* key, var_t&, rest_t&&... rest) + bool _check_json(const json::value& in, std::string& error_key, const char* key, const var_t&, + rest_t&&... rest) const { - if (auto opt = in.find(key)) { - if (!opt->is()) { - error_key = key; - return false; - } - _next_is_optional = false; - } - else if (!_next_is_optional) { + auto opt = in.find(key); + if (!opt || !opt->is()) { error_key = key; return false; } return _check_json(in, error_key, std::forward(rest)...); } - template - bool _check_json(const json::value& in, std::string& error_key, const char*, next_is_optional_t, rest_t&&... rest) + template + bool _check_json(const json::value& in, std::string& error_key, const char*, next_is_optional_t, const char* key, + const var_t&, rest_t&&... rest) const { - _next_is_optional = true; + auto opt = in.find(key); + if (opt) { + if (!opt->is()) { + error_key = key; + return false; + } + } // next_is_optional_t, ignore key not found + return _check_json(in, error_key, std::forward(rest)...); } - bool _check_json(const json::value&, std::string&, va_arg_end) { return true; } - -private: - bool _next_is_optional = false; + bool _check_json(const json::value&, std::string&, va_arg_end) const { return true; } }; -class loader +struct loader { -public: template - bool _from_json(const json::value& in, std::string& error_key, const char* key, var_t& var, rest_t&&... rest) + bool _from_json(const json::value& in, std::string& error_key, const char* key, var_t& var, rest_t&&... rest) const { - if (auto opt = in.find(key)) { - if (!opt->is()) { - auto test = opt->dumps(); - error_key = key; - return false; - } - var = std::move(opt)->as(); - _next_is_optional = false; - } - else if (!_next_is_optional) { + auto opt = in.find(key); + if (!opt || !opt->is()) { error_key = key; return false; } + var = std::move(opt)->as(); + return _from_json(in, error_key, std::forward(rest)...); } - template - bool _from_json(const json::value& in, std::string& error_key, const char*, next_is_optional_t, rest_t&&... rest) + template + bool _from_json(const json::value& in, std::string& error_key, const char*, next_is_optional_t, const char* key, + var_t& var, rest_t&&... rest) const { - _next_is_optional = true; + auto opt = in.find(key); + if (opt) { + if (!opt->is()) { + error_key = key; + return false; + } + var = std::move(opt)->as(); + } // next_is_optional_t, ignore key not found + return _from_json(in, error_key, std::forward(rest)...); } - bool _from_json(const json::value&, std::string&, va_arg_end) { return true; } - -private: - bool _next_is_optional = false; + bool _from_json(const json::value&, std::string&, va_arg_end) const { return true; } }; } // namespace json::_jsonization_helper diff --git a/sample/sample.cpp b/sample/sample.cpp index 34245cd..e4169dc 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -308,7 +308,7 @@ json::value to_json(const TypeFromOtherLibrary& t) { return t.a_i; } -bool check_json(const json::value& j, TypeFromOtherLibrary) +bool check_json(const json::value& j, const TypeFromOtherLibrary&) { return j.is_number(); } @@ -335,7 +335,7 @@ void test_jsonization() int b_i = 10; double b_d = 0.5; - AAA b_aaa; + std::vector b_aaa; MEO_JSONIZATION(b_i, MEO_OPT b_d, b_aaa); };