Permalink
Browse files

Added an instance for the 'Fixed' class.

  • Loading branch information...
1 parent 5781d6b commit 4bc20647b6da193dc51447b59b23a4bd5823752d @cgaebel committed Oct 25, 2012
Showing with 12 additions and 0 deletions.
  1. +12 −0 Data/Aeson/Types/Class.hs
View
@@ -43,6 +43,7 @@ import Control.Applicative ((<$>), (<*>), pure)
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)
@@ -305,6 +306,17 @@ instance FromJSON (Ratio Integer) where
parseJSON v = typeMismatch "Ratio Integer" v
{-# INLINE parseJSON #-}
+instance HasResolution a => ToJSON (Fixed a) where
+ toJSON = Number . fromRational . toRational
@basvandijk

basvandijk Oct 25, 2012

Why not use realToFrac for fromRational . toRational? (The same comment for the parseJSON case)

+ {-# 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 #-}

0 comments on commit 4bc2064

Please sign in to comment.