Skip to main content

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

Release files for kiwi-json 0.10.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kiwi-json 0.10.1
File Size Uploaded
kiwi-json-0.10.1.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kiwi-json 0.10.1
File Interpreter ABI Platform
kiwi_json-0.10.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / kiwi-json-0.10.1.tar.gz

Download URL kiwi-json-0.10.1.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bf73e1a0a47bd35263151e6f8bdeef66dfb07968021e5bb8eaebd53fb988d637
BLAKE2b-256 checksum
How to use checksums
2ce8d7a900fd64e3fa6a4eaf35a80060036a8964f3df9542aa58d58e32b17d61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / kiwi_json-0.10.1-py3-none-any.whl

Download URL kiwi_json-0.10.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4552f1e684dc28791c7d402b481cd8f02b2aa3804c3f399a2865bfbe29bb9d1
BLAKE2b-256 checksum
How to use checksums
36b6dcfbd907deb7f1de77c2db072a3e5a0d760adcf39dee5aed9ee06e388e7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page