| Conditions | 5 |
| Total Lines | 70 |
| Code Lines | 26 |
| 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 | """ |
||
| 20 | def load( |
||
| 21 | json_obj: object, |
||
| 22 | cls: Optional[type] = None, |
||
| 23 | strict: bool = False, |
||
| 24 | fork_inst: Optional[type] = StateHolder, |
||
| 25 | attr_getters: Optional[Dict[str, Callable[[], object]]] = None, |
||
| 26 | **kwargs) -> object: |
||
| 27 | """ |
||
| 28 | Deserialize the given ``json_obj`` to an object of type ``cls``. If the |
||
| 29 | contents of ``json_obj`` do not match the interface of ``cls``, a |
||
| 30 | DeserializationError is raised. |
||
| 31 | |||
| 32 | If ``json_obj`` contains a value that belongs to a custom class, there must |
||
| 33 | be a type hint present for that value in ``cls`` to let this function know |
||
| 34 | what type it should deserialize that value to. |
||
| 35 | |||
| 36 | |||
| 37 | **Example**: |
||
| 38 | |||
| 39 | >>> from typing import List |
||
| 40 | >>> import jsons |
||
| 41 | >>> class Person: |
||
| 42 | ... # No type hint required for name |
||
| 43 | ... def __init__(self, name): |
||
| 44 | ... self.name = name |
||
| 45 | >>> class Family: |
||
| 46 | ... # Person is a custom class, use a type hint |
||
| 47 | ... def __init__(self, persons: List[Person]): |
||
| 48 | ... self.persons = persons |
||
| 49 | >>> loaded = jsons.load({'persons': [{'name': 'John'}]}, Family) |
||
| 50 | >>> loaded.persons[0].name |
||
| 51 | 'John' |
||
| 52 | |||
| 53 | If no ``cls`` is given, a dict is simply returned, but contained values |
||
| 54 | (e.g. serialized ``datetime`` values) are still deserialized. |
||
| 55 | |||
| 56 | If `strict` mode is off and the type of `json_obj` exactly matches `cls` |
||
| 57 | then `json_obj` is simply returned. |
||
| 58 | |||
| 59 | :param json_obj: the dict that is to be deserialized. |
||
| 60 | :param cls: a matching class of which an instance should be returned. |
||
| 61 | :param strict: a bool to determine if the deserializer should be strict |
||
| 62 | (i.e. fail on a partially deserialized `json_obj` or on `None`). |
||
| 63 | :param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
||
| 64 | :param attr_getters: a ``dict`` that may hold callables that return values |
||
| 65 | for certain attributes. |
||
| 66 | :param kwargs: the keyword args are passed on to the deserializer function. |
||
| 67 | :return: an instance of ``cls`` if given, a dict otherwise. |
||
| 68 | """ |
||
| 69 | if _should_skip(json_obj, cls, strict): |
||
| 70 | return json_obj |
||
| 71 | if isinstance(cls, str): |
||
| 72 | cls = get_cls_from_str(cls, json_obj, fork_inst) |
||
| 73 | cls, meta_hints = _check_and_get_cls_and_meta_hints( |
||
| 74 | json_obj, cls, fork_inst, kwargs.get('_inferred_cls', False)) |
||
| 75 | |||
| 76 | deserializer = get_deserializer(cls, fork_inst) |
||
| 77 | kwargs_ = { |
||
| 78 | 'strict': strict, |
||
| 79 | 'fork_inst': fork_inst, |
||
| 80 | 'attr_getters': attr_getters, |
||
| 81 | 'meta_hints': meta_hints, |
||
| 82 | **kwargs |
||
| 83 | } |
||
| 84 | try: |
||
| 85 | return deserializer(json_obj, cls, **kwargs_) |
||
| 86 | except Exception as err: |
||
| 87 | if isinstance(err, JsonsError): |
||
| 88 | raise |
||
| 89 | raise DeserializationError(str(err), json_obj, cls) |
||
| 90 | |||
| 180 |