Skip to main content

Float formatting control for JSON encoding.

Project description

PyPI PyPI - Python Version PyPI - Wheel Tests

FuJSON: less decimals, more fuzzy JSON

FuJSON is a Python module that allows to control the formatting of floats when JSON encoding, typically to dial down the number of decimals.

It builds on the standard library's json.JSONEncoder and monkey-patches it to inject custom float formatting.

Usage

Simple dumps call:

>>> from fujson import dumps
>>> dumps({"x": 1.23456789}, float_format=".2f")
'{"x": 1.23}'

Reusable encoder:

>>> from fujson import FuJsonEncoder
>>> encoder = FuJsonEncoder(float_format=".2f")
>>> encoder.encode({"x": 1.23456789})
'{"x": 1.23}'
>>> encoder.encode([1.1, 2.22, 3.333, 4.4444])
'[1.10, 2.22, 3.33, 4.44]'

Do JSON dump with floats in scientific notation:

>>> from fujson import FuJsonEncoder
>>> encoder = FuJsonEncoder(float_format=".2e")
>>> encoder.encode([.123, 0.0000123, 12345678.9])
'[1.23e-01, 1.23e-05, 1.23e+07]'

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

fujson-0.1.1.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distribution

fujson-0.1.1-py3-none-any.whl (3.6 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