Skip to content

Commit

Permalink
docs: JSONIZATION
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 25, 2024
1 parent a9ae9a2 commit 796812e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ void serializing()
### 序列化

```c++
// 如果使用 MSVC, 请添加 "/Zc:preprocessor" 到项目配置中
// 如果使用 AppleClang, 请添加 "-Wno-gnu-zero-variadic-macro-arguments" 到项目配置中
void test_jsonization()
{
struct MyStruct
Expand All @@ -269,7 +271,6 @@ void test_jsonization()
int i = 0;
double d = 0;

// 如果你正在使用 MSVC, 请添加 "/Zc:preprocessor" 到项目设置中
// MEO_OPT 表示该变量是一个可选项
// 即使输入中不存在该字段依然可以读取
Expand All @@ -282,7 +283,7 @@ void test_jsonization()
a.i = 100;
a.d = 0.5;

json::object dumps = a.to_json();
json::value dumps = a;

// output: { "d" : 0.500000, "i" : 100, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
std::cout << dumps << std::endl;
Expand All @@ -293,11 +294,10 @@ void test_jsonization()

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

// output: { "d" : 0.500000, "i" : 0, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
// 我们从 dumps 中删除了 "i", 所以 "i" 是 0
std::cout << b.to_json() << std::endl;
std::cout << json::value(b) << std::endl;
}
```
11 changes: 5 additions & 6 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ void serializing()
### 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
Expand All @@ -269,8 +271,6 @@ void test_jsonization()
int i = 0;
double d = 0;

// if you are using MSVC, please add "/Zc:preprocessor" to your project
// 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);
Expand All @@ -282,7 +282,7 @@ void test_jsonization()
a.i = 100;
a.d = 0.5;

json::object dumps = a.to_json();
json::value dumps = a;

// output: { "d" : 0.500000, "i" : 100, "map" : { "key" : 5 }, "vec" : [ 1, 2, 3 ] }
std::cout << dumps << std::endl;
Expand All @@ -293,11 +293,10 @@ void test_jsonization()

// MEO_OPT means the var is optional
// and can still be read even if the field doesn't exist in the input.
MyStruct b;
b.from_json(dumps);
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 << b.to_json() << std::endl;
std::cout << json::value(b) << std::endl;
}
```

0 comments on commit 796812e

Please sign in to comment.