Skip to content

Commit 2885461

Browse files
Documentation to serialize DynamicData to JSON (#844)
* Documentation to serialize DynamicData to JSON Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Change section name and set JSON to not filled DynamicData Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Add Supported Types Serialization section and add more verbosity Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Add json_serialize API, references and change example Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Apply suggested changes Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Fix spelling errors Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Remove unnecessary idl Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Apply suggested changes Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Add bitset section Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Apply minor change Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Add missing blank space in file Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> --------- Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com>
1 parent 4fa7593 commit 2885461

20 files changed

Lines changed: 417 additions & 0 deletions

code/DDSCodeTester.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilder.hpp>
3838
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp>
3939
#include <fastdds/dds/xtypes/type_representation/TypeObject.hpp>
40+
#include <fastdds/dds/xtypes/utils.hpp>
4041
#include <fastdds/rtps/attributes/ThreadSettings.hpp>
4142
#include <fastdds/rtps/common/WriteParams.hpp>
4243
#include <fastdds/rtps/history/IPayloadPool.hpp>
@@ -1156,6 +1157,36 @@ class RemoteDiscoveryDomainParticipantListener : public DomainParticipantListene
11561157
};
11571158
//!--
11581159

1160+
//!--REMOTE_TYPE_INTROSPECTION
1161+
class TypeIntrospectionSubscriber : public DomainParticipantListener
1162+
{
1163+
//!--DYNDATA_JSON_SERIALIZATION
1164+
void on_data_available(
1165+
DataReader* reader)
1166+
{
1167+
// Dynamic DataType
1168+
DynamicData::_ref_type new_data =
1169+
DynamicDataFactory::get_instance()->create_data(dyn_type_);
1170+
1171+
SampleInfo info;
1172+
1173+
while ((RETCODE_OK == reader->take_next_sample(&new_data, &info)))
1174+
{
1175+
std::stringstream output;
1176+
output << std::setw(4);
1177+
1178+
// Serialize DynamicData into JSON string format
1179+
json_serialize(new_data, DynamicDataJsonFormat::EPROSIMA, output);
1180+
std::cout << "Message received:\n" << output.str() << std::endl;
1181+
}
1182+
}
1183+
1184+
// DynamicType created in discovery callback
1185+
DynamicType::_ref_type dyn_type_;
1186+
//!--
1187+
};
1188+
//!--
1189+
11591190
void dds_discovery_examples()
11601191
{
11611192
using Locator_t = eprosima::fastdds::rtps::Locator_t;

code/DynamicTypesIDLExamples.idl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ struct ArrayStruct
122122
};
123123
//!--
124124

125+
//!--IDL_ARRAYS_JSON
126+
struct ArrayStruct
127+
{
128+
long long_array[2][3];
129+
};
130+
//!--
131+
125132
//!--IDL_MAPS
126133
struct MapStruct
127134
{
@@ -190,6 +197,22 @@ struct BitsetStruct
190197
};
191198
//!--
192199

200+
//!--IDL_BITSET_JSON
201+
bitset MyBitSet
202+
{
203+
bitfield<3> a;
204+
bitfield<1> b;
205+
bitfield<4>;
206+
bitfield<10> c;
207+
bitfield<12, short> d;
208+
};
209+
210+
struct BitsetStruct
211+
{
212+
MyBitSet my_bitset;
213+
};
214+
//!--
215+
193216
//!--IDL_CUSTOM_ANNOTATION
194217
@annotation MyAnnotation
195218
{

code/json/Arrays.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"long_array": [
3+
[
4+
0,
5+
0,
6+
0
7+
],
8+
[
9+
0,
10+
0,
11+
0
12+
]
13+
]
14+
}

code/json/Bitmask_EPROSIMA.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"my_bitmask": {
3+
"active": [],
4+
"binary": "00000000",
5+
"value": 0
6+
}
7+
}

code/json/Bitmask_OMG.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"my_bitmask": 0
3+
}

code/json/Bitsets.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"my_bitset": {
3+
"a": 0,
4+
"b": 0,
5+
"c": 0,
6+
"d": 0
7+
}
8+
}

code/json/Enum_EPROSIMA.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"my_enum": {
3+
"name": "A",
4+
"value": 0
5+
}
6+
}

code/json/Enum_OMG.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"my_enum": "A"
3+
}

code/json/Maps.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"short_long_map": null,
3+
"string_alias_unbounded_map": null
4+
}

code/json/Primitives.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"my_bool": false,
3+
"my_octet": 0,
4+
"my_char": "\u0000",
5+
"my_wchar": "\u0000",
6+
"my_long": 0,
7+
"my_ulong": 0,
8+
"my_int8": 0,
9+
"my_uint8": 0,
10+
"my_short": 0,
11+
"my_ushort": 0,
12+
"my_longlong": 0,
13+
"my_ulonglong": 0,
14+
"my_float": 0.0,
15+
"my_double": 0.0,
16+
"my_longdouble": 0.0
17+
}

0 commit comments

Comments
 (0)