on master help sign in
NaNs and infinite RealFloats are encoded to Null
commit1cb40a054d8ba60e27bdb081f56076c3caa59d6a authorBas van Dijk <v.dijk.bas@gmail.com> dateWed 23 Oct 2013, 23:07 (13 years ago) parent86793e2
1 file changed+10−8
Data/Aeson/Types/Class.hs+10−8
Data/Aeson/Types/Class.hs+10−8
@@ -313,9 +313,15 @@ instance FromJSON Char where
313313 {-# INLINE parseJSON #-}
314314
315315instance ToJSON Double where
316− toJSON = Number . realToFrac
316+ toJSON = realFloatToJSON
317317 {-# INLINE toJSON #-}
318318
319+realFloatToJSON :: RealFloat a => a -> Value
320+realFloatToJSON d
321+ | isNaN d || isInfinite d = Null
322+ | otherwise = Number $ realToFrac d
323+{-# INLINE realFloatToJSON #-}
324+
319325instance FromJSON Double where
320326 parseJSON (Number s) = pure $ realToFrac s
321327 parseJSON Null = pure (0/0)
@@ -323,7 +329,8 @@ instance FromJSON Double where
323329 {-# INLINE parseJSON #-}
324330
325331instance ToJSON Number where
326− toJSON = Number . numberToScientific
332+ toJSON (D d) = toJSON d
333+ toJSON (I i) = Number $ fromInteger i
327334 {-# INLINE toJSON #-}
328335
329336instance FromJSON Number where
@@ -333,7 +340,7 @@ instance FromJSON Number where
333340 {-# INLINE parseJSON #-}
334341
335342instance ToJSON Float where
336− toJSON = Number . realToFrac
343+ toJSON = realFloatToJSON
337344 {-# INLINE toJSON #-}
338345
339346instance FromJSON Float where
@@ -967,8 +974,3 @@ scientificToNumber s
967974 e = base10Exponent s
968975 c = coefficient s
969976{-# INLINE scientificToNumber #-}
970−
971−numberToScientific :: Number -> Scientific
972−numberToScientific (I i) = fromInteger i
973−numberToScientific (D d) = realToFrac d
974−{-# INLINE numberToScientific #-}