-
Notifications
You must be signed in to change notification settings - Fork 3
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
Non-finite values of IEEE 754 floats are not decodable #3
Comments
I suppose it's possible to write a decoder that would exhibit this behavior, but we would likely lose performance as it's not supported by simdjson: simdjson/simdjson#1540 |
It could be offered as an optional option for those that want to sacrifice performance for compatibility. Here is an implementation, what do you think? diff --git a/src/Data/Hermes.hs b/src/Data/Hermes.hs
index ef8eff4..5d5e52c 100644
--- a/src/Data/Hermes.hs
+++ b/src/Data/Hermes.hs
@@ -29,6 +29,7 @@ module Data.Hermes
, atKey
, atOptionalKey
, atOrderedKey
+ , atKeyNonFinite
-- * JSON pointer decoder
, Pointer
, mkPointer
@@ -745,6 +746,15 @@ bool = getBool
double :: Value -> Decoder Double
double = getDouble
+atKeyNonFinite :: Key -> Object -> Decoder Double
+atKeyNonFinite key obj = do
+ mString <- atKey key (nullable string) obj
+ case mString of
+ Just "+inf" -> pure $ 1/0
+ Just "-inf" -> pure $ (-1)/0
+ Just _ -> fail "Expected either a null or the strings 'inf' or '-inf'"
+ Nothing -> pure $ 0/0
+
-- | Parse a Scientific from a Value.
scientific :: Value -> Decoder Sci.Scientific
scientific = withRawByteString parseScientific In testing:
One problem is that with this error message, it could be confusing to the user because of how Alternative doesn't allow for composing error messages. It doesn't tell the user a JSON numeral is a valid value. One way to fix that would be to use https://hackage.haskell.org/package/validation .
|
I implemented your approach and benchmarked it against Let me know if that looks good to you, or if you think we can do better here. |
Ok, maybe you don't even wanna support this. But it would be nice to specify if this is on purpose.
Aeson 2 can roundtrip plus/minus infinity and NaN just fine:
But Hermes can't decode these encodings:
Given that people are likely to use this library as a replacement for Aeson, I think it would be useful to point out differences like this.
The text was updated successfully, but these errors were encountered: