mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-04-28 14:32:58 +02:00
py3: Allow serializing utf-8 bytestrings to JSON
This matches behavior of py2
This commit is contained in:
parent
b23ba9dbdb
commit
dcb128eb8b
2 changed files with 4 additions and 0 deletions
|
|
@ -203,6 +203,8 @@ def to_json(obj):
|
|||
'__value__': isoformat(obj, as_utc=True)}
|
||||
if isinstance(obj, (set, frozenset)):
|
||||
return {'__class__': 'set', '__value__': tuple(obj)}
|
||||
if isinstance(obj, bytes):
|
||||
return obj.decode('utf-8')
|
||||
if hasattr(obj, 'toBase64'): # QByteArray
|
||||
return {'__class__': 'bytearray',
|
||||
'__value__': bytes(obj.toBase64()).decode('ascii')}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,8 @@ def encoder(obj):
|
|||
return encoded(3, fm_as_dict(obj), ExtType)
|
||||
elif isinstance(obj, Tag):
|
||||
return encoded(4, obj.as_dict(), ExtType)
|
||||
if for_json and isinstance(obj, bytes):
|
||||
return obj.decode('utf-8')
|
||||
raise TypeError('Cannot serialize objects of type {}'.format(type(obj)))
|
||||
|
||||
return encoder
|
||||
|
|
|
|||
Loading…
Reference in a new issue