Commits

wowus committed dfaba1c

Added an instance for the 'Fixed' class.

Comments (0)

Files changed (1)

Data/Aeson/Types/Class.hs

 import Data.Aeson.Functions
 import Data.Aeson.Types.Internal
 import Data.Attoparsec.Char8 (Number(..))
+import Data.Fixed
 import Data.Hashable (Hashable(..))
 import Data.Int (Int8, Int16, Int32, Int64)
 import Data.Maybe (fromMaybe)
     parseJSON v          = typeMismatch "Ratio Integer" v
     {-# INLINE parseJSON #-}
 
+instance HasResolution a => ToJSON (Fixed a) where
+    toJSON = Number . fromRational . toRational
+    {-# INLINE toJSON #-}
+
+instance HasResolution a => FromJSON (Fixed a) where
+    parseJSON (Number n) = pure $ case n of
+                                    D d -> fromRational $ toRational d
+                                    I i -> fromIntegral i
+    parseJSON v          = typeMismatch "Fixed" v
+    {-# INLINE parseJSON #-}
+
 instance ToJSON Int where
     toJSON = Number . fromIntegral
     {-# INLINE toJSON #-}