@@ -85,6 +85,25 @@ fn demonstrate_printing() {
85
85
) ;
86
86
}
87
87
88
+ fn demonstrate_serde ( ) -> Result < ( ) , Error > {
89
+ // When the serde feature is turned on, messages are able to be serialized
90
+ // to and deserialized from a variety of formats. Here JSON is used as an
91
+ // example.
92
+ // Works with RMW-native and idiomatic messages.
93
+ let idiomatic_msg = rclrs_example_msgs:: msg:: VariousTypes :: default ( ) ;
94
+ let rmw_msg = rclrs_example_msgs:: msg:: rmw:: VariousTypes :: default ( ) ;
95
+ println ! ( "================= JSON serialization with Serde ==================" ) ;
96
+ let idiomatic_serialized = serde_json:: to_string_pretty ( & idiomatic_msg) ?;
97
+ let rmw_serialized = serde_json:: to_string_pretty ( & rmw_msg) ?;
98
+ assert_eq ! ( idiomatic_serialized, rmw_serialized) ;
99
+ println ! ( "{}" , rmw_serialized) ;
100
+ let idiomatic_deserialized = serde_json:: from_str ( & idiomatic_serialized) ?;
101
+ let rmw_deserialized = serde_json:: from_str ( & rmw_serialized) ?;
102
+ assert_eq ! ( idiomatic_msg, idiomatic_deserialized) ;
103
+ assert_eq ! ( rmw_msg, rmw_deserialized) ;
104
+ Ok ( ( ) )
105
+ }
106
+
88
107
fn demonstrate_sequences ( ) {
89
108
// Convenient creation of (bounded) sequences with the seq! macro
90
109
// This one has three items and a length bound of 5
@@ -151,6 +170,7 @@ fn demonstrate_pubsub() -> Result<(), Error> {
151
170
fn main ( ) -> Result < ( ) , Error > {
152
171
check_default_values ( ) ;
153
172
demonstrate_printing ( ) ;
173
+ demonstrate_serde ( ) ?;
154
174
demonstrate_sequences ( ) ;
155
175
demonstrate_pubsub ( ) ?;
156
176
Ok ( ( ) )
0 commit comments