Skip to content

Commit

Permalink
Merge pull request #80 from DenisBiryukov91/cpp-faillable-serialization
Browse files Browse the repository at this point in the history
docs: zenoh-cpp: update custom struct serialization example
  • Loading branch information
DenisBiryukov91 authored Oct 17, 2024
2 parents 83edb76 + 00127a9 commit 03993fa
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions content/docs/migration_1.0/C++.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,24 @@ void receive_bytes(const Sample &sample) {
Additionally `zenoh::ext` namespace provides support for serialization/deserialziation of typed data to/into `Bytes`:
```cpp
// arithmetic types
double pi = 3.1415926;
Bytes b = ext::serialize(pi);
assert(ext::deserialize<doulbe>(b) == pi);
// Composite types
std::vector<float> v = {0.1f, 0.2f, 0.3f};
b = ext::serialize(v);
assert(ext::deserialize<std::vector<float>>(b) == v);
std::unordered_map<std::string, std::deque<double>> m = {
{"a", {0.5, 0.2}},
{"b", {-123.45, 0.4}},
{"abc", {3.1415926, -1.0} }
};
b = ext::serialize(m);
assert(ext::deserialize<std::unordered_map<std::string, std::deque<double>>>(b) == m);
// arithmetic types
double pi = 3.1415926;
Bytes b = ext::serialize(pi);
assert(ext::deserialize<doulbe>(b) == pi);
// Composite types
std::vector<float> v = {0.1f, 0.2f, 0.3f};
b = ext::serialize(v);
assert(ext::deserialize<std::vector<float>>(b) == v);
std::unordered_map<std::string, std::deque<double>> m = {
{"a", {0.5, 0.2}},
{"b", {-123.45, 0.4}},
{"abc", {3.1415926, -1.0} }
};
b = ext::serialize(m);
assert(ext::deserialize<std::unordered_map<std::string, std::deque<double>>>(b) == m);
```

Users can easily define serialization/deserialization for their own custom types by using `ext::Serializer` and `ext::Deserializer` classes:
Expand All @@ -136,11 +136,20 @@ struct CustomStruct {
std::string s;
};

// One needs to implement __zenoh_serialize_with_serializer in the same namespace as CustomStruct
void __zenoh_serialize_with_serializer(ext::Serializer& serializer, const CustomStruct& s) {
serializer.serialize(s.vd);
serializer.serialize(s.i);
serializer.serialize(s.s);
// One needs to implement __zenoh_serialize_with_serializer and __zenoh_deserialize_with_deserializer
// in the same namespace, where CustomStruct is defined.
// To simplify implementation users are allowed to use
// serialize_with_serializer and deserialize_with_deserializer functions defined in zenoh::ext::detail namespace.
bool __zenoh_serialize_with_serializer(zenoh::ext::Serializer& serializer, const CustomStruct& s, ZResult* err) {
return zenoh::ext::detail::serialize_with_serializer(serializer, s.vd, err) &&
zenoh::ext::detail::serialize_with_serializer(serializer, s.i, err) &&
zenoh::ext::detail::serialize_with_serializer(serializer, s.s, err);
}

bool __zenoh_deserialize_with_deserializer(zenoh::ext::Deserializer& deserializer, CustomStruct& s, ZResult* err) {
return zenoh::ext::detail::deserialize_with_deserializer(deserializer, s.vd, err) &&
zenoh::ext::detail::deserialize_with_deserializer(deserializer, s.i, err) &&
zenoh::ext::detail::deserialize_with_deserializer(deserializer, s.s, err);
}

void serialize_custom() {
Expand Down

1 comment on commit 03993fa

@Loic-kd
Copy link

@Loic-kd Loic-kd commented on 03993fa Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is <doulbe> a typo on line 113 ?

Please sign in to comment.