Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parse_float (or some other name) in msgspec.json.decode #507

Closed
jcrist opened this issue Aug 4, 2023 · 0 comments · Fixed by #511
Closed

Support parse_float (or some other name) in msgspec.json.decode #507

jcrist opened this issue Aug 4, 2023 · 0 comments · Fixed by #511

Comments

@jcrist
Copy link
Owner

jcrist commented Aug 4, 2023

Description

The standard library's json.loads function includes a parse_float hook. If configured, this will be called for any floating point number with the raw input str. This may be used to parse all float values as decimal.Decimal types by passing parse_float=decimal.Decimal.

We might want to support an option like this. Due to msgspec's design, we'd probably go with:

  • Calling the option float_hook to mirror the other callback names
  • If configured this would be called to decode any float value that didn't have an explicit type configured (e.g. type=Any only). This would effectively let the user configure how untyped float values are decoded - float values with specified output types wouldn't use this mechanism.

To parse all untyped floats as decimals you'd do something like msgspec.json.decode(msg, float_hook=decimal.Decimal).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant