From 74cb3d2c5405a1d01571c327c2aa98df50bea9b8 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 3 Sep 2025 18:14:26 +0900 Subject: [PATCH] dump_data and new json_dumps dump_data adds flag to dump without indent json_dumps is dump_data like, but will be geared towards secure dump of dict to json for strage --- src/corelibs/debug_handling/dump_data.py | 5 +++-- src/corelibs/json_handling/json_helper.py | 14 +++++++++++++- uv.lock | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/corelibs/debug_handling/dump_data.py b/src/corelibs/debug_handling/dump_data.py index 7be7892..cc769cd 100644 --- a/src/corelibs/debug_handling/dump_data.py +++ b/src/corelibs/debug_handling/dump_data.py @@ -6,7 +6,7 @@ import json from typing import Any -def dump_data(data: Any) -> str: +def dump_data(data: Any, use_indent: bool = True) -> str: """ dump formated output from dict/list @@ -16,6 +16,7 @@ def dump_data(data: Any) -> str: Returns: str: _description_ """ - return json.dumps(data, indent=4, ensure_ascii=False, default=str) + indent = 4 if use_indent else None + return json.dumps(data, indent=indent, ensure_ascii=False, default=str) # __END__ diff --git a/src/corelibs/json_handling/json_helper.py b/src/corelibs/json_handling/json_helper.py index 4db2ff0..4da8ecc 100644 --- a/src/corelibs/json_handling/json_helper.py +++ b/src/corelibs/json_handling/json_helper.py @@ -3,7 +3,7 @@ json encoder for datetime """ from typing import Any -from json import JSONEncoder +from json import JSONEncoder, dumps from datetime import datetime, date @@ -28,4 +28,16 @@ def default(obj: Any) -> str | None: return obj.isoformat() return None +def json_dumps(data: Any): + """ + wrapper for json.dumps with sure dump without throwing Exceptions + + Arguments: + data {Any} -- _description_ + + Returns: + _type_ -- _description_ + """ + return dumps(data, ensure_ascii=False, default=str) + # __END__ diff --git a/uv.lock b/uv.lock index 3e4e1eb..f0a03e7 100644 --- a/uv.lock +++ b/uv.lock @@ -53,7 +53,7 @@ wheels = [ [[package]] name = "corelibs" -version = "0.22.5" +version = "0.22.6" source = { editable = "." } dependencies = [ { name = "jmespath" },