Marshal/unmarshal attrs-based data classes to and from JSON
Project description
fieldmarshal – marshal/unmarshal attrs-based data classes to and from JSON
Note: This is module is still in development - APIs might change in backwards-incompatible ways.
Example
>>> from fieldmarshal import struct, field, marshal, unmarshal
>>> from typing import List, Set
>>> @struct
... class Post:
... title: str
... tags: Set[str]
...
>>> @struct
... class User:
... id: int
... name: str
... posts: List[Post]
... is_admin: bool = field("is-admin", default=False)
...
>>> fred = User(1, "fred", [Post("hello world!", tags={"a", "b"})])
>>> data = marshal(fred)
>>> data
{'id': 1, 'name': 'fred', 'posts': [{'title': 'hello world!', 'tags': ['a', 'b']}], 'is-admin': False}
>>> assert unmarshal(data, User) == fred
>>>
The struct and field helpers are just convenience wrappers around attr.s
and attr.ib. The equivalent code with attrs is:
>>> import attr
>>> from fieldmarshal import Options
>>> @attr.s(slots=True, auto_attribs=True)
... class User:
... # ...
... is_admin: bool = attr.ib(
... default=False,
... metadata={'fieldmarshal': Options(name="is-admin")},
... )
>>>
This module provides marshalling/unmarshalling (or serialization/deserialization) of attrs-based "data classes" to and from JSON.
The main goal is to make it easy to quickly build useful (partial) class
representations for real-world JSON data, such as those received from HTTP APIs
(See the examples subdirectory), and to allow efficient
marshalling/unmarshalling to and from JSON.
Features:
- Support for renaming fields (see Example above).
- Unknown/extra JSON keys are ignored by default.
- Hook system to customize marshalling/unmarshalling of custom or complex
types (e.g.
Unions) - Built-in handling of common cases, such as
Enums, simpleUnions. - Limited support for non-string dict keys (bool, int, float, Enum).
- Tries to be unobtrusive: Does not require subclassing and can work with
plain
attrs-based classes.
The API is inspired by Go's json.Marshal/json.Unmarshal and cattrs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fieldmarshal-0.0.2.tar.gz.
File metadata
- Download URL: fieldmarshal-0.0.2.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7131489b3d2744a0df0aa5177065a4fe7ec152e04ff30fcf4fed0e10b64fedda
|
|
| MD5 |
babadff1958b023c174cfe9a4985a2b6
|
|
| BLAKE2b-256 |
8b8f60c5f50b0f103ab4af12ffe3615bb78df96b40b92f99e6cacd65579ed30a
|
File details
Details for the file fieldmarshal-0.0.2-py3-none-any.whl.
File metadata
- Download URL: fieldmarshal-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df934aa6889dc14d4fd54f6e054aec1a4a40b796a706edd94fa452296227badb
|
|
| MD5 |
085575157725026e06fb849dc87e5822
|
|
| BLAKE2b-256 |
0c283a5862bfcc3c3fdd59dfc13eb14245e5da3dfc16f04ffdbb4232ffdef2d0
|