Skip to content

Commit 68ff18f

Browse files
JSON to DynamicData deserializer documentation (#1093)
* Refs #22202. JSON to DynamicData deserializer Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com> * Refs #22202. Apply suggestions Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com> --------- Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>
1 parent 098915b commit 68ff18f

3 files changed

Lines changed: 84 additions & 12 deletions

File tree

code/DDSCodeTester.cpp

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,11 @@ class TypeIntrospectionSubscriber : public DomainParticipantListener
12391239

12401240
// Serialize DynamicType into its IDL representation
12411241
std::stringstream idl;
1242-
idl_serialize(remote_type, idl);
1242+
if (RETCODE_OK != idl_serialize(remote_type, idl))
1243+
{
1244+
// Error
1245+
return;
1246+
}
12431247

12441248
// Print IDL representation
12451249
std::cout << "Type discovered:\n" << idl.str() << std::endl;
@@ -1255,8 +1259,8 @@ class TypeIntrospectionSubscriber : public DomainParticipantListener
12551259
// Get remote type information
12561260
xtypes::TypeObject remote_type_object;
12571261
if (RETCODE_OK != DomainParticipantFactory::get_instance()->type_object_registry().get_type_object(
1258-
info.type_information.type_information.complete().typeid_with_size().type_id(),
1259-
remote_type_object))
1262+
info.type_information.type_information.complete().typeid_with_size().type_id(),
1263+
remote_type_object))
12601264
{
12611265
// Error
12621266
return;
@@ -1268,7 +1272,11 @@ class TypeIntrospectionSubscriber : public DomainParticipantListener
12681272

12691273
// Serialize DynamicType into its IDL representation
12701274
std::stringstream idl;
1271-
idl_serialize(remote_type, idl);
1275+
if (RETCODE_OK != idl_serialize(remote_type, idl))
1276+
{
1277+
// Error
1278+
return;
1279+
}
12721280

12731281
// Print IDL representation
12741282
std::cout << "Type discovered:\n" << idl.str() << std::endl;
@@ -1280,25 +1288,59 @@ class TypeIntrospectionSubscriber : public DomainParticipantListener
12801288
void on_data_available(
12811289
DataReader* reader)
12821290
{
1283-
// Dynamic DataType
1291+
// Create data using the DynamicType created from discovered TypeObject or through Dynamic Language Binding API
12841292
DynamicData::_ref_type new_data =
1285-
DynamicDataFactory::get_instance()->create_data(dyn_type_);
1293+
DynamicDataFactory::get_instance()->create_data(dyn_type_serialization_);
12861294

12871295
SampleInfo info;
1288-
12891296
while ((RETCODE_OK == reader->take_next_sample(&new_data, &info)))
12901297
{
12911298
std::stringstream output;
12921299
output << std::setw(4);
12931300

12941301
// Serialize DynamicData into JSON string format
1295-
json_serialize(new_data, DynamicDataJsonFormat::EPROSIMA, output);
1302+
if (RETCODE_OK != json_serialize(
1303+
new_data,
1304+
DynamicDataJsonFormat::EPROSIMA,
1305+
output))
1306+
{
1307+
// Error
1308+
return;
1309+
}
1310+
1311+
// Print JSON representation
12961312
std::cout << "Message received:\n" << output.str() << std::endl;
12971313
}
12981314
}
12991315

1300-
// DynamicType created in discovery callback
1301-
DynamicType::_ref_type dyn_type_;
1316+
// DynamicType corresponding to received data
1317+
DynamicType::_ref_type dyn_type_serialization_;
1318+
//!--
1319+
1320+
//!--
1321+
1322+
//!--JSON_DYNDATA_DESERIALIZATION
1323+
void json_to_data(
1324+
const std::string& json_data)
1325+
{
1326+
// Deserialize JSON string into DynamicData
1327+
// The required DynamicType can be created from discovered TypeObject or through Dynamic Language Binding API
1328+
DynamicData::_ref_type new_data;
1329+
if (RETCODE_OK != json_deserialize(
1330+
json_data,
1331+
dyn_type_deserialization_,
1332+
DynamicDataJsonFormat::EPROSIMA,
1333+
new_data))
1334+
{
1335+
// Error
1336+
return;
1337+
}
1338+
1339+
// Process the new data
1340+
}
1341+
1342+
// DynamicType corresponding to JSON data to be deserialized into DynamicData
1343+
DynamicType::_ref_type dyn_type_deserialization_;
13021344
//!--
13031345
};
13041346
//!--

