Skip to content

Commit

Permalink
fix: deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 1, 2024
1 parent 21791a2 commit d2ad38a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ struct Outter2
};
```

如果你不喜欢愚蠢的侵入式函数,你可以使用 `json::serialize``json::deserialize` 进行更优雅的转换:

```c++
struct Serializer
{
json::value operator()(const ThirdPartyStruct& t) const { return t.a; }
};
struct Deserializer
{
bool operator()(const json::value& j, ThirdPartyStruct& t) const
{
if (!j.is_number()) return false;
t.a = j.as_integer();
return true;
}
};

std::map<std::string, ThirdPartyStruct> third;
third["key"] = { 100 };
json::value jthird = json::serialize(third, Serializer {});

std::cout << jthird << std::endl;

std::map<std::string, ThirdPartyStruct> new_third;
bool ret = json::deserialize(jthird, new_third, Deserializer {});
```
还有一些琐碎的特性:
```c++
Expand Down
27 changes: 27 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ struct Outter2
};
```

If you don't like stupid invasive function, you can use `json::serialize` and `json::deserialize` for more elegant conversion:

```c++
struct Serializer
{
json::value operator()(const ThirdPartyStruct& t) const { return t.a; }
};
struct Deserializer
{
bool operator()(const json::value& j, ThirdPartyStruct& t) const
{
if (!j.is_number()) return false;
t.a = j.as_integer();
return true;
}
};

std::map<std::string, ThirdPartyStruct> third;
third["key"] = { 100 };
json::value jthird = json::serialize(third, Serializer {});

std::cout << jthird << std::endl;

std::map<std::string, ThirdPartyStruct> new_third;
bool ret = json::deserialize(jthird, new_third, Deserializer {});
```
And some trivial features:
```c++
Expand Down
6 changes: 3 additions & 3 deletions include/common/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace _serialization_helper
class is_serializable
{
template <typename U>
static auto test(int) -> decltype(std::declval<serializer_t>()(std::declval<in_t>()), std::true_type());
static auto test(int) -> decltype(std::declval<serializer_t>()(std::declval<U>()), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down Expand Up @@ -51,7 +51,7 @@ namespace _serialization_helper
{
template <typename U>
static auto test(int)
-> decltype(std::declval<deserializer_t>()(std::declval<basic_value<string_t>>(), std::declval<out_t&>()),
-> decltype(std::declval<deserializer_t>()(std::declval<basic_value<string_t>>(), std::declval<U&>()),
std::true_type());

template <typename U>
Expand Down Expand Up @@ -127,7 +127,7 @@ bool deserialize(const basic_value<string_t>& in, out_t& out, const deserializer
return deserializer(in, out);
}
else if constexpr (std::is_constructible_v<out_t, basic_value<string_t>>) {
out = in;
out = out_t(in);
return true;
}
else if constexpr (_utils::is_collection<std::decay_t<out_t>>) {
Expand Down
12 changes: 6 additions & 6 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,19 @@ void third_party_jsonization_2()
{
bool operator()(const json::value& j, ThirdPartyStruct& t) const
{
if (!j.is_number()) {
return false;
}

if (!j.is_number()) return false;
t.a = j.as_integer();
return true;
}
};

ThirdPartyStruct third;
std::map<std::string, ThirdPartyStruct> third;
third["key"] = { 100 };
json::value jthird = json::serialize(third, Serializer {});

ThirdPartyStruct new_third;
std::cout << jthird << std::endl;

std::map<std::string, ThirdPartyStruct> new_third;
bool ret = json::deserialize(jthird, new_third, Deserializer {});
}

Expand Down

0 comments on commit d2ad38a

Please sign in to comment.