diff --git a/code/DDSCodeTester.cpp b/code/DDSCodeTester.cpp index 121c33376..9ab74693f 100644 --- a/code/DDSCodeTester.cpp +++ b/code/DDSCodeTester.cpp @@ -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; @@ -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; @@ -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; @@ -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_; //!-- }; //!-- diff --git a/docs/03-exports/aliases-api.include b/docs/03-exports/aliases-api.include index db7e316d5..8623478de 100644 --- a/docs/03-exports/aliases-api.include +++ b/docs/03-exports/aliases-api.include @@ -1093,6 +1093,7 @@ .. |TypeObjectUtils-api| replace:: :cpp:class:`TypeObjectUtils ` .. |XTypesUtils-idl_serialize-api| replace:: :cpp:func:`idl_serialize ` .. |XTypesUtils-json_serialize-api| replace:: :cpp:func:`json_serialize ` +.. |XTypesUtils-json_deserialize-api| replace:: :cpp:func:`json_deserialize ` .. }}} .. |DomainParticipant::set_listener-api| replace:: :cpp:func:`DomainParticipant::set_listener()` diff --git a/docs/fastdds/xtypes/type_serializing.rst b/docs/fastdds/xtypes/type_serializing.rst index 572b2316c..29a86f523 100644 --- a/docs/fastdds/xtypes/type_serializing.rst +++ b/docs/fastdds/xtypes/type_serializing.rst @@ -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. @@ -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`. Supported Types ^^^^^^^^^^^^^^^ @@ -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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -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`, +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: //!--