Skip to main content

fieldmarshal – marshal/unmarshal attrs-based data classes to and from JSON

Build Status

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, simple Unions.
  • 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.

Metadata

Release files for fieldmarshal 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fieldmarshal 0.0.2
File Size Uploaded Provenance Actions
fieldmarshal-0.0.2.tar.gz 14.1 kB Without provenance Details for fieldmarshal-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fieldmarshal 0.0.2
File Interpreter ABI Platform Actions
fieldmarshal-0.0.2-py3-none-any.whl Python 3 none any Details for fieldmarshal-0.0.2-py3-none-any.whl

Total release size: 22.6 kB

Release files / fieldmarshal-0.0.2.tar.gz

Download URL fieldmarshal-0.0.2.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7131489b3d2744a0df0aa5177065a4fe7ec152e04ff30fcf4fed0e10b64fedda
BLAKE2b-256 checksum
How to use checksums
8b8f60c5f50b0f103ab4af12ffe3615bb78df96b40b92f99e6cacd65579ed30a
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / fieldmarshal-0.0.2-py3-none-any.whl

Download URL fieldmarshal-0.0.2-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df934aa6889dc14d4fd54f6e054aec1a4a40b796a706edd94fa452296227badb
BLAKE2b-256 checksum
How to use checksums
0c283a5862bfcc3c3fdd59dfc13eb14245e5da3dfc16f04ffdbb4232ffdef2d0
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for fieldmarshal 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page