json_message_converter.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2019-2022, Martin Günther (DFKI GmbH) and others
6 # Copyright (c) 2013-2016, Brandon Alexander
7 #
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 #
14 # * Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # * Redistributions in binary form must reproduce the above
17 # copyright notice, this list of conditions and the following
18 # disclaimer in the documentation and/or other materials provided
19 # with the distribution.
20 # * Neither the name of this project nor the names of its
21 # contributors may be used to endorse or promote products derived
22 # from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 # POSSIBILITY OF SUCH DAMAGE.
36 
37 import json
38 
39 from rospy_message_converter import message_converter
40 
41 
42 def convert_json_to_ros_message(message_type, json_message, strict_mode=True, log_level='error'):
43  """
44  Takes in the message type and a JSON-formatted string and returns a ROS
45  message.
46 
47  :param message_type: The desired ROS message type of the result
48  :type message_type: str
49  :param json_message: A JSON-formatted string
50  :type json_message: str
51  :param strict_mode: If strict_mode is set, an exception will be thrown if the json message contains extra fields.
52  :type strict_mode: bool, optional
53  :param log_level: The log level to be used. Available levels: debug, info, warning, error, critical
54  :type log_level: str, optional
55  :return: A ROS message
56  :rtype: class:`genpy.Message`
57 
58  Example:
59  >>> msg_type = "std_msgs/String"
60  >>> json_msg = '{"data": "Hello, Robot"}'
61  >>> convert_json_to_ros_message(msg_type, json_msg)
62  data: "Hello, Robot"
63  """
64  dictionary = json.loads(json_message)
65  return message_converter.convert_dictionary_to_ros_message(
66  message_type, dictionary, strict_mode=strict_mode, log_level=log_level
67  )
68 
69 
70 def convert_ros_message_to_json(message, binary_array_as_bytes=True):
71  """
72  Takes in a ROS message and returns a JSON-formatted string.
73 
74  :param message: A ROS message to convert
75  :type message: class:`genpy.Message`
76  :param binary_array_as_bytes: rospy treats `uint8[]` data as a `bytes`, which is the Python representation for byte
77  data. In Python 2, this is the same as `str`. If this parameter is `False`, all `uint8[]` fields will be
78  converted to `list(int)` instead.
79  :type binary_array_as_bytes: bool, optional
80  :return: A JSON-formatted string
81  :rtype: str
82 
83  Example:
84  >>> import std_msgs.msg
85  >>> ros_message = std_msgs.msg.String(data="Hello, Robot")
86  >>> convert_ros_message_to_json(ros_message)
87  '{"data": "Hello, Robot"}'
88  """
89  dictionary = message_converter.convert_ros_message_to_dictionary(message, binary_array_as_bytes)
90  json_message = json.dumps(dictionary)
91  return json_message
92 
93 
94 if __name__ == "__main__":
95  import doctest
96 
97  doctest.testmod()
def convert_json_to_ros_message(message_type, json_message, strict_mode=True, log_level='error')
def convert_ros_message_to_json(message, binary_array_as_bytes=True)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Thu Dec 22 2022 03:33:21