docs/03-exports/aliases-api.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@
10931093
.. |TypeObjectUtils-api| replace:: :cpp:class:`TypeObjectUtils <eprosima::fastdds::dds::xtypes::TypeObjectUtils>`
10941094
.. |XTypesUtils-idl_serialize-api| replace:: :cpp:func:`idl_serialize <eprosima::fastdds::dds::idl_serialize>`
10951095
.. |XTypesUtils-json_serialize-api| replace:: :cpp:func:`json_serialize <eprosima::fastdds::dds::json_serialize>`
1096+
.. |XTypesUtils-json_deserialize-api| replace:: :cpp:func:`json_deserialize <eprosima::fastdds::dds::json_deserialize>`
10961097
.. }}}
10971098

10981099
.. |DomainParticipant::set_listener-api| replace:: :cpp:func:`DomainParticipant::set_listener()<eprosima::fastdds::dds::DomainParticipant::set_listener>`

docs/fastdds/xtypes/type_serializing.rst

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ remote type discovery.
5353
:end-before: //!--
5454
:dedent: 4
5555

56+
.. _xtypes_serialization_utilities_dyndata_json:
57+
5658
DynamicData to JSON
57-
--------------------
59+
-------------------
5860

5961
In the context of DDS (Data Distribution Service), |DynamicType-api| represents the structure of the data being
6062
distributed across the system.
@@ -64,6 +66,8 @@ To enhance interoperability and readability, it is often useful to serialize |Dy
6466
manageable format, to enable easier data processing and analysis across different systems and applications.
6567
The method |XTypesUtils-json_serialize-api| converts a |DynamicData-api| object into a JSON object, then
6668
dumped into a ``std::ostream``.
69+
The inverse conversion is also possible using the method |XTypesUtils-json_deserialize-api|, as described in the
70+
:ref:`corresponding section<xtypes_serialization_utilities_json_dyndata>`.
6771

6872
Supported Types
6973
^^^^^^^^^^^^^^^
@@ -288,7 +292,7 @@ would be serialized as follows:
288292
.. literalinclude:: /../code/json/Bitsets.json
289293
:language: json
290294

291-
.. _xtypes_serialization_utilities_json_example:
295+
.. _xtypes_serialization_utilities_dyndata_json_example:
292296

293297
Example: Convert received data into JSON format
294298
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -305,3 +309,28 @@ remote type discovery.
305309
:dedent: 4
306310
:start-after: //!--DYNDATA_JSON_SERIALIZATION
307311
:end-before: //!--
312+
313+
.. _xtypes_serialization_utilities_json_dyndata:
314+
315+
JSON to DynamicData
316+
-------------------
317+
318+
Apart from having the possibility to serialize :ref:`DynamicData to JSON<xtypes_serialization_utilities_dyndata_json>`,
319+
Fast DDS also provides a way to perform the inverse conversion.
320+
The method |XTypesUtils-json_deserialize-api| is able to convert a JSON object into a |DynamicData-api| instance,
321+
by only providing its associated |DynamicType-api|.
322+
This method is useful for injecting data from external sources into a DDS network, allowing for the integration of data
323+
from various systems and applications.
324+
325+
.. _xtypes_serialization_utilities_json_dyndata_example:
326+
327+
Example: Convert JSON data into DynamicData
328+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
329+
The following code demonstrates how to use the |XTypesUtils-json_deserialize-api| function in Fast DDS to convert
330+
JSON data into a |DynamicData-api| object.
331+
332+
.. literalinclude:: /../code/DDSCodeTester.cpp
333+
:language: c++
334+
:dedent: 4
335+
:start-after: //!--JSON_DYNDATA_DESERIALIZATION
336+
:end-before: //!--

0 commit comments

Comments
 (0)