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

recursively deserialize attributes #4

Open
dynofu opened this issue Dec 26, 2019 · 0 comments
Open

recursively deserialize attributes #4

dynofu opened this issue Dec 26, 2019 · 0 comments

Comments

@dynofu
Copy link

dynofu commented Dec 26, 2019

import attr
from attrs_serde import serde

name_path = ["contact", "personal", "Name"]
phone_path = ["contact", "Phone"]


@serde
@attr.s(auto_attribs=True, frozen=True)
class Name:
    first: str
    last: str


@serde
@attr.s(auto_attribs=True, frozen=True)
class Person:
    name: Name = attr.ib(metadata={"to": name_path, "from": name_path})
    phone: str = attr.ib(metadata={"to": phone_path, "from": phone_path})


person_json = {"contact": {"personal": {"Name": {"first": "John", "last": "Smith"}}, "Phone": "555-112233"}}

# to/from only works on serde
p = Person(name=Name(first="John", last="Smith"), phone="555-112233")
print(p.to_dict())
# {'contact': {'personal': {'Name': {'first': 'John', 'last': 'Smith'}}, 'Phone': '555-112233'}}
p1 = Person.from_dict(person_json)
print(f"p1={p1}") # <---
# p1=Person(name={'first': 'John', 'last': 'Smith'}, phone='555-112233')

cattrs does the recursive right, but lack of attribute mapping.
attrs-serde has the mapping but does not do recursive deserialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant