json_transport.py
Go to the documentation of this file.
1 # Software License Agreement (BSD)
2 #
3 # \file json_transport.py
4 # \authors Paul Bovbel <pbovbel@locusrobotics.com>
5 # \copyright Copyright (c) (2018,), Locus Robotics, All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification, are permitted
8 # provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice, this list of conditions
11 # and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13 # conditions and the following disclaimer in the documentation and/or other materials provided with
14 # the distribution.
15 # 3. Neither the name of the copyright holder nor the names of its contributors may be used to
16 # endorse or promote products derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 import msgpack
27 
28 from json_msgs import msg as json_msg
29 
30 
31 def pack(data):
32  return json_msg.Json(bytes=msgpack.packb(data))
33 
34 
35 def unpack(message):
36  return msgpack.unpackb(message.bytes, encoding="utf-8")
37 
38 
39 class PackedJson(json_msg.Json):
40 
41  def __init__(self, data=None):
42  self.data = data
43 
44  def set_data(self, data):
45  self.bytes = msgpack.packb(data)
46 
47  def get_data(self):
48  return msgpack.unpackb(self.bytes, encoding="utf-8")
49 
50  data = property(get_data, set_data)
51 
52  def __str__(self):
53  return str(self.data)
54 
55  def __repr__(self):
56  return("{name}({data})".format(name=self.__class__.__name__, data=repr(self.data)))


json_transport
Author(s): Paul Bovbel
autogenerated on Thu Jun 13 2019 19:12:24