Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions code/DDSCodeTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,11 @@ class TypeIntrospectionSubscriber : public DomainParticipantListener

// Serialize DynamicType into its IDL representation
std::stringstream idl;
idl_serialize(remote_type, idl);
if (RETCODE_OK != idl_serialize(remote_type, idl))
{
// Error
return;
}

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

// Serialize DynamicType into its IDL representation
std::stringstream idl;
idl_serialize(remote_type, idl);
if (RETCODE_OK != idl_serialize(remote_type, idl))
{
// Error
return;
}

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

SampleInfo info;

while ((RETCODE_OK == reader->take_next_sample(&new_data, &info)))
{
std::stringstream output;
output << std::setw(4);

// Serialize DynamicData into JSON string format
json_serialize(new_data, DynamicDataJsonFormat::EPROSIMA, output);
if (RETCODE_OK != json_serialize(
new_data,
DynamicDataJsonFormat::EPROSIMA,
output))
{
// Error
return;
}

// Print JSON representation
std::cout << "Message received:\n" << output.str() << std::endl;
}
}

// DynamicType created in discovery callback
DynamicType::_ref_type dyn_type_;
// DynamicType corresponding to received data
DynamicType::_ref_type dyn_type_serialization_;
//!--

//!--

//!--JSON_DYNDATA_DESERIALIZATION
void json_to_data(
const std::string& json_data)
{
// Deserialize JSON string into DynamicData
// The required DynamicType can be created from discovered TypeObject or through Dynamic Language Binding API
DynamicData::_ref_type new_data;
if (RETCODE_OK != json_deserialize(
json_data,
dyn_type_deserialization_,
DynamicDataJsonFormat::EPROSIMA,
new_data))
{
// Error
return;
}

// Process the new data
}

// DynamicType corresponding to JSON data to be deserialized into DynamicData
DynamicType::_ref_type dyn_type_deserialization_;
//!--
};
//!--
Expand Down
1 change: 1 addition & 0 deletions docs/03-exports/aliases-api.include
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@
.. |TypeObjectUtils-api| replace:: :cpp:class:`TypeObjectUtils <eprosima::fastdds::dds::xtypes::TypeObjectUtils>`
.. |XTypesUtils-idl_serialize-api| replace:: :cpp:func:`idl_serialize <eprosima::fastdds::dds::idl_serialize>`
.. |XTypesUtils-json_serialize-api| replace:: :cpp:func:`json_serialize <eprosima::fastdds::dds::json_serialize>`
.. |XTypesUtils-json_deserialize-api| replace:: :cpp:func:`json_deserialize <eprosima::fastdds::dds::json_deserialize>`
.. }}}

.. |DomainParticipant::set_listener-api| replace:: :cpp:func:`DomainParticipant::set_listener()<eprosima::fastdds::dds::DomainParticipant::set_listener>`
Expand Down
33 changes: 31 additions & 2 deletions docs/fastdds/xtypes/type_serializing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ remote type discovery.
:end-before: //!--
:dedent: 4

.. _xtypes_serialization_utilities_dyndata_json:

DynamicData to JSON
--------------------
-------------------

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

Supported Types
^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -288,7 +292,7 @@ would be serialized as follows:
.. literalinclude:: /../code/json/Bitsets.json
:language: json

.. _xtypes_serialization_utilities_json_example:
.. _xtypes_serialization_utilities_dyndata_json_example:

Example: Convert received data into JSON format
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -305,3 +309,28 @@ remote type discovery.
:dedent: 4
:start-after: //!--DYNDATA_JSON_SERIALIZATION
:end-before: //!--

.. _xtypes_serialization_utilities_json_dyndata:

JSON to DynamicData
-------------------

Apart from having the possibility to serialize :ref:`DynamicData to JSON<xtypes_serialization_utilities_dyndata_json>`,
Fast DDS also provides a way to perform the inverse conversion.
The method |XTypesUtils-json_deserialize-api| is able to convert a JSON object into a |DynamicData-api| instance,
by only providing its associated |DynamicType-api|.
This method is useful for injecting data from external sources into a DDS network, allowing for the integration of data
from various systems and applications.

.. _xtypes_serialization_utilities_json_dyndata_example:

Example: Convert JSON data into DynamicData
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The following code demonstrates how to use the |XTypesUtils-json_deserialize-api| function in Fast DDS to convert
JSON data into a |DynamicData-api| object.

.. literalinclude:: /../code/DDSCodeTester.cpp
:language: c++
:dedent: 4
:start-after: //!--JSON_DYNDATA_DESERIALIZATION
:end-before: //!--