python-benedict/benedict/core/dump.py

22 lines
521 B
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from benedict.serializers import JSONSerializer
2020-02-06 14:18:01 +00:00
def dump(obj, **kwargs):
serializer = JSONSerializer()
2020-02-06 14:18:01 +00:00
options = {
'indent': 4,
'sort_keys': True,
}
options.update(**kwargs)
try:
output = serializer.encode(obj, **options)
return output
except TypeError as error:
sort_keys = options.pop('sort_keys', False)
if sort_keys:
output = serializer.encode(obj, **options)
return output
raise error