cbor
cbor copied to clipboard
feature: Options to reject floating-point NaN and Inf values on encode and decode
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:
- Extend the existing
NaNConvertModeandInfConvertModewith new modesNaNConvertRejectandInfConvertReject, respectively, because configuring an accepted conversion is mutually exclusive with rejection. If so configured, marshaling will returnUnsupportedValueErrorwhen asked to encode a NaN or infinity value. The default mode for both options will remain unchanged. - Add new decode options
NaNDecandInfDecwith "Allow" (default) and "Reject" modes. In the reject modes, unmarshaling will returnUnacceptableDataItemErrorif a NaN or infinity floating-point value is encountered in the input.
Describe alternatives you've considered
Additional context
I have a complete implementation in https://github.com/fxamacker/cbor/pull/513 if this proposal seems reasonable and useful.
Thanks Ben! :tada: Closed by #513.