Robot Framework
jsonwriter.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 class JsonWriter:
17 
18  def __init__(self, output, separator=''):
19  self._writer_writer = JsonDumper(output)
20  self._separator_separator = separator
21 
22  def write_json(self, prefix, data, postfix=';\n', mapping=None,
23  separator=True):
24  self._writer_writer.write(prefix)
25  self._writer_writer.dump(data, mapping)
26  self._writer_writer.write(postfix)
27  self._write_separator_write_separator(separator)
28 
29  def write(self, string, postfix=';\n', separator=True):
30  self._writer_writer.write(string + postfix)
31  self._write_separator_write_separator(separator)
32 
33  def _write_separator(self, separator):
34  if separator and self._separator_separator:
35  self._writer_writer.write(self._separator_separator)
36 
37 
38 class JsonDumper:
39 
40  def __init__(self, output):
41  self.writewrite = output.write
42  self._dumpers_dumpers = (MappingDumper(self),
43  IntegerDumper(self),
44  TupleListDumper(self),
45  StringDumper(self),
46  NoneDumper(self),
47  DictDumper(self))
48 
49  def dump(self, data, mapping=None):
50  for dumper in self._dumpers_dumpers:
51  if dumper.handles(data, mapping):
52  dumper.dump(data, mapping)
53  return
54  raise ValueError('Dumping %s not supported.' % type(data))
55 
56 
57 class _Dumper:
58 
61  _handled_types = None
62 
63  def __init__(self, jsondumper):
64  self._dump_dump = jsondumper.dump
65  self._write_write = jsondumper.write
66 
67  def handles(self, data, mapping):
68  return isinstance(data, self._handled_types_handled_types)
69 
70  def dump(self, data, mapping):
71  raise NotImplementedError
72 
73 
75 
78  _handled_types = str
79 
82  _search_and_replace = [('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'),
83  ('\n', '\\n'), ('\r', '\\r'), ('</', '\\x3c/')]
84 
85  def dump(self, data, mapping):
86  self._write_write('"%s"' % (self._escape_escape(data) if data else ''))
87 
88  def _escape(self, string):
89  for search, replace in self._search_and_replace_search_and_replace:
90  if search in string:
91  string = string.replace(search, replace)
92  return string
93 
94 
96  # Handles also bool
97 
100  _handled_types = int
101 
102  def dump(self, data, mapping):
103  self._write_write(str(data).lower())
104 
105 
107 
110  _handled_types = dict
111 
112  def dump(self, data, mapping):
113  write = self._write_write
114  dump = self._dump_dump
115  write('{')
116  last_index = len(data) - 1
117  for index, key in enumerate(sorted(data)):
118  dump(key, mapping)
119  write(':')
120  dump(data[key], mapping)
121  if index < last_index:
122  write(',')
123  write('}')
124 
125 
127 
130  _handled_types = (tuple, list)
131 
132  def dump(self, data, mapping):
133  write = self._write_write
134  dump = self._dump_dump
135  write('[')
136  last_index = len(data) - 1
137  for index, item in enumerate(data):
138  dump(item, mapping)
139  if index < last_index:
140  write(',')
141  write(']')
142 
143 
145 
146  def handles(self, data, mapping):
147  try:
148  return mapping and data in mapping
149  except TypeError:
150  return False
151 
152  def dump(self, data, mapping):
153  self._write_write(mapping[data])
154 
155 
157 
158  def handles(self, data, mapping):
159  return data is None
160 
161  def dump(self, data, mapping):
162  self._write_write('null')
def dump(self, data, mapping)
Definition: jsonwriter.py:112
def dump(self, data, mapping)
Definition: jsonwriter.py:102
def dump(self, data, mapping=None)
Definition: jsonwriter.py:49
def write_json(self, prefix, data, postfix=';\n', mapping=None, separator=True)
Definition: jsonwriter.py:23
def _write_separator(self, separator)
Definition: jsonwriter.py:33
def __init__(self, output, separator='')
Definition: jsonwriter.py:18
def write(self, string, postfix=';\n', separator=True)
Definition: jsonwriter.py:29
def handles(self, data, mapping)
Definition: jsonwriter.py:146
def dump(self, data, mapping)
Definition: jsonwriter.py:152
def handles(self, data, mapping)
Definition: jsonwriter.py:158
def dump(self, data, mapping)
Definition: jsonwriter.py:161
def dump(self, data, mapping)
Definition: jsonwriter.py:85
def handles(self, data, mapping)
Definition: jsonwriter.py:67
def dump(self, data, mapping)
Definition: jsonwriter.py:70
def __init__(self, jsondumper)
Definition: jsonwriter.py:63
def write(msg, level='INFO', html=False)
Writes the message to the log file using the given level.
Definition: logger.py:84