mirror of
https://github.com/MODSetter/SurfSense.git
synced 2025-09-01 18:19:08 +00:00
27 lines
No EOL
755 B
Python
27 lines
No EOL
755 B
Python
from collections import OrderedDict
|
|
|
|
try:
|
|
from collections.abc import Mapping, Sequence
|
|
except ImportError:
|
|
from collections import Mapping, Sequence
|
|
|
|
import json
|
|
|
|
COMPACT_SEPARATORS = (',', ':')
|
|
|
|
def order_by_key(kv):
|
|
key, val = kv
|
|
return key
|
|
|
|
def recursive_order(node):
|
|
if isinstance(node, Mapping):
|
|
ordered_mapping = OrderedDict(sorted(node.items(), key=order_by_key))
|
|
for key, value in ordered_mapping.items():
|
|
ordered_mapping[key] = recursive_order(value)
|
|
return ordered_mapping
|
|
elif isinstance(node, Sequence) and not isinstance(node, (str, bytes)):
|
|
return [recursive_order(item) for item in node]
|
|
return node
|
|
|
|
def stringify(node):
|
|
return json.dumps(recursive_order(node), separators=COMPACT_SEPARATORS) |