From 45d40fd8cecbc4685f53226c687cacc7424c6656 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 23 Jan 2024 18:40:41 +0800 Subject: [PATCH] tmp: JOBJECT --- sample/sample.cpp | 65 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/sample/sample.cpp b/sample/sample.cpp index e00c3be..0a5beb2 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -11,20 +11,23 @@ bool parsing(); bool parsing_width(); bool serializing(); +void test_JOBJECT(); int main() { - std::cout << "\n****** Parsing ******\n" << std::endl; + // std::cout << "\n****** Parsing ******\n" << std::endl; - if (!parsing()) { - return -1; - } + // if (!parsing()) { + // return -1; + // } - std::cout << "\n****** Serializing ******\n" << std::endl; + // std::cout << "\n****** Serializing ******\n" << std::endl; - if (!serializing()) { - return -1; - } + // if (!serializing()) { + // return -1; + // } + + test_JOBJECT(); return 0; } @@ -293,3 +296,49 @@ bool serializing() return true; } + +void test_JOBJECT() +{ + +#define DEF_DUMP_TO_JSON(x) \ + json::object dump_to_json() const \ + { \ + return { \ + { #x, json::serialize(x) }, \ + }; \ + } + +#define DEF_LOAD_FROM_JSON(x) \ + bool load_from_json(const json::object& raw) \ + { \ + auto x##opt = raw.find(#x); \ + if (!x##opt) { \ + return false; \ + } \ + x = *std::move(x##opt); \ + return true; \ + } + + struct MyStruct + { + std::vector x; + int y = 0; + + double a = 0; + double b = 0; + + DEF_DUMP_TO_JSON(x); + DEF_LOAD_FROM_JSON(x); + }; + + MyStruct a; + a.x = { 1, 2, 3, 4, 5 }; + + json::object j = a.dump_to_json(); + std::cout << j << std::endl; + + MyStruct b; + b.load_from_json(j); + + std::cout << b.dump_to_json() << std::endl; +}