From 9c007b53f57b659ff0cb0731d2fc98c632f6f40e Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 24 Jan 2024 23:21:24 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=95=B4=E7=90=86MEO=5FOPT=E5=8F=8A?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 +++++++++++------- README_en.md | 19 +++++++++++------- include/json.hpp | 49 +---------------------------------------------- sample/sample.cpp | 2 +- 4 files changed, 26 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 366eae9..fdc7f6e 100644 --- a/README.md +++ b/README.md @@ -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; } ``` diff --git a/README_en.md b/README_en.md index 809f6ea..986e3dd 100644 --- a/README_en.md +++ b/README_en.md @@ -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; } ``` diff --git a/include/json.hpp b/include/json.hpp index b9c61b5..2106ec1 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -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 - void __to_json(json::object& obj, Tag, const Var& var, const char* name, Rest&&... rest) const - { - obj[name] = json::serialize(var); - __to_json(obj, std::forward(rest)...); - } - - template - bool __from_json(const json::object& raw, Tag, Var& var, const char* name, Rest&&... rest) - { - if (auto opt = raw.find(name)) { - if (!opt->is()) { - return false; - } - var = std::move(opt)->as(); - } - else { - if constexpr (Tag::required) { - return false; - } - } - return __from_json(raw, std::forward(rest)...); - } -}; } // namespace json namespace json::_jsonization_helper @@ -2837,7 +2794,7 @@ class dumper template json::object to_json(const char* key, const var_t& var, rest_t&&... rest) const { - auto result = to_json(std::forward(rest)...); + json::object result = to_json(std::forward(rest)...); if constexpr (!std::is_same_v) { result.emplace(key, json::serialize(var)); } @@ -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 {}, diff --git a/sample/sample.cpp b/sample/sample.cpp index b28b9f3..6fa4622 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -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;