on master help sign in
Merge pull request #89 from wowus/master

Added an instance for the 'Fixed' class.

commitab4671230775973f8889105e6e6e242c6f1faddb authorBryan O'Sullivan <bos@serpentine.com> dateMon 19 Nov 2012, 20:05 (14 years ago) parentffdc19e3d4178f
1 file changed+12
Data/Aeson/Types/Class.hs+12
Data/Aeson/Types/Class.hs+12
@@ -43,6 +43,7 @@ import Control.Applicative ((<$>), (<*>), pure)
4343import Data.Aeson.Functions
4444import Data.Aeson.Types.Internal
4545import Data.Attoparsec.Char8 (Number(..))
46+import Data.Fixed
4647import Data.Hashable (Hashable(..))
4748import Data.Int (Int8, Int16, Int32, Int64)
4849import Data.Maybe (fromMaybe)
@@ -305,6 +306,17 @@ instance FromJSON (Ratio Integer) where
305306 parseJSON v = typeMismatch "Ratio Integer" v
306307 {-# INLINE parseJSON #-}
307308
309+instance HasResolution a => ToJSON (Fixed a) where
310+ toJSON = Number . realToFrac
311+ {-# INLINE toJSON #-}
312+
313+instance HasResolution a => FromJSON (Fixed a) where
314+ parseJSON (Number n) = pure $ case n of
315+ D d -> realToFrac d
316+ I i -> fromIntegral i
317+ parseJSON v = typeMismatch "Fixed" v
318+ {-# INLINE parseJSON #-}
319+
308320instance ToJSON Int where
309321 toJSON = Number . fromIntegral
310322 {-# INLINE toJSON #-}