rosjson.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2009, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 # Revision $Id: rosweb.py 4149 2009-04-13 17:00:09Z sfkwc $
35 
36 import roslib; roslib.load_manifest('rosjson')
37 
38 import cStringIO
39 import os
40 
41 import rospy
42 import genpy
43 
44 class ROSJSONException(rospy.ROSException): pass
45 
46 _JSON_ESCAPE = {'\\':r'\\', '"':'\\"', '\b':r'\b', '\f':r'\f', '\n':r'\n', '\r':r'\r', '\t':r'\t'}
47 
48 ## Convert value to JSON representation
49 ## @param v Any: value to convert to JSON. Supported types are Python primitives (str, int, float, long, bool) as well as rospy Message-related types (Message, Time, Duration).
50 ## @return str: JSON string representation of \a v
51 ## @throws ROSJSONException if \a msg cannot be converted to JSON
53  if type(v) == str:
54  buff = cStringIO.StringIO()
55 
56  buff.write('"')
57  for c in v:
58  if c in _JSON_ESCAPE:
59  buff.write(_JSON_ESCAPE[c])
60  else:
61  buff.write(c)
62  buff.write('"')
63 
64  return buff.getvalue()
65  elif type(v) in (int, float, long):
66  return "%s"%v
67  elif type(v) in (list, tuple):
68  return '['+','.join([value_to_json(x) for x in v]) + ']'
69  elif type(v) == bool:
70  if v:
71  return 'true'
72  else:
73  return 'false'
74  elif isinstance(v, rospy.Message):
75  return ros_message_to_json(v)
76  elif isinstance(v, (genpy.rostime.Time, genpy.rostime.Duration)):
77  return v.to_sec()
78  else:
79  raise ROSJSONException("unknown type: %s"%type(v))
80 
81 ## Convert ROS message to JSON representation. JSON representation is
82 ## a simple dictionary of dictionaries, where a dictionary represents
83 ## a ROS message. Time and duration are represented by their float value
84 ## in seconds.
85 
89  if not isinstance(msg, rospy.Message):
90  raise ROSJSONException("not a valid rospy Message instance: %s"%msg.__class__.__name__)
91  buff = cStringIO.StringIO()
92  buff.write('{')
93  buff.write(','.join(['"%s": %s'%(f, value_to_json(getattr(msg, f))) for f in msg.__slots__]))
94  buff.write('}')
95  return buff.getvalue()
def value_to_json(v)
Convert value to JSON representation.
Definition: rosjson.py:52
def ros_message_to_json(msg)
Convert ROS message to JSON representation.
Definition: rosjson.py:88


rosjson
Author(s): Ken Conley
autogenerated on Mon Jun 10 2019 15:51:17