From f3ddff5a0a642a8ff73264d78aea0ca38c59a17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 17 Nov 2022 18:15:56 +0100 Subject: [PATCH] No sort_keys and pretty print by default in JSON renderers Make them configurable --- README.md | 3 +++ acceptance_tests/tests/tests/test_json.py | 4 --- c2cwsgiutils/pretty_json.py | 30 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) delete mode 100644 acceptance_tests/tests/tests/test_json.py diff --git a/README.md b/README.md index 72765aab4..e172329ee 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,9 @@ accessed with a good secret, a cookie is stored and the secret can be omitted. An alternative of using `C2C_SECRET` is to use an authentication on GitHub, [create the GitHub application](https://github.com/settings/applications/new). +Configure the json renderers with the `C2C_JSON_PRETTY_PRINT` and `C2C_JSON_SORT_KEYS` environment +variables or `c2c.json.pretty_print`and `c2c.json.sort_keys` properties. Default is `false`. + Then it will redirect the user to the github authentication form if not already authenticated (using `C2C_AUTH_GITHUB_CLIENT_ID`, `C2C_AUTH_GITHUB_CLIENT_SECRET` and `C2C_AUTH_GITHUB_SCOPE`). diff --git a/acceptance_tests/tests/tests/test_json.py b/acceptance_tests/tests/tests/test_json.py deleted file mode 100644 index 0b07f182a..000000000 --- a/acceptance_tests/tests/tests/test_json.py +++ /dev/null @@ -1,4 +0,0 @@ -def test_pretty_print(app_connection): - response = app_connection.get_raw("c2c/versions.json", cors=False) - print("response=" + response.text) - assert "\n" in response.text diff --git a/c2cwsgiutils/pretty_json.py b/c2cwsgiutils/pretty_json.py index 92840db07..c0366cd4f 100644 --- a/c2cwsgiutils/pretty_json.py +++ b/c2cwsgiutils/pretty_json.py @@ -5,10 +5,24 @@ import ujson from pyramid.renderers import JSON +from c2cwsgiutils.config_utils import config_bool, env_or_config -def fast_dumps(v: Any, **_kargv: Any) -> str: + +class _FastDumps: """Dump the json fast using ujson.""" - return ujson.dumps(v, ensure_ascii=False, indent=2, sort_keys=True, escape_forward_slashes=False) + + def __init__(self, pretty_print: bool, sort_keys: bool) -> None: + self.pretty_print = pretty_print + self.sort_keys = sort_keys + + def __call__(self, v: Any, **_kargv: Any) -> str: + return ujson.dumps( + v, + ensure_ascii=False, + indent=2 if self.pretty_print else 0, + sort_keys=self.sort_keys, + escape_forward_slashes=False, + ) def init(config: pyramid.config.Configurator) -> None: @@ -19,5 +33,13 @@ def init(config: pyramid.config.Configurator) -> None: def includeme(config: pyramid.config.Configurator) -> None: """Initialize json and fast_json renderer.""" - config.add_renderer("json", JSON(indent=2, sort_keys=True)) - config.add_renderer("fast_json", JSON(serializer=fast_dumps)) + + pretty_print = config_bool( + env_or_config(config, "C2C_JSON_PRETTY_PRINT", "c2c.json.pretty_print", "false") + ) + sort_keys = config_bool(env_or_config(config, "C2C_JSON_SORT_KEYS", "c2c.json.sort_keys", "false")) + + fast_dump = _FastDumps(pretty_print, sort_keys) + + config.add_renderer("json", JSON(indent=2 if pretty_print else None, sort_keys=sort_keys)) + config.add_renderer("fast_json", JSON(serializer=fast_dump))