Skip to content

Commit

Permalink
chore: 整理MEO_OPT及文档
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 24, 2024
1 parent 1e45d6f commit 9c007b5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 63 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,29 @@ void test_jsonization()
double d = 0;

// 如果你正在使用 MSVC, 请添加 "/Zc:preprocessor" 到项目设置中
MEO_JSONIZATION(vec, map, i, d);
MEO_JSONIZATION(vec, map, MEO_OPT i, d);
};

MyStruct a;
a.vec = { 1, 2, 3, 4, 5 };
a.vec = { 1, 2, 3 };
a.map = { { "key", 5 } };
a.i = 100;
a.d = 0.5;

json::object j = a.dump_to_json();
json::object dumps = a.to_json();

// output: { "d" : 0.500000, "i" : 100, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
std::cout << j << std::endl;
std::cout << dumps << std::endl;

// MEO_OPT 表示该变量是一个可选项
// 即使输入中不存在该字段依然可以读取
dumps.erase("i")

MyStruct b;
b.load_from_json(j);
b.from_json(dumps);

// output: { "d" : 0.500000, "i" : 100, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
std::cout << b.dump_to_json() << std::endl;
// output: { "d" : 0.500000, "i" : 0, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
// 我们从 dumps 中删除了 "i", 所以 "i" 是 0
std::cout << b.to_json() << std::endl;
}
```
19 changes: 12 additions & 7 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,29 @@ void test_jsonization()
double d = 0;

// if you are using MSVC, please add "/Zc:preprocessor" to your project
MEO_JSONIZATION(vec, map, i, d);
MEO_JSONIZATION(vec, map, MEO_OPT i, d);
};

MyStruct a;
a.vec = { 1, 2, 3, 4, 5 };
a.vec = { 1, 2, 3 };
a.map = { { "key", 5 } };
a.i = 100;
a.d = 0.5;

json::object j = a.dump_to_json();
json::object dumps = a.to_json();

// output: { "d" : 0.500000, "i" : 100, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
std::cout << j << std::endl;
std::cout << dumps << std::endl;

// MEO_OPT means the var is optional
// and can still be read even if the field doesn't exist in the input.
dumps.erase("i")

MyStruct b;
b.load_from_json(j);
b.from_json(dumps);

// output: { "d" : 0.500000, "i" : 100, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
std::cout << b.dump_to_json() << std::endl;
// output: { "d" : 0.500000, "i" : 0, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
// "i" is 0 because we erase "i" from the dumps
std::cout << b.to_json() << std::endl;
}
```
49 changes: 1 addition & 48 deletions include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,49 +2779,6 @@ static constexpr string_t unescape_string(const string_t& str)

return result;
}

namespace _jsonization_helper
{
struct required_tag
{
constexpr static bool required = true;
};
struct optional_tag
{
constexpr static bool required = false;
};
}

struct bind_helper
{
void __to_json(json::object&) const {}

bool __from_json(const json::object&) { return true; }

template <typename Tag, typename Var, typename... Rest>
void __to_json(json::object& obj, Tag, const Var& var, const char* name, Rest&&... rest) const
{
obj[name] = json::serialize<false>(var);
__to_json(obj, std::forward<Rest>(rest)...);
}

template <typename Tag, typename Var, typename... Rest>
bool __from_json(const json::object& raw, Tag, Var& var, const char* name, Rest&&... rest)
{
if (auto opt = raw.find(name)) {
if (!opt->is<Var>()) {
return false;
}
var = std::move(opt)->as<Var>();
}
else {
if constexpr (Tag::required) {
return false;
}
}
return __from_json(raw, std::forward<Rest>(rest)...);
}
};
} // namespace json

namespace json::_jsonization_helper
Expand All @@ -2837,7 +2794,7 @@ class dumper
template <typename var_t, typename... rest_t>
json::object to_json(const char* key, const var_t& var, rest_t&&... rest) const
{
auto result = to_json(std::forward<rest_t>(rest)...);
json::object result = to_json(std::forward<rest_t>(rest)...);
if constexpr (!std::is_same_v<next_is_optional_t, var_t>) {
result.emplace(key, json::serialize<false>(var));
}
Expand Down Expand Up @@ -2989,10 +2946,6 @@ namespace json::_private_macro
return json::_jsonization_helper::loader().from_json(_MEOJSON_VARNAME(in), _MEOJSON_VARNAME(error_key), \
_MEOJSON_FOR_EACH(_MEOJSON_KEY_VALUE, __VA_ARGS__) \
json::_jsonization_helper::va_arg_end()); \
} \
explicit operator json::object() const \
{ \
return to_json(); \
}

#define MEO_OPT json::_jsonization_helper::next_is_optional_t {},
2 changes: 1 addition & 1 deletion sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ void test_jsonization()
json::object j = a.to_json();
std::cout << j << std::endl;

// for test MEO_OPTIONAL
// for test MEO_OPT
j.erase("i");

MyStruct b;
Expand Down

0 comments on commit 9c007b5

Please sign in to comment.