| Conditions | 3 |
| Total Lines | 73 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 25 | def load( |
||
| 26 | json_obj: object, |
||
| 27 | cls: Optional[Type[T]] = None, |
||
| 28 | strict: bool = False, |
||
| 29 | fork_inst: Optional[type] = StateHolder, |
||
| 30 | attr_getters: Optional[Dict[str, Callable[[], object]]] = None, |
||
| 31 | **kwargs) -> T: |
||
| 32 | """ |
||
| 33 | Deserialize the given ``json_obj`` to an object of type ``cls``. If the |
||
| 34 | contents of ``json_obj`` do not match the interface of ``cls``, a |
||
| 35 | DeserializationError is raised. |
||
| 36 | |||
| 37 | If ``json_obj`` contains a value that belongs to a custom class, there must |
||
| 38 | be a type hint present for that value in ``cls`` to let this function know |
||
| 39 | what type it should deserialize that value to. |
||
| 40 | |||
| 41 | |||
| 42 | **Example**: |
||
| 43 | |||
| 44 | >>> from typing import List |
||
| 45 | >>> import jsons |
||
| 46 | >>> class Person: |
||
| 47 | ... # No type hint required for name |
||
| 48 | ... def __init__(self, name): |
||
| 49 | ... self.name = name |
||
| 50 | >>> class Family: |
||
| 51 | ... # Person is a custom class, use a type hint |
||
| 52 | ... def __init__(self, persons: List[Person]): |
||
| 53 | ... self.persons = persons |
||
| 54 | >>> loaded = jsons.load({'persons': [{'name': 'John'}]}, Family) |
||
| 55 | >>> loaded.persons[0].name |
||
| 56 | 'John' |
||
| 57 | |||
| 58 | If no ``cls`` is given, a dict is simply returned, but contained values |
||
| 59 | (e.g. serialized ``datetime`` values) are still deserialized. |
||
| 60 | |||
| 61 | If `strict` mode is off and the type of `json_obj` exactly matches `cls` |
||
| 62 | then `json_obj` is simply returned. |
||
| 63 | |||
| 64 | :param json_obj: the dict that is to be deserialized. |
||
| 65 | :param cls: a matching class of which an instance should be returned. |
||
| 66 | :param strict: a bool to determine if the deserializer should be strict |
||
| 67 | (i.e. fail on a partially deserialized `json_obj` or on `None`). |
||
| 68 | :param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
||
| 69 | :param attr_getters: a ``dict`` that may hold callables that return values |
||
| 70 | for certain attributes. |
||
| 71 | :param kwargs: the keyword args are passed on to the deserializer function. |
||
| 72 | :return: an instance of ``cls`` if given, a dict otherwise. |
||
| 73 | """ |
||
| 74 | _check_for_none(json_obj, cls) |
||
| 75 | if _should_skip(json_obj, cls, strict): |
||
| 76 | validate(json_obj, cls, fork_inst) |
||
| 77 | return json_obj |
||
| 78 | if isinstance(cls, str): |
||
| 79 | cls = get_cls_from_str(cls, json_obj, fork_inst) |
||
| 80 | cls, meta_hints = _check_and_get_cls_and_meta_hints( |
||
| 81 | json_obj, cls, fork_inst, kwargs.get('_inferred_cls', False)) |
||
| 82 | |||
| 83 | deserializer = get_deserializer(cls, fork_inst) |
||
| 84 | |||
| 85 | # Is this the initial call or a nested? |
||
| 86 | initial = kwargs.get('_initial', True) |
||
| 87 | |||
| 88 | kwargs_ = { |
||
| 89 | 'strict': strict, |
||
| 90 | 'fork_inst': fork_inst, |
||
| 91 | 'attr_getters': attr_getters, |
||
| 92 | 'meta_hints': meta_hints, |
||
| 93 | '_initial': False, |
||
| 94 | **kwargs |
||
| 95 | } |
||
| 96 | |||
| 97 | return _do_load(json_obj, deserializer, cls, initial, **kwargs_) |
||
| 98 | |||
| 214 |