Skip to content

Commit

Permalink
docs: more sample
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 6, 2024
1 parent e1ffb4e commit 963eb6c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ std::string content = R"(
{ "C_str": "i am a distraction" },
{ "C_str": "you found me!" }
]
}
},
"my_type": { "i": 99 }
})";

// 它是一个 std::optional<json::value>
Expand Down Expand Up @@ -309,6 +310,16 @@ if (opt_n) {
// Output: 3.141600
std::cout << *opt_n << std::endl;
}

// 如你所想,`get` and `find` 也可以用于自定义类型
struct MyType
{
int i = 0;

MEO_JSONIZATION(i);
};
MyType get_custom_value = value.get("my_type", MyType {});
auto find_custom_opt = value.find<MyType>("my_type");
```
还有一些你在序列化中已经见过的技巧
Expand Down
13 changes: 12 additions & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ std::string content = R"(
{ "C_str": "i am a distraction" },
{ "C_str": "you found me!" }
]
}
},
"my_type": { "i": 99 }
})";

// it's a std::optional<json::value>
Expand Down Expand Up @@ -309,6 +310,16 @@ if (opt_n) {
// output: 3.141600
std::cout << *opt_n << std::endl;
}

// following your intuition, `get` and `find can also be used for custom types
struct MyType
{
int i = 0;

MEO_JSONIZATION(i);
};
MyType get_custom_value = value.get("my_type", MyType {});
auto find_custom_opt = value.find<MyType>("my_type");
```
There are also a few tricks you've already seen with Serializing
Expand Down
13 changes: 12 additions & 1 deletion sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ void parsing()
{ "C_str": "i am a distraction" },
{ "C_str": "you found me!" }
]
}
},
"my_type": { "i": 99 }
})";

// it's a std::optional<json::value>
Expand Down Expand Up @@ -294,6 +295,16 @@ void parsing()
std::cout << *opt_n << std::endl;
}

// following your intuition, `get` and `find can also be used for custom types
struct MyType
{
int i = 0;

MEO_JSONIZATION(i);
};
MyType get_custom_value = value.get("my_type", MyType {});
auto find_custom_opt = value.find<MyType>("my_type");

/* There are also a few tricks you've already seen with Serializing */

bool is_vec = value["list"].is<std::vector<int>>();
Expand Down

0 comments on commit 963eb6c

Please sign in to comment.