hermes icon indicating copy to clipboard operation
hermes copied to clipboard

Non-finite values of IEEE 754 floats are not decodable

Open ysangkok opened this issue 2 years ago • 3 comments

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:

repl> Data.Aeson.decode @Double $ encode @Double ((1)/0)
Just Infinity
repl> Data.Aeson.decode @Double $ encode @Double ((-1)/0)
Just (-Infinity)
repl> Data.Aeson.decode @Double $ encode @Double ((0)/0)
Just NaN

But Hermes can't decode these encodings:

repl> newtype Person = Person Double deriving (Eq, Show)
repl> personDecoder = Data.Hermes.withObject $ \obj -> Person <$> atKey "lol" double obj
repl> Data.Hermes.decodeEither personDecoder $ "{\"lol\": " <> (toStrict $ encode @Double ((1)/0)) <> "}"
Left (SIMDException (HError {path = "/lol", errorMsg = "Error while getting value of type double. The JSON element does not have the requested type.", docLocation = "\"+inf\"}", docDebug = "json_iterator [ depth : 2, structural : '\"', offset : 8', error : No error ]"}))
repl> Data.Hermes.decodeEither personDecoder $ "{\"lol\": " <> (toStrict $ encode @Double ((-1)/0)) <> "}"
Left (SIMDException (HError {path = "/lol", errorMsg = "Error while getting value of type double. The JSON element does not have the requested type.", docLocation = "\"-inf\"}", docDebug = "json_iterator [ depth : 2, structural : '\"', offset : 8', error : No error ]"}))
repl> Data.Hermes.decodeEither personDecoder $ "{\"lol\": " <> (toStrict $ encode @Double ((0)/0)) <> "}"
Left (SIMDException (HError {path = "/lol", errorMsg = "Error while getting value of type double. The JSON element does not have the requested type.", docLocation = "null}", docDebug = "json_iterator [ depth : 2, structural : 'n', offset : 8', error : No error ]"}))

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.

ysangkok avatar Nov 04 '21 23:11 ysangkok