Skip to content

Commit

Permalink
Merge pull request #89 from wowus/master
Browse files Browse the repository at this point in the history
Added an instance for the 'Fixed' class.
  • Loading branch information
bos committed Nov 20, 2012
2 parents 48a750f + 558d4b7 commit 69130db
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Data/Aeson/Types/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 . realToFrac
{-# INLINE toJSON #-}

instance HasResolution a => FromJSON (Fixed a) where
parseJSON (Number n) = pure $ case n of
D d -> realToFrac d
I i -> fromIntegral i
parseJSON v = typeMismatch "Fixed" v
{-# INLINE parseJSON #-}

instance ToJSON Int where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
Expand Down

0 comments on commit 69130db

Please sign in to comment.