Skip to content

Commit 46280de

Browse files
authored
Merge pull request #338 from jacebrowning/json5
Add JSON5 support
2 parents 0328150 + 2123a96 commit 46280de

22 files changed

+1038
-192
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## 2.3 (2024-10-30)
4+
5+
- Added support for the [JSON5](https://json5.org) file format.
6+
37
## 2.2.3 (2024-05-26)
48

59
- Added caching to default factory calls.

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ notebooks: install
127127
@ cd notebooks; for filename in *.ipynb; do \
128128
poetry run papermill $$filename $$filename; \
129129
done
130-
git config filter.nbstripout.extrakeys 'cell.id cell.metadata.execution cell.metadata.papermill metadata.papermill'
131-
poetry run nbstripout --keep-output notebooks/*.ipynb
130+
poetry run nbstripout --keep-output --extra-keys="cell.metadata.papermill metadata.papermill" notebooks/*.ipynb
132131

133132
# RELEASE #####################################################################
134133

datafiles/formats.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from pathlib import Path
88
from typing import IO, Dict, List, Union
99

10+
import json5
1011
import log
12+
from ruamel.yaml import YAML as _YAML
1113

1214
from . import types, utils
1315

@@ -54,6 +56,22 @@ def serialize(cls, data):
5456
return json.dumps(data, indent=2)
5557

5658

59+
class JSON5(Formatter):
60+
"""Formatter for "JSON for Humans" language."""
61+
62+
@classmethod
63+
def extensions(cls):
64+
return {".json5"}
65+
66+
@classmethod
67+
def deserialize(cls, file_object):
68+
return json5.load(file_object)
69+
70+
@classmethod
71+
def serialize(cls, data):
72+
return json5.dumps(data, indent=2)
73+
74+
5775
class TOML(Formatter):
5876
"""Formatter for (round-trip) Tom's Obvious Minimal Language."""
5977

@@ -83,8 +101,6 @@ def extensions(cls):
83101

84102
@classmethod
85103
def deserialize(cls, file_object):
86-
from ruamel.yaml import YAML as _YAML
87-
88104
yaml = _YAML()
89105
yaml.preserve_quotes = True # type: ignore
90106
try:
@@ -95,8 +111,6 @@ def deserialize(cls, file_object):
95111

96112
@classmethod
97113
def serialize(cls, data):
98-
from ruamel.yaml import YAML as _YAML
99-
100114
yaml = _YAML()
101115
yaml.register_class(types.List)
102116
yaml.register_class(types.Dict)

datafiles/tests/test_formats.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def with_empty_file(expect, path):
5959
data = formats.deserialize(path, ".json")
6060
expect(data) == {}
6161

62+
def describe_json5():
63+
def with_empty_file(expect, path):
64+
path.write_text("{}")
65+
data = formats.deserialize(path, ".json5")
66+
expect(data) == {}
67+
6268
def describe_toml():
6369
def with_empty_file(expect, path):
6470
data = formats.deserialize(path, ".toml")

docs/api/manager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MyModel:
1212
my_value: int = 0
1313
```
1414

15-
Many of the following examples are also shown in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/manager_api.ipynb).
15+
Many of the following examples are also shown in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/manager_api.ipynb).
1616

1717
## `get()`
1818

docs/api/mapper.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MyModel:
1616
>>> model = MyModel("foo")
1717
```
1818

19-
Many of the following examples are also shown in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/mapper_api.ipynb).
19+
Many of the following examples are also shown in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/mapper_api.ipynb).
2020

2121
## `path`
2222

docs/formats.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ my_int: 42
2525
my_str: Hello, world!
2626
```
2727
28-
Where possible, comments and whitespace are preserved in files as shown in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/roundtrip_comments.ipynb).
28+
Where possible, comments and whitespace are preserved in files as shown in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/roundtrip_comments.ipynb).
2929
3030
## JSON
3131
3232
The [JSON language](https://www.json.org/) is also supported.
3333
Any of the following file extensions will use this format:
3434
3535
- `.json`
36+
- `.json5`
3637

3738
Sample output:
3839

@@ -56,7 +57,7 @@ Sample output:
5657
}
5758
```
5859

59-
Additional examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/format_options.ipynb).
60+
Additional examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/format_options.ipynb).
6061

6162
## TOML
6263

@@ -83,7 +84,7 @@ value = 2
8384
value = 0
8485
```
8586

86-
Additional examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/format_options.ipynb).
87+
Additional examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/format_options.ipynb).
8788

8889
## Custom Formats
8990

docs/types/containers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from typing import List, Optional
1717
| `foobar: List[int]` | `foobar = None` | `foobar:`<br>&nbsp;&nbsp;&nbsp;&nbsp;`-` |
1818
| `foobar: Optional[List[int]]` | `foobar = None` | `foobar:` |
1919

20-
More examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/patched_containers.ipynb).
20+
More examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/patched_containers.ipynb).
2121

2222
## Sets
2323

@@ -97,4 +97,4 @@ class Nested:
9797
qux: str
9898
```
9999

100-
More examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/nested_dataclass.ipynb).
100+
More examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/nested_dataclass.ipynb).

docs/types/generics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ contents:
6161
- 3.14
6262
```
6363
64-
An example of using generic types can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/generic_types.ipynb).
64+
An example of using generic types can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/generic_types.ipynb).

docs/utilities.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ numbers:
4242
- 3
4343
```
4444
45-
Additional examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/file_inference.ipynb).
45+
Additional examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/file_inference.ipynb).
4646
4747
## `frozen()`
4848

0 commit comments

Comments
 (0)