Skip to main content

json with rudimentary type encoding/decoding for Python

Project description

Adds support for a couple of new Python magic methods to make Python object JSON encoding and decoding a bit easier.

Tutorial

Instead of:

from json import loads, dumps
from uuid import UUID, uuid4

obj = uuid4()
encoded = dumps(str(obj))
decoded = UUID(loads(encoded))

assert obj == decoded

You can do:

from jsonlight import loads, dumps
from uuid import UUID, uuid4

obj = uuid4()
encoded = dumps(obj)
decoded = loads(UUID, encoded)
assert obj == decoded

This is because jsonlight patches uuid.UUID class to add the following methods:

  • __jsondump__: return a representation of self with JSON data types

  • __jsonload__: instanciate an object based on the result from __jsondump__

You can see that the main difference with json.loads is that jsonlight.loads requires a type as first argument. This is because jsonlight.loads will first call json.loads to convert the string into a Python object with basic JSON tyes, and then pass that to the type’s __jsonload__ function.

Other types can’t be monkey patched, so you have to import them from jsonlight instead, which is the sad case of datetime:

from jsonlight import loads, dumps, datetime
obj = datetime.now()
assert obj == loads(datetime, dumps(obj))

You may also define __jsondump__ and __jsonload__ methods on your own classes, example:

from jsonlight import load

class YourClass:
    def __init__(self, uuid=None):
        self.uuid = uuid or uuid4()

    def __jsondump__(self):
        return dict(uuid=self.uuid)

    @classmethod
    def __jsonload__(cls, data):
        return cls(load(UUID, data['uuid'])

        # This also works, but would not illustrate how to support recursion
        # return cls(UUID(data['uuid']))

As you can see:

  • you don’t have to worry about calling __jsondump__ on return values of your own __jsondump__ because jsonlight.dumps will do that recursively,

  • you have full control on deserialization just like with __setstate__,

Monkey-patches

Monkey-patched stdlib objects are:

  • UUID

  • Path

Feel free to add more.

Stdlib objects that couldn’t be monkey patched, and that you have to import from jsonlight instead are:

  • datetime

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

jsonlight-0.0.1.dev2.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file jsonlight-0.0.1.dev2.tar.gz.

File metadata

  • Download URL: jsonlight-0.0.1.dev2.tar.gz
  • Upload date:
  • Size: 2.8 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.8.2

File hashes

Hashes for jsonlight-0.0.1.dev2.tar.gz
Algorithm Hash digest
SHA256 74412845610151bc2923bdc8888d6e6b87a0c4a4dac73b561b2f19cb6fa180ba
MD5 fcbcc4332d5f85c17614cd302a9836f1
BLAKE2b-256 56b22a7a431bdf4ac33316c961c86e2b012f1eabab5767524d2943d51402caf9

See more details on using hashes here.

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