add 'output.num-to-str' option

... to convert any numeric values to string when outputting them as JSON
(during '--dump-json' or otherwise)
This commit is contained in:
Mike Fährmann
2018-10-08 20:28:54 +02:00
parent af3f81c7d9
commit 48a8717a7c
2 changed files with 24 additions and 3 deletions

View File

@@ -57,7 +57,7 @@ def raises(obj):
def combine_dict(a, b):
"""Recursively combine the contents of b into a"""
"""Recursively combine the contents of 'b' into 'a'"""
for key, value in b.items():
if key in a and isinstance(value, dict) and isinstance(a[key], dict):
combine_dict(a[key], value)
@@ -66,6 +66,20 @@ def combine_dict(a, b):
return a
def transform_dict(a, func):
"""Recursively apply 'func' to all values in 'a'"""
for key, value in a.items():
if isinstance(value, dict):
transform_dict(value, func)
else:
a[key] = func(value)
def number_to_string(value):
"""Convert numbers (int, float) to string; Return everything else as is."""
return str(value) if isinstance(value, (int, float)) else value
def expand_path(path):
"""Expand environment variables and tildes (~)"""
if not path: