Skip to content

Commit

Permalink
perf: remove next_is_optional member var
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 25, 2024
1 parent b0ade5a commit 0f34600
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
77 changes: 37 additions & 40 deletions include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2905,9 +2905,8 @@ struct next_is_optional_t
struct va_arg_end
{};

class dumper
struct dumper
{
public:
template <typename var_t, typename... rest_t>
json::value _to_json(const char* key, const var_t& var, rest_t&&... rest) const
{
Expand All @@ -2923,68 +2922,66 @@ class dumper
json::value _to_json(va_arg_end) const { return {}; }
};

class checker
struct checker
{
public:
template <typename var_t, typename... rest_t>
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<var_t>()) {
error_key = key;
return false;
}
_next_is_optional = false;
}
else if (!_next_is_optional) {
auto opt = in.find(key);
if (!opt || !opt->is<var_t>()) {
error_key = key;
return false;
}
return _check_json(in, error_key, std::forward<rest_t>(rest)...);
}
template <typename... rest_t>
bool _check_json(const json::value& in, std::string& error_key, const char*, next_is_optional_t, rest_t&&... rest)
template <typename var_t, typename... rest_t>
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<var_t>()) {
error_key = key;
return false;
}
} // next_is_optional_t, ignore key not found

return _check_json(in, error_key, std::forward<rest_t>(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 <typename var_t, typename... rest_t>
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<var_t>()) {
auto test = opt->dumps();
error_key = key;
return false;
}
var = std::move(opt)->as<var_t>();
_next_is_optional = false;
}
else if (!_next_is_optional) {
auto opt = in.find(key);
if (!opt || !opt->is<var_t>()) {
error_key = key;
return false;
}
var = std::move(opt)->as<var_t>();

return _from_json(in, error_key, std::forward<rest_t>(rest)...);
}
template <typename... rest_t>
bool _from_json(const json::value& in, std::string& error_key, const char*, next_is_optional_t, rest_t&&... rest)
template <typename var_t, typename... rest_t>
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<var_t>()) {
error_key = key;
return false;
}
var = std::move(opt)->as<var_t>();
} // next_is_optional_t, ignore key not found

return _from_json(in, error_key, std::forward<rest_t>(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

Expand Down
4 changes: 2 additions & 2 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -335,7 +335,7 @@ void test_jsonization()
int b_i = 10;
double b_d = 0.5;

AAA b_aaa;
std::vector<AAA> b_aaa;

MEO_JSONIZATION(b_i, MEO_OPT b_d, b_aaa);
};
Expand Down

0 comments on commit 0f34600

Please sign in to comment.