{"id":14938,"library":"standardjson","title":"Standard JSON","description":"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.","status":"active","version":"0.3.1","language":"en","source_language":"en","source_url":"https://github.com/audreyr/standardjson","tags":["json","serialization","datetime","decimal","encoder","compliance"],"install":[{"cmd":"pip install standardjson","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"StandardJSONEncoder","correct":"from standardjson import StandardJSONEncoder"},{"note":"In versions 0.3.0 and later, StandardJSONEncoder was moved to the 'encoders' module, though the top-level import might be re-exposed via __init__.py in 0.3.1. For strictness, or if the top-level import fails, use the explicit path.","wrong":"from standardjson import StandardJSONEncoder","symbol":"StandardJSONEncoder (pre-0.3.0)","correct":"from standardjson.encoders import StandardJSONEncoder"}],"quickstart":{"code":"import datetime\nimport json\nfrom standardjson import StandardJSONEncoder\n\n# Example with datetime.date\ndata = {'event_date': datetime.date(2023, 1, 15)}\njson_output = json.dumps(data, cls=StandardJSONEncoder, indent=2)\nprint(f\"Serialized date: {json_output}\")\n# Expected output: {\"event_date\": \"2023-01-15\"}\n\nimport decimal\n\n# Example with decimal.Decimal\ndata_decimal = {'price': decimal.Decimal('19.99')}\njson_output_decimal = json.dumps(data_decimal, cls=StandardJSONEncoder, indent=2)\nprint(f\"Serialized decimal: {json_output_decimal}\")\n# Expected output: {\"price\": \"19.99\"}","lang":"python","description":"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()`."},"warnings":[{"fix":"Consider using more actively maintained alternatives like `orjson`, `jsons`, or handling custom types with the `default` parameter of the standard `json` library for modern Python applications.","message":"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.","severity":"deprecated","affected_versions":"<=0.3.1"},{"fix":"If upgrading from pre-0.3.0, update imports from `from standardjson import StandardJSONEncoder` to `from standardjson.encoders import StandardJSONEncoder` if the top-level import is not resolved.","message":"Version 0.3.0 introduced breaking changes, including renaming the package to `standardjson` and moving `StandardJSONEncoder` into the `encoders` submodule.","severity":"breaking","affected_versions":"0.3.0, 0.3.1"},{"fix":"Thoroughly test on your target Python version or use a more modern JSON serialization library that explicitly supports your Python runtime.","message":"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+.","severity":"gotcha","affected_versions":"All versions on Python >3.5"},{"fix":"Be aware of these differences, especially when interacting with systems expecting strict JSON compliance. `standardjson` itself aims to adhere to the spec, but ensure the final output is validated against your receiving system's expectations if using other tools in the pipeline.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Pass `cls=StandardJSONEncoder` to `json.dumps()` or `json.dump()`. Example: `json.dumps(data, cls=StandardJSONEncoder)`.","cause":"Attempting to serialize `datetime.date` (or `datetime.datetime`, `datetime.time`, `decimal.Decimal`) objects using the standard `json.dumps` function without specifying `StandardJSONEncoder`.","error":"TypeError: Object of type datetime.date is not JSON serializable"},{"fix":"Try importing explicitly from the `encoders` submodule: `from standardjson.encoders import StandardJSONEncoder`. Ensure you are on version 0.3.0 or later for this path.","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.","error":"AttributeError: module 'standardjson' has no attribute 'StandardJSONEncoder'"},{"fix":"Ensure 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.","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.","error":"json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"}],"ecosystem":"pypi"}