# How to deserialize a ros message without knowing its type or content

Hey there,

I'm trying to serialize any ros message received using ros::serialization and once this is done I need a way to deserialize it again but without knowing the message type. I'll try to explain how I intend to do this and I'd like to know if there's a better way to accomplish it.

I need to do this just to fill an xml with all the data contained in the message received like so:

<message_a>
<x type="float64" value="25.2" />
<y type="float64" value="23.2" />
etc..
</message_a>


I'm just bothering with all the serialization and deserialization because I don't seem to find another way to accomplish that without knowing the message type (I need to make a generic function that fills an xml no matter what the message looks like).

First of all, I serialize the message this way:

const std::string& publisher_name = event.getPublisherName() ;
const cola2_common::BhResponse::ConstPtr& msg = event.getMessage() ; //BhResponse is a message I have defined.

ROS_INFO( "BhResponse received from: [%s]", publisher_name.c_str() ) ;

uint32_t serial_size = ros::serialization::serializationLength( *msg ) ;
boost::shared_array< uint8_t > buffer( new uint8_t[ serial_size ] ) ;

ros::serialization::OStream stream( buffer.get(), serial_size ) ;
ros::serialization::serialize( stream, *msg ) ;


Once I have the serialized stream I'd like to deserialize it again but without knowing the message type (so I can't use ros::serialization::deserialize). I have thought to do that asking for the definition of the message (so I get the type and name of all the contained data) and then keep rebuilding manually.

If I ask for the definition of the previous message I get this:

ROS_INFO( "BhResponse message Definition: %s", msg->__getMessageDefinition().c_str() ) ;

/**BhResponse message Definition:
float64[6] setPoints
float64[6] activationLevels
int32      priority */


I find the first problem here: __getMessageDefinition() is deprecated; how do I get this information without calling this method?

And finally, once I have the serialized message and its definition, I was going to keep building the xml step by step; first I'd add the name of the variable, then I'd add the type and after that, manually deserialize the stream to get its value, etc.

I'd appreciate any help I can get! Thanks!

edit retag close merge delete

Sort by » oldest newest most voted
more