Coder Social home page Coder Social logo

kiwi-json's Introduction

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

kiwi-json's People

Contributors

lucas03 avatar yedpodtrzitko avatar aexvir avatar batisteo avatar janbednarik avatar

Stargazers

Sonny Lazuardi avatar josef podany avatar

Watchers

Ivica Munitić avatar James Cloos avatar Petr Šnobelt avatar Stanislav Komanec avatar Jozef Képesi avatar Pavel Řezníček avatar  avatar  avatar Laďa Radoň avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.