Serialize/deserialize IDL data structures with JSON

2 posts / 0 new
Last post
Last seen: 4 years 4 months ago
Joined: 01/09/2013
Posts: 10
Serialize/deserialize IDL data structures with JSON


I would like to serialize and deserialize data exchanged through DDS with JSON. I found in a document from Gerardo Pardo that let me hope it is easily feasible (

After generating the Java class from the IDL description, I succeeds in serializing it into JSON format. The deserialization seems more difficult, perhaps due to the use of sequences and embedded substructures.

Do you have examples of Java code to do such a deserialization?

Thanks in advance.

JavierPovedano's picture
Last seen: 8 months 1 week ago
Joined: 06/05/2013
Posts: 16


The types generated by rtiddsgen are standard java classes, so it can be easily serialized/serialized using any third party library that does that. One good example is Google Gson library. This library provides JSON serializers/deserializers to Java objects that can be easily incorporated in your code.

Imagine you have this idl definition:

struct Inner {
        long id;

struct MyHello{
        string<256> mystring;
        long mylong;
        sequence<double,10> myseq;
        Inner myinner;

Here it is a snippet of how it can be serialized/deserialize using Google Gson:

instance.mystring = "String " + count;
instance.mylong = count;

for(int i=0;i< 10;++i){
} = count;

System.out.println("Original " + instance.toString());
/* Modify the instance to be written here */
System.out.println("Serialized : " + gson.toJson(instance));

String serialized = gson.toJson(instance);

MyHello deserialized = gson.fromJson(serialized,MyHello.class);

System.out.println("\nDeserialized " + deserialized.toString());   

The output for this program will be: 

 Original :
    mystring: String 1
    mylong: 1
    myseq: 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
    myinner :
        id: 1

Serialized : {"mystring":"String 1","mylong":1,"myseq":[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0],"myinner":{"id":1}}

Deserialized :
    mystring: String 1
    mylong: 1
    myseq: 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
    myinner :
        id: 1


Note that might be necessary to build your custom serializer/deserializers for certain data types or complex structures.

Let me know if you have further questions,

- Javier