Skip to content

Commit 5216dda

Browse files
committed
**Changed:** Documentation - Update the examples with json.dump and json.load by passing additional keyword arguments.
1 parent 62026e2 commit 5216dda

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Releases
22

3+
- **Changed:** Documentation - Update the examples with `json.dump` and `json.load` by passing additional keyword arguments.
34
- **Changed:** Documentation - Add UTF-8 headers to the Python code snippets shown.
45
- **Changed:** Enable more `ruff` rules and pass all checks.
56
- **Changed:** Docstrings - Enable all `sphinx-linter` rules and pass all checks.

docs/dump.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Dump an iterable to a JSON Lines file incrementally.
22

3-
Dump JSON Lines (jsonl) files incrementally, supporting both uncompressed and compressed formats and allowing
3+
Dump JSON Lines **(jsonl)** files incrementally, supporting both uncompressed and compressed formats and allowing
44
custom serialization and opener callbacks.
55

66
#### Dump the data to an uncompressed file at the specified path
@@ -133,6 +133,8 @@ jsonl.dump(data, MyCustomFile2(), text_mode=True)
133133

134134
#### Dump data with a custom serialization
135135

136+
##### Passing a `json_dumps` function
137+
136138
The `json_dumps` parameter allows for custom serialization and must take a Python object and return a
137139
JSON-formatted string.
138140

@@ -161,8 +163,27 @@ jsonl.dump(data, "foo.jsonl", json_dumps=ujson.dumps, ensure_ascii=False)
161163

162164
# Dump the data using the orjson library.
163165
jsonl.dump(data, "var.jsonl", json_dumps=orjson.dumps)
164-
165-
# Dumping data into a JSON file with compact output using separators (',', ':')
166-
# to remove unnecessary whitespaces.
167-
jsonl.dump(data, "bar.jsonl", separators=(',', ':'))
168166
```
167+
168+
##### Passing additional keyword arguments
169+
170+
The `jsonl.dump` function accepts additional keyword arguments that are passed directly to the underlying
171+
serialization function (by default, `json.dumps`). This allows for customization of the serialization process,
172+
such as controlling indentation, sorting keys, or handling special data types.
173+
174+
Here’s an example that demonstrates how to use additional keyword arguments to customize the JSON serialization:
175+
176+
```python
177+
# -*- coding: utf-8 -*-
178+
179+
import json
180+
181+
import jsonl
182+
183+
data = [
184+
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]},
185+
{"name": "May", "wins": []},
186+
]
187+
# Dump the data with compact separators to minimize file size.
188+
jsonl.dump(data, "compact.jsonl", separators=(',', ':'))
189+
```

docs/load.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Load JSON Lines files
1+
# Load JSON Lines files
22

33
Load JSON Lines **(jsonl)** files incrementally, supporting both uncompressed and compressed formats, handling broken
44
lines, and allowing custom deserialization and opener callbacks.
@@ -45,7 +45,7 @@ Check [note](#note-compression) for more details
4545

4646
import jsonl
4747

48-
path = "file.jsonl.gz" # gzip compressed file, but it can be ".bz2" or ".xz"
48+
path = "file.jsonl.gz" # gzip compressed file, but it can be ".bz2" or ".xz"
4949

5050
# Example data to save in the file
5151
data = [
@@ -90,7 +90,7 @@ with open(path) as fp:
9090

9191
#### Load from a URL
9292

93-
You can load a JSON Lines directly from a URL incrementally, if needed you can also create custom
93+
You can load a JSON Lines directly from a URL incrementally, if needed you can also create custom
9494
requests using `urllib.request.Request`.
9595

9696
```python
@@ -140,6 +140,8 @@ WARNING:root:Broken line at 2: Expecting ',' delimiter: line 2 column 1 (char 28
140140

141141
#### Load a file using a custom deserialization
142142

143+
##### Passing a `json_loads` function
144+
143145
The `json_loads` parameter allows for custom deserialization and must take a JSON-formatted
144146
string as input and return a Python object.
145147

@@ -185,6 +187,37 @@ print(tuple(iterator1))
185187
print(tuple(iterator2))
186188
```
187189

190+
##### Passing additional keyword arguments
191+
192+
The `jsonl.load` function also accepts additional keyword arguments that are passed to the underlying
193+
JSON deserialization function (by default, `json.loads`). This is useful when you want to customize the behavior of the
194+
deserialization
195+
196+
Here’s an example using the built-in `json` module to parse float values as `decimal.Decimal`:
197+
198+
```python
199+
# -*- coding: utf-8 -*-
200+
201+
import decimal
202+
203+
import jsonl
204+
205+
path = "file.jsonl"
206+
207+
# Example data to save in the file with `float` values
208+
data = [
209+
{"name": "Gilbert", "wins_avg": 2.5},
210+
{"name": "May", "wins_avg": 3.75},
211+
]
212+
213+
# Save the data to the jsonl file
214+
jsonl.dump(data, path)
215+
216+
# Load the data back, parsing `float` values as `decimal.Decimal` using the `parse_float` keyword argument
217+
iterator = jsonl.load(path, parse_float=decimal.Decimal)
218+
print(tuple(iterator))
219+
```
220+
188221
#### Load a file using a custom opener
189222

190223
The `opener` parameter allows loading files from custom sources, such as a ZIP archive. Here’s how to use it:

0 commit comments

Comments
 (0)