Skip to content

Commit

Permalink
feat: MEO_JSONIZATION (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: nekosu <[email protected]>
  • Loading branch information
MistEO and neko-para authored Jan 25, 2024
1 parent ab2cea1 commit 820bb65
Show file tree
Hide file tree
Showing 5 changed files with 542 additions and 32 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ option(BUILD_TESTING "Build testing" ON)
set(CMAKE_CXX_STANDARD 17)

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
add_compile_options("/utf-8" "/W4" "/WX")
add_compile_options("/utf-8" "/W4" "/WX" "/Zc:preprocessor")
else()
add_compile_options("-Wall;-Wextra;-Wpedantic;-Werror;-mtune=native")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options("-Wno-gnu-zero-variadic-macro-arguments")
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|EM64T|x86_64")
add_compile_options("-msse4.1")
endif()
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,48 @@ void serializing()
ofs.close();
}
```

### 序列化

```c++
// 如果使用 MSVC, 请添加 "/Zc:preprocessor" 到项目配置中
// 如果使用 AppleClang, 请添加 "-Wno-gnu-zero-variadic-macro-arguments" 到项目配置中
void test_jsonization()
{
struct MyStruct
{
std::vector<int> vec;
std::map<std::string, int> map;
int i = 0;
double d = 0;

// MEO_OPT 表示该变量是一个可选项
// 即使输入中不存在该字段依然可以读取
MEO_JSONIZATION(vec, map, MEO_OPT i, d);
};

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

json::value dumps = a;

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

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

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

// output: { "d" : 0.500000, "i" : 0, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
// 我们从 dumps 中删除了 "i", 所以 "i" 是 0
std::cout << json::value(b) << std::endl;
}
```
44 changes: 44 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,47 @@ void serializing()
ofs.close();
}
```

### JSONization

```c++
// if you are using MSVC, please add "/Zc:preprocessor" to your project
// if you are using AppleClang, please add "-Wno-gnu-zero-variadic-macro-arguments" to your project
void test_jsonization()
{
struct MyStruct
{
std::vector<int> vec;
std::map<std::string, int> map;
int i = 0;
double d = 0;

// MEO_OPT means the var is optional
// and can still be read even if the field doesn't exist in the input.
MEO_JSONIZATION(vec, map, MEO_OPT i, d);
};

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

json::value dumps = a;

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

dumps.erase("i")
// output: { "d" : 0.500000, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
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.
MyStruct b(dumps);

// 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 << json::value(b) << std::endl;
}
```
Loading

0 comments on commit 820bb65

Please sign in to comment.