From c967e2fbfa265a50b2932da7414d3103c6cce7b1 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 17 Oct 2024 10:35:10 +0200 Subject: [PATCH 1/2] docs: zenoh-cpp: update custom struct serialization example --- content/docs/migration_1.0/C++.md | 57 ++++++++++++++++++------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/content/docs/migration_1.0/C++.md b/content/docs/migration_1.0/C++.md index 8e3f678b..9915f87d 100644 --- a/content/docs/migration_1.0/C++.md +++ b/content/docs/migration_1.0/C++.md @@ -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(b) == pi); - - // Composite types - std::vector v = {0.1f, 0.2f, 0.3f}; - b = ext::serialize(v); - assert(ext::deserialize>(b) == v); - - std::unordered_map> m = { - {"a", {0.5, 0.2}}, - {"b", {-123.45, 0.4}}, - {"abc", {3.1415926, -1.0} } - }; - - b = ext::serialize(m); - assert(ext::deserialize>>(b) == m); +// arithmetic types +double pi = 3.1415926; +Bytes b = ext::serialize(pi); +assert(ext::deserialize(b) == pi); + +// Composite types +std::vector v = {0.1f, 0.2f, 0.3f}; +b = ext::serialize(v); +assert(ext::deserialize>(b) == v); + +std::unordered_map> m = { + {"a", {0.5, 0.2}}, + {"b", {-123.45, 0.4}}, + {"abc", {3.1415926, -1.0} } +}; + +b = ext::serialize(m); +assert(ext::deserialize>>(b) == m); ``` Users can easily define serialization/deserialization for their own custom types by using `ext::Serializer` and `ext::Deserializer` classes: @@ -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() { @@ -217,7 +226,7 @@ while (true) { if (std::has_alternative(res)) { const auto& sample = std::get(res).get_ok(); std::cout << "Received ('" << sample.get_keyexpr().as_string_view() << "' : '" - << sample.get_payload().as_string() << "')\n"; + << sample.get_payload().as_string() << "')\n"; } else if (std::get(res) == channels::RecvError::Z_NODATA) { // try_recv is non-blocking call, so may fail to return a reply if the Fifo buffer is empty std::cout << "."; From 00127a91b2730d7fa86b7e0a19fbd2a9ead055e0 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 17 Oct 2024 11:09:51 +0200 Subject: [PATCH 2/2] fixes --- content/docs/migration_1.0/C++.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/docs/migration_1.0/C++.md b/content/docs/migration_1.0/C++.md index 9915f87d..3ecf75ee 100644 --- a/content/docs/migration_1.0/C++.md +++ b/content/docs/migration_1.0/C++.md @@ -137,7 +137,7 @@ struct CustomStruct { }; // One needs to implement __zenoh_serialize_with_serializer and __zenoh_deserialize_with_deserializer -// in the same namespace where CustomStruct is defined. +// 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) { @@ -226,7 +226,7 @@ while (true) { if (std::has_alternative(res)) { const auto& sample = std::get(res).get_ok(); std::cout << "Received ('" << sample.get_keyexpr().as_string_view() << "' : '" - << sample.get_payload().as_string() << "')\n"; + << sample.get_payload().as_string() << "')\n"; } else if (std::get(res) == channels::RecvError::Z_NODATA) { // try_recv is non-blocking call, so may fail to return a reply if the Fifo buffer is empty std::cout << ".";