Skip to main content

DRY JSON encoder.

Project description

Kiwi JSON

Kiwi JSON Logo

Purpose

At the time of creating this lib, there were (at least) three implementation of default_encoder() copy-pasted from one place, evolving its own way and about to being copy-pasted further. If you have read the story about AI and paperclips, you have an idea where this would lead us to.

To prevent this from happening, this library should unify all the implementation, and provide reusable implementation of the JSON encoding.

Installation

Add kiwi-json into your requirements.in file

kiwi-json

Usage

If you use your own JSON encoder as a class, use default_encoder() in there.

import simplejson
from kw.json import default_encoder, mask_dict

class OurJSONEncoder(simplejson.JSONEncoder):

    def default(self, obj):
        return default_encoder(obj, mask_dict)

kiwi-json provides a simple implementation for masking dictionary values with kw.json.mask_dict. Or you can create a masking function for it by yourself. It supports customizing placeholder, blacklist and whitelist:

from kw.json import mask_dict_factory

mask_dict = mask_dict_factory(placeholder='0_0', blacklist={'secret'}, whitelist={'not-so-secret'})

If you want to use json.dumps directly, you can do it the following way:

import simplejson
from kw.json import default_encoder

dumps = partial(simplejson.dumps, default=default_encoder)

If you have simplejson installed, you can use Decimal as JSON number type:

from decimal import Decimal
from kw.json import dumps, loads

assert dumps({"num": Decimal("1.234")}, use_decimal=True) == '{"num": 1.234}'
assert loads('{"num": 1.234}', use_decimal=True) == {"num": Decimal("1.234")}

Flask-based application could utilize the extension:

from kw.json.flask import JSONExtension


def create_app():
    ...
    JSONExtension(app)
    ...

Extension will install an encoder to given app.

If you want to make sure that the encoder dumps classes, you can use the raw_encoder:

from kw.json import raw_encoder, dumps

dumps(data, default=raw_encoder)

To dump dates and datetimes as unix time, use date_as_unix_time=True:

import arrow
from datetime import datetime
from kw.json import dumps

dumps({1: datetime.now(), 2: arrow.now()}, date_as_unix_time=True)

If you want to combine the powers of date_as_unix_time and raw_encoder, you can create your own encoder using partial:

from kw.json import dumps, raw_encoder
from functools import partial

my_encoder = partial(raw_encoder, date_as_unix_time=True)
dumps(obj, default=my_encoder)

Running tests

To run the tests we use tox. Before you can run the tests please make sure you have postgres database running and the DATABASE_URI env variable set

export DATABASE_URI='postgres://[username]:[password]@[host]:[port]/[database]'

Once you have this set up just execute:

tox

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

kiwi-json-0.10.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

kiwi_json-0.10.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file kiwi-json-0.10.1.tar.gz.

File metadata

  • Download URL: kiwi-json-0.10.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for kiwi-json-0.10.1.tar.gz
Algorithm Hash digest
SHA256 bf73e1a0a47bd35263151e6f8bdeef66dfb07968021e5bb8eaebd53fb988d637
MD5 9ebee4dce819fcb408d96f3d0572f0d9
BLAKE2b-256 2ce8d7a900fd64e3fa6a4eaf35a80060036a8964f3df9542aa58d58e32b17d61

See more details on using hashes here.

File details

Details for the file kiwi_json-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: kiwi_json-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for kiwi_json-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4552f1e684dc28791c7d402b481cd8f02b2aa3804c3f399a2865bfbe29bb9d1
MD5 916042d9b3bb07bbe82d9fb4c0c8ea40
BLAKE2b-256 36b6dcfbd907deb7f1de77c2db072a3e5a0d760adcf39dee5aed9ee06e388e7a

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