# pynamodb-encoder
![Build](https://github.com/lyang/pynamodb-encoder/actions/workflows/build.yml/badge.svg) ![CodeQL](https://github.com/lyang/pynamodb-encoder/actions/workflows/codeql-analysis.yml/badge.svg) [![codecov](https://codecov.io/gh/lyang/pynamodb-encoder/branch/main/graph/badge.svg?token=P51YVL86N8)](https://codecov.io/gh/lyang/pynamodb-encoder) [![Maintainability](https://api.codeclimate.com/v1/badges/1e5c3b615dedb2bffb0c/maintainability)](https://codeclimate.com/github/lyang/pynamodb-encoder/maintainability) [![PyPI version](https://badge.fury.io/py/pynamodb-encoder.svg)](https://badge.fury.io/py/pynamodb-encoder)
[![PyPI Supported Python Versions](https://img.shields.io/pypi/pyversions/pynamodb-encoder.svg)](https://pypi.python.org/pypi/pynamodb-encoder/)
## Introduction
`pynamodb-encoder` provides helper classes that can convert [PynamoDB](https://github.com/pynamodb/PynamoDB) `Model` objects into `JSON` serializable `dict`. It can also decode such `dict` back into those `Model` objects. [Polymorphic](https://pynamodb.readthedocs.io/en/latest/polymorphism.html) models and attributes are also supported.
## Examples
```python
def test_encode_complex_model(encoder: Encoder):
class Pet(DynamicMapAttribute):
cls = DiscriminatorAttribute()
name = UnicodeAttribute()
class Cat(Pet, discriminator="Cat"):
pass
class Dog(Pet, discriminator="Dog"):
pass
class Human(Model):
name = UnicodeAttribute()
pets = ListAttribute(of=Pet)
jon = Human(name="Jon", pets=[Cat(name="Garfield", age=43), Dog(name="Odie")])
assert encoder.encode(jon) == {
"name": "Jon",
"pets": [{"cls": "Cat", "name": "Garfield", "age": 43}, {"cls": "Dog", "name": "Odie"}],
}
def test_decode_complex_model(decoder: Decoder):
class Pet(DynamicMapAttribute):
cls = DiscriminatorAttribute()
class Cat(Pet, discriminator="Cat"):
name = UnicodeAttribute()
class Dog(Pet, discriminator="Dog"):
breed = UnicodeAttribute()
class Human(Model):
name = UnicodeAttribute()
age = NumberAttribute()
pets = ListAttribute(of=Pet)
jon = decoder.decode(
Human,
{
"name": "Jon",
"age": 70,
"pets": [{"cls": "Cat", "name": "Garfield"}, {"cls": "Dog", "breed": "Terrier"}],
},
)
assert jon.name == "Jon"
assert jon.age == 70
assert isinstance(jon.pets, list)
assert len(jon.pets) == 2
assert isinstance(jon.pets[0], Cat)
assert jon.pets[0].name == "Garfield"
assert isinstance(jon.pets[1], Dog)
assert jon.pets[1].breed == "Terrier"
```
More examples can be found in [encoder_test.py](tests/encoder_test.py) and [decoder_test.py](tests/decoder_test.py)
Raw data
{
"_id": null,
"home_page": "https://github.com/lyang/pynamodb-encoder",
"name": "pynamodb-encoder",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "dynamodb,pynamodb,json,encode,decode",
"author": "Lin Yang",
"author_email": "github@linyang.me",
"download_url": "https://files.pythonhosted.org/packages/56/24/86bf19b12cc5d5ed804d424c664e734faef1be11177a5fc17eb74bb6859a/pynamodb_encoder-0.1.2.tar.gz",
"platform": null,
"description": "# pynamodb-encoder\n![Build](https://github.com/lyang/pynamodb-encoder/actions/workflows/build.yml/badge.svg) ![CodeQL](https://github.com/lyang/pynamodb-encoder/actions/workflows/codeql-analysis.yml/badge.svg) [![codecov](https://codecov.io/gh/lyang/pynamodb-encoder/branch/main/graph/badge.svg?token=P51YVL86N8)](https://codecov.io/gh/lyang/pynamodb-encoder) [![Maintainability](https://api.codeclimate.com/v1/badges/1e5c3b615dedb2bffb0c/maintainability)](https://codeclimate.com/github/lyang/pynamodb-encoder/maintainability) [![PyPI version](https://badge.fury.io/py/pynamodb-encoder.svg)](https://badge.fury.io/py/pynamodb-encoder)\n[![PyPI Supported Python Versions](https://img.shields.io/pypi/pyversions/pynamodb-encoder.svg)](https://pypi.python.org/pypi/pynamodb-encoder/)\n\n## Introduction\n`pynamodb-encoder` provides helper classes that can convert [PynamoDB](https://github.com/pynamodb/PynamoDB) `Model` objects into `JSON` serializable `dict`. It can also decode such `dict` back into those `Model` objects. [Polymorphic](https://pynamodb.readthedocs.io/en/latest/polymorphism.html) models and attributes are also supported.\n\n## Examples\n```python\ndef test_encode_complex_model(encoder: Encoder):\n class Pet(DynamicMapAttribute):\n cls = DiscriminatorAttribute()\n name = UnicodeAttribute()\n\n class Cat(Pet, discriminator=\"Cat\"):\n pass\n\n class Dog(Pet, discriminator=\"Dog\"):\n pass\n\n class Human(Model):\n name = UnicodeAttribute()\n pets = ListAttribute(of=Pet)\n\n jon = Human(name=\"Jon\", pets=[Cat(name=\"Garfield\", age=43), Dog(name=\"Odie\")])\n assert encoder.encode(jon) == {\n \"name\": \"Jon\",\n \"pets\": [{\"cls\": \"Cat\", \"name\": \"Garfield\", \"age\": 43}, {\"cls\": \"Dog\", \"name\": \"Odie\"}],\n }\n\ndef test_decode_complex_model(decoder: Decoder):\n class Pet(DynamicMapAttribute):\n cls = DiscriminatorAttribute()\n\n class Cat(Pet, discriminator=\"Cat\"):\n name = UnicodeAttribute()\n\n class Dog(Pet, discriminator=\"Dog\"):\n breed = UnicodeAttribute()\n\n class Human(Model):\n name = UnicodeAttribute()\n age = NumberAttribute()\n pets = ListAttribute(of=Pet)\n\n jon = decoder.decode(\n Human,\n {\n \"name\": \"Jon\",\n \"age\": 70,\n \"pets\": [{\"cls\": \"Cat\", \"name\": \"Garfield\"}, {\"cls\": \"Dog\", \"breed\": \"Terrier\"}],\n },\n )\n\n assert jon.name == \"Jon\"\n assert jon.age == 70\n assert isinstance(jon.pets, list)\n assert len(jon.pets) == 2\n assert isinstance(jon.pets[0], Cat)\n assert jon.pets[0].name == \"Garfield\"\n assert isinstance(jon.pets[1], Dog)\n assert jon.pets[1].breed == \"Terrier\"\n```\n\nMore examples can be found in [encoder_test.py](tests/encoder_test.py) and [decoder_test.py](tests/decoder_test.py)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Helper classes that encode/decode pynamodb models to/from JSON serializable dict",
"version": "0.1.2",
"split_keywords": [
"dynamodb",
"pynamodb",
"json",
"encode",
"decode"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6b8a08483c879a5b9b340ce91407977fe58e76e6f027f7f3f6cf45667492af1e",
"md5": "ef7ed3cd4055fad782e02bb3b7477946",
"sha256": "f8c81a2271cd9b6fadbc95496301458b0560f892a0bd622160f6ad0d654fb511"
},
"downloads": -1,
"filename": "pynamodb_encoder-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ef7ed3cd4055fad782e02bb3b7477946",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 6233,
"upload_time": "2023-03-23T18:10:21",
"upload_time_iso_8601": "2023-03-23T18:10:21.753587Z",
"url": "https://files.pythonhosted.org/packages/6b/8a/08483c879a5b9b340ce91407977fe58e76e6f027f7f3f6cf45667492af1e/pynamodb_encoder-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "562486bf19b12cc5d5ed804d424c664e734faef1be11177a5fc17eb74bb6859a",
"md5": "266a0b3cc9f5390fe56d74d3b95f573c",
"sha256": "f564bc21dda1bd8a82948057c5842316da5e55b146c6193c33f73122068300e6"
},
"downloads": -1,
"filename": "pynamodb_encoder-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "266a0b3cc9f5390fe56d74d3b95f573c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 4123,
"upload_time": "2023-03-23T18:10:22",
"upload_time_iso_8601": "2023-03-23T18:10:22.844025Z",
"url": "https://files.pythonhosted.org/packages/56/24/86bf19b12cc5d5ed804d424c664e734faef1be11177a5fc17eb74bb6859a/pynamodb_encoder-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-23 18:10:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "lyang",
"github_project": "pynamodb-encoder",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "pynamodb-encoder"
}