Skip to content

Commit

Permalink
feat(json): change default indent from 2 to 4
Browse files Browse the repository at this point in the history
This is not a breaking change for user code
that follows current type annotations.

Allow boolean `json_indent` in public functions.
Now this default is part of the API.
  • Loading branch information
dbohdan committed Feb 17, 2024
1 parent a8d85d7 commit 3b4e808
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 71 deletions.
106 changes: 53 additions & 53 deletions example.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
{
"title": "TOML Example",
"owner": {
"name": "Tom Preston-Werner",
"organization": "GitHub",
"bio": "GitHub Cofounder & CEO\nLikes tater tots and beer.",
"dob": "1979-05-27T07:32:00+00:00"
},
"database": {
"server": "192.168.1.1",
"ports": [
8001,
8001,
8002
],
"connection_max": 5000,
"enabled": true
},
"servers": {
"alpha": {
"ip": "10.0.0.1",
"dc": "eqdc10"
"title": "TOML Example",
"owner": {
"name": "Tom Preston-Werner",
"organization": "GitHub",
"bio": "GitHub Cofounder & CEO\nLikes tater tots and beer.",
"dob": "1979-05-27T07:32:00+00:00"
},
"beta": {
"ip": "10.0.0.2",
"dc": "eqdc10",
"country": "中国"
}
},
"clients": {
"data": [
[
"gamma",
"delta"
],
[
1,
2
]
],
"hosts": [
"alpha",
"omega"
]
},
"products": [
{
"name": "Hammer",
"sku": 738594937
"database": {
"server": "192.168.1.1",
"ports": [
8001,
8001,
8002
],
"connection_max": 5000,
"enabled": true
},
"servers": {
"alpha": {
"ip": "10.0.0.1",
"dc": "eqdc10"
},
"beta": {
"ip": "10.0.0.2",
"dc": "eqdc10",
"country": "中国"
}
},
{
"name": "Nail",
"sku": 284758393,
"color": "gray"
}
]
"clients": {
"data": [
[
"gamma",
"delta"
],
[
1,
2
]
],
"hosts": [
"alpha",
"omega"
]
},
"products": [
{
"name": "Hammer",
"sku": 738594937
},
{
"name": "Nail",
"sku": 284758393,
"color": "gray"
}
]
}
8 changes: 5 additions & 3 deletions src/remarshal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class YAMLOptions:
__all__ = [
"DEFAULT_MAX_VALUES",
"FORMATS",
"JSON_INDENT_TRUE",
"RICH_ARGPARSE_STYLES",
"Document",
"TooManyValuesError",
Expand All @@ -75,6 +76,7 @@ class YAMLOptions:
}
DEFAULT_MAX_VALUES = 1000000
FORMATS = ["cbor", "json", "msgpack", "toml", "yaml"]
JSON_INDENT_TRUE = 4
UTF_8 = "utf-8"

RICH_ARGPARSE_STYLES: dict[str, StyleType] = {
Expand Down Expand Up @@ -547,7 +549,7 @@ def _encode_json(
stringify: bool,
) -> str:
if indent is True:
indent = 2
indent = JSON_INDENT_TRUE

separators = (",", ": " if indent else ":")

Expand Down Expand Up @@ -668,7 +670,7 @@ def encode(
output_format: str,
data: Document,
*,
json_indent: int | None,
json_indent: bool | int | None,
sort_keys: bool,
stringify: bool,
yaml_options: YAMLOptions,
Expand Down Expand Up @@ -712,7 +714,7 @@ def remarshal(
input: Path | str,
output: Path | str,
*,
json_indent: int | None = None,
json_indent: bool | int | None = None,
max_values: int = DEFAULT_MAX_VALUES,
sort_keys: bool = True,
stringify: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion tests/date.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"foo": "2012-12-12"
"foo": "2012-12-12"
}
2 changes: 1 addition & 1 deletion tests/datetime-local.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"foo": "2012-12-12T12:34:56"
"foo": "2012-12-12T12:34:56"
}
2 changes: 1 addition & 1 deletion tests/datetime-tz.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"foo": "2012-12-12T12:34:56+00:00"
"foo": "2012-12-12T12:34:56+00:00"
}
8 changes: 4 additions & 4 deletions tests/long-line.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"balanceHeld": {
"description": "Balance of the account held for transferring to other users (in cents) and then a bunch more text after that",
"type": "integer"
}
"balanceHeld": {
"description": "Balance of the account held for transferring to other users (in cents) and then a bunch more text after that",
"type": "integer"
}
}
6 changes: 3 additions & 3 deletions tests/order.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"foo": 1,
"bar": 2,
"baz": 3
"foo": 1,
"bar": 2,
"baz": 3
}
6 changes: 3 additions & 3 deletions tests/sorted.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"bar": 2,
"baz": 3,
"foo": 1
"bar": 2,
"baz": 3,
"foo": 1
}
2 changes: 1 addition & 1 deletion tests/test_remarshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _convert_and_read(
output_format: str,
*,
output_filename: str,
json_indent: int | None = 2,
json_indent: bool | int | None = True,
sort_keys: bool = False,
stringify: bool = False,
transform: Callable[[remarshal.Document], remarshal.Document] | None = None,
Expand Down
2 changes: 1 addition & 1 deletion tests/time.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"foo": "12:34:56"
"foo": "12:34:56"
}

0 comments on commit 3b4e808

Please sign in to comment.