Skip to content

Extend Deserialization Support to Pydantic Models and Dataclasses #23

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

LockedThread
Copy link
Contributor

This PR builds on the previous work by extending the serialization and deserialization capabilities to include support for Pydantic models and Python dataclasses. The new commit 1aacb80 introduces functionality that further bridges Python's dynamic data structures with Rust’s type-safe ecosystem.

Overview

  • Pydantic Support:
    Pydantic models are now supported out-of-the-box. The implementation detects when a Python object is a Pydantic model and appropriately serializes its underlying data.

  • Dataclasses Support:
    Similarly, Python dataclasses are now handled seamlessly. The logic checks for dataclass instances and extracts their fields for serialization.

Implementation Details

  • Type Checking:
    The implementation introduces checks to determine if an object is an instance of a Pydantic model or a dataclass. This is performed before falling back to the general Python object handling.

  • Data Extraction:

    • For Pydantic models, the model's dict representation is extracted and serialized.
    • For dataclasses, the field values are similarly extracted, ensuring that both explicit, default, and nested values are considered during the deserialization process.

Testing

  • Unit Tests:
    New tests have been added to verify:
    • Dserialization cycles for Pydantic models and dataclasses.

Conclusion

This PR significantly enhances the serde-pyobject library by incorporating first-class support for Pydantic models and Python dataclasses. These changes make it easier for developers to work with Python’s modern data modeling tools in a Rust environment, ensuring a smoother and more robust interoperation between the two ecosystems.

Please review the changes and share any feedback. Further improvements and refinements can be addressed in subsequent PRs.

@LockedThread
Copy link
Contributor Author

Ignore until #22 is merged.

@LockedThread LockedThread changed the title Extend Serialization Support to Pydantic Models and Dataclasses Extend Deserialization Support to Pydantic Models and Dataclasses Apr 2, 2025
@LockedThread LockedThread marked this pull request as ready for review May 24, 2025 20:29
@LockedThread
Copy link
Contributor Author

@termoshtt Thank you for merging #22, this PR depends on the changes of #22. Now that it's merged can you please review this? Let me know if you have any requested modifications.

@termoshtt termoshtt self-requested a review May 25, 2025 01:07
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

Successfully merging this pull request may close these issues.

1 participant