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

feature: Options to reject floating-point NaN and Inf values on encode and decode #512

Closed
benluddy opened this issue Apr 2, 2024 · 2 comments

Comments

@benluddy
Copy link
Contributor

benluddy commented Apr 2, 2024

Is your feature request related to a problem? Please describe.

I am implementing CBOR support as a binary alternative to an existing (encoding/json-based) JSON serialization layer. It's essential that all objects entering the system can continue to be JSON-encoded for consumption by existing clients. The JSON serializer refuses to encode or decode floating-point NaN or +/- Inf values, so I want to be able to configure the CBOR serializer to reject them as well.

Describe the solution you'd like

Based on https://www.rfc-editor.org/rfc/rfc8949.html#section-5-3:

CBOR-based protocols MAY specify that they treat arbitrary valid data as unexpected.

I would like to be able to configure both EncMode and DecMode to return errors rather than encode or decode NaN and Inf floating-point values. Specifically:

  1. Extend the existing NaNConvertMode and InfConvertMode with new modes NaNConvertReject and InfConvertReject, respectively, because configuring an accepted conversion is mutually exclusive with rejection. If so configured, marshaling will return UnsupportedValueError when asked to encode a NaN or infinity value. The default mode for both options will remain unchanged.
  2. Add new decode options NaNDec and InfDec with "Allow" (default) and "Reject" modes. In the reject modes, unmarshaling will return UnacceptableDataItemError if a NaN or infinity floating-point value is encountered in the input.

Describe alternatives you've considered

Additional context

@benluddy
Copy link
Contributor Author

benluddy commented Apr 2, 2024

I have a complete implementation in #513 if this proposal seems reasonable and useful.

@fxamacker
Copy link
Owner

Thanks Ben! 🎉 Closed by #513.

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

No branches or pull requests

2 participants