json_message_converter.py
Go to the documentation of this file.
1 import json
2 
3 from rospy_message_converter import message_converter
4 
5 
6 def convert_json_to_ros_message(message_type, json_message, strict_mode=True):
7  """
8  Takes in the message type and a JSON-formatted string and returns a ROS
9  message.
10 
11  :param message_type: The desired ROS message type of the result
12  :type message_type: str
13  :param json_message: A JSON-formatted string
14  :type json_message: str
15  :param strict_mode: If strict_mode is set, an exception will be thrown if the json message contains extra fields.
16  :type strict_mode: bool, optional
17  :return: A ROS message
18  :rtype: class:`genpy.Message`
19 
20  Example:
21  >>> msg_type = "std_msgs/String"
22  >>> json_msg = '{"data": "Hello, Robot"}'
23  >>> convert_json_to_ros_message(msg_type, json_msg)
24  data: "Hello, Robot"
25  """
26  dictionary = json.loads(json_message)
27  return message_converter.convert_dictionary_to_ros_message(message_type, dictionary, strict_mode=strict_mode)
28 
29 
30 def convert_ros_message_to_json(message, binary_array_as_bytes=True):
31  """
32  Takes in a ROS message and returns a JSON-formatted string.
33 
34  :param message: A ROS message to convert
35  :type message: class:`genpy.Message`
36  :param binary_array_as_bytes: rospy treats `uint8[]` data as a `bytes`, which is the Python representation for byte
37  data. In Python 2, this is the same as `str`. If this parameter is `False`, all `uint8[]` fields will be
38  converted to `list(int)` instead.
39  :type binary_array_as_bytes: bool, optional
40  :return: A JSON-formatted string
41  :rtype: str
42 
43  Example:
44  >>> import std_msgs.msg
45  >>> ros_message = std_msgs.msg.String(data="Hello, Robot")
46  >>> convert_ros_message_to_json(ros_message)
47  '{"data": "Hello, Robot"}'
48  """
49  dictionary = message_converter.convert_ros_message_to_dictionary(message, binary_array_as_bytes)
50  json_message = json.dumps(dictionary)
51  return json_message
52 
53 
54 if __name__ == "__main__":
55  import doctest
56 
57  doctest.testmod()
def convert_ros_message_to_json(message, binary_array_as_bytes=True)
def convert_json_to_ros_message(message_type, json_message, strict_mode=True)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Tue Mar 2 2021 03:04:27