Standard JSON

0.3.1 · active · verified Thu Apr 16

StandardJSON is a Python library that provides a JSON encoder aiming for full compliance with the ECMA-262 (JavaScript) and ECMA-404 (JSON Data Interchange) specifications. It extends Python's built-in `json.JSONEncoder` to natively serialize additional Python types such as `datetime.datetime`, `datetime.date`, `datetime.time`, and `decimal.Decimal` objects. The latest version is 0.3.1, released in May 2014, and the project appears to be unmaintained.

Common errors

Warnings

Install

Imports

Quickstart

Use `StandardJSONEncoder` as you would use `json.JSONEncoder` from the Python standard library, passing it to the `cls` argument of `json.dumps()` or `json.dump()`.

import datetime
import json
from standardjson import StandardJSONEncoder

# Example with datetime.date
data = {'event_date': datetime.date(2023, 1, 15)}
json_output = json.dumps(data, cls=StandardJSONEncoder, indent=2)
print(f"Serialized date: {json_output}")
# Expected output: {"event_date": "2023-01-15"}

import decimal

# Example with decimal.Decimal
data_decimal = {'price': decimal.Decimal('19.99')}
json_output_decimal = json.dumps(data_decimal, cls=StandardJSONEncoder, indent=2)
print(f"Serialized decimal: {json_output_decimal}")
# Expected output: {"price": "19.99"}

view raw JSON →