pyjsonfrag 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyjsonfrag-0.0.1/.gitignore +1 -0
- pyjsonfrag-0.0.1/.pyproject.toml.swp +0 -0
- pyjsonfrag-0.0.1/LICENSE +19 -0
- pyjsonfrag-0.0.1/PKG-INFO +230 -0
- pyjsonfrag-0.0.1/README.md +216 -0
- pyjsonfrag-0.0.1/examples/customers.json +16 -0
- pyjsonfrag-0.0.1/examples/parse_frag.py +43 -0
- pyjsonfrag-0.0.1/examples/parse_stream.py +59 -0
- pyjsonfrag-0.0.1/examples/parse_tree.py +4 -0
- pyjsonfrag-0.0.1/examples/pyjsonfrag/__init__.py +765 -0
- pyjsonfrag-0.0.1/howtobuild.txt +1 -0
- pyjsonfrag-0.0.1/howtoupload.txt +1 -0
- pyjsonfrag-0.0.1/pyproject.toml +22 -0
- pyjsonfrag-0.0.1/src/customers.json +16 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/dist
|
|
Binary file
|
pyjsonfrag-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Juha-Matti Tilli
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to
|
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
8
|
+
so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyjsonfrag
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Combined tree/event based JSON parser
|
|
5
|
+
Project-URL: Homepage, https://github.com/jmtilli/pyjsonfrag
|
|
6
|
+
Project-URL: Issues, https://github.com/jmtilli/pyjsonfrag/issues
|
|
7
|
+
Author-email: Juha-Matti Tilli <juha-matti.tilli@iki.fi>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# PyJsonFrag: a powerful combined tree-based and event-based parser for JSON
|
|
16
|
+
|
|
17
|
+
Typically, JSON is parsed by a tree-based parser unlike XML that can be parsed by a tree-based parser or an event-based parser. Event-based parsers are fast and have a low memory footprint, but a drawback is that it is cumbersome to write the required event handlers. Tree-based parsers make the code easier to write, to understand and to maintain but have a large memory footprint as a drawback. Sometimes, JSON is used for huge files such as database dumps that would be preferably parsed by event-based parsing, or so it would appear at a glance, because a tree-based parser cannot hold the whole parse tree in memory at the same time, if the file is huge.
|
|
18
|
+
|
|
19
|
+
## How to install: PyJsonFrag at PyPI
|
|
20
|
+
|
|
21
|
+
PyJsonFrag is available at [PyPI](https://pypi.org/project/pyjsonfrag/).
|
|
22
|
+
|
|
23
|
+
How to install:
|
|
24
|
+
```
|
|
25
|
+
python3 -m pip install pyjsonfrag
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Example application: customers in a major bank
|
|
29
|
+
|
|
30
|
+
Let us consider an example application: a listing of a customers in a major bank that has 30 million customers. The test file is in the following format:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
{
|
|
34
|
+
"customers": [
|
|
35
|
+
{
|
|
36
|
+
"id": 1,
|
|
37
|
+
"name": "Clark Henson",
|
|
38
|
+
"accountCount": 1,
|
|
39
|
+
"totalBalance": 5085.96
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"id": 2,
|
|
43
|
+
"name": "Elnora Ericson",
|
|
44
|
+
"accountCount": 3,
|
|
45
|
+
"totalBalance": 3910.11
|
|
46
|
+
},
|
|
47
|
+
...
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The example format requires about 100 bytes per customer plus customer name length. If we assume an average customer name is 15 characters long, the required storage is about 115 bytes per customer. For 30 million customers, this is 3.5 gigabytes. In the example, the file is read to the following structure:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
class Customer(object):
|
|
56
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
57
|
+
if customerId is not None:
|
|
58
|
+
self.customerId = int(customerId)
|
|
59
|
+
else:
|
|
60
|
+
self.customerId = None
|
|
61
|
+
self.name = name
|
|
62
|
+
if accountCount is not None:
|
|
63
|
+
self.accountCount = int(accountCount)
|
|
64
|
+
else:
|
|
65
|
+
self.accountCount = None
|
|
66
|
+
if totalBalance is not None:
|
|
67
|
+
self.totalBalance = float(totalBalance)
|
|
68
|
+
else:
|
|
69
|
+
self.totalBalance = None
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Python jsonstream API
|
|
73
|
+
|
|
74
|
+
For XML, there is Simple API for XML (SAX). However, for JSON the usual parse
|
|
75
|
+
methods read the whole data into memory at once, not supporting event-driven
|
|
76
|
+
parsing. Thus, we provide Python jsonstream API to provide the possibility
|
|
77
|
+
for event-driven parsing. It is faster and less memory-hungry than the "read
|
|
78
|
+
all at once" parsing methods, but it is cumbersome.
|
|
79
|
+
|
|
80
|
+
A jsonstream-based parser is implemented here:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
import pyjsonfrag
|
|
84
|
+
|
|
85
|
+
class Customer(object):
|
|
86
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
87
|
+
if customerId is not None:
|
|
88
|
+
self.customerId = int(customerId)
|
|
89
|
+
else:
|
|
90
|
+
self.customerId = None
|
|
91
|
+
self.name = name
|
|
92
|
+
if accountCount is not None:
|
|
93
|
+
self.accountCount = int(accountCount)
|
|
94
|
+
else:
|
|
95
|
+
self.accountCount = None
|
|
96
|
+
if totalBalance is not None:
|
|
97
|
+
self.totalBalance = float(totalBalance)
|
|
98
|
+
else:
|
|
99
|
+
self.totalBalance = None
|
|
100
|
+
def __repr__(s):
|
|
101
|
+
return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))
|
|
102
|
+
|
|
103
|
+
context = []
|
|
104
|
+
cs = {}
|
|
105
|
+
c = None
|
|
106
|
+
|
|
107
|
+
class MyHandler(pyjsonfrag.JsonHandler):
|
|
108
|
+
def start_dict(stream, key):
|
|
109
|
+
global c
|
|
110
|
+
context.append(key)
|
|
111
|
+
if context == [None, "customers", None]:
|
|
112
|
+
c = Customer()
|
|
113
|
+
def start_array(stream, key):
|
|
114
|
+
context.append(key)
|
|
115
|
+
def end_dict(stream, key):
|
|
116
|
+
context.pop()
|
|
117
|
+
def end_array(stream, key):
|
|
118
|
+
context.pop()
|
|
119
|
+
def handle_string(stream, key, val):
|
|
120
|
+
if key == "name":
|
|
121
|
+
c.name = val
|
|
122
|
+
def handle_number(stream, key, num, is_integer):
|
|
123
|
+
if key == "id":
|
|
124
|
+
cs[int(num)] = c
|
|
125
|
+
c.customerId = int(num)
|
|
126
|
+
elif key == "accountCount":
|
|
127
|
+
c.accountCount = int(num)
|
|
128
|
+
elif key == "totalBalance":
|
|
129
|
+
c.totalBalance = num
|
|
130
|
+
|
|
131
|
+
handler = MyHandler()
|
|
132
|
+
stream = pyjsonfrag.JsonStream(handler)
|
|
133
|
+
with open("customers.json", "r") as f:
|
|
134
|
+
while True:
|
|
135
|
+
buf = f.read(4096)
|
|
136
|
+
if buf == '':
|
|
137
|
+
stream.feed(buf, 0, len(buf), True)
|
|
138
|
+
break
|
|
139
|
+
else:
|
|
140
|
+
stream.feed(buf, 0, len(buf), False)
|
|
141
|
+
print(cs)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
It can be seen that the parser is quite cumbersome and the code to construct a customer is scattered to two different places. Yet it is fast and has a low memory footprint.
|
|
145
|
+
|
|
146
|
+
## Parser with the new library
|
|
147
|
+
|
|
148
|
+
What if we could combine the benefits of the jsonstream-based approach with the benefits of the "read whole parse tree into memory" based approach? A parse tree fragment for a single customer dictionary is small enough to be kept in memory. This is what the new library is about. Here is the code to parse the customer file with the new library:
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
import pyjsonfrag
|
|
152
|
+
|
|
153
|
+
class Customer(object):
|
|
154
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
155
|
+
if customerId is not None:
|
|
156
|
+
self.customerId = int(customerId)
|
|
157
|
+
else:
|
|
158
|
+
self.customerId = None
|
|
159
|
+
self.name = name
|
|
160
|
+
if accountCount is not None:
|
|
161
|
+
self.accountCount = int(accountCount)
|
|
162
|
+
else:
|
|
163
|
+
self.accountCount = None
|
|
164
|
+
if totalBalance is not None:
|
|
165
|
+
self.totalBalance = float(totalBalance)
|
|
166
|
+
else:
|
|
167
|
+
self.totalBalance = None
|
|
168
|
+
def __repr__(s):
|
|
169
|
+
return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))
|
|
170
|
+
|
|
171
|
+
cs = {}
|
|
172
|
+
|
|
173
|
+
class MyHandler(pyjsonfrag.FragmentHandler):
|
|
174
|
+
def start_frag_dict(self, key):
|
|
175
|
+
if self.path_is([None, "customers", None]):
|
|
176
|
+
self.start_frag_collection()
|
|
177
|
+
def end_frag_dict(self, key, val):
|
|
178
|
+
if self.path_is([None, "customers", None]):
|
|
179
|
+
c = Customer(customerId=val["id"], name=val["name"],
|
|
180
|
+
accountCount=val["accountCount"], totalBalance=val["totalBalance"])
|
|
181
|
+
cs[c.customerId] = c
|
|
182
|
+
|
|
183
|
+
handler = MyHandler()
|
|
184
|
+
stream = pyjsonfrag.JsonStream(handler)
|
|
185
|
+
with open("customers.json", "r") as f:
|
|
186
|
+
while True:
|
|
187
|
+
buf = f.read(4096)
|
|
188
|
+
if buf == '':
|
|
189
|
+
stream.feed(buf, 0, len(buf), True)
|
|
190
|
+
break
|
|
191
|
+
else:
|
|
192
|
+
stream.feed(buf, 0, len(buf), False)
|
|
193
|
+
print(cs)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Note how the code is significantly more simple than for the event-based approach. Performance is close to the event-based approach, and memory consumption is essentially the same as for the event-based approach.
|
|
197
|
+
|
|
198
|
+
Of course, the new library supports getting the whole parse tree in memory:
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
import pyjsonfrag
|
|
202
|
+
|
|
203
|
+
with open("customers.json", "r") as f:
|
|
204
|
+
print(pyjsonfrag.jsonstream_tree_parse(f.read()))
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## License
|
|
208
|
+
|
|
209
|
+
All of the material related to PyJsonFrag is licensed under the following MIT
|
|
210
|
+
license:
|
|
211
|
+
|
|
212
|
+
Copyright (C) 2026 Juha-Matti Tilli
|
|
213
|
+
|
|
214
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
215
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
216
|
+
the Software without restriction, including without limitation the rights to
|
|
217
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
218
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
219
|
+
so, subject to the following conditions:
|
|
220
|
+
|
|
221
|
+
The above copyright notice and this permission notice shall be included in all
|
|
222
|
+
copies or substantial portions of the Software.
|
|
223
|
+
|
|
224
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
225
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
226
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
227
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
228
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
229
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
230
|
+
SOFTWARE.
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# PyJsonFrag: a powerful combined tree-based and event-based parser for JSON
|
|
2
|
+
|
|
3
|
+
Typically, JSON is parsed by a tree-based parser unlike XML that can be parsed by a tree-based parser or an event-based parser. Event-based parsers are fast and have a low memory footprint, but a drawback is that it is cumbersome to write the required event handlers. Tree-based parsers make the code easier to write, to understand and to maintain but have a large memory footprint as a drawback. Sometimes, JSON is used for huge files such as database dumps that would be preferably parsed by event-based parsing, or so it would appear at a glance, because a tree-based parser cannot hold the whole parse tree in memory at the same time, if the file is huge.
|
|
4
|
+
|
|
5
|
+
## How to install: PyJsonFrag at PyPI
|
|
6
|
+
|
|
7
|
+
PyJsonFrag is available at [PyPI](https://pypi.org/project/pyjsonfrag/).
|
|
8
|
+
|
|
9
|
+
How to install:
|
|
10
|
+
```
|
|
11
|
+
python3 -m pip install pyjsonfrag
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Example application: customers in a major bank
|
|
15
|
+
|
|
16
|
+
Let us consider an example application: a listing of a customers in a major bank that has 30 million customers. The test file is in the following format:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
{
|
|
20
|
+
"customers": [
|
|
21
|
+
{
|
|
22
|
+
"id": 1,
|
|
23
|
+
"name": "Clark Henson",
|
|
24
|
+
"accountCount": 1,
|
|
25
|
+
"totalBalance": 5085.96
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"id": 2,
|
|
29
|
+
"name": "Elnora Ericson",
|
|
30
|
+
"accountCount": 3,
|
|
31
|
+
"totalBalance": 3910.11
|
|
32
|
+
},
|
|
33
|
+
...
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The example format requires about 100 bytes per customer plus customer name length. If we assume an average customer name is 15 characters long, the required storage is about 115 bytes per customer. For 30 million customers, this is 3.5 gigabytes. In the example, the file is read to the following structure:
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
class Customer(object):
|
|
42
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
43
|
+
if customerId is not None:
|
|
44
|
+
self.customerId = int(customerId)
|
|
45
|
+
else:
|
|
46
|
+
self.customerId = None
|
|
47
|
+
self.name = name
|
|
48
|
+
if accountCount is not None:
|
|
49
|
+
self.accountCount = int(accountCount)
|
|
50
|
+
else:
|
|
51
|
+
self.accountCount = None
|
|
52
|
+
if totalBalance is not None:
|
|
53
|
+
self.totalBalance = float(totalBalance)
|
|
54
|
+
else:
|
|
55
|
+
self.totalBalance = None
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Python jsonstream API
|
|
59
|
+
|
|
60
|
+
For XML, there is Simple API for XML (SAX). However, for JSON the usual parse
|
|
61
|
+
methods read the whole data into memory at once, not supporting event-driven
|
|
62
|
+
parsing. Thus, we provide Python jsonstream API to provide the possibility
|
|
63
|
+
for event-driven parsing. It is faster and less memory-hungry than the "read
|
|
64
|
+
all at once" parsing methods, but it is cumbersome.
|
|
65
|
+
|
|
66
|
+
A jsonstream-based parser is implemented here:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
import pyjsonfrag
|
|
70
|
+
|
|
71
|
+
class Customer(object):
|
|
72
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
73
|
+
if customerId is not None:
|
|
74
|
+
self.customerId = int(customerId)
|
|
75
|
+
else:
|
|
76
|
+
self.customerId = None
|
|
77
|
+
self.name = name
|
|
78
|
+
if accountCount is not None:
|
|
79
|
+
self.accountCount = int(accountCount)
|
|
80
|
+
else:
|
|
81
|
+
self.accountCount = None
|
|
82
|
+
if totalBalance is not None:
|
|
83
|
+
self.totalBalance = float(totalBalance)
|
|
84
|
+
else:
|
|
85
|
+
self.totalBalance = None
|
|
86
|
+
def __repr__(s):
|
|
87
|
+
return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))
|
|
88
|
+
|
|
89
|
+
context = []
|
|
90
|
+
cs = {}
|
|
91
|
+
c = None
|
|
92
|
+
|
|
93
|
+
class MyHandler(pyjsonfrag.JsonHandler):
|
|
94
|
+
def start_dict(stream, key):
|
|
95
|
+
global c
|
|
96
|
+
context.append(key)
|
|
97
|
+
if context == [None, "customers", None]:
|
|
98
|
+
c = Customer()
|
|
99
|
+
def start_array(stream, key):
|
|
100
|
+
context.append(key)
|
|
101
|
+
def end_dict(stream, key):
|
|
102
|
+
context.pop()
|
|
103
|
+
def end_array(stream, key):
|
|
104
|
+
context.pop()
|
|
105
|
+
def handle_string(stream, key, val):
|
|
106
|
+
if key == "name":
|
|
107
|
+
c.name = val
|
|
108
|
+
def handle_number(stream, key, num, is_integer):
|
|
109
|
+
if key == "id":
|
|
110
|
+
cs[int(num)] = c
|
|
111
|
+
c.customerId = int(num)
|
|
112
|
+
elif key == "accountCount":
|
|
113
|
+
c.accountCount = int(num)
|
|
114
|
+
elif key == "totalBalance":
|
|
115
|
+
c.totalBalance = num
|
|
116
|
+
|
|
117
|
+
handler = MyHandler()
|
|
118
|
+
stream = pyjsonfrag.JsonStream(handler)
|
|
119
|
+
with open("customers.json", "r") as f:
|
|
120
|
+
while True:
|
|
121
|
+
buf = f.read(4096)
|
|
122
|
+
if buf == '':
|
|
123
|
+
stream.feed(buf, 0, len(buf), True)
|
|
124
|
+
break
|
|
125
|
+
else:
|
|
126
|
+
stream.feed(buf, 0, len(buf), False)
|
|
127
|
+
print(cs)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
It can be seen that the parser is quite cumbersome and the code to construct a customer is scattered to two different places. Yet it is fast and has a low memory footprint.
|
|
131
|
+
|
|
132
|
+
## Parser with the new library
|
|
133
|
+
|
|
134
|
+
What if we could combine the benefits of the jsonstream-based approach with the benefits of the "read whole parse tree into memory" based approach? A parse tree fragment for a single customer dictionary is small enough to be kept in memory. This is what the new library is about. Here is the code to parse the customer file with the new library:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
import pyjsonfrag
|
|
138
|
+
|
|
139
|
+
class Customer(object):
|
|
140
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
141
|
+
if customerId is not None:
|
|
142
|
+
self.customerId = int(customerId)
|
|
143
|
+
else:
|
|
144
|
+
self.customerId = None
|
|
145
|
+
self.name = name
|
|
146
|
+
if accountCount is not None:
|
|
147
|
+
self.accountCount = int(accountCount)
|
|
148
|
+
else:
|
|
149
|
+
self.accountCount = None
|
|
150
|
+
if totalBalance is not None:
|
|
151
|
+
self.totalBalance = float(totalBalance)
|
|
152
|
+
else:
|
|
153
|
+
self.totalBalance = None
|
|
154
|
+
def __repr__(s):
|
|
155
|
+
return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))
|
|
156
|
+
|
|
157
|
+
cs = {}
|
|
158
|
+
|
|
159
|
+
class MyHandler(pyjsonfrag.FragmentHandler):
|
|
160
|
+
def start_frag_dict(self, key):
|
|
161
|
+
if self.path_is([None, "customers", None]):
|
|
162
|
+
self.start_frag_collection()
|
|
163
|
+
def end_frag_dict(self, key, val):
|
|
164
|
+
if self.path_is([None, "customers", None]):
|
|
165
|
+
c = Customer(customerId=val["id"], name=val["name"],
|
|
166
|
+
accountCount=val["accountCount"], totalBalance=val["totalBalance"])
|
|
167
|
+
cs[c.customerId] = c
|
|
168
|
+
|
|
169
|
+
handler = MyHandler()
|
|
170
|
+
stream = pyjsonfrag.JsonStream(handler)
|
|
171
|
+
with open("customers.json", "r") as f:
|
|
172
|
+
while True:
|
|
173
|
+
buf = f.read(4096)
|
|
174
|
+
if buf == '':
|
|
175
|
+
stream.feed(buf, 0, len(buf), True)
|
|
176
|
+
break
|
|
177
|
+
else:
|
|
178
|
+
stream.feed(buf, 0, len(buf), False)
|
|
179
|
+
print(cs)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Note how the code is significantly more simple than for the event-based approach. Performance is close to the event-based approach, and memory consumption is essentially the same as for the event-based approach.
|
|
183
|
+
|
|
184
|
+
Of course, the new library supports getting the whole parse tree in memory:
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
import pyjsonfrag
|
|
188
|
+
|
|
189
|
+
with open("customers.json", "r") as f:
|
|
190
|
+
print(pyjsonfrag.jsonstream_tree_parse(f.read()))
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
All of the material related to PyJsonFrag is licensed under the following MIT
|
|
196
|
+
license:
|
|
197
|
+
|
|
198
|
+
Copyright (C) 2026 Juha-Matti Tilli
|
|
199
|
+
|
|
200
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
201
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
202
|
+
the Software without restriction, including without limitation the rights to
|
|
203
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
204
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
205
|
+
so, subject to the following conditions:
|
|
206
|
+
|
|
207
|
+
The above copyright notice and this permission notice shall be included in all
|
|
208
|
+
copies or substantial portions of the Software.
|
|
209
|
+
|
|
210
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
211
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
212
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
213
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
214
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
215
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
216
|
+
SOFTWARE.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import pyjsonfrag
|
|
2
|
+
|
|
3
|
+
class Customer(object):
|
|
4
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
5
|
+
if customerId is not None:
|
|
6
|
+
self.customerId = int(customerId)
|
|
7
|
+
else:
|
|
8
|
+
self.customerId = None
|
|
9
|
+
self.name = name
|
|
10
|
+
if accountCount is not None:
|
|
11
|
+
self.accountCount = int(accountCount)
|
|
12
|
+
else:
|
|
13
|
+
self.accountCount = None
|
|
14
|
+
if totalBalance is not None:
|
|
15
|
+
self.totalBalance = float(totalBalance)
|
|
16
|
+
else:
|
|
17
|
+
self.totalBalance = None
|
|
18
|
+
def __repr__(s):
|
|
19
|
+
return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))
|
|
20
|
+
|
|
21
|
+
cs = {}
|
|
22
|
+
|
|
23
|
+
class MyHandler(pyjsonfrag.FragmentHandler):
|
|
24
|
+
def start_frag_dict(self, key):
|
|
25
|
+
if self.path_is([None, "customers", None]):
|
|
26
|
+
self.start_frag_collection()
|
|
27
|
+
def end_frag_dict(self, key, val):
|
|
28
|
+
if self.path_is([None, "customers", None]):
|
|
29
|
+
c = Customer(customerId=val["id"], name=val["name"],
|
|
30
|
+
accountCount=val["accountCount"], totalBalance=val["totalBalance"])
|
|
31
|
+
cs[c.customerId] = c
|
|
32
|
+
|
|
33
|
+
handler = MyHandler()
|
|
34
|
+
stream = pyjsonfrag.JsonStream(handler)
|
|
35
|
+
with open("customers.json", "r") as f:
|
|
36
|
+
while True:
|
|
37
|
+
buf = f.read(4096)
|
|
38
|
+
if buf == '':
|
|
39
|
+
stream.feed(buf, 0, len(buf), True)
|
|
40
|
+
break
|
|
41
|
+
else:
|
|
42
|
+
stream.feed(buf, 0, len(buf), False)
|
|
43
|
+
print(cs)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import pyjsonfrag
|
|
2
|
+
|
|
3
|
+
class Customer(object):
|
|
4
|
+
def __init__(self, customerId = None, name = None, accountCount = None, totalBalance = None):
|
|
5
|
+
if customerId is not None:
|
|
6
|
+
self.customerId = int(customerId)
|
|
7
|
+
else:
|
|
8
|
+
self.customerId = None
|
|
9
|
+
self.name = name
|
|
10
|
+
if accountCount is not None:
|
|
11
|
+
self.accountCount = int(accountCount)
|
|
12
|
+
else:
|
|
13
|
+
self.accountCount = None
|
|
14
|
+
if totalBalance is not None:
|
|
15
|
+
self.totalBalance = float(totalBalance)
|
|
16
|
+
else:
|
|
17
|
+
self.totalBalance = None
|
|
18
|
+
def __repr__(s):
|
|
19
|
+
return ("Customer(%d,%s,%d,%.2f)" % (s.customerId,s.name,s.accountCount,s.totalBalance))
|
|
20
|
+
|
|
21
|
+
context = []
|
|
22
|
+
cs = {}
|
|
23
|
+
c = None
|
|
24
|
+
|
|
25
|
+
class MyHandler(pyjsonfrag.JsonHandler):
|
|
26
|
+
def start_dict(stream, key):
|
|
27
|
+
global c
|
|
28
|
+
context.append(key)
|
|
29
|
+
if context == [None, "customers", None]:
|
|
30
|
+
c = Customer()
|
|
31
|
+
def start_array(stream, key):
|
|
32
|
+
context.append(key)
|
|
33
|
+
def end_dict(stream, key):
|
|
34
|
+
context.pop()
|
|
35
|
+
def end_array(stream, key):
|
|
36
|
+
context.pop()
|
|
37
|
+
def handle_string(stream, key, val):
|
|
38
|
+
if key == "name":
|
|
39
|
+
c.name = val
|
|
40
|
+
def handle_number(stream, key, num, is_integer):
|
|
41
|
+
if key == "id":
|
|
42
|
+
cs[int(num)] = c
|
|
43
|
+
c.customerId = int(num)
|
|
44
|
+
elif key == "accountCount":
|
|
45
|
+
c.accountCount = int(num)
|
|
46
|
+
elif key == "totalBalance":
|
|
47
|
+
c.totalBalance = num
|
|
48
|
+
|
|
49
|
+
handler = MyHandler()
|
|
50
|
+
stream = pyjsonfrag.JsonStream(handler)
|
|
51
|
+
with open("customers.json", "r") as f:
|
|
52
|
+
while True:
|
|
53
|
+
buf = f.read(4096)
|
|
54
|
+
if buf == '':
|
|
55
|
+
stream.feed(buf, 0, len(buf), True)
|
|
56
|
+
break
|
|
57
|
+
else:
|
|
58
|
+
stream.feed(buf, 0, len(buf), False)
|
|
59
|
+
print(cs)
|