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 details)

Uploaded Source

Built Distribution

fujson-0.1.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file fujson-0.1.1.tar.gz.

File metadata

  • Download URL: fujson-0.1.1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.11

File hashes

Hashes for fujson-0.1.1.tar.gz
Algorithm Hash digest
SHA256 46df837f4110eecdaf5195b5467e3cddba03869608252e0ae5ddf0cca7bf0bc6
MD5 ffccab605c736570e4ced041b87e34f4
BLAKE2b-256 03dff189796b6cc2d1e59886a1fbcb5f30c8922a2b1b36ff8c5a72c064253c81

See more details on using hashes here.

File details

Details for the file fujson-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fujson-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.11

File hashes

Hashes for fujson-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e3e5eab03f6d8c4fac4877ae022b96c7b9d09089957c275306315182764e95b
MD5 14183c57866800bf9eadc4d87eeda82a
BLAKE2b-256 7fe766e07c2be02210ed5b019e639170e9c2b0f4fdf7e0d4528ad0480ccc6dcd

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