|
1 | 1 | # jsonapi-pydantic
|
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <em><a href="https://jsonapi.org" target="_blank">JSON:API</a> implementation with <a href="https://pydantic-docs.helpmanual.io" target="_blank">Pydantic.</a> |
| 5 | + </em> |
| 6 | +</p> |
| 7 | +<p align="center"> |
| 8 | + <a href="https://pypi.org/project/jsonapi-pydantic/" target="_blank"> |
| 9 | + <img src="https://img.shields.io/pypi/v/jsonapi-pydantic" alt="PyPI"> |
| 10 | + </a> |
| 11 | + <a href="https://github.com/impocode/jsonapi-pydantic/blob/master/license.md" target="_blank"> |
| 12 | + <img src="https://img.shields.io/github/license/impocode/jsonapi-pydantic.svg" alt="License"> |
| 13 | + </a> |
| 14 | +</p> |
| 15 | + |
| 16 | +## Description |
| 17 | + |
| 18 | +`jsonapi-pydantic` provides a suite of Pydantic models matching the JSON:API specification. |
| 19 | + |
| 20 | +## Install |
| 21 | + |
| 22 | +```shell |
| 23 | +$ pip install jsonapi-pydantic |
| 24 | +``` |
| 25 | + |
| 26 | +Or use your python package manager. |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Object with primary data: |
| 31 | + |
| 32 | +```python |
| 33 | +from jsonapi_pydantic.v1_0 import TopLevel |
| 34 | + |
| 35 | +external_data = { |
| 36 | + "data": [ |
| 37 | + { |
| 38 | + "type": "articles", |
| 39 | + "id": "1", |
| 40 | + "attributes": { |
| 41 | + "title": "JSON:API paints my bikeshed!", |
| 42 | + "body": "The shortest article. Ever.", |
| 43 | + "created": "2015-05-22T14:56:29.000Z", |
| 44 | + "updated": "2015-05-22T14:56:28.000Z", |
| 45 | + }, |
| 46 | + "relationships": {"author": {"data": {"id": "42", "type": "people"}}}, |
| 47 | + } |
| 48 | + ], |
| 49 | + "included": [ |
| 50 | + {"type": "people", "id": "42", "attributes": {"name": "John", "age": 80, "gender": "male"}} |
| 51 | + ], |
| 52 | +} |
| 53 | + |
| 54 | +top_level = TopLevel(**external_data) |
| 55 | + |
| 56 | +print(top_level.dict(exclude_unset=True)) |
| 57 | +""" |
| 58 | +{ |
| 59 | + "data": [ |
| 60 | + { |
| 61 | + "type": "articles", |
| 62 | + "id": "1", |
| 63 | + "attributes": { |
| 64 | + "title": "JSON:API paints my bikeshed!", |
| 65 | + "body": "The shortest article. Ever.", |
| 66 | + "created": "2015-05-22T14:56:29.000Z", |
| 67 | + "updated": "2015-05-22T14:56:28.000Z", |
| 68 | + }, |
| 69 | + "relationships": {"author": {"data": {"id": "42", "type": "people"}}}, |
| 70 | + } |
| 71 | + ], |
| 72 | + "included": [ |
| 73 | + {"type": "people", "id": "42", "attributes": {"name": "John", "age": 80, "gender": "male"}} |
| 74 | + ], |
| 75 | +} |
| 76 | +""" |
| 77 | +print(top_level.data) |
| 78 | +""" |
| 79 | +[ |
| 80 | + Resource( |
| 81 | + type="articles", |
| 82 | + id="1", |
| 83 | + attributes={ |
| 84 | + "title": "JSON:API paints my bikeshed!", |
| 85 | + "body": "The shortest article. Ever.", |
| 86 | + "created": "2015-05-22T14:56:29.000Z", |
| 87 | + "updated": "2015-05-22T14:56:28.000Z", |
| 88 | + }, |
| 89 | + relationships={ |
| 90 | + "author": Relationship( |
| 91 | + links=None, data=ResourceIdentifier(id="42", type="people", meta=None), meta=None |
| 92 | + ) |
| 93 | + }, |
| 94 | + links=None, |
| 95 | + meta=None, |
| 96 | + ) |
| 97 | +] |
| 98 | +""" |
| 99 | +``` |
| 100 | + |
| 101 | +## License |
| 102 | + |
| 103 | +See [license.md](https://github.com/impocode/jsonapi-pydantic/blob/master/license.md). |
0 commit comments