Skip to main content

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

Source Code

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.

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.35.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xtelligent_serial-1.0.35-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xtelligent-serial-1.0.35.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.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.35.tar.gz
Algorithm Hash digest
SHA256 54579d781da68ab403c684f104ec37ddf2cdd40c9b61ea6d70ed502ea772783d
MD5 aba8795440ba819d48da30caf92f9644
BLAKE2b-256 792387b7561be679278324e27cc51f8f2dd0323a738b2dc0c174cc95a011220b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xtelligent_serial-1.0.35-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.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.35-py3-none-any.whl
Algorithm Hash digest
SHA256 36cbf0ad2ab42d5bb6660e9da9531d851737fd0fe2b3fce5f4921a9dd159ce29
MD5 8eb88fa8937038cf32f482619c936de2
BLAKE2b-256 15d32cbad906b98277f099fa261e00a29ad3a0a2f2bdf631e9a9b9e7d9cab296

See more details on using hashes here.

Supported by

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