streamjson 1.0.2__tar.gz → 1.0.4__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.
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Zairon Jacobs
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zairon Jacobs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
@@ -1,90 +1,89 @@
1
- Metadata-Version: 2.1
2
- Name: streamjson
3
- Version: 1.0.2
4
- Summary: Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream.
5
- Home-page: https://github.com/zaironjacobs/streamjson
6
- Download-URL: https://github.com/zaironjacobs/streamjson/archive/v1.0.2.tar.gz
7
- Author: Zairon Jacobs
8
- Author-email: zaironjacobs@gmail.com
9
- License: MIT
10
- Keywords: json,stream,write,file,read,objects,arrays
11
- Classifier: Development Status :: 5 - Production/Stable
12
- Classifier: Intended Audience :: Developers
13
- Classifier: License :: OSI Approved :: MIT License
14
- Classifier: Operating System :: OS Independent
15
- Classifier: Programming Language :: Python :: 3.9
16
- Classifier: Natural Language :: English
17
- Requires-Python: >=3
18
- Description-Content-Type: text/markdown
19
- License-File: LICENSE
20
-
21
- StreamJSON
22
- =================
23
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
24
- [![PyPI](https://img.shields.io/pypi/v/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
25
- [![PyPI - Status](https://img.shields.io/pypi/status/streamjson)](https://pypi.python.org/pypi/streamjson)
26
- [![PyPI - License](https://img.shields.io/pypi/l/streamjson)](https://pypi.python.org/pypi/streamjson)
27
-
28
- Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
29
- memory, for example when you need to save large amounts of data from a database to a single JSON file.
30
-
31
- Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
32
-
33
- ## Install
34
-
35
- To install:
36
-
37
- ```console
38
- pip install streamjson
39
- ```
40
-
41
- To upgrade:
42
-
43
- ```console
44
- pip install streamjson --upgrade
45
- ```
46
-
47
- ## How to write to JSON file
48
-
49
- Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
50
- A new file with the given name will be created, the root of the JSON file is an array by default.
51
-
52
- ```Python
53
- from streamjson import StreamJSONWriter
54
-
55
- persons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
56
- {'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]
57
-
58
- with StreamJSONWriter('persons.json', indent=2) as writer:
59
- for person in persons:
60
- writer.send(person)
61
- ```
62
-
63
- persons.json:
64
-
65
- ```JSON
66
- [
67
- {
68
- "id": "0001",
69
- "first_name": "John",
70
- "last_name": "Doe"
71
- },
72
- {
73
- "id": "0002",
74
- "first_name": "Jane",
75
- "last_name": "Doe"
76
- }
77
- ]
78
- ```
79
-
80
- ## How to read from JSON file
81
-
82
- The reader will stream each object from the JSON file.
83
-
84
- ```Python
85
- from streamjson import StreamJSONReader
86
-
87
- with StreamJSONReader('persons.json') as reader:
88
- for obj in reader.find():
89
- print(obj)
90
- ```
1
+ Metadata-Version: 2.4
2
+ Name: streamjson
3
+ Version: 1.0.4
4
+ Summary: Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream.
5
+ Author-email: Zairon Jacobs <zaironjacobs@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: homepage, https://github.com/zaironjacobs/streamjson
8
+ Project-URL: download, https://github.com/zaironjacobs/streamjson/archive/v1.0.4.tar.gz
9
+ Keywords: json,stream,write,file,read,objects,arrays
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Operating System :: POSIX :: Linux
19
+ Classifier: Operating System :: Microsoft :: Windows
20
+ Classifier: Operating System :: MacOS :: MacOS X
21
+ Classifier: Natural Language :: English
22
+ Requires-Python: >=3
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Dynamic: license-file
26
+
27
+ # StreamJSON
28
+
29
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
30
+ [![PyPI](https://img.shields.io/pypi/v/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
31
+ [![PyPI - License](https://img.shields.io/pypi/l/streamjson)](https://pypi.python.org/pypi/streamjson)
32
+
33
+ [![tests](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml/badge.svg)](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml)
34
+
35
+ Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
36
+ memory, for example when you need to save large amounts of data from a database to a single JSON file.
37
+
38
+ Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
39
+
40
+ ## Install
41
+
42
+ ```console
43
+ pip install streamjson
44
+ ```
45
+
46
+ ## How to write to JSON file
47
+
48
+ Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
49
+ A new file with the given name will be created, the root of the JSON file is an array by default.
50
+
51
+ ```Python
52
+ from streamjson import StreamJSONWriter
53
+
54
+ persons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
55
+ {'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]
56
+
57
+ with StreamJSONWriter('persons.json', indent=2) as writer:
58
+ for person in persons:
59
+ writer.send(person)
60
+ ```
61
+
62
+ persons.json:
63
+
64
+ ```JSON
65
+ [
66
+ {
67
+ "id": "0001",
68
+ "first_name": "John",
69
+ "last_name": "Doe"
70
+ },
71
+ {
72
+ "id": "0002",
73
+ "first_name": "Jane",
74
+ "last_name": "Doe"
75
+ }
76
+ ]
77
+ ```
78
+
79
+ ## How to read from JSON file
80
+
81
+ The reader will stream each object from the JSON file.
82
+
83
+ ```Python
84
+ from streamjson import StreamJSONReader
85
+
86
+ with StreamJSONReader('persons.json') as reader:
87
+ for obj in reader.find():
88
+ print(obj)
89
+ ```
@@ -1,70 +1,63 @@
1
- StreamJSON
2
- =================
3
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
4
- [![PyPI](https://img.shields.io/pypi/v/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
5
- [![PyPI - Status](https://img.shields.io/pypi/status/streamjson)](https://pypi.python.org/pypi/streamjson)
6
- [![PyPI - License](https://img.shields.io/pypi/l/streamjson)](https://pypi.python.org/pypi/streamjson)
7
-
8
- Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
9
- memory, for example when you need to save large amounts of data from a database to a single JSON file.
10
-
11
- Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
12
-
13
- ## Install
14
-
15
- To install:
16
-
17
- ```console
18
- pip install streamjson
19
- ```
20
-
21
- To upgrade:
22
-
23
- ```console
24
- pip install streamjson --upgrade
25
- ```
26
-
27
- ## How to write to JSON file
28
-
29
- Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
30
- A new file with the given name will be created, the root of the JSON file is an array by default.
31
-
32
- ```Python
33
- from streamjson import StreamJSONWriter
34
-
35
- persons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
36
- {'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]
37
-
38
- with StreamJSONWriter('persons.json', indent=2) as writer:
39
- for person in persons:
40
- writer.send(person)
41
- ```
42
-
43
- persons.json:
44
-
45
- ```JSON
46
- [
47
- {
48
- "id": "0001",
49
- "first_name": "John",
50
- "last_name": "Doe"
51
- },
52
- {
53
- "id": "0002",
54
- "first_name": "Jane",
55
- "last_name": "Doe"
56
- }
57
- ]
58
- ```
59
-
60
- ## How to read from JSON file
61
-
62
- The reader will stream each object from the JSON file.
63
-
64
- ```Python
65
- from streamjson import StreamJSONReader
66
-
67
- with StreamJSONReader('persons.json') as reader:
68
- for obj in reader.find():
69
- print(obj)
70
- ```
1
+ # StreamJSON
2
+
3
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
4
+ [![PyPI](https://img.shields.io/pypi/v/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
5
+ [![PyPI - License](https://img.shields.io/pypi/l/streamjson)](https://pypi.python.org/pypi/streamjson)
6
+
7
+ [![tests](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml/badge.svg)](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml)
8
+
9
+ Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
10
+ memory, for example when you need to save large amounts of data from a database to a single JSON file.
11
+
12
+ Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
13
+
14
+ ## Install
15
+
16
+ ```console
17
+ pip install streamjson
18
+ ```
19
+
20
+ ## How to write to JSON file
21
+
22
+ Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
23
+ A new file with the given name will be created, the root of the JSON file is an array by default.
24
+
25
+ ```Python
26
+ from streamjson import StreamJSONWriter
27
+
28
+ persons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
29
+ {'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]
30
+
31
+ with StreamJSONWriter('persons.json', indent=2) as writer:
32
+ for person in persons:
33
+ writer.send(person)
34
+ ```
35
+
36
+ persons.json:
37
+
38
+ ```JSON
39
+ [
40
+ {
41
+ "id": "0001",
42
+ "first_name": "John",
43
+ "last_name": "Doe"
44
+ },
45
+ {
46
+ "id": "0002",
47
+ "first_name": "Jane",
48
+ "last_name": "Doe"
49
+ }
50
+ ]
51
+ ```
52
+
53
+ ## How to read from JSON file
54
+
55
+ The reader will stream each object from the JSON file.
56
+
57
+ ```Python
58
+ from streamjson import StreamJSONReader
59
+
60
+ with StreamJSONReader('persons.json') as reader:
61
+ for obj in reader.find():
62
+ print(obj)
63
+ ```
@@ -0,0 +1,46 @@
1
+ [build-system]
2
+ requires = ["setuptools>=84.0.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "streamjson"
7
+ version = "1.0.4"
8
+ description = "Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream."
9
+ readme = "README.md"
10
+ keywords = ["json", "stream", "write", "file", "read", "objects", "arrays"]
11
+ license = "MIT"
12
+ license-files = [
13
+ "LICENSE"
14
+ ]
15
+ requires-python = ">=3"
16
+ authors = [
17
+ { name = "Zairon Jacobs", email = "zaironjacobs@gmail.com" },
18
+ ]
19
+ classifiers = [
20
+ "Development Status :: 5 - Production/Stable",
21
+ "Intended Audience :: Developers",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
26
+ "Programming Language :: Python :: 3.14",
27
+ "Operating System :: OS Independent",
28
+ "Operating System :: POSIX :: Linux",
29
+ "Operating System :: Microsoft :: Windows",
30
+ "Operating System :: MacOS :: MacOS X",
31
+ "Natural Language :: English"
32
+ ]
33
+ dependencies = []
34
+
35
+ [dependency-groups]
36
+ test = [
37
+ "pytest>=9.0.3"
38
+ ]
39
+
40
+ [project.urls]
41
+ homepage = "https://github.com/zaironjacobs/streamjson"
42
+ download = "https://github.com/zaironjacobs/streamjson/archive/v1.0.4.tar.gz"
43
+
44
+ [tool.setuptools.packages.find]
45
+ where = [""]
46
+ namespaces = false
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -1,2 +1,2 @@
1
- from .streamjsonwriter import StreamJSONWriter
2
- from .streamjsonreader import StreamJSONReader
1
+ from .streamjsonwriter import StreamJSONWriter
2
+ from .streamjsonreader import StreamJSONReader
@@ -1,126 +1,126 @@
1
- import json
2
- from json import JSONDecodeError
3
- from typing import Any
4
-
5
-
6
- class StreamJSONReader:
7
- def __init__(self, file: str, encoding: str = "utf8", **kwds: Any):
8
- """
9
- Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
10
-
11
- :param file: The file
12
- :param encoding: Determines the encoding
13
- :param kwds: Arguments for json.loads()
14
- """
15
-
16
- self.__file = file
17
- self.__encoding = encoding
18
- self.__kwds = kwds
19
-
20
- self.__reader = self.Reader(
21
- file=self.__file, encoding=self.__encoding, **self.__kwds
22
- )
23
-
24
- def find(self):
25
- return self.__reader.find()
26
-
27
- def close(self):
28
- self.__reader.close()
29
-
30
- def __enter__(self):
31
- return self.__reader
32
-
33
- def __exit__(self, exc_type, exc_val, exc_tb):
34
- self.__reader.close()
35
-
36
- class Reader:
37
- def __init__(self, file: str, encoding: str = "utf8", **kwds: Any):
38
- """
39
- :param file: The file
40
- :param encoding: Determines the encoding
41
- :param kwds: Arguments for json.loads()
42
- """
43
-
44
- self.__file = file
45
- self.__encoding = encoding
46
- self.__kwds = kwds
47
-
48
- self.__opened_file = None
49
- self.__tmp_lines = ""
50
- self.__object_is_pending = False
51
- self.__opening_bracket_index = 0
52
- self.__size_bytes = 0
53
-
54
- def find(self):
55
- """Find JSON object"""
56
-
57
- # Open the file
58
- self.__opened_file = open(self.__file, "r", encoding=self.__encoding)
59
-
60
- # Loop through each line in the JSON file and while doing so, yield JSON objects as they are found
61
- for line in self.__opened_file:
62
- # Append new line to tmp_lines
63
- new_line = "".join(line.splitlines())
64
- self.__tmp_lines += new_line
65
-
66
- # FIND OPENING BRACKET {
67
-
68
- # Not object_is_pending means there is no opening bracket expecting a closing bracket at the moment
69
- # Therefore it is necessary to look for the opening bracket
70
- if not self.__object_is_pending:
71
- # Find the opening bracket and its index
72
- self.__opening_bracket_index = self.__tmp_lines.find("{", 0)
73
- # If nothing was found: Clear tmp_lines and continue to try again with the next line from the file
74
- if self.__opening_bracket_index == -1:
75
- self.__tmp_lines = ""
76
- continue
77
- else:
78
- # If the opening bracket was found:
79
- # Set object_is_pending to true meaning a closing bracket is expected next to yield an object
80
- self.__object_is_pending = True
81
-
82
- # FIND CLOSING BRACKET }
83
-
84
- # Get all indices of closing brackets in the string
85
- closing_brackets_indices = [
86
- i for i, char in enumerate(self.__tmp_lines) if char == "}"
87
- ]
88
-
89
- # Try all closing brackets for possible JSON object completion
90
- for closing_bracket_index in closing_brackets_indices:
91
- str_possible_json_object = self.__tmp_lines[
92
- self.__opening_bracket_index : closing_bracket_index + 1
93
- ]
94
- try:
95
- # Try to load the string as a JSON object
96
- json_obj = json.loads(s=str_possible_json_object, **self.__kwds)
97
-
98
- # Clear part of tmp_lines that was used in the object
99
- self.__tmp_lines = self.__tmp_lines[closing_bracket_index + 1 :]
100
-
101
- # Set False to start looking for opening bracket next
102
- self.__object_is_pending = False
103
-
104
- # Yield the JSON object
105
- yield json_obj
106
-
107
- # Append the size of the object
108
- self.__size_bytes += len(
109
- str_possible_json_object.encode("utf-8")
110
- )
111
-
112
- break
113
- except JSONDecodeError:
114
- continue
115
-
116
- def get_size(self):
117
- """Get size of total strings successfully converted to JSON objects from file in bytes"""
118
-
119
- return self.__size_bytes
120
-
121
- def close(self):
122
- """Close the reader"""
123
-
124
- # Close the file
125
- if self.__opened_file:
126
- self.__opened_file.close()
1
+ import json
2
+ from json import JSONDecodeError
3
+ from typing import Any
4
+
5
+
6
+ class StreamJSONReader:
7
+ def __init__(self, file: str, encoding: str = "utf8", **kwds: Any):
8
+ """
9
+ Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
10
+
11
+ :param file: The file
12
+ :param encoding: Determines the encoding
13
+ :param kwds: Arguments for json.loads()
14
+ """
15
+
16
+ self.__file = file
17
+ self.__encoding = encoding
18
+ self.__kwds = kwds
19
+
20
+ self.__reader = self.Reader(
21
+ file=self.__file, encoding=self.__encoding, **self.__kwds
22
+ )
23
+
24
+ def find(self):
25
+ return self.__reader.find()
26
+
27
+ def close(self):
28
+ self.__reader.close()
29
+
30
+ def __enter__(self):
31
+ return self.__reader
32
+
33
+ def __exit__(self, exc_type, exc_val, exc_tb):
34
+ self.__reader.close()
35
+
36
+ class Reader:
37
+ def __init__(self, file: str, encoding: str = "utf8", **kwds: Any):
38
+ """
39
+ :param file: The file
40
+ :param encoding: Determines the encoding
41
+ :param kwds: Arguments for json.loads()
42
+ """
43
+
44
+ self.__file = file
45
+ self.__encoding = encoding
46
+ self.__kwds = kwds
47
+
48
+ self.__opened_file = None
49
+ self.__tmp_lines = ""
50
+ self.__object_is_pending = False
51
+ self.__opening_bracket_index = 0
52
+ self.__size_bytes = 0
53
+
54
+ def find(self):
55
+ """Find JSON object"""
56
+
57
+ # Open the file
58
+ self.__opened_file = open(self.__file, "r", encoding=self.__encoding)
59
+
60
+ # Loop through each line in the JSON file and while doing so, yield JSON objects as they are found
61
+ for line in self.__opened_file:
62
+ # Append new line to tmp_lines
63
+ new_line = "".join(line.splitlines())
64
+ self.__tmp_lines += new_line
65
+
66
+ # FIND OPENING BRACKET {
67
+
68
+ # Not object_is_pending means there is no opening bracket expecting a closing bracket at the moment
69
+ # Therefore it is necessary to look for the opening bracket
70
+ if not self.__object_is_pending:
71
+ # Find the opening bracket and its index
72
+ self.__opening_bracket_index = self.__tmp_lines.find("{", 0)
73
+ # If nothing was found: Clear tmp_lines and continue to try again with the next line from the file
74
+ if self.__opening_bracket_index == -1:
75
+ self.__tmp_lines = ""
76
+ continue
77
+ else:
78
+ # If the opening bracket was found:
79
+ # Set object_is_pending to true meaning a closing bracket is expected next to yield an object
80
+ self.__object_is_pending = True
81
+
82
+ # FIND CLOSING BRACKET }
83
+
84
+ # Get all indices of closing brackets in the string
85
+ closing_brackets_indices = [
86
+ i for i, char in enumerate(self.__tmp_lines) if char == "}"
87
+ ]
88
+
89
+ # Try all closing brackets for possible JSON object completion
90
+ for closing_bracket_index in closing_brackets_indices:
91
+ str_possible_json_object = self.__tmp_lines[
92
+ self.__opening_bracket_index : closing_bracket_index + 1
93
+ ]
94
+ try:
95
+ # Try to load the string as a JSON object
96
+ json_obj = json.loads(s=str_possible_json_object, **self.__kwds)
97
+
98
+ # Clear part of tmp_lines that was used in the object
99
+ self.__tmp_lines = self.__tmp_lines[closing_bracket_index + 1 :]
100
+
101
+ # Set False to start looking for opening bracket next
102
+ self.__object_is_pending = False
103
+
104
+ # Yield the JSON object
105
+ yield json_obj
106
+
107
+ # Append the size of the object
108
+ self.__size_bytes += len(
109
+ str_possible_json_object.encode("utf-8")
110
+ )
111
+
112
+ break
113
+ except JSONDecodeError:
114
+ continue
115
+
116
+ def get_size(self):
117
+ """Get size of total strings successfully converted to JSON objects from file in bytes"""
118
+
119
+ return self.__size_bytes
120
+
121
+ def close(self):
122
+ """Close the reader"""
123
+
124
+ # Close the file
125
+ if self.__opened_file:
126
+ self.__opened_file.close()
@@ -1,120 +1,120 @@
1
- import json
2
- import os
3
-
4
-
5
- class StreamJSONWriter:
6
- def __init__(self, file: str, indent: int = 2, ensure_ascii: bool = True):
7
- """
8
- Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of
9
- data in memory, for example when you need to save large amounts of data from a database to a single JSON file.
10
-
11
- :param file: The file
12
- :param indent: Spaces to use at the beginning of line
13
- :param ensure_ascii: ascii-only json output (replace non-ascii to \\uNNNN), True by default
14
- """
15
-
16
- self.__file = file
17
- self.__indent = indent if indent >= 0 else 0
18
- self.__ensure_ascii = ensure_ascii
19
-
20
- self.__writer = self.Writer(self.__file, self.__indent, self.__ensure_ascii)
21
-
22
- def send(self, value):
23
- self.__writer.send(value)
24
-
25
- def close(self):
26
- self.__writer.close()
27
-
28
- def __enter__(self):
29
- return self.__writer
30
-
31
- def __exit__(self, exc_type, exc_val, exc_tb):
32
- self.__writer.close()
33
-
34
- class Writer:
35
- def __init__(self, file: str, indent: int, ensure_ascii: bool):
36
- """
37
- :param file: The file
38
- :param indent: Spaces to use at the beginning of line
39
- :param ensure_ascii: ascii-only json output (replace non-ascii to \\uNNNN), True by default
40
- """
41
-
42
- self.__file = file
43
- self.__indent = indent
44
- self.__stream_started = False
45
- self.__opened_file = None
46
- self.__ensure_ascii = ensure_ascii
47
-
48
- def send(self, value):
49
- """
50
- Send value to file
51
-
52
- :param value: The value to send to the file
53
- """
54
-
55
- if not self.__stream_started:
56
- self.__stream_started = True
57
-
58
- # Remove the file before writing to it
59
- if os.path.exists(self.__file):
60
- os.remove(self.__file)
61
-
62
- # Open the file
63
- self.__opened_file = open(self.__file, "a")
64
-
65
- # Add opening bracket at first write
66
- self.__opened_file.write("[")
67
-
68
- # Create JSON string from value
69
- json_value = json.dumps(
70
- json.loads(json.dumps(value, ensure_ascii=self.__ensure_ascii)),
71
- indent=self.__indent,
72
- ensure_ascii=self.__ensure_ascii,
73
- )
74
-
75
- # Indent the whole value
76
- json_value_indented = self.__indent_string(json_value)
77
-
78
- self.__opened_file.write(f"\n{json_value_indented},")
79
-
80
- def __indent_string(self, string) -> str:
81
- """
82
- Indent a string
83
-
84
- :param string: String to indent
85
- :return: The indented string
86
- """
87
-
88
- def get_indent() -> str:
89
- """String representing the indentation"""
90
-
91
- indent_string = ""
92
- for x in range(self.__indent):
93
- indent_string += " "
94
- return indent_string
95
-
96
- indent = get_indent()
97
- return indent + string.replace("\n", "\n" + indent)
98
-
99
- def __remove_last_comma(self, file):
100
- """
101
- Remove the last comma from the file
102
-
103
- :param file: The file
104
- """
105
-
106
- file.seek(file.tell() - 1, os.SEEK_SET)
107
- file.truncate()
108
-
109
- def close(self):
110
- """Close the writer"""
111
-
112
- if self.__stream_started and self.__opened_file:
113
- # Remove last comma
114
- self.__remove_last_comma(self.__opened_file)
115
-
116
- # Add the closing bracket
117
- self.__opened_file.write("\n]")
118
-
119
- # Close the file
120
- self.__opened_file.close()
1
+ import json
2
+ import os
3
+
4
+
5
+ class StreamJSONWriter:
6
+ def __init__(self, file: str, indent: int = 2, ensure_ascii: bool = True):
7
+ """
8
+ Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of
9
+ data in memory, for example when you need to save large amounts of data from a database to a single JSON file.
10
+
11
+ :param file: The file
12
+ :param indent: Spaces to use at the beginning of line
13
+ :param ensure_ascii: ascii-only json output (replace non-ascii to \\uNNNN), True by default
14
+ """
15
+
16
+ self.__file = file
17
+ self.__indent = indent if indent >= 0 else 0
18
+ self.__ensure_ascii = ensure_ascii
19
+
20
+ self.__writer = self.Writer(self.__file, self.__indent, self.__ensure_ascii)
21
+
22
+ def send(self, value):
23
+ self.__writer.send(value)
24
+
25
+ def close(self):
26
+ self.__writer.close()
27
+
28
+ def __enter__(self):
29
+ return self.__writer
30
+
31
+ def __exit__(self, exc_type, exc_val, exc_tb):
32
+ self.__writer.close()
33
+
34
+ class Writer:
35
+ def __init__(self, file: str, indent: int, ensure_ascii: bool):
36
+ """
37
+ :param file: The file
38
+ :param indent: Spaces to use at the beginning of line
39
+ :param ensure_ascii: ascii-only json output (replace non-ascii to \\uNNNN), True by default
40
+ """
41
+
42
+ self.__file = file
43
+ self.__indent = indent
44
+ self.__stream_started = False
45
+ self.__opened_file = None
46
+ self.__ensure_ascii = ensure_ascii
47
+
48
+ def send(self, value):
49
+ """
50
+ Send value to file
51
+
52
+ :param value: The value to send to the file
53
+ """
54
+
55
+ if not self.__stream_started:
56
+ self.__stream_started = True
57
+
58
+ # Remove the file before writing to it
59
+ if os.path.exists(self.__file):
60
+ os.remove(self.__file)
61
+
62
+ # Open the file
63
+ self.__opened_file = open(self.__file, "a")
64
+
65
+ # Add opening bracket at first write
66
+ self.__opened_file.write("[")
67
+
68
+ # Create JSON string from value
69
+ json_value = json.dumps(
70
+ json.loads(json.dumps(value, ensure_ascii=self.__ensure_ascii)),
71
+ indent=self.__indent,
72
+ ensure_ascii=self.__ensure_ascii,
73
+ )
74
+
75
+ # Indent the whole value
76
+ json_value_indented = self.__indent_string(json_value)
77
+
78
+ self.__opened_file.write(f"\n{json_value_indented},")
79
+
80
+ def __indent_string(self, string) -> str:
81
+ """
82
+ Indent a string
83
+
84
+ :param string: String to indent
85
+ :return: The indented string
86
+ """
87
+
88
+ def get_indent() -> str:
89
+ """String representing the indentation"""
90
+
91
+ indent_string = ""
92
+ for x in range(self.__indent):
93
+ indent_string += " "
94
+ return indent_string
95
+
96
+ indent = get_indent()
97
+ return indent + string.replace("\n", "\n" + indent)
98
+
99
+ def __remove_last_comma(self, file):
100
+ """
101
+ Remove the last comma from the file
102
+
103
+ :param file: The file
104
+ """
105
+
106
+ file.seek(file.tell() - 1, os.SEEK_SET)
107
+ file.truncate()
108
+
109
+ def close(self):
110
+ """Close the writer"""
111
+
112
+ if self.__stream_started and self.__opened_file:
113
+ # Remove last comma
114
+ self.__remove_last_comma(self.__opened_file)
115
+
116
+ # Add the closing bracket
117
+ self.__opened_file.write("\n]")
118
+
119
+ # Close the file
120
+ self.__opened_file.close()
@@ -1,90 +1,89 @@
1
- Metadata-Version: 2.1
2
- Name: streamjson
3
- Version: 1.0.2
4
- Summary: Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream.
5
- Home-page: https://github.com/zaironjacobs/streamjson
6
- Download-URL: https://github.com/zaironjacobs/streamjson/archive/v1.0.2.tar.gz
7
- Author: Zairon Jacobs
8
- Author-email: zaironjacobs@gmail.com
9
- License: MIT
10
- Keywords: json,stream,write,file,read,objects,arrays
11
- Classifier: Development Status :: 5 - Production/Stable
12
- Classifier: Intended Audience :: Developers
13
- Classifier: License :: OSI Approved :: MIT License
14
- Classifier: Operating System :: OS Independent
15
- Classifier: Programming Language :: Python :: 3.9
16
- Classifier: Natural Language :: English
17
- Requires-Python: >=3
18
- Description-Content-Type: text/markdown
19
- License-File: LICENSE
20
-
21
- StreamJSON
22
- =================
23
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
24
- [![PyPI](https://img.shields.io/pypi/v/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
25
- [![PyPI - Status](https://img.shields.io/pypi/status/streamjson)](https://pypi.python.org/pypi/streamjson)
26
- [![PyPI - License](https://img.shields.io/pypi/l/streamjson)](https://pypi.python.org/pypi/streamjson)
27
-
28
- Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
29
- memory, for example when you need to save large amounts of data from a database to a single JSON file.
30
-
31
- Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
32
-
33
- ## Install
34
-
35
- To install:
36
-
37
- ```console
38
- pip install streamjson
39
- ```
40
-
41
- To upgrade:
42
-
43
- ```console
44
- pip install streamjson --upgrade
45
- ```
46
-
47
- ## How to write to JSON file
48
-
49
- Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
50
- A new file with the given name will be created, the root of the JSON file is an array by default.
51
-
52
- ```Python
53
- from streamjson import StreamJSONWriter
54
-
55
- persons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
56
- {'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]
57
-
58
- with StreamJSONWriter('persons.json', indent=2) as writer:
59
- for person in persons:
60
- writer.send(person)
61
- ```
62
-
63
- persons.json:
64
-
65
- ```JSON
66
- [
67
- {
68
- "id": "0001",
69
- "first_name": "John",
70
- "last_name": "Doe"
71
- },
72
- {
73
- "id": "0002",
74
- "first_name": "Jane",
75
- "last_name": "Doe"
76
- }
77
- ]
78
- ```
79
-
80
- ## How to read from JSON file
81
-
82
- The reader will stream each object from the JSON file.
83
-
84
- ```Python
85
- from streamjson import StreamJSONReader
86
-
87
- with StreamJSONReader('persons.json') as reader:
88
- for obj in reader.find():
89
- print(obj)
90
- ```
1
+ Metadata-Version: 2.4
2
+ Name: streamjson
3
+ Version: 1.0.4
4
+ Summary: Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream.
5
+ Author-email: Zairon Jacobs <zaironjacobs@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: homepage, https://github.com/zaironjacobs/streamjson
8
+ Project-URL: download, https://github.com/zaironjacobs/streamjson/archive/v1.0.4.tar.gz
9
+ Keywords: json,stream,write,file,read,objects,arrays
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Operating System :: POSIX :: Linux
19
+ Classifier: Operating System :: Microsoft :: Windows
20
+ Classifier: Operating System :: MacOS :: MacOS X
21
+ Classifier: Natural Language :: English
22
+ Requires-Python: >=3
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Dynamic: license-file
26
+
27
+ # StreamJSON
28
+
29
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
30
+ [![PyPI](https://img.shields.io/pypi/v/streamjson?color=blue)](https://pypi.python.org/pypi/streamjson)
31
+ [![PyPI - License](https://img.shields.io/pypi/l/streamjson)](https://pypi.python.org/pypi/streamjson)
32
+
33
+ [![tests](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml/badge.svg)](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml)
34
+
35
+ Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
36
+ memory, for example when you need to save large amounts of data from a database to a single JSON file.
37
+
38
+ Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
39
+
40
+ ## Install
41
+
42
+ ```console
43
+ pip install streamjson
44
+ ```
45
+
46
+ ## How to write to JSON file
47
+
48
+ Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
49
+ A new file with the given name will be created, the root of the JSON file is an array by default.
50
+
51
+ ```Python
52
+ from streamjson import StreamJSONWriter
53
+
54
+ persons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
55
+ {'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]
56
+
57
+ with StreamJSONWriter('persons.json', indent=2) as writer:
58
+ for person in persons:
59
+ writer.send(person)
60
+ ```
61
+
62
+ persons.json:
63
+
64
+ ```JSON
65
+ [
66
+ {
67
+ "id": "0001",
68
+ "first_name": "John",
69
+ "last_name": "Doe"
70
+ },
71
+ {
72
+ "id": "0002",
73
+ "first_name": "Jane",
74
+ "last_name": "Doe"
75
+ }
76
+ ]
77
+ ```
78
+
79
+ ## How to read from JSON file
80
+
81
+ The reader will stream each object from the JSON file.
82
+
83
+ ```Python
84
+ from streamjson import StreamJSONReader
85
+
86
+ with StreamJSONReader('persons.json') as reader:
87
+ for obj in reader.find():
88
+ print(obj)
89
+ ```
@@ -1,7 +1,6 @@
1
1
  LICENSE
2
2
  README.md
3
- setup.cfg
4
- setup.py
3
+ pyproject.toml
5
4
  streamjson/__init__.py
6
5
  streamjson/streamjsonreader.py
7
6
  streamjson/streamjsonwriter.py
@@ -9,5 +8,5 @@ streamjson.egg-info/PKG-INFO
9
8
  streamjson.egg-info/SOURCES.txt
10
9
  streamjson.egg-info/dependency_links.txt
11
10
  streamjson.egg-info/top_level.txt
12
- streamjson/test/__init__.py
13
- streamjson/test/test_streamjson.py
11
+ tests/__init__.py
12
+ tests/test_app.py
@@ -1,60 +1,59 @@
1
- import os
2
- import json
3
-
4
- import pytest
5
-
6
- from streamjson import StreamJSONWriter, StreamJSONReader
7
-
8
- test_json_file = os.path.join(os.path.dirname(__file__), "test.json")
9
-
10
-
11
- class TestStreamJSON:
12
- def test_writer_send(self):
13
- """Test the writer"""
14
-
15
- persons = [
16
- {"id": "0001", "first_name": "John", "last_name": "Doe"},
17
- {"id": "0002", "first_name": "Jane", "last_name": "Doe"},
18
- ]
19
-
20
- with StreamJSONWriter(test_json_file, indent=2) as writer:
21
- for person in persons:
22
- writer.send(person)
23
-
24
- with open(test_json_file, "r") as opened_file:
25
- expected = persons
26
- result = json.load(opened_file)
27
- assert expected == result
28
-
29
- def test_reader_find(self):
30
- """Test the reader"""
31
-
32
- persons_json_file = os.path.join(os.path.dirname(__file__), "persons.json")
33
- with StreamJSONReader(persons_json_file) as reader:
34
- reader_generator = reader.find()
35
- obj_1 = next(reader_generator)
36
- assert obj_1 == {
37
- "id": "0001",
38
- "first_name": "John",
39
- "last_name": "Doe",
40
- "hobbies": [
41
- {"name": "photography"},
42
- {"name": "programming"},
43
- {"name": "hiking"},
44
- ],
45
- }
46
- obj_2 = next(reader_generator)
47
- assert obj_2 == {
48
- "id": "0002",
49
- "first_name": "Jane",
50
- "last_name": "Doe",
51
- "hobbies": [{"name": "dancing"}],
52
- }
53
-
54
- @pytest.fixture(scope="session", autouse=True)
55
- def cleanup(self):
56
- """Delete test file after all tests are finished"""
57
-
58
- yield
59
- if os.path.exists(test_json_file):
60
- os.remove(test_json_file)
1
+ import os
2
+ import json
3
+
4
+ import pytest
5
+
6
+ from streamjson import StreamJSONWriter, StreamJSONReader
7
+
8
+ tmp_json_file = os.path.join(os.path.dirname(__file__), "test.json")
9
+
10
+
11
+ class TestStreamJSON:
12
+ def test_writer_send(self):
13
+ persons = [
14
+ {"id": "0001", "first_name": "John", "last_name": "Doe"},
15
+ {"id": "0002", "first_name": "Jane", "last_name": "Doe"},
16
+ ]
17
+
18
+ with StreamJSONWriter(tmp_json_file, indent=2) as writer:
19
+ for person in persons:
20
+ writer.send(person)
21
+
22
+ with open(tmp_json_file, "r") as file:
23
+ expected = persons
24
+ result = json.load(file)
25
+
26
+ assert expected == result
27
+
28
+ def test_reader_find(self):
29
+ persons_json_file = os.path.join(os.path.dirname(__file__), "persons.json")
30
+ with StreamJSONReader(persons_json_file) as reader:
31
+ reader_generator = reader.find()
32
+ obj_1 = next(reader_generator)
33
+ obj_2 = next(reader_generator)
34
+
35
+ assert obj_1 == {
36
+ "id": "0001",
37
+ "first_name": "John",
38
+ "last_name": "Doe",
39
+ "hobbies": [
40
+ {"name": "photography"},
41
+ {"name": "programming"},
42
+ {"name": "hiking"},
43
+ ],
44
+ } and obj_2 == {
45
+ "id": "0002",
46
+ "first_name": "Jane",
47
+ "last_name": "Doe",
48
+ "hobbies": [{"name": "dancing"}],
49
+ }
50
+
51
+ @pytest.fixture(scope="session", autouse=True)
52
+ def cleanup(self):
53
+ """
54
+ Delete file after tests are finished.
55
+ """
56
+
57
+ yield
58
+ if os.path.exists(tmp_json_file):
59
+ os.remove(tmp_json_file)
@@ -1,8 +0,0 @@
1
- [metadata]
2
- description-file = README.md
3
- long_description_content_type = text/markdown
4
-
5
- [egg_info]
6
- tag_build =
7
- tag_date = 0
8
-
streamjson-1.0.2/setup.py DELETED
@@ -1,34 +0,0 @@
1
- from setuptools import setup
2
- from setuptools import find_packages
3
-
4
- name = "streamjson"
5
- version = "1.0.2"
6
-
7
- with open("README.md", "r") as fh:
8
- long_description = fh.read()
9
-
10
- requires = []
11
-
12
- setup(
13
- name=name,
14
- version=version,
15
- author="Zairon Jacobs",
16
- author_email="zaironjacobs@gmail.com",
17
- description="Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream.",
18
- long_description=long_description,
19
- url="https://github.com/zaironjacobs/streamjson",
20
- download_url=f"https://github.com/zaironjacobs/streamjson/archive/v{version}.tar.gz",
21
- keywords=["json", "stream", "write", "file", "read", "objects", "arrays"],
22
- packages=find_packages(),
23
- install_requires=requires,
24
- license="MIT",
25
- classifiers=[
26
- "Development Status :: 5 - Production/Stable",
27
- "Intended Audience :: Developers",
28
- "License :: OSI Approved :: MIT License",
29
- "Operating System :: OS Independent",
30
- "Programming Language :: Python :: 3.9",
31
- "Natural Language :: English",
32
- ],
33
- python_requires=">=3",
34
- )