on master help sign in
ToJSON and FromJSON instances for Version type.
e18f9a9Daniel Díaz
Data/Aeson/Types/Instances.hs+14
@@ -79,6 +79,8 @@ import Data.Time.Format (FormatTime, formatTime, parseTime)
79import Data.Traversable as Tr (sequence)
79import Data.Traversable as Tr (sequence)
80import Data.Vector (Vector)
80import Data.Vector (Vector)
81import Data.Word (Word8, Word16, Word32, Word64)
81import Data.Word (Word8, Word16, Word32, Word64)
82+import Data.Version (Version, showVersion, parseVersion)
83+import Text.ParserCombinators.ReadP (readP_to_S)
82import Foreign.Storable (Storable)
84import Foreign.Storable (Storable)
83import Prelude hiding (foldr)
85import Prelude hiding (foldr)
84import qualified Data.Aeson.Encode.Builder as E
86import qualified Data.Aeson.Encode.Builder as E
@@ -1466,6 +1468,18 @@ instance FromJSON a => FromJSON (Last a) where
1466 parseJSON = fmap Last . parseJSON
1468 parseJSON = fmap Last . parseJSON
1467 {-# INLINE parseJSON #-}
1469 {-# INLINE parseJSON #-}
1468
1470
1471+instance ToJSON Version where
1472+ toJSON = toJSON . showVersion
1473+ {-# INLINE toJSON #-}
1474+
1475+instance FromJSON Version where
1476+ {-# INLINE parseJSON #-}
1477+ parseJSON = withText "Version" $ go . readP_to_S parseVersion . unpack
1478+ where
1479+ go [(v,_)] = return v
1480+ go (_ : xs) = go xs
1481+ go _ = fail $ "could not parse Version"
1482+
1469-- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object@
1483-- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object@
1470-- and fails using @'typeMismatch' expected@ otherwise.
1484-- and fails using @'typeMismatch' expected@ otherwise.
1471withObject :: String -> (Object -> Parser a) -> Value -> Parser a
1485withObject :: String -> (Object -> Parser a) -> Value -> Parser a