def _dump(obj, indent):
if isinstance(obj, Mapping):
new_indent = indent + ' '
items = []
for k, v in sorted(obj.items()):
items.append(
'\n' + new_indent + "'" + k + "': " + _dump(v, new_indent))
return '{' + ','.join(items) + '}'
elif isinstance(obj, bool):
return 'true' if obj else 'false'
elif isinstance(obj, list):
new_indent = indent + ' '
b = ','.join('\n' + new_indent + _dump(v, new_indent) for v in obj)
return '[' + b + ']'
elif isinstance(obj, (int, float, Decimal)):
return str(obj)
elif obj is None:
return 'null'
elif isinstance(obj, str):
quote = LONG_QUOTE if '\n' in obj or '\r' in obj else SHORT_QUOTE
return quote + obj + quote
elif isinstance(obj, bytearray):
return '{{ ' + b64encode(obj).decode() + ' }}'
elif isinstance(obj, bytes):
return "{{ '" + obj.decode() + "' }}"
elif isinstance(obj, Datetime):
if obj.tzinfo is None or obj.tzinfo == Timezone.utc:
fmt = UTC_FORMAT
else:
fmt = TZ_FORMAT
return obj.strftime(fmt)
else:
raise PionException("Type " + str(type(obj)) + " not recognised.")
评论列表
文章目录