on master help sign in
Added an instance for the 'Fixed' class.
1034181Clark Gaebel
Data/Aeson/Types/Class.hs+12
@@ -43,6 +43,7 @@ import Control.Applicative ((<$>), (<*>), pure)
43import Data.Aeson.Functions
43import Data.Aeson.Functions
44import Data.Aeson.Types.Internal
44import Data.Aeson.Types.Internal
45import Data.Attoparsec.Char8 (Number(..))
45import Data.Attoparsec.Char8 (Number(..))
46+import Data.Fixed
46import Data.Hashable (Hashable(..))
47import Data.Hashable (Hashable(..))
47import Data.Int (Int8, Int16, Int32, Int64)
48import Data.Int (Int8, Int16, Int32, Int64)
48import Data.Maybe (fromMaybe)
49import Data.Maybe (fromMaybe)
@@ -305,6 +306,17 @@ instance FromJSON (Ratio Integer) where
305 parseJSON v = typeMismatch "Ratio Integer" v
306 parseJSON v = typeMismatch "Ratio Integer" v
306 {-# INLINE parseJSON #-}
307 {-# INLINE parseJSON #-}
307
308
309+instance HasResolution a => ToJSON (Fixed a) where
310+ toJSON = Number . fromRational . toRational
311+ {-# INLINE toJSON #-}
312+
313+instance HasResolution a => FromJSON (Fixed a) where
314+ parseJSON (Number n) = pure $ case n of
315+ D d -> fromRational $ toRational d
316+ I i -> fromIntegral i
317+ parseJSON v = typeMismatch "Fixed" v
318+ {-# INLINE parseJSON #-}
319+
308instance ToJSON Int where
320instance ToJSON Int where
309 toJSON = Number . fromIntegral
321 toJSON = Number . fromIntegral
310 {-# INLINE toJSON #-}
322 {-# INLINE toJSON #-}