Skip to main content

json encoder using singledispatch pattern instead of JSONEncoder class overwrites

Project description

  • json encoder using singledispatch pattern instead of JSONEncoder class overwrites.

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

  • 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

https://travis-ci.org/NZME/json-encoder.svg?branch=master

Installation

$ pip install json-encoder

Quick start

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

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

Configuration

  • Chose json implementation:

    # simplejson library is uses as default json implementation
    # if simplejson isn't installed then python json implementation is used
    # to use other json implementation globally, do:
    
    import ujson as json
    from json_encoder import use_json_library
    
    use_json_library(json)
  • To change json implementation for concrete call do:

    from json_encoder import json
    import simplejson
    
    result = json.dumps(json, 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_decimal(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

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

json-encoder-0.4.2.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file json-encoder-0.4.2.tar.gz.

File metadata

  • Download URL: json-encoder-0.4.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for json-encoder-0.4.2.tar.gz
Algorithm Hash digest
SHA256 392d4576c35aed083a8ace06ed67df896291fdba7475bc9dcacea9adf59b2837
MD5 b56da27b8064c9418c59e80d3bd45efa
BLAKE2b-256 14a437213753ffccf6653b62d10d0bc521d0997800e0a87cdf031c79b02451fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page