Class JsonSerializer

Class Documentation

class JsonSerializer

JSON serializer for ROS 2 messages using dynmsg.

This class provides the main API for converting between JSON and ROS 2 messages at runtime. It uses dynmsg’s YAML capabilities internally and converts between JSON and YAML formats.

Note

This class is stateless and thread-safe.

Public Functions

JsonSerializer() = default
~JsonSerializer() = default
JsonSerializer(const JsonSerializer&) = delete
JsonSerializer &operator=(const JsonSerializer&) = delete
JsonSerializer(JsonSerializer&&) = default
JsonSerializer &operator=(JsonSerializer&&) = default
nlohmann::json to_json(const TypeInfo_Cpp *type_info, const void *message_data) const

Convert a ROS 2 message to JSON

Parameters:
  • type_info – Type introspection info

  • message_data – Pointer to the message data

Throws:

JsonConversionError – if conversion fails

Returns:

JSON representation of the message

nlohmann::json to_json(const std::string &type_string, const void *message_data) const

Convert a ROS 2 message to JSON using type string

Parameters:
  • type_string – Full type string (e.g., “std_msgs/msg/String”)

  • message_data – Pointer to the message data

Throws:
Returns:

JSON representation of the message

RosMessage_Cpp from_json(const TypeInfo_Cpp *type_info, const nlohmann::json &json) const

Convert JSON to a ROS 2 message

Note

Caller is responsible for calling ros_message_destroy_with_allocator

Parameters:
  • type_info – Type introspection info

  • json – JSON data to convert

Throws:

JsonConversionError – if conversion fails

Returns:

RosMessage_Cpp containing allocated message data

RosMessage_Cpp from_json(const std::string &type_string, const nlohmann::json &json) const

Convert JSON to a ROS 2 message using type string

Note

Caller is responsible for calling ros_message_destroy_with_allocator

Parameters:
  • type_string – Full type string (e.g., “std_msgs/msg/String”)

  • json – JSON data to convert

Throws:
Returns:

RosMessage_Cpp containing allocated message data

void from_json_to_message(const TypeInfo_Cpp *type_info, const nlohmann::json &json, void *message_data) const

Populate an existing message from JSON

Parameters:
  • type_info – Type introspection info

  • json – JSON data to convert

  • message_data – Pointer to pre-allocated message to populate

Throws:

JsonConversionError – if conversion fails

nlohmann::json get_schema(const TypeInfo_Cpp *type_info) const

Generate a JSON schema for a ROS 2 type

Parameters:

type_info – Type introspection info

Returns:

JSON schema object

nlohmann::json get_schema(const std::string &type_string) const

Generate a JSON schema using type string

Parameters:

type_string – Full type string

Throws:

TypeNotFoundError – if type cannot be loaded

Returns:

JSON schema object

nlohmann::json get_defaults(const TypeInfo_Cpp *type_info) const

Get default values for a ROS 2 type as JSON

Parameters:

type_info – Type introspection info

Returns:

JSON object with default values

nlohmann::json get_defaults(const std::string &type_string) const

Get default values using type string

Parameters:

type_string – Full type string

Throws:

TypeNotFoundError – if type cannot be loaded

Returns:

JSON object with default values

rclcpp::SerializedMessage serialize(const std::string &type_string, const nlohmann::json &json) const

Serialize JSON to CDR format for use with GenericClient

Parameters:
  • type_string – Full type string (e.g., “std_srvs/srv/SetBool_Request”)

  • json – JSON data to serialize

Throws:
Returns:

SerializedMessage containing CDR data

nlohmann::json deserialize(const std::string &type_string, const rclcpp::SerializedMessage &serialized_msg) const

Deserialize CDR data to JSON

Parameters:
  • type_string – Full type string

  • serialized_msg – SerializedMessage containing CDR data

Throws:
Returns:

JSON representation of the message

Public Static Functions

static nlohmann::json yaml_to_json(const YAML::Node &yaml)

Convert YAML node to JSON

Parameters:

yaml – YAML node to convert

Returns:

JSON representation

static YAML::Node json_to_yaml(const nlohmann::json &json)

Convert JSON to YAML node

Parameters:

json – JSON to convert

Returns:

YAML node representation