DynamicData object to JSON String

3 posts / 0 new
Last post
Offline
Last seen: 6 years 8 months ago
Joined: 05/03/2016
Posts: 3
DynamicData object to JSON String

Hello,

I currently have an application that subscribes to several DDS topics and records messages using rtiddsgen compiled IDL files. After receiving a Sample and converting it to the compiled data type, I use GSON libraries to generate JSON strings from the message data.

I recently came across the "MonitorData.java" example and was wondering if I could somehow make my DDS recorder more generic. Is there any way for me to convert/print a DynamicData message object to a JSON string with fieldname=fieldvalue pairs?

I saw some references to a "DynamicDataFormatter" in the forum search history, but I don't see any mention of this class in the java api.

Thank you in advance,

Kyle

rip
rip's picture
Offline
Last seen: 10 hours 46 min ago
Joined: 04/06/2012
Posts: 324

Hi Kyle,

I've done this in the past -- well, similar.  I have an XML background, so I used the discovery (Publisher) BuiltIn Topics to auto-generate an XSD (XML Schema Document) of the type ("TypeCode") on the wire passed around during discovery.  From that I built a tool that would capture instances/samples as they were published, and generate XML files that validated against the XSD.  using the Discovery topics (+ TypeCode) is available in the "File Exchange" to the left (code snippets > Java for example, "MonitorDiscoveryInformation" is your starting point).

I'm pretty sure you want something simpler, because JSON isn't constrained to follow a schema validation requirement, but you still need the TypeCode to teach your decomposing engine how the data is serialized.

You'll need to learn how to walk the DynamicData tree, after using the discovery topics.  When your code discovers a TypeCode object on the wire, it can use that to create a DynamicData DataReader based on that TypeCode.  When your reader is called with a new DynamicData instance, you simply walk the instance while generating JSON from it.

I don't have access to the code I wrote when I was working at RTI, and in any case I don't have a connext license that would allow me to recreate the infrastructure, sorry. 

Other references to start: 

https://www.rti.com/blog/2014/09/26/leveraging-w3c-org-documents-xml-etc-in-rti-connext-dds/

https://community.rti.com/forum-topic/using-xml-inputoutput-data-rti-dds

In the second link, Gianpiero mentions a recent RTI tool that might be a good place to start, it may already be able to generate JSON directly.

Hope that helps,

rip

gianpiero's picture
Offline
Last seen: 9 months 3 weeks ago
Joined: 06/02/2010
Posts: 177

Hello Kyle,

In Connext DDS 5.3.0 we expose an internal API called DDS_DynamicDataFormatter_to_json. You can find the definition in this file include/ndds/dds_c/dds_c_dynamicdataformatter.h

unsigned int BUFFSIZE = 250; 
char *buffer = new char[BUFFSIZE];
rc = DDS_DynamicDataFormatter_to_json(data, buffer, &BUFFSIZE, 0);
if (rc != DDS_RETCODE_OK) {
    printf("Error converting from Dynamic Data to JSON");
} 
else {
    printf("JSON Data:\n%s\n", buffer);
}

 

where data is a pointer to a valid Dynamic Data.

This function is undocumented and I think is not ported to Java. You could try to call it yourself using jni..

I hope this helps, 
   Gianpiero