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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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