Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/serializers.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import base64 | import base64 | ||||
import datetime | import datetime | ||||
import json | import json | ||||
import types | import types | ||||
from uuid import UUID | from uuid import UUID | ||||
import arrow | import arrow | ||||
import dateutil.parser | import iso8601 | ||||
import msgpack | import msgpack | ||||
from typing import Any, Dict, Union, Tuple | from typing import Any, Dict, Union, Tuple | ||||
from requests import Response | from requests import Response | ||||
ENCODERS = [ | ENCODERS = [ | ||||
(arrow.Arrow, 'arrow', arrow.Arrow.isoformat), | (arrow.Arrow, 'arrow', arrow.Arrow.isoformat), | ||||
(datetime.datetime, 'datetime', datetime.datetime.isoformat), | (datetime.datetime, 'datetime', datetime.datetime.isoformat), | ||||
(datetime.timedelta, 'timedelta', lambda o: { | (datetime.timedelta, 'timedelta', lambda o: { | ||||
'days': o.days, | 'days': o.days, | ||||
'seconds': o.seconds, | 'seconds': o.seconds, | ||||
'microseconds': o.microseconds, | 'microseconds': o.microseconds, | ||||
}), | }), | ||||
(UUID, 'uuid', str), | (UUID, 'uuid', str), | ||||
# Only for JSON: | # Only for JSON: | ||||
(bytes, 'bytes', lambda o: base64.b85encode(o).decode('ascii')), | (bytes, 'bytes', lambda o: base64.b85encode(o).decode('ascii')), | ||||
] | ] | ||||
DECODERS = { | DECODERS = { | ||||
'arrow': arrow.get, | 'arrow': arrow.get, | ||||
'datetime': dateutil.parser.parse, | 'datetime': lambda d: iso8601.parse_date(d, default_timezone=None), | ||||
'timedelta': lambda d: datetime.timedelta(**d), | 'timedelta': lambda d: datetime.timedelta(**d), | ||||
'uuid': UUID, | 'uuid': UUID, | ||||
# Only for JSON: | # Only for JSON: | ||||
'bytes': base64.b85decode, | 'bytes': base64.b85decode, | ||||
} | } | ||||
▲ Show 20 Lines • Show All 170 Lines • Show Last 20 Lines |