Skip to content

Commit

Permalink
Merge pull request #338 from jacebrowning/json5
Browse files Browse the repository at this point in the history
Add JSON5 support
  • Loading branch information
jacebrowning authored Oct 30, 2024
2 parents 0328150 + 2123a96 commit 46280de
Show file tree
Hide file tree
Showing 22 changed files with 1,038 additions and 192 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 2.3 (2024-10-30)

- Added support for the [JSON5](https://json5.org) file format.

## 2.2.3 (2024-05-26)

- Added caching to default factory calls.
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ notebooks: install
@ cd notebooks; for filename in *.ipynb; do \
poetry run papermill $$filename $$filename; \
done
git config filter.nbstripout.extrakeys 'cell.id cell.metadata.execution cell.metadata.papermill metadata.papermill'
poetry run nbstripout --keep-output notebooks/*.ipynb
poetry run nbstripout --keep-output --extra-keys="cell.metadata.papermill metadata.papermill" notebooks/*.ipynb

# RELEASE #####################################################################

Expand Down
22 changes: 18 additions & 4 deletions datafiles/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from pathlib import Path
from typing import IO, Dict, List, Union

import json5
import log
from ruamel.yaml import YAML as _YAML

from . import types, utils

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


class JSON5(Formatter):
"""Formatter for "JSON for Humans" language."""

@classmethod
def extensions(cls):
return {".json5"}

@classmethod
def deserialize(cls, file_object):
return json5.load(file_object)

@classmethod
def serialize(cls, data):
return json5.dumps(data, indent=2)


class TOML(Formatter):
"""Formatter for (round-trip) Tom's Obvious Minimal Language."""

Expand Down Expand Up @@ -83,8 +101,6 @@ def extensions(cls):

@classmethod
def deserialize(cls, file_object):
from ruamel.yaml import YAML as _YAML

yaml = _YAML()
yaml.preserve_quotes = True # type: ignore
try:
Expand All @@ -95,8 +111,6 @@ def deserialize(cls, file_object):

@classmethod
def serialize(cls, data):
from ruamel.yaml import YAML as _YAML

yaml = _YAML()
yaml.register_class(types.List)
yaml.register_class(types.Dict)
Expand Down
6 changes: 6 additions & 0 deletions datafiles/tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def with_empty_file(expect, path):
data = formats.deserialize(path, ".json")
expect(data) == {}

def describe_json5():
def with_empty_file(expect, path):
path.write_text("{}")
data = formats.deserialize(path, ".json5")
expect(data) == {}

def describe_toml():
def with_empty_file(expect, path):
data = formats.deserialize(path, ".toml")
Expand Down
2 changes: 1 addition & 1 deletion docs/api/manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyModel:
my_value: int = 0
```

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

## `get()`

Expand Down
2 changes: 1 addition & 1 deletion docs/api/mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MyModel:
>>> model = MyModel("foo")
```

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

## `path`

Expand Down
7 changes: 4 additions & 3 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ my_int: 42
my_str: Hello, world!
```
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).
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).
## JSON
The [JSON language](https://www.json.org/) is also supported.
Any of the following file extensions will use this format:
- `.json`
- `.json5`

Sample output:

Expand All @@ -56,7 +57,7 @@ Sample output:
}
```

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

## TOML

Expand All @@ -83,7 +84,7 @@ value = 2
value = 0
```

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

## Custom Formats

Expand Down
4 changes: 2 additions & 2 deletions docs/types/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from typing import List, Optional
| `foobar: List[int]` | `foobar = None` | `foobar:`<br>&nbsp;&nbsp;&nbsp;&nbsp;`-` |
| `foobar: Optional[List[int]]` | `foobar = None` | `foobar:` |

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

## Sets

Expand Down Expand Up @@ -97,4 +97,4 @@ class Nested:
qux: str
```

More examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/nested_dataclass.ipynb).
More examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/nested_dataclass.ipynb).
2 changes: 1 addition & 1 deletion docs/types/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ contents:
- 3.14
```
An example of using generic types can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/generic_types.ipynb).
An example of using generic types can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/generic_types.ipynb).
2 changes: 1 addition & 1 deletion docs/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ numbers:
- 3
```
Additional examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/file_inference.ipynb).
Additional examples can be found in [this notebook](https://github.com/jacebrowning/datafiles/blob/main/notebooks/file_inference.ipynb).
## `frozen()`

Expand Down
10 changes: 5 additions & 5 deletions notebooks/file_inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "e3794ade",
"id": "0",
"metadata": {
"tags": []
},
Expand All @@ -13,7 +13,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "e8ee27c3",
"id": "1",
"metadata": {
"tags": []
},
Expand All @@ -40,7 +40,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "418fe5d8",
"id": "2",
"metadata": {
"tags": []
},
Expand All @@ -67,7 +67,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "17241aab",
"id": "3",
"metadata": {
"tags": []
},
Expand All @@ -79,7 +79,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3c493823",
"id": "4",
"metadata": {
"tags": []
},
Expand Down
17 changes: 17 additions & 0 deletions notebooks/files/format_options.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"my_dict": {
"value": 0
},
"my_list": [
{
"value": 1
},
{
"value": 2
}
],
"my_bool": true,
"my_float": 1.23,
"my_int": 42,
"my_str": "Hello, world!"
}
Loading

0 comments on commit 46280de

Please sign in to comment.