Float formatting control for JSON encoding.
Project description
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
Release history Release notifications | RSS feed
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.0.tar.gz
(2.8 kB
view hashes)
Built Distribution
fujson-0.1.0-py3-none-any.whl
(3.7 kB
view hashes)