Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textual type hints crash deserialization #28

Closed
ramonhagenaars opened this issue Mar 24, 2019 · 1 comment
Closed

Textual type hints crash deserialization #28

ramonhagenaars opened this issue Mar 24, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@ramonhagenaars
Copy link
Owner

This issue is possibly related Issue#26.

Textual type hints cannot be deserialized as jsons does not handle string type hints well. This makes it impossible to deserialize into a recursive structure.

Example:

from dataclasses import dataclass
from typing import Optional
import jsons


@dataclass
class Node:
    value: object
    next: Optional['Node']


linked_list = Node(10, Node(20, Node(30, None)))
dumped = jsons.dump(linked_list)

So far so good. The value of dumped is now:

{'next': {'next': {'next': None, 'value': 30}, 'value': 20}, 'value': 10}

Now to deserialize:

jsons.load(dumped, Node)

This raises the following error:

Traceback (most recent call last):
  File "C:\Projects\Personal\jsons\jsons\_main_impl.py", line 128, in load
    return deserializer(json_obj, cls, **kwargs_)
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 31, in default_object_deserializer
    constructor_args = _get_constructor_args(obj, cls, **kwargs)
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 58, in _get_constructor_args
    constructor_args_in_obj = {key: value for key, value in args_gen if key}
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 58, in <dictcomp>
    constructor_args_in_obj = {key: value for key, value in args_gen if key}
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 57, in <genexpr>
    in signature_parameters.items() if sig_key != 'self')
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 72, in _get_value_for_attr
    if obj and sig_key in obj:
TypeError: argument of type 'int' is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Projects/Personal/jsons/effe.py", line 20, in <module>
    jsons.load(dumped, Node)
  File "C:\Projects\Personal\jsons\jsons\_main_impl.py", line 128, in load
    return deserializer(json_obj, cls, **kwargs_)
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 31, in default_object_deserializer
    constructor_args = _get_constructor_args(obj, cls, **kwargs)
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 58, in _get_constructor_args
    constructor_args_in_obj = {key: value for key, value in args_gen if key}
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 58, in <dictcomp>
    constructor_args_in_obj = {key: value for key, value in args_gen if key}
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 57, in <genexpr>
    in signature_parameters.items() if sig_key != 'self')
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 75, in _get_value_for_attr
    meta_hints, **kwargs)
  File "C:\Projects\Personal\jsons\jsons\deserializers\default_object.py", line 109, in _get_value_from_obj
    value = load(obj[sig_key], arg_cls, meta_hints=new_hints, **kwargs)
  File "C:\Projects\Personal\jsons\jsons\_main_impl.py", line 132, in load
    raise DeserializationError(str(err), json_obj, cls)
jsons.exceptions.DeserializationError: argument of type 'int' is not iterable
@ramonhagenaars ramonhagenaars added the bug Something isn't working label Mar 24, 2019
@ramonhagenaars ramonhagenaars added the in progress This issue is worked on label Mar 31, 2019
@ramonhagenaars
Copy link
Owner Author

This has been solved in 0.8.4.

@ramonhagenaars ramonhagenaars removed the in progress This issue is worked on label Apr 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant