Skip to main content
  • json encoder uses singledispatch pattern instead of JSONEncoder class overwrites.

  • No more json.dumps(data, cls=MyJSONEncoder) everywhere.

  • Comes with default serialization for time, date, datetime, UUID and Decimal

  • Easy to use, easy to change serialization behaviour

  • Not tight to any json implementation json, simplejson, ujson

  • It parse json float numbers into Decimal objects to prevent python float precision issues.

https://travis-ci.org/NZME/json-encoder.svg?branch=master&maxAge=259200 https://img.shields.io/pypi/v/json-encoder.svg?maxAge=259200

Installation

$ pip install json-encoder

Quick start

  • Use “json_encoder.json” instead of default python json:

    from json_encoder import json
    
    result = json.dumps(data)

Configuration

  • Chose json implementation:

    # simplejson library is used as default json implementation if present
    # otherwise standard python json implementation is used
    # to use other json implementation globally, do:
    
    import ujson
    from json_encoder import use_json_library
    
    use_json_library(ujson)
  • To change json implementation for concrete call do:

    from json_encoder import json
    import simplejson
    
    result = json.dumps(data, json=simplejson)
  • To make your object JSON serializable do:

    # example how to make python fraction object json serializable
    
    from fractions import Fraction
    from json_encoder.encoder import json_encoder
    
    @json_encoder.register(Fraction)
    def encode_fraction(obj):
        return '{}/{}'.format(obj.numerator, obj.denominator)
  • To overwrite JSON serializer behaviour defined in json_encoder.encoder:

    from uuid import UUID
    from six import text_type
    from json_encoder.encoder import json_encoder
    
    @json_encoder.register(UUID)
    def encode_uuid(obj):
        return text_type(obj).replace('-', '')

Requirements

Release files for json-encoder 0.4.4

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

Source distribution (sdist)

Source distribution for json-encoder 0.4.4
File Size Uploaded
json-encoder-0.4.4.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-encoder 0.4.4
File Interpreter ABI Platform
json_encoder-0.4.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size:12.6 kB

Release files / json-encoder-0.4.4.tar.gz

Download URL json-encoder-0.4.4.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
26e9711008a44e379d0689108738f2365e653dc17961bdbe701f61ad165beab5
BLAKE2b-256 checksum
How to use checksums
cc6aacc76d660b75f6c565ee80d907296f46e72ebd49fc997df8b253833a2fad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / json_encoder-0.4.4-py2.py3-none-any.whl

Download URL json_encoder-0.4.4-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ca5c9aa211ac79d13f125c7d0554ae55a0c872cebc81a61fc4d228e9cc83efd8
BLAKE2b-256 checksum
How to use checksums
c41b6825d96e98bb90baf0498fdd6b5d2c735e3caf0f944bcb158bc0bd0ba68c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.4 This release

2 release files

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.0.2

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