Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added an instance for the 'Fixed' class. #89

Merged
merged 2 commits into from

3 participants

@cgaebel

Nothing fancy here. I did just what the title says.

@basvandijk

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

@cgaebel

Good call. Done.

@bos bos merged commit 69130db into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +12 −0 Data/Aeson/Types/Class.hs
View
12 Data/Aeson/Types/Class.hs
@@ -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 . 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 #-}
Something went wrong with that request. Please try again.