2 Backend independent higher level interfaces, common exceptions.
5 class JSONError(Exception):
7 Base exception for all parsing errors.
11 class IncompleteJSONError(JSONError):
13 Raised when the parser expects data and it's not available. May be
14 caused by malformed syntax or a broken source stream.
17 super(IncompleteJSONError, self).__init__('Incomplete or empty JSON data')
19 def parse(basic_events):
21 An iterator returning parsing events with the information about their location
22 with the JSON object tree. Events are tuples ``(prefix, type, value)``.
24 Available types and values are:
27 ('boolean', <True or False>)
28 ('number', <int or Decimal>)
36 Prefixes represent the path to the nested elements from the root of the JSON
37 document. For example, given this document::
46 the parser would yield events:
48 ('', 'start_map', None)
49 ('', 'map_key', 'array')
50 ('array', 'start_array', None)
51 ('array.item', 'number', 1)
52 ('array.item', 'number', 2)
53 ('array', 'end_array', None)
54 ('', 'map_key', 'map')
55 ('map', 'start_map', None)
56 ('map', 'map_key', 'key')
57 ('map.key', 'string', u'value')
58 ('map', 'end_map', None)
63 for event, value in basic_events:
64 if event == 'map_key':
65 prefix = '.'.join(path[:-1])
67 elif event == 'start_map':
68 prefix = '.'.join(path)
70 elif event == 'end_map':
72 prefix = '.'.join(path)
73 elif event == 'start_array':
74 prefix = '.'.join(path)
76 elif event == 'end_array':
78 prefix = '.'.join(path)
79 else: # any scalar value
80 prefix = '.'.join(path)
82 yield prefix, event, value
85 class ObjectBuilder(object):
87 Incrementally builds an object from JSON parser events. Events are passed
88 into the `event` function that accepts two parameters: event type and
89 value. The object being built is available at any time from the `value`
94 from StringIO import StringIO
95 from ijson.parse import basic_parse
96 from ijson.utils import ObjectBuilder
98 builder = ObjectBuilder()
99 f = StringIO('{"key": "value"})
100 for event, value in basic_parse(f):
101 builder.event(event, value)
106 def initial_set(value):
108 self.containers = [initial_set]
110 def event(self, event, value):
111 if event == 'map_key':
113 elif event == 'start_map':
115 self.containers[-1](map)
117 map[self.key] = value
118 self.containers.append(setter)
119 elif event == 'start_array':
121 self.containers[-1](array)
122 self.containers.append(array.append)
123 elif event == 'end_array' or event == 'end_map':
124 self.containers.pop()
126 self.containers[-1](value)
128 def items(prefixed_events, prefix):
130 An iterator returning native Python objects constructed from the events
131 under a given prefix.
133 prefixed_events = iter(prefixed_events)
136 current, event, value = next(prefixed_events)
137 if current == prefix:
138 if event in ('start_map', 'start_array'):
139 builder = ObjectBuilder()
140 end_event = event.replace('start', 'end')
141 while (current, event) != (prefix, end_event):
142 builder.event(event, value)
143 current, event, value = next(prefixed_events)
147 except StopIteration: