Robot Framework Integrated Development Environment (RIDE)
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 from robotide.lib.robot.utils import PY2
17 
18 
19 class JsonWriter():
20 
21  def __init__(self, output, separator=''):
22  self._writer_writer = JsonDumper(output)
23  self._separator_separator = separator
24 
25  def write_json(self, prefix, data, postfix=';\n', mapping=None,
26  separator=True):
27  self._writer_writer.write(prefix)
28  self._writer_writer.dump(data, mapping)
29  self._writer_writer.write(postfix)
30  self._write_separator_write_separator(separator)
31 
32  def write(self, string, postfix=';\n', separator=True):
33  self._writer_writer.write(string + postfix)
34  self._write_separator_write_separator(separator)
35 
36  def _write_separator(self, separator):
37  if separator and self._separator_separator:
38  self._writer_writer.write(self._separator_separator)
39 
40 
41 class JsonDumper():
42 
43  def __init__(self, output):
44  self._output_output = output
45  self._dumpers_dumpers = (MappingDumper(self),
46  IntegerDumper(self),
47  TupleListDumper(self),
48  StringDumper(self),
49  NoneDumper(self),
50  DictDumper(self))
51 
52  def dump(self, data, mapping=None):
53  for dumper in self._dumpers_dumpers:
54  if dumper.handles(data, mapping):
55  dumper.dump(data, mapping)
56  return
57  raise ValueError('Dumping %s not supported.' % type(data))
58 
59  def write(self, data):
60  self._output_output.write(data)
61 
62 
63 class _Dumper():
64 
67  _handled_types = None
68 
69  def __init__(self, jsondumper):
70  self._dump_dump = jsondumper.dump
71  self._write_write = jsondumper.write
72 
73  def handles(self, data, mapping):
74  return isinstance(data, self._handled_types_handled_types)
75 
76  def dump(self, data, mapping):
77  raise NotImplementedError
78 
79 
81 
84  _handled_types = (str, unicode) if PY2 else str
85 
88  _search_and_replace = [('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'),
89  ('\n', '\\n'), ('\r', '\\r'), ('</', '\\x3c/')]
90 
91  def dump(self, data, mapping):
92  self._write_write('"%s"' % (self._escape_escape(data) if data else ''))
93 
94  def _escape(self, string):
95  for search, replace in self._search_and_replace_search_and_replace:
96  if search in string:
97  string = string.replace(search, replace)
98  return string
99 
100 
102  # Handles also bool
103 
106  _handled_types = (int, long) if PY2 else int
107 
108  def dump(self, data, mapping):
109  self._write_write(str(data).lower())
110 
111 
113 
116  _handled_types = dict
117 
118  def dump(self, data, mapping):
119  self._write_write('{')
120  last_index = len(data) - 1
121  for index, key in enumerate(sorted(data)):
122  self._dump_dump(key, mapping)
123  self._write_write(':')
124  self._dump_dump(data[key], mapping)
125  if index < last_index:
126  self._write_write(',')
127  self._write_write('}')
128 
129 
131 
134  _handled_types = (tuple, list)
135 
136  def dump(self, data, mapping):
137  self._write_write('[')
138  last_index = len(data) - 1
139  for index, item in enumerate(data):
140  self._dump_dump(item, mapping)
141  if index < last_index:
142  self._write_write(',')
143  self._write_write(']')
144 
145 
147 
148  def handles(self, data, mapping):
149  try:
150  return mapping and data in mapping
151  except TypeError:
152  return False
153 
154  def dump(self, data, mapping):
155  self._write_write(mapping[data])
156 
157 
159 
160  def handles(self, data, mapping):
161  return data is None
162 
163  def dump(self, data, mapping):
164  self._write_write('null')
def write(self, string, postfix=';\n', separator=True)
Definition: jsonwriter.py:32
def __init__(self, output, separator='')
Definition: jsonwriter.py:21
def write_json(self, prefix, data, postfix=';\n', mapping=None, separator=True)
Definition: jsonwriter.py:26