# Pydantic Dict Encoders
Simple wrapper of pydantic for support custom dict encoders like json encoders
[![CI](https://github.com/i8enn/pydantic_dict_encoders/actions/workflows/testing.yml/badge.svg?branch=master)](https://github.com/i8enn/pydantic_dict_encoders/actions?query=branch%3Amaster+workflow%3Atesting)
[![Coverage](https://codecov.io/gh/i8enn/pydantic_dict_encoders/branch/master/graph/badge.svg?token=TER6OGX2Z7)](https://codecov.io/gh/i8enn/pydantic_dict_encoders)
[![pypi](https://img.shields.io/pypi/v/pydantic_dict_encoders.svg)](https://pypi.python.org/pypi/pydantic_dict_encoders)
[![downloads](https://pepy.tech/badge/pydantic_dict_encoders/month)](https://pepy.tech/project/pydantic_dict_encoders)
[![versions](https://img.shields.io/pypi/pyversions/pydantic_dict_encoders.svg)](https://github.com/i8enn/pydantic_dict_encoders)
[![license](https://img.shields.io/github/license/i8enn/pydantic_dict_encoders.svg)](https://github.com/i8enn/pydantic_dict_encoders/blob/master/LICENSE)
This wrapper created for solve serialization problem in ariadne resolvers, where needed return dict only with simple objects.
After research [some problems](https://github.com/pydantic/pydantic/issues/1409) and pydantic features, it was decided to make this wrapper more like a crutch for solve my problem.
This should be resolved in [Pydantic V2](https://github.com/pydantic/pydantic/discussions/4456), but it's not production ready yet.
And even there is a [PR](https://github.com/pydantic/pydantic/pull/4978) that was made some time ago. But for now, it seemed to me sufficient to use this solution and perhaps it will be useful to someone else.
*Thanks to @samuelcolvin for create and being active on pydantic and starting to solve problems before some people think about them! :)*
> **IMPORTANT!** Remember: this is a crutch that you use at your own risk. It will most likely not be needed after some time, but if everything suits you - go for it ;)
---
## Usage
This wrapper has 2 possibilities:
1. Define custom dict encoders that work the same as pydantic [`json_encoders`](https://docs.pydantic.dev/usage/exporting_models/#json_encoders) when calling `model.dict()`
2. Encode each field value as if they were passed to pydantic json.
To use this just inherit your model from `PydanticDictEncodersMixin`:
```python
class AnyModel(PydanticDictEncodersMixin, BaseModel):
any_field: str | None = None
class Config:
dict_encoders = {}
jsonify_dict_encode = False
```
> **WARNING!** Please, remember about python MRO: BaseModel MUST BE after mixin.
---
## Pros and cons
### Pros:
- Pretty simple and fast way to get the required behavior when exporting to a python dict
### Cons:
- In nested models that do not inherit from the mixin, serialization will break
- Dirty decision
Raw data
{
"_id": null,
"home_page": "https://github.com/i8enn/pydantic_dict_encoders",
"name": "pydantic-dict-encoders",
"maintainer": "Ivan Galin",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "i.galin@devartsteam.ru",
"keywords": "pydantic,dict,encoders,json",
"author": "Ivan Galin",
"author_email": "i.galin@devartsteam.ru",
"download_url": "https://files.pythonhosted.org/packages/d8/66/e7fb3372eded89fbed12c46d8e01adbab586d85b626f7d80936434362bcd/pydantic_dict_encoders-0.2.tar.gz",
"platform": null,
"description": "# Pydantic Dict Encoders\n\nSimple wrapper of pydantic for support custom dict encoders like json encoders\n\n[![CI](https://github.com/i8enn/pydantic_dict_encoders/actions/workflows/testing.yml/badge.svg?branch=master)](https://github.com/i8enn/pydantic_dict_encoders/actions?query=branch%3Amaster+workflow%3Atesting)\n[![Coverage](https://codecov.io/gh/i8enn/pydantic_dict_encoders/branch/master/graph/badge.svg?token=TER6OGX2Z7)](https://codecov.io/gh/i8enn/pydantic_dict_encoders)\n[![pypi](https://img.shields.io/pypi/v/pydantic_dict_encoders.svg)](https://pypi.python.org/pypi/pydantic_dict_encoders)\n[![downloads](https://pepy.tech/badge/pydantic_dict_encoders/month)](https://pepy.tech/project/pydantic_dict_encoders)\n[![versions](https://img.shields.io/pypi/pyversions/pydantic_dict_encoders.svg)](https://github.com/i8enn/pydantic_dict_encoders)\n[![license](https://img.shields.io/github/license/i8enn/pydantic_dict_encoders.svg)](https://github.com/i8enn/pydantic_dict_encoders/blob/master/LICENSE)\n\n\nThis wrapper created for solve serialization problem in ariadne resolvers, where needed return dict only with simple objects.\nAfter research [some problems](https://github.com/pydantic/pydantic/issues/1409) and pydantic features, it was decided to make this wrapper more like a crutch for solve my problem.\n\nThis should be resolved in [Pydantic V2](https://github.com/pydantic/pydantic/discussions/4456), but it's not production ready yet.\nAnd even there is a [PR](https://github.com/pydantic/pydantic/pull/4978) that was made some time ago. But for now, it seemed to me sufficient to use this solution and perhaps it will be useful to someone else.\n\n*Thanks to @samuelcolvin for create and being active on pydantic and starting to solve problems before some people think about them! :)*\n\n> **IMPORTANT!** Remember: this is a crutch that you use at your own risk. It will most likely not be needed after some time, but if everything suits you - go for it ;)\n\n---\n\n## Usage\n\nThis wrapper has 2 possibilities:\n\n1. Define custom dict encoders that work the same as pydantic [`json_encoders`](https://docs.pydantic.dev/usage/exporting_models/#json_encoders) when calling `model.dict()`\n2. Encode each field value as if they were passed to pydantic json.\n\nTo use this just inherit your model from `PydanticDictEncodersMixin`:\n\n```python\nclass AnyModel(PydanticDictEncodersMixin, BaseModel):\n any_field: str | None = None\n\n class Config:\n dict_encoders = {}\n jsonify_dict_encode = False\n```\n\n> **WARNING!** Please, remember about python MRO: BaseModel MUST BE after mixin.\n\n---\n## Pros and cons\n\n### Pros:\n- Pretty simple and fast way to get the required behavior when exporting to a python dict\n\n### Cons:\n- In nested models that do not inherit from the mixin, serialization will break\n- Dirty decision\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pydantic mixins for support custom encoding dict",
"version": "0.2",
"split_keywords": [
"pydantic",
"dict",
"encoders",
"json"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0dc8b15234e4886fce30e581bbcf2c91250c125b7652ec627a82bdfb7c9262ec",
"md5": "2e63b147d5ba712ec7bfc1382ee5e0b6",
"sha256": "684e6fe73ed4b75a50c68a43fe74519d3c1243d7ef1b734db4ae6ebd2e5a4c9f"
},
"downloads": -1,
"filename": "pydantic_dict_encoders-0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e63b147d5ba712ec7bfc1382ee5e0b6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 5132,
"upload_time": "2023-03-13T23:18:00",
"upload_time_iso_8601": "2023-03-13T23:18:00.802624Z",
"url": "https://files.pythonhosted.org/packages/0d/c8/b15234e4886fce30e581bbcf2c91250c125b7652ec627a82bdfb7c9262ec/pydantic_dict_encoders-0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d866e7fb3372eded89fbed12c46d8e01adbab586d85b626f7d80936434362bcd",
"md5": "469c5019013aa541915acd3a9d3262d7",
"sha256": "704f908ebeaca779189df9ae26ddf4ca6f144aa593b953a2ba0eba38afdfcb24"
},
"downloads": -1,
"filename": "pydantic_dict_encoders-0.2.tar.gz",
"has_sig": false,
"md5_digest": "469c5019013aa541915acd3a9d3262d7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 5615,
"upload_time": "2023-03-13T23:18:02",
"upload_time_iso_8601": "2023-03-13T23:18:02.499458Z",
"url": "https://files.pythonhosted.org/packages/d8/66/e7fb3372eded89fbed12c46d8e01adbab586d85b626f7d80936434362bcd/pydantic_dict_encoders-0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-13 23:18:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "i8enn",
"github_project": "pydantic_dict_encoders",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pydantic-dict-encoders"
}