-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
Closed as duplicate of#60739
Closed as duplicate of#60739
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
Currently, the standard library json module does not natively serialize decimal.Decimal instances as JSON numbers. Users must either:
- Provide a custom default function
- Subclass JSONEncoder
- Or convert Decimal values manually before serialization
This proposal suggests adding an optional parameter (defaulting to False) that allows automatic conversion of Decimal instances to JSON numbers.
This way:
- Decimal is part of the Python standard library.
- JSON supports arbitrary precision numbers conceptually.
- Many web frameworks already provide similar functionality.
- This avoids repetitive custom encoder boilerplate.
- The default remains safe and backward compatible.
Current behaviour:
json.dumps({"price": Decimal("10.23")})
TypeError: Object of type Decimal is not JSON serializable
when serializing dict item 'price'
Desired behaviour:
json.dumps({"price": Decimal("10.23")}, decimal_as_number=True)
{"price": 19.99}
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
type-featureA feature request or enhancementA feature request or enhancement