mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use the msgpack ext type for a more compact representation of custom types
This commit is contained in:
parent
fb4ea11ce2
commit
c5dd2977eb
@ -16,28 +16,31 @@ MSGPACK_MIME = 'application/x-msgpack'
|
|||||||
CANARY = 'jPoAv3zOyHvQ5JFNYg4hJ9'
|
CANARY = 'jPoAv3zOyHvQ5JFNYg4hJ9'
|
||||||
|
|
||||||
|
|
||||||
def encoded(typ, data):
|
def encoded(typ, data, for_json):
|
||||||
|
if for_json:
|
||||||
return {CANARY: typ, 'v': data}
|
return {CANARY: typ, 'v': data}
|
||||||
|
return msgpack.ExtType(typ, msgpack_dumps(data))
|
||||||
|
|
||||||
|
|
||||||
def encoder(obj, for_json=False):
|
def encoder(obj, for_json=False):
|
||||||
if isinstance(obj, datetime):
|
if isinstance(obj, datetime):
|
||||||
return encoded(0, unicode(obj.isoformat()))
|
return encoded(0, unicode(obj.isoformat()), for_json)
|
||||||
if isinstance(obj, (set, frozenset)):
|
if isinstance(obj, (set, frozenset)):
|
||||||
return encoded(1, tuple(obj))
|
return encoded(1, tuple(obj), for_json)
|
||||||
if hasattr(obj, '__calibre_serializable__'):
|
if hasattr(obj, '__calibre_serializable__'):
|
||||||
from calibre.ebooks.metadata.book.base import Metadata
|
from calibre.ebooks.metadata.book.base import Metadata
|
||||||
from calibre.library.field_metadata import FieldMetadata, fm_as_dict
|
from calibre.library.field_metadata import FieldMetadata, fm_as_dict
|
||||||
if isinstance(obj, Metadata):
|
if isinstance(obj, Metadata):
|
||||||
from calibre.ebooks.metadata.book.serialize import metadata_as_dict
|
from calibre.ebooks.metadata.book.serialize import metadata_as_dict
|
||||||
return encoded(2, metadata_as_dict(obj, encode_cover_data=for_json))
|
return encoded(
|
||||||
|
2, metadata_as_dict(obj, encode_cover_data=for_json), for_json
|
||||||
|
)
|
||||||
elif isinstance(obj, FieldMetadata):
|
elif isinstance(obj, FieldMetadata):
|
||||||
return encoded(3, fm_as_dict(obj))
|
return encoded(3, fm_as_dict(obj), for_json)
|
||||||
raise TypeError('Cannot serialize objects of type {}'.format(type(obj)))
|
raise TypeError('Cannot serialize objects of type {}'.format(type(obj)))
|
||||||
|
|
||||||
|
|
||||||
def msgpack_dumps(data):
|
msgpack_dumps = partial(msgpack.packb, default=encoder, use_bin_type=True)
|
||||||
return msgpack.packb(data, use_bin_type=True, default=encoder)
|
|
||||||
|
|
||||||
|
|
||||||
def json_dumps(data, **kw):
|
def json_dumps(data, **kw):
|
||||||
@ -65,23 +68,24 @@ def decode_field_metadata(x, for_json):
|
|||||||
|
|
||||||
|
|
||||||
decoders = (
|
decoders = (
|
||||||
lambda x, fj: parse_iso8601(x, assume_utc=True),
|
lambda x, fj: parse_iso8601(x, assume_utc=True), lambda x, fj: set(x),
|
||||||
lambda x, fj: set(x),
|
decode_metadata, decode_field_metadata,
|
||||||
decode_metadata,
|
|
||||||
decode_field_metadata,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def decoder(obj, for_json=False):
|
def json_decoder(obj):
|
||||||
typ = obj.get(CANARY)
|
typ = obj.get(CANARY)
|
||||||
if typ is None:
|
if typ is None:
|
||||||
return obj
|
return obj
|
||||||
return decoders[typ](obj['v'], for_json)
|
return decoders[typ](obj['v'], True)
|
||||||
|
|
||||||
|
|
||||||
def msgpack_loads(data):
|
def msgpack_decoder(code, data):
|
||||||
return msgpack.unpackb(data, encoding='utf-8', object_hook=decoder)
|
return decoders[code](msgpack_loads(data), False)
|
||||||
|
|
||||||
|
|
||||||
|
msgpack_loads = partial(msgpack.unpackb, encoding='utf-8', ext_hook=msgpack_decoder)
|
||||||
|
|
||||||
|
|
||||||
def json_loads(data):
|
def json_loads(data):
|
||||||
return json.loads(data, object_hook=partial(decoder, for_json=True))
|
return json.loads(data, object_hook=json_decoder)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user