on master help sign in
Instances of ‘ToJSON’ and ‘FromJSON’ for ‘Natural’
commit477036c10c4680cbc97e6ae1cc6f64dec948b324 authormrkkrp <markkarpov@opmbx.org> dateWed 2 Dec 2015, 14:46 (11 years ago) parent91ec3be
1 file changed+15−1
Data/Aeson/Types/Instances.hs+15−1
Data/Aeson/Types/Instances.hs+15−1
@@ -106,7 +106,9 @@ import qualified Data.Vector.Primitive as VP
106106import qualified Data.Vector.Storable as VS
107107import qualified Data.Vector.Unboxed as VU
108108
109−#if !MIN_VERSION_base(4,8,0)
109+#if MIN_VERSION_base(4,8,0)
110+import Numeric.Natural
111+#else
110112import Control.Applicative ((<$>), (<*>), pure)
111113import Data.Foldable (Foldable)
112114import Data.Monoid (mempty)
@@ -348,6 +350,18 @@ instance FromJSON Integer where
348350 parseJSON = withScientific "Integral" $ pure . truncate
349351 {-# INLINE parseJSON #-}
350352
353+#if MIN_VERSION_base(4,8,0)
354+instance ToJSON Natural where
355+ toJSON = toJSON . toInteger
356+ {-# INLINE toJSON #-}
357+
358+ toEncoding = toEncoding . toInteger
359+ {-# INLINE toEncoding #-}
360+
361+instance FromJSON Natural where
362+ parseJSON = withScientific "Integral" $ pure . truncate
363+#endif
364+
351365instance ToJSON Int8 where
352366 toJSON = Number . fromIntegral
353367 {-# INLINE toJSON #-}