Working on making the from_json work better
This commit is contained in:
@@ -4,30 +4,30 @@ from datetime import datetime
|
|||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
|
|
||||||
|
|
||||||
class APIObject:
|
def _from_json(cls, data):
|
||||||
@classmethod
|
|
||||||
def from_json(cls, data):
|
|
||||||
"""
|
"""
|
||||||
Approach for deserialization here:
|
Approach for deserialization here:
|
||||||
https://stackoverflow.com/a/40639688/2319844
|
https://stackoverflow.com/a/40639688/2319844
|
||||||
"""
|
"""
|
||||||
|
print(cls, data)
|
||||||
annotations: Dict[str, Any] = getattr(cls, '__annotations__', {})
|
annotations: Dict[str, Any] = getattr(cls, '__annotations__', {})
|
||||||
|
|
||||||
# Handle lists of objects.
|
# Handle lists of objects.
|
||||||
|
if cls == str:
|
||||||
|
return data
|
||||||
if issubclass(cls, List):
|
if issubclass(cls, List):
|
||||||
list_type = cls.__args__[0]
|
list_type = cls.__args__[0]
|
||||||
instance: List[list_type] = list()
|
instance: List[list_type] = list()
|
||||||
for value in data:
|
for value in data:
|
||||||
instance.append(list_type.from_json(value))
|
instance.append(_from_json(list_type, value))
|
||||||
|
|
||||||
# Handle dictionaries of objects.
|
# Handle dictionaries of objects.
|
||||||
elif issubclass(cls, Dict):
|
elif issubclass(cls, Dict):
|
||||||
key_type = cls.__args__[0]
|
key_type, val_type = cls.__args__
|
||||||
val_type = cls.__args__[1]
|
|
||||||
instance: Dict[key_type, val_type] = dict()
|
instance: Dict[key_type, val_type] = dict()
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
instance.update(key_type.from_json(key),
|
instance.update(_from_json(key_type, key),
|
||||||
key_type.from_json(value))
|
_from_json(val_type, value))
|
||||||
|
|
||||||
# Handle everything else by first instantiating the class, then adding
|
# Handle everything else by first instantiating the class, then adding
|
||||||
# all of the sub-elements, recursively calling from_json on them.
|
# all of the sub-elements, recursively calling from_json on them.
|
||||||
@@ -37,17 +37,18 @@ class APIObject:
|
|||||||
field_type = annotations.get(name)
|
field_type = annotations.get(name)
|
||||||
print('ohea', field_type, value)
|
print('ohea', field_type, value)
|
||||||
if inspect.isclass(field_type):
|
if inspect.isclass(field_type):
|
||||||
if isinstance(value, (dict, tuple, list, set, frozenset)):
|
setattr(instance, name, _from_json(field_type, value))
|
||||||
setattr(instance, name, field_type.from_json(value))
|
|
||||||
elif field_type == datetime:
|
|
||||||
setattr(instance, name, parser.parse(value))
|
|
||||||
else:
|
|
||||||
setattr(instance, name, value)
|
|
||||||
else:
|
else:
|
||||||
setattr(instance, name, value)
|
setattr(instance, name, value)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
class APIObject:
|
||||||
|
@classmethod
|
||||||
|
def from_json(cls, data):
|
||||||
|
return _from_json(cls, data)
|
||||||
|
|
||||||
def get(self, field, default=None):
|
def get(self, field, default=None):
|
||||||
return getattr(self, field, default)
|
return getattr(self, field, default)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user