Support encoding decimals as numbers - #465
Merged
Merged
Conversation
Previously we always encoded `decimal.Decimal` values as strings. While this is the best format for these types (prevents precision loss, widely supported), sometimes there is a need to encode `Decimal` objects the same as numbers. This adds a new `decimal_format` option to all `Encoder` classes. This value defaults to `"string"` (to encode decimals as strings), but may be set to `"number"` to encode them the same as the protocol's numeric types. We opt not to support this configuration in the top-level `encode` functions for now, since this is a less-common setting.
Member
Author
|
A quick demo: In [1]: import msgspec, decimal
In [2]: x = decimal.Decimal("1.3")
In [3]: msgspec.json.encode(x) # by default encodes as a string
Out[3]: b'"1.3"'
In [4]: encoder = msgspec.json.Encoder(decimal_format="number") # create an encoder to override
In [5]: encoder.encode(x) # now encodes as a number
Out[5]: b'1.3'
In [6]: import json
In [7]: decimal.Decimal(json.loads(b'1.3')) # downstream tools may have trouble loading decimals from numbers without precision loss
Out[7]: Decimal('1.3000000000000000444089209850062616169452667236328125')
In [8]: msgspec.json.decode(b'1.3', type=decimal.Decimal) # msgspec has no issues with this though, but other tools may
Out[8]: Decimal('1.3') |
Member
Author
|
If you wish to try this out before the next release, please see our instructions for installing from GitHub. |
|
Brilliant, thank you very much for taking this on board and providing the ability to output as a decimal. I do take you point that there may be a loss of precision. I will utilize configuration to select whether we wish to output as a string or decimal using this feature. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously we always encoded
decimal.Decimalvalues as strings. While this is the best format for these types (prevents precision loss, widely supported), sometimes there is a need to encodeDecimalobjects the same as numbers. This adds a newdecimal_formatoption to allEncoderclasses. This value defaults to"string"(to encode decimals as strings), but may be set to"number"to encode them the same as the protocol's numeric types.We opt not to support this configuration in the top-level
encodefunctions for now, since this is a less-common setting.Fixes #440.