feat(json): change default indent from 2 to 4

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.
This commit is contained in:
D. Bohdan 2024-02-17 20:56:39 +00:00
parent a8d85d7749
commit 3b4e808afb
10 changed files with 73 additions and 71 deletions

View File

@ -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"
"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": "中国"
}
},
"clients": {
"data": [
[
"gamma",
"delta"
],
[
1,
2
]
],
"hosts": [
"alpha",
"omega"
]
},
"products": [
{
"name": "Hammer",
"sku": 738594937
},
{
"name": "Nail",
"sku": 284758393,
"color": "gray"
}
]
},
"products": [
{
"name": "Hammer",
"sku": 738594937
},
{
"name": "Nail",
"sku": 284758393,
"color": "gray"
}
]
}

View File

@ -56,6 +56,7 @@ class YAMLOptions:
__all__ = [
"DEFAULT_MAX_VALUES",
"FORMATS",
"JSON_INDENT_TRUE",
"RICH_ARGPARSE_STYLES",
"Document",
"TooManyValuesError",
@ -75,6 +76,7 @@ CLI_DEFAULTS: dict[str, Any] = {
}
DEFAULT_MAX_VALUES = 1000000
FORMATS = ["cbor", "json", "msgpack", "toml", "yaml"]
JSON_INDENT_TRUE = 4
UTF_8 = "utf-8"
RICH_ARGPARSE_STYLES: dict[str, StyleType] = {
@ -547,7 +549,7 @@ def _encode_json(
stringify: bool,
) -> str:
if indent is True:
indent = 2
indent = JSON_INDENT_TRUE
separators = (",", ": " if indent else ":")
@ -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,
@ -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,

View File

@ -1,3 +1,3 @@
{
"foo": "2012-12-12"
"foo": "2012-12-12"
}

View File

@ -1,3 +1,3 @@
{
"foo": "2012-12-12T12:34:56"
"foo": "2012-12-12T12:34:56"
}

View File

@ -1,3 +1,3 @@
{
"foo": "2012-12-12T12:34:56+00:00"
"foo": "2012-12-12T12:34:56+00:00"
}

View File

@ -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"
}
}

View File

@ -1,5 +1,5 @@
{
"foo": 1,
"bar": 2,
"baz": 3
"foo": 1,
"bar": 2,
"baz": 3
}

View File

@ -1,5 +1,5 @@
{
"bar": 2,
"baz": 3,
"foo": 1
"bar": 2,
"baz": 3,
"foo": 1
}

View File

@ -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,

View File

@ -1,3 +1,3 @@
{
"foo": "12:34:56"
"foo": "12:34:56"
}