on master help sign in
Add Key instances for time-types
1874b37Oleg Grenrus
Data/Aeson/Types/FromJSON.hs+20
@@ -1382,18 +1382,38 @@ instance FromJSON DotNetTime where
1382instance FromJSON Day where
1382instance FromJSON Day where
1383 parseJSON = withText "Day" (Time.run Time.day)
1383 parseJSON = withText "Day" (Time.run Time.day)
1384
1384
1385+instance FromJSONKey Day where
1386+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.day)
1387+
1388+
1385instance FromJSON TimeOfDay where
1389instance FromJSON TimeOfDay where
1386 parseJSON = withText "TimeOfDay" (Time.run Time.timeOfDay)
1390 parseJSON = withText "TimeOfDay" (Time.run Time.timeOfDay)
1387
1391
1392+instance FromJSONKey TimeOfDay where
1393+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.timeOfDay)
1394+
1395+
1388instance FromJSON LocalTime where
1396instance FromJSON LocalTime where
1389 parseJSON = withText "LocalTime" (Time.run Time.localTime)
1397 parseJSON = withText "LocalTime" (Time.run Time.localTime)
1390
1398
1399+instance FromJSONKey LocalTime where
1400+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.localTime)
1401+
1402+
1391instance FromJSON ZonedTime where
1403instance FromJSON ZonedTime where
1392 parseJSON = withText "ZonedTime" (Time.run Time.zonedTime)
1404 parseJSON = withText "ZonedTime" (Time.run Time.zonedTime)
1393
1405
1406+instance FromJSONKey ZonedTime where
1407+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.zonedTime)
1408+
1409+
1394instance FromJSON UTCTime where
1410instance FromJSON UTCTime where
1395 parseJSON = withText "UTCTime" (Time.run Time.utcTime)
1411 parseJSON = withText "UTCTime" (Time.run Time.utcTime)
1396
1412
1413+instance FromJSONKey UTCTime where
1414+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.utcTime)
1415+
1416+
1397-- | /WARNING:/ Only parse lengths of time from trusted input
1417-- | /WARNING:/ Only parse lengths of time from trusted input
1398-- since an attacker could easily fill up the memory of the target
1418-- since an attacker could easily fill up the memory of the target
1399-- system by specifying a scientific number with a big exponent like
1419-- system by specifying a scientific number with a big exponent like
Data/Aeson/Types/ToJSON.hs+17
@@ -1676,24 +1676,41 @@ instance ToJSON Day where
1676 toJSON = stringEncoding . E.day
1676 toJSON = stringEncoding . E.day
1677 toEncoding = E.day
1677 toEncoding = E.day
1678
1678
1679+instance ToJSONKey Day where
1680+ toJSONKey = toJSONKeyTextEnc E.day
1681+
1679
1682
1680instance ToJSON TimeOfDay where
1683instance ToJSON TimeOfDay where
1681 toJSON = stringEncoding . E.timeOfDay
1684 toJSON = stringEncoding . E.timeOfDay
1682 toEncoding = E.timeOfDay
1685 toEncoding = E.timeOfDay
1683
1686
1687+instance ToJSONKey TimeOfDay where
1688+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
1689+
1684
1690
1685instance ToJSON LocalTime where
1691instance ToJSON LocalTime where
1686 toJSON = stringEncoding . E.localTime
1692 toJSON = stringEncoding . E.localTime
1687 toEncoding = E.localTime
1693 toEncoding = E.localTime
1688
1694
1695+instance ToJSONKey LocalTime where
1696+ toJSONKey = toJSONKeyTextEnc E.localTime
1697+
1698+
1689instance ToJSON ZonedTime where
1699instance ToJSON ZonedTime where
1690 toJSON = stringEncoding . E.zonedTime
1700 toJSON = stringEncoding . E.zonedTime
1691 toEncoding = E.zonedTime
1701 toEncoding = E.zonedTime
1692
1702
1703+instance ToJSONKey ZonedTime where
1704+ toJSONKey = toJSONKeyTextEnc E.zonedTime
1705+
1706+
1693instance ToJSON UTCTime where
1707instance ToJSON UTCTime where
1694 toJSON = stringEncoding . E.utcTime
1708 toJSON = stringEncoding . E.utcTime
1695 toEncoding = E.utcTime
1709 toEncoding = E.utcTime
1696
1710
1711+instance ToJSONKey UTCTime where
1712+ toJSONKey = toJSONKeyTextEnc E.utcTime
1713+
1697-- | Encode something t a JSON string.
1714-- | Encode something t a JSON string.
1698stringEncoding :: Encoding -> Value
1715stringEncoding :: Encoding -> Value
1699stringEncoding = String
1716stringEncoding = String
aeson.cabal+3
@@ -174,6 +174,9 @@ test-suite tests
174 if !impl(ghc >= 7.10)
174 if !impl(ghc >= 7.10)
175 build-depends: nats >=1 && <1.2
175 build-depends: nats >=1 && <1.2
176
176
177+ if impl(ghc >= 7.8)
178+ build-depends: hashable-time >= 0.2 && <0.3
179+
177source-repository head
180source-repository head
178 type: git
181 type: git
179 location: git://github.com/bos/aeson.git
182 location: git://github.com/bos/aeson.git
tests/Instances.hs+3
@@ -28,6 +28,9 @@ import qualified Data.DList as DList
28
28
29import Data.Orphans ()
29import Data.Orphans ()
30import Test.QuickCheck.Instances ()
30import Test.QuickCheck.Instances ()
31+#if MIN_VERSION_base(4,7,0)
32+import Data.Hashable.Time ()
33+#endif
31
34
32#if !MIN_VERSION_base(4,8,0) && !MIN_VERSION_QuickCheck(2,8,3)
35#if !MIN_VERSION_base(4,8,0) && !MIN_VERSION_QuickCheck(2,8,3)
33import Numeric.Natural
36import Numeric.Natural
tests/Properties.hs+7−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, RecordWildCards, ScopedTypeVariables #-}
1+{-# LANGUAGE CPP, OverloadedStrings, ScopedTypeVariables #-}
2
2
3module Properties where
3module Properties where
4
4
@@ -186,6 +186,12 @@ tests = testGroup "properties" [
186 , testProperty "Natural" $ roundTripKey (undefined :: Natural)
186 , testProperty "Natural" $ roundTripKey (undefined :: Natural)
187 , testProperty "Float" $ roundTripKey (undefined :: Float)
187 , testProperty "Float" $ roundTripKey (undefined :: Float)
188 , testProperty "Double" $ roundTripKey (undefined :: Double)
188 , testProperty "Double" $ roundTripKey (undefined :: Double)
189+#if MIN_VERSION_base(4,7,0)
190+ , testProperty "Day" $ roundTripKey (undefined :: Day)
191+ , testProperty "LocalTime" $ roundTripKey (undefined :: LocalTime)
192+ , testProperty "TimeOfDay" $ roundTripKey (undefined :: TimeOfDay)
193+ , testProperty "UTCTime" $ roundTripKey (undefined :: UTCTime)
194+#endif
189 ]
195 ]
190 , testGroup "toFromJSON" [
196 , testGroup "toFromJSON" [
191 testProperty "Integer" (toFromJSON :: Integer -> Property)
197 testProperty "Integer" (toFromJSON :: Integer -> Property)