Commits

basvandijk committed 8ab7c63

NaNs and infinite RealFloats are encoded to Null

  • Participants
  • Parent commits 9c977cb

Comments (0)

Files changed (1)

File Data/Aeson/Types/Class.hs

     {-# INLINE parseJSON #-}
 
 instance ToJSON Double where
-    toJSON = Number . realToFrac
+    toJSON = realFloatToJSON
     {-# INLINE toJSON #-}
 
+realFloatToJSON :: RealFloat a => a -> Value
+realFloatToJSON d
+    | isNaN d || isInfinite d = Null
+    | otherwise = Number $ realToFrac d
+{-# INLINE realFloatToJSON #-}
+
 instance FromJSON Double where
     parseJSON (Number s) = pure $ realToFrac s
     parseJSON Null       = pure (0/0)
     {-# INLINE parseJSON #-}
 
 instance ToJSON Number where
-    toJSON = Number . numberToScientific
+    toJSON (D d) = toJSON d
+    toJSON (I i) = Number $ fromInteger i
     {-# INLINE toJSON #-}
 
 instance FromJSON Number where
     {-# INLINE parseJSON #-}
 
 instance ToJSON Float where
-    toJSON = Number . realToFrac
+    toJSON = realFloatToJSON
     {-# INLINE toJSON #-}
 
 instance FromJSON Float where
     e = base10Exponent s
     c = coefficient s
 {-# INLINE scientificToNumber #-}
-
-numberToScientific :: Number -> Scientific
-numberToScientific (I i) = fromInteger i
-numberToScientific (D d) = realToFrac d
-{-# INLINE numberToScientific #-}