|
1 | 1 | # Code generated by jtd-codegen for Python v0.2.0 |
2 | 2 |
|
| 3 | +import re |
3 | 4 | from dataclasses import dataclass |
4 | | -from typing import Any, Union, get_args, get_origin |
| 5 | +from datetime import datetime, timedelta, timezone |
| 6 | +from typing import Any, Dict, Type, Union, get_args, get_origin |
5 | 7 |
|
6 | | -def _from_json(cls, data): |
7 | | - if data is None or cls in [bool, int, float, str, object] or cls is Any: |
8 | | - return data |
9 | | - if get_origin(cls) is Union: |
10 | | - return _from_json(get_args(cls)[0], data) |
11 | | - if get_origin(cls) is list: |
12 | | - return [_from_json(get_args(cls)[0], d) for d in data] |
13 | | - if get_origin(cls) is dict: |
14 | | - return { k: _from_json(get_args(cls)[1], v) for k, v in data.items() } |
15 | | - return cls.from_json(data) |
16 | | - |
17 | | -def _to_json(data): |
18 | | - if data is None or type(data) in [bool, int, float, str, object]: |
19 | | - return data |
20 | | - if type(data) is list: |
21 | | - return [_to_json(d) for d in data] |
22 | | - if type(data) is dict: |
23 | | - return { k: _to_json(v) for k, v in data.items() } |
24 | | - return data.to_json() |
25 | 8 |
|
26 | 9 | @dataclass |
27 | 10 | class Root: |
28 | 11 | foo: 'str' |
29 | 12 |
|
30 | 13 | @classmethod |
31 | | - def from_json(cls, data) -> 'Root': |
32 | | - return { |
| 14 | + def from_json_data(cls, data: Any) -> 'Root': |
| 15 | + variants: Dict[str, Type[Root]] = { |
33 | 16 | "BAR_BAZ": RootBarBaz, |
34 | 17 | "QUUX": RootQuux, |
35 | | - }[data["foo"]].from_json(data) |
| 18 | + } |
36 | 19 |
|
37 | | - def to_json(self): |
| 20 | + return variants[data["foo"]].from_json_data(data) |
| 21 | + |
| 22 | + def to_json_data(self) -> Any: |
38 | 23 | pass |
39 | 24 |
|
40 | 25 | @dataclass |
41 | 26 | class RootBarBaz(Root): |
42 | 27 | baz: 'str' |
43 | 28 |
|
44 | 29 | @classmethod |
45 | | - def from_json(cls, data) -> 'RootBarBaz': |
| 30 | + def from_json_data(cls, data: Any) -> 'RootBarBaz': |
46 | 31 | return cls( |
47 | 32 | "BAR_BAZ", |
48 | | - _from_json(str, data.get("baz")), |
| 33 | + _from_json_data(str, data.get("baz")), |
49 | 34 | ) |
50 | 35 |
|
51 | | - def to_json(self): |
| 36 | + def to_json_data(self) -> Any: |
52 | 37 | data = { "foo": "BAR_BAZ" } |
53 | | - data["baz"] = _to_json(self.baz) |
| 38 | + data["baz"] = _to_json_data(self.baz) |
54 | 39 | return data |
55 | 40 |
|
56 | 41 | @dataclass |
57 | 42 | class RootQuux(Root): |
58 | 43 | quuz: 'str' |
59 | 44 |
|
60 | 45 | @classmethod |
61 | | - def from_json(cls, data) -> 'RootQuux': |
| 46 | + def from_json_data(cls, data: Any) -> 'RootQuux': |
62 | 47 | return cls( |
63 | 48 | "QUUX", |
64 | | - _from_json(str, data.get("quuz")), |
| 49 | + _from_json_data(str, data.get("quuz")), |
65 | 50 | ) |
66 | 51 |
|
67 | | - def to_json(self): |
| 52 | + def to_json_data(self) -> Any: |
68 | 53 | data = { "foo": "QUUX" } |
69 | | - data["quuz"] = _to_json(self.quuz) |
| 54 | + data["quuz"] = _to_json_data(self.quuz) |
| 55 | + return data |
| 56 | + |
| 57 | +def _from_json_data(cls: Any, data: Any) -> Any: |
| 58 | + if data is None or cls in [bool, int, float, str, object] or cls is Any: |
| 59 | + return data |
| 60 | + if cls is datetime: |
| 61 | + return _parse_rfc3339(data) |
| 62 | + if get_origin(cls) is Union: |
| 63 | + return _from_json_data(get_args(cls)[0], data) |
| 64 | + if get_origin(cls) is list: |
| 65 | + return [_from_json_data(get_args(cls)[0], d) for d in data] |
| 66 | + if get_origin(cls) is dict: |
| 67 | + return { k: _from_json_data(get_args(cls)[1], v) for k, v in data.items() } |
| 68 | + return cls.from_json_data(data) |
| 69 | + |
| 70 | +def _to_json_data(data: Any) -> Any: |
| 71 | + if data is None or type(data) in [bool, int, float, str, object]: |
70 | 72 | return data |
| 73 | + if type(data) is datetime: |
| 74 | + return data.isoformat() |
| 75 | + if type(data) is list: |
| 76 | + return [_to_json_data(d) for d in data] |
| 77 | + if type(data) is dict: |
| 78 | + return { k: _to_json_data(v) for k, v in data.items() } |
| 79 | + return data.to_json_data() |
| 80 | + |
| 81 | +def _parse_rfc3339(s: str) -> datetime: |
| 82 | + datetime_re = '^(\d{4})-(\d{2})-(\d{2})[tT](\d{2}):(\d{2}):(\d{2})(\.\d+)?([zZ]|((\+|-)(\d{2}):(\d{2})))$' |
| 83 | + match = re.match(datetime_re, s) |
| 84 | + if not match: |
| 85 | + raise ValueError('Invalid RFC3339 date/time', s) |
| 86 | + |
| 87 | + (year, month, day, hour, minute, second, frac_seconds, offset, |
| 88 | + *tz) = match.groups() |
| 89 | + |
| 90 | + frac_seconds_parsed = None |
| 91 | + if frac_seconds: |
| 92 | + frac_seconds_parsed = int(float(frac_seconds) * 1_000_000) |
| 93 | + else: |
| 94 | + frac_seconds_parsed = 0 |
| 95 | + |
| 96 | + tzinfo = None |
| 97 | + if offset == 'Z': |
| 98 | + tzinfo = timezone.utc |
| 99 | + else: |
| 100 | + hours = int(tz[2]) |
| 101 | + minutes = int(tz[3]) |
| 102 | + sign = 1 if tz[1] == '+' else -1 |
| 103 | + |
| 104 | + if minutes not in range(60): |
| 105 | + raise ValueError('minute offset must be in 0..59') |
| 106 | + |
| 107 | + tzinfo = timezone(timedelta(minutes=sign * (60 * hours + minutes))) |
| 108 | + |
| 109 | + second_parsed = int(second) |
| 110 | + if second_parsed == 60: |
| 111 | + second_parsed = 59 |
| 112 | + |
| 113 | + return datetime(int(year), int(month), int(day), int(hour), int(minute), |
| 114 | + second_parsed, frac_seconds_parsed, tzinfo) |
0 commit comments