Skip to main content

PyMarshal

pymarshal replicates the feature of (un)marshalling structs in Golang. Rather than replicating the exact feature as it exists in Go, pymarshal aims for elegant, Pythonic simplicity, and to fix the flaws in Go's implementation such as:

  • extra keys being silently ignored
  • lack of mandatory fields
  • lack of default values See control variables for the many options that can be configured per-class.

PyMarshal v2.0+

Support for the following has been removed:

  • YAML docstring API docs format
  • Python2

If you need any of these, use 1.6.2

Currently supported serialization formats

As YAML is compatible with JSON, use PyYAML to load or dump data with the pymarshal.json module, there is no explicit YAML module.

Installation

It is recommended that you install from PyPI using pip install pymarshal

Overview

The only modification required to your class code is to use the type_assert functions to assign __init__ arguments to class fields of the same name. pymarshal provides the type_assert functions to both enforce the type, and to unmarshal nested objects.

Example:

class MyModel:
    def __init__(
        self,
        a,
        b=5,
    ):
        self.a = type_assert(a, str)
        self.b = type_assert(b, int)

>>> from pymarshal.json import *
>>> x = marshal_json(MyModel("test", 6))
>>> x
{"a": "test", "b": 6}
>>> y = unmarshal_json(x, MyModel)
>>> y.a
"test"

NOTE: Your classes must not implement __call__ (which is an antipattern anyway). Whatever you would've implemented with __call__ should just be a normal, named method.

Your __init__ methods should only use simple assignment through the type_assert functions. If you have a use-case for a constructor that does more than simple assignment, use a separate 'factory' static method.

There is also:

  • type_assert_iter for iterables
  • type_assert_dict for anything that implements .items() -> k, v

Rather than using the Golang "tag" syntax, simply create a _marshal_key_swap and _unmarshal_key_swap dict in your class, and any re-named keys will be swapped before being passed to the class constructor or before being marshalled to JSON. The full list of control variables are documented HERE.

Examples

See the documentation folder

Metadata

Release files for pymarshal 2.2.3

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

Source distribution (sdist)

Source distribution for pymarshal 2.2.3
File Size Uploaded
pymarshal-2.2.3.tar.gz 14.9 kB Details

Built distribution (wheel)

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

Total release size: 31.3 kB

Release files / pymarshal-2.2.3.tar.gz

Download URL pymarshal-2.2.3.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
43161a7b03dbc3b1e28630dbbab3684177502c691f10d21665fb0253a33f0bd2
BLAKE2b-256 checksum
How to use checksums
805e50cb3d0d7428ee29d14e8ec1e2aa261141a83ec1e9d352da01326c630d78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release files / pymarshal-2.2.3-py3-none-any.whl

Download URL pymarshal-2.2.3-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48b7947871e9d160fc36f6b543fa635fa0310d46067e2970e8ccdff9f47067dd
BLAKE2b-256 checksum
How to use checksums
a6e18f1653cd615d39afae7c4f9bfe66d49151835f58017c25f21b96fafc5210
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release history Release notifications | RSS feed

This release

2.2.3 This release

2 release files

2.2.2

2 release files

2.2.1

1 release file

2.2.0

1 release file

2.1.0

1 release file

2.0.4

1 release file

2.0.3

1 release file

2.0.2

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.6.2

1 release file

1.6.1

1 release file

1.6.0

1 release file

1.5.2

1 release file

1.5.1

1 release file

1.5.0

1 release file

1.3.5

1 release file

1.3.4

1 release file

1.3.3

1 release file

1.3.2

1 release file

1.3.1

1 release file

1.3.0

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.9

1 release file

1.0.0

1 release file

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