Skip to main content

Serialize and Deserialize Python Classes using JSON

Project description

Weaver - Read and Write Python Objects

Green Python in a Woven Basket

Weaver writes Python objects to a JSON format, storing the larger items as binary blobs on the local filesystem.

Why?

I wanted a way of saving Python objects that could still be examined in a common, human-readable, format, while still containing all the original data. Also it's nice to be able to load and save from different versions of a library for breaking changes.

How?

Same things as Pickle really. Save all the object state to a dictionary, throw it into JSON. When it comes to load, create a new object via __new__ and add the state back in. Then do that recursively both ways.

What are the requirements for loading them back?

You need the same libraries loaded. Maybe not the same versions of those libraries though. If you have a data format that you need to save in one version, and load in the other, you can implement serializers for it;

Let's write a serializer for a class that looks like set from base python.

from weaver.registry import WeaverSerializer, WeaverRegistry
from weaver.data import WovenClass, ItemMetadataWithVersion
from weaver.version import Version
from my_package import MySet
from typing import Any, Callable

class WeaverSetSerializer(WeaverSerializer[set]):
    _metadata = ItemMetadataWithVersion(
        module=tuple(["MyLibrary"]), name="MySet", version=Version(0, 1, 1)
    )

    @classmethod
    def weave(
            cls,
            item: MySet,
            registry: WeaverRegistry,
            cache: dict[int, Any],
            weave_fn: Callable,
    ) -> WovenClass:
        return WovenClass(
            pointer=id(item),
            metadata=cls._metadata,
            artefacts=set(),
            documentation={cls._metadata: MySet.__doc__},
            method_source={},
            json={"__inner__": [weave_fn(i) for i in item]},
        )

And a deserializer on the other side for our custom class that looks a lot like set.

from weaver.registry import WeaverDeserializer, WeaverRegistry
from weaver.data import WovenClass, ItemMetadataWithVersion
from weaver.version import Version
from my_package import MySet
from typing import Any, Callable

class WeaverSetDeserializer(WeaverDeserializer[MySet]):
    _metadata = ItemMetadataWithVersion(
        module=tuple(["MyLibrary"]), name="MySet", version=Version(0, 1, 1)
    )

    @classmethod
    def unweave(
            cls,
            item: WovenClass,
            registry: WeaverRegistry,
            cache: dict[int, Any],
            unweave_fn: Callable,
    ) -> MySet:
        return MySet({unweave_fn(i) for i in item.json["__inner__"]})

So we can read from Version 0.1.1 and write to Version 0.1.1. We can also specify that we can read from 'AllVersions', although more complex constraints don't exist yet.

Isn't this Camel, but for JSON?

Yes, but with tweaks. We fall back to Pickle, they had a clear philosophy against it.

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

weaver_json-0.0.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

weaver_json-0.0.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file weaver_json-0.0.2.tar.gz.

File metadata

  • Download URL: weaver_json-0.0.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Windows/10

File hashes

Hashes for weaver_json-0.0.2.tar.gz
Algorithm Hash digest
SHA256 73a68d98d16e6d66843d6bb008f20e568a75b0dae30c2029bfb9d694a5940fab
MD5 5e0aba3875772ec4eba80001167f80e0
BLAKE2b-256 08d67650fe6b684cde1f93b8c6539334c1be33e8a5ec187cff7cf87359395e84

See more details on using hashes here.

File details

Details for the file weaver_json-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: weaver_json-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Windows/10

File hashes

Hashes for weaver_json-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c2dd31bdb8a598099ae272c4dad977659e877d3b21f7fddd10f8acd77d2518aa
MD5 7f3db7250b71512f4c16faa26268e02c
BLAKE2b-256 642cec4b7c81e946e495db11a6f955b992701370869733d235244c7fc3f6f266

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 Pingdom Monitoring Sentry Error logging StatusPage Status page