Standard JSON
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
-
TypeError: Object of type datetime.date is not JSON serializable
cause Attempting to serialize `datetime.date` (or `datetime.datetime`, `datetime.time`, `decimal.Decimal`) objects using the standard `json.dumps` function without specifying `StandardJSONEncoder`.fixPass `cls=StandardJSONEncoder` to `json.dumps()` or `json.dump()`. Example: `json.dumps(data, cls=StandardJSONEncoder)`. -
AttributeError: module 'standardjson' has no attribute 'StandardJSONEncoder'
cause This error occurs if `StandardJSONEncoder` is not exposed directly in the `standardjson` top-level package or if an older version (pre-0.3.0) is in use with an incorrect import path, or if `__init__.py` does not re-export it.fixTry importing explicitly from the `encoders` submodule: `from standardjson.encoders import StandardJSONEncoder`. Ensure you are on version 0.3.0 or later for this path. -
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
cause Attempting to decode a string that is not valid JSON, such as one with single quotes instead of double quotes for keys/strings, unquoted keys, or trailing commas.fixEnsure the JSON string strictly adheres to the JSON specification (e.g., all keys and string values must be double-quoted, no trailing commas, no comments). Use a JSON validator to identify syntax errors.
Warnings
- deprecated The `standardjson` library appears unmaintained, with its last release (0.3.1) in May 2014. It has not received updates for Python 3.6+ compatibility or recent JSON specification changes.
- breaking Version 0.3.0 introduced breaking changes, including renaming the package to `standardjson` and moving `StandardJSONEncoder` into the `encoders` submodule.
- gotcha The library was primarily tested for Python 2.6, 2.7, and 3.3. While it might run on newer Python versions (3.4, 3.5), it is not officially supported or tested beyond these versions, which may lead to unexpected behavior or incompatibilities on Python 3.6+.
- gotcha Despite its name, Python's built-in `json` module is not strictly compliant with the JSON specification (ECMA-404 / RFC 8259) by default, particularly regarding `NaN`, `Infinity`, and `-Infinity` floating-point values which it encodes as their JavaScript equivalents rather than raising an error. `standardjson` aims for stricter compliance.
Install
-
pip install standardjson
Imports
- StandardJSONEncoder
from standardjson import StandardJSONEncoder
- StandardJSONEncoder (pre-0.3.0)
from standardjson import StandardJSONEncoder
from standardjson.encoders import StandardJSONEncoder
Quickstart
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"}