36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
import json
|
|
from typing import Any
|
|
|
|
|
|
def varsify(item) -> Any:
|
|
result = {}
|
|
try:
|
|
if isinstance(item, (str, int, float, bool)):
|
|
return item
|
|
elif isinstance(item, (list, set)):
|
|
l_result = []
|
|
for i, x in enumerate(item):
|
|
l_result.append(varsify(x))
|
|
return l_result
|
|
else:
|
|
for k, v in vars(item).items():
|
|
if isinstance(v, dict):
|
|
result[k] = varsify(v)
|
|
elif isinstance(v, list):
|
|
result[k] = []
|
|
for i, x in enumerate(v):
|
|
result[k].insert(i, varsify(x))
|
|
else:
|
|
if not k.startswith('_'):
|
|
result[k] = varsify(v)
|
|
return result
|
|
except:
|
|
return item
|
|
|
|
|
|
def jsonify_anything(item, pretty: bool = False):
|
|
if pretty:
|
|
return json.dumps(varsify(item), indent=4, sort_keys=True, default=str)
|
|
else:
|
|
return json.dumps(varsify(item), separators=(',', ':'), sort_keys=True, default=str)
|