Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added an instance for the 'Fixed' class.

  • Loading branch information...
commit 4bc20647b6da193dc51447b59b23a4bd5823752d 1 parent 5781d6b
@cgaebel authored
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 . fromRational . toRational

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {-# 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 #-}
Please sign in to comment.
Something went wrong with that request. Please try again.