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 json
27 
28 from json_msgs import msg as json_msg
29 from jsonschema import validate, ValidationError
30 
31 
32 def pack(data):
33  return json_msg.Json(json=json.dumps(data))
34 
35 
36 def unpack(message):
37  return json.loads(message.json)
38 
39 
40 class PackedJson(json_msg.Json):
41 
42  def __init__(self, data=None, schema=None):
43  if schema:
44  validate(instance=data, schema=schema)
45  self.data = data
46 
47  def set_data(self, data):
48  self.json = json.dumps(data)
49 
50  def get_data(self):
51  return json.loads(self.json)
52 
53  data = property(get_data, set_data)
54 
55  def __str__(self):
56  return str(self.data)
57 
58  def __repr__(self):
59  return("{name}({data})".format(name=self.__class__.__name__, data=repr(self.data)))
def __init__(self, data=None, schema=None)


json_transport
Author(s): Paul Bovbel
autogenerated on Wed Jan 27 2021 03:58:48