|
1
|
|
|
import collections |
|
|
|
|
|
|
2
|
|
|
from functools import partial |
|
3
|
|
|
from itertools import chain |
|
4
|
|
|
from types import MappingProxyType |
|
5
|
|
|
from typing import Callable |
|
|
|
|
|
|
6
|
|
|
from uuid import UUID |
|
7
|
|
|
|
|
8
|
|
|
import iso8601 |
|
|
|
|
|
|
9
|
|
|
from foil.parsers import parse_iso_date as _parse_iso_date |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def parse_uuid(value): |
|
|
|
|
|
|
13
|
|
|
try: |
|
14
|
|
|
value = UUID(value, version=4) |
|
15
|
|
|
except ValueError: |
|
16
|
|
|
pass |
|
17
|
|
|
|
|
18
|
|
|
return value |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def parse_iso_date(value): |
|
|
|
|
|
|
22
|
|
|
try: |
|
23
|
|
|
value = _parse_iso_date(value) |
|
24
|
|
|
except AttributeError: |
|
25
|
|
|
pass |
|
26
|
|
|
|
|
27
|
|
|
return value |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def parse_iso_datetime(value): |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
# Prevent iso8601 over-zealous datetime parsing |
|
33
|
|
|
if '-' in value and ':' in value: |
|
34
|
|
|
try: |
|
35
|
|
|
value = iso8601.parse_date(value) |
|
36
|
|
|
except iso8601.ParseError: |
|
37
|
|
|
pass |
|
38
|
|
|
|
|
39
|
|
|
return value |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
STRING_DECODERS = (parse_uuid, parse_iso_date, parse_iso_datetime) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def json_decoder_hook(dct, str_decoders=STRING_DECODERS, |
|
46
|
|
|
converters=MappingProxyType(dict())) -> dict: |
|
47
|
|
|
"""Decoder for parsing typical objects like uuid's and dates.""" |
|
48
|
|
|
|
|
49
|
|
|
for k, v in dct.items(): |
|
50
|
|
|
if k in converters: |
|
51
|
|
|
parse_func = converters[k] |
|
52
|
|
|
dct[k] = parse_func(v) |
|
53
|
|
|
|
|
54
|
|
|
elif isinstance(v, str): |
|
55
|
|
|
for decode_func in str_decoders: |
|
56
|
|
|
v = decode_func(v) |
|
57
|
|
|
|
|
58
|
|
|
if not isinstance(v, str): |
|
59
|
|
|
break |
|
60
|
|
|
|
|
61
|
|
|
dct[k] = v |
|
62
|
|
|
elif isinstance(v, collections.Mapping): |
|
63
|
|
|
dct[k] = json_decoder_hook(v, str_decoders, converters) |
|
64
|
|
|
|
|
65
|
|
|
return dct |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def make_json_decoder_hook(str_decoders=STRING_DECODERS, |
|
69
|
|
|
extra_str_decoders=tuple(), |
|
70
|
|
|
converters=MappingProxyType(dict())) -> Callable: |
|
71
|
|
|
"""Customize JSON string decoder hooks. |
|
72
|
|
|
|
|
73
|
|
|
Object hook for typical deserialization scenarios. |
|
74
|
|
|
|
|
75
|
|
|
Notes |
|
76
|
|
|
----- |
|
77
|
|
|
Specifying a field in converters will ensure custom decoding/passthrough. |
|
78
|
|
|
|
|
79
|
|
|
Parameters |
|
80
|
|
|
---------- |
|
81
|
|
|
str_decoders: functions for decoding strings to objects. |
|
82
|
|
|
extra_str_decoders: appends additional string decoders to str_decoders. |
|
83
|
|
|
converters: field / parser function mapping. |
|
84
|
|
|
""" |
|
85
|
|
|
|
|
86
|
|
|
str_decoders = tuple(chain(str_decoders, extra_str_decoders)) |
|
87
|
|
|
object_hook = partial(json_decoder_hook, str_decoders=str_decoders, |
|
88
|
|
|
converters=converters) |
|
89
|
|
|
|
|
90
|
|
|
return object_hook |
|
91
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.