Skip to main content

Python object serialization focused on JSON

Project description

Serialization + JSON

Lint+Test

Synopsis

This library is intended to serialize objects to and from Python primitives. That is, objects will be represented as dict, list, int, float, bool, str, and None. The resulting primitives may be easily serialized to JSON using python.json or this library.

Because the library focuses on representation with native primitives, it could be useful for serialization to other formats.

Usage

The library uses decorators to mark methods in charge of serialization. There's a serializer decorator that associates a function with serializing a specific type, and there's a corresponding deserializer:

@serializer(datetime)
def dthandler(dt: datetime):
    return dt.isoformat()


@deserializer(datetime)
def str2dt(datestr: str):
    return datetime.fromisoformat(datestr)

Finally, the library implements a serialization decorator to make a class in charge of serializing itself. Please see the example to illustrate.

The xtelligent_serial.json namespace includes two convenience methods for reading and writing to and from JSON. The from_json and to_json functions are documented here. The functions serialize types that your code supports with the serialization decorators.

Automatic dataclass support

Python's dataclass with the frozen option creates the rough equivalent of a NamedTuple, but you may still add methods and properties. Immutability is a great practice for creating testable, readable code. A frozen dataclass does not acquire the "infinite state machine" behaviors common to object-oriented class design. It is beyond the scope of this document to create full justification of immutability, and there is an abundance of material on this subject. It is obvious that a frozen dataclass maps very well to JSON documents. The consistent structure of dataclass types make it straightforward for this library to support these classes automatically, without decorators. The only caveat is that all member attribute types must be supported by decorators, or they must also be primitive or dataclass types. Again, please see the example to illustrate.

Documentation

API Reference

Example

Roadmap

  • Integration with json.JSONDecoder and json.JSONEncoder. For now, this module is an alternative to the json module.
  • Serialization convenience methods on the decorators.
  • Support for automatic deserialization. Right now, it is required to pass a parameter indicating the type to deserialize to.
  • Research automatic support for NamedTuple.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xtelligent-serial-1.0.47.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

xtelligent_serial-1.0.47-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file xtelligent-serial-1.0.47.tar.gz.

File metadata

  • Download URL: xtelligent-serial-1.0.47.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for xtelligent-serial-1.0.47.tar.gz
Algorithm Hash digest
SHA256 5e34a0f7112aa3f923e884fbd8ecb5e89ad5b2707851a5ad02fe3478c7f302fb
MD5 194bf495cc8307596529aa654d4393eb
BLAKE2b-256 530a9a2449c16b9f013b15f3b74e92e5e609a234907a3bbc644e76c425382e8e

See more details on using hashes here.

File details

Details for the file xtelligent_serial-1.0.47-py3-none-any.whl.

File metadata

  • Download URL: xtelligent_serial-1.0.47-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for xtelligent_serial-1.0.47-py3-none-any.whl
Algorithm Hash digest
SHA256 019fdda0052740ad54362b6f5fe05baa679d61aa3845a3e09833382eeac80869
MD5 b1418a00ec630d4b83afcb0040505cff
BLAKE2b-256 73eb7899ab0c4e1328666c41e16e07ba59eb36f74ea979f068f07964621abc67

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page