on master help sign in
Change encoding of Rational to use full precision
21cfa8bBas van Dijk
Data/Aeson/Types/Class.hs+7−6
@@ -61,7 +61,7 @@ import Data.Hashable (Hashable(..))
61import Data.Int (Int8, Int16, Int32, Int64)
61import Data.Int (Int8, Int16, Int32, Int64)
62import Data.Maybe (fromMaybe)
62import Data.Maybe (fromMaybe)
63import Data.Monoid (Dual(..), First(..), Last(..), mappend)
63import Data.Monoid (Dual(..), First(..), Last(..), mappend)
64−import Data.Ratio (Ratio)
64+import Data.Ratio (Ratio, (%), numerator, denominator)
65import Data.Text (Text, pack, unpack)
65import Data.Text (Text, pack, unpack)
66import Data.Text.Encoding (encodeUtf8)
66import Data.Text.Encoding (encodeUtf8)
67import Data.Time (UTCTime, ZonedTime(..), TimeZone(..))
67import Data.Time (UTCTime, ZonedTime(..), TimeZone(..))
@@ -343,14 +343,15 @@ instance FromJSON Float where
343 {-# INLINE parseJSON #-}
343 {-# INLINE parseJSON #-}
344
344
345instance ToJSON (Ratio Integer) where
345instance ToJSON (Ratio Integer) where
346− toJSON = Number . fromRational
346+ toJSON r = object [ "numerator" .= numerator r
347+ , "denominator" .= denominator r
348+ ]
347 {-# INLINE toJSON #-}
349 {-# INLINE toJSON #-}
348
350
349instance FromJSON (Ratio Integer) where
351instance FromJSON (Ratio Integer) where
350− parseJSON = withNumber "Ration Integer" $ \n ->
352+ parseJSON = withObject "Rational" $ \obj ->
351− pure $ case n of
353+ (%) <$> obj .: "numerator"
352− D d -> toRational d
354+ <*> obj .: "denominator"
353− I i -> fromIntegral i
354 {-# INLINE parseJSON #-}
355 {-# INLINE parseJSON #-}
355
356
356instance HasResolution a => ToJSON (Fixed a) where
357instance HasResolution a => ToJSON (Fixed a) where