DynamicData to JSON/XML w/ Modern C++ API

5 posts / 0 new
Last post
Offline
Last seen: 2 years 1 week ago
Joined: 01/28/2016
Posts: 9
DynamicData to JSON/XML w/ Modern C++ API

Hi,

I'd like to serialize a DynamicData sample to JSON/XML using the Modern C++ APIs.

I realize there are internal APIs:  DDS_DynamicDataFormatter_to_xml and DDS_DynamicDataFormatter_to_json.  But, these APIs seem to take DDS_DynamicData types as arguments.  Is there an equivalent API for the Modern C++ DynamicData type ?  Thanks!

Fernando Garcia's picture
Offline
Last seen: 4 months 4 weeks ago
Joined: 05/18/2011
Posts: 199

Hi Kang,

RTI Connext DDS 5.3.0 introduced a public APIs to transform DDS samples into XML or JSON. You can use these APIs with both DynamicData and IDL-generated types.

In the case of the Modern C++ API you will have to use rti::topic::to_string, which is documented here. Here is an example on how to transform a sample into JSON:

#include <rti/topic/to_string.hpp>

// ...
std::string str = rti::topic::to_string(sample, rti::topic::PrintFormatProperty::Json());
// ...

Let me know if this works for you.

Thanks,
Fernando.

Offline
Last seen: 2 years 1 week ago
Joined: 01/28/2016
Posts: 9

This is excellant.  I didn't realize this was added to 5.3.0.   Thanks for your help Fernando!

Offline
Last seen: 4 years 2 months ago
Joined: 08/03/2017
Posts: 13

This would be great to have for someone using 5.2.3... Any way around it for us stuck on older versions? (Using Java on my end).

Offline
Last seen: 5 years 3 weeks ago
Joined: 06/01/2017
Posts: 4

Is there any similar API in Java  to print received DDS sample  in Json or XML format.  ? 

C++ std::string str = rti::topic::to_string(sample, rti::topic::PrintFormatProperty::Json());