Conditions | 1 |
Total Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | import collections |
||
9 | def create_json_encoder(**kwargs): |
||
10 | class JSONEncoder(json.JSONEncoder): |
||
|
|||
11 | |||
12 | @classmethod |
||
13 | def _filter_params(cls, op, nop): |
||
14 | params = set(op) | set(nop) |
||
15 | return {key: kwargs[key] for key in set(kwargs) & (params)} |
||
16 | |||
17 | def default(self, obj): |
||
18 | if hasattr(obj, "__json__"): |
||
19 | fdata = FunctionMetadata.from_function(obj.__json__) |
||
20 | params = self._filter_params( |
||
21 | fdata.optional_params, fdata.non_optional_params) |
||
22 | return obj.__json__(**params) |
||
23 | elif isinstance(obj, collections.Iterable): |
||
24 | return list(obj) |
||
25 | elif isinstance(obj, datetime): |
||
26 | return obj.isoformat() |
||
27 | elif hasattr(obj, "__getitem__") and hasattr(obj, "keys"): |
||
28 | return dict(obj) |
||
29 | elif hasattr(obj, "__dict__"): |
||
30 | return {member: getattr(obj, member) |
||
31 | for member in get_public_members(obj)} |
||
32 | |||
33 | return json.JSONEncoder.default(self, obj) |
||
34 | return JSONEncoder |
||
35 |