on master help sign in
Add Key instances for time-types
commit1874b375b2d2ff5a3d88f424beacf8518b85ccdd authorOleg Grenrus <oleg.grenrus@iki.fi> dateWed 8 Jun 2016, 12:45 (10 years ago) parent5788284
5 files changed+50−1
Data/Aeson/Types/FromJSON.hs+20 Data/Aeson/Types/ToJSON.hs+17 aeson.cabal+3 tests/Instances.hs+3 tests/Properties.hs+7−1
Data/Aeson/Types/FromJSON.hs+20
@@ -1382,18 +1382,38 @@ instance FromJSON DotNetTime where
13821382instance FromJSON Day where
13831383 parseJSON = withText "Day" (Time.run Time.day)
13841384
1385+instance FromJSONKey Day where
1386+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.day)
1387+
1388+
13851389instance FromJSON TimeOfDay where
13861390 parseJSON = withText "TimeOfDay" (Time.run Time.timeOfDay)
13871391
1392+instance FromJSONKey TimeOfDay where
1393+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.timeOfDay)
1394+
1395+
13881396instance FromJSON LocalTime where
13891397 parseJSON = withText "LocalTime" (Time.run Time.localTime)
13901398
1399+instance FromJSONKey LocalTime where
1400+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.localTime)
1401+
1402+
13911403instance FromJSON ZonedTime where
13921404 parseJSON = withText "ZonedTime" (Time.run Time.zonedTime)
13931405
1406+instance FromJSONKey ZonedTime where
1407+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.zonedTime)
1408+
1409+
13941410instance FromJSON UTCTime where
13951411 parseJSON = withText "UTCTime" (Time.run Time.utcTime)
13961412
1413+instance FromJSONKey UTCTime where
1414+ fromJSONKey = FromJSONKeyTextParser (Time.run Time.utcTime)
1415+
1416+
13971417-- | /WARNING:/ Only parse lengths of time from trusted input
13981418-- since an attacker could easily fill up the memory of the target
13991419-- 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
16761676 toJSON = stringEncoding . E.day
16771677 toEncoding = E.day
16781678
1679+instance ToJSONKey Day where
1680+ toJSONKey = toJSONKeyTextEnc E.day
1681+
16791682
16801683instance ToJSON TimeOfDay where
16811684 toJSON = stringEncoding . E.timeOfDay
16821685 toEncoding = E.timeOfDay
16831686
1687+instance ToJSONKey TimeOfDay where
1688+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
1689+
16841690
16851691instance ToJSON LocalTime where
16861692 toJSON = stringEncoding . E.localTime
16871693 toEncoding = E.localTime
16881694
1695+instance ToJSONKey LocalTime where
1696+ toJSONKey = toJSONKeyTextEnc E.localTime
1697+
1698+
16891699instance ToJSON ZonedTime where
16901700 toJSON = stringEncoding . E.zonedTime
16911701 toEncoding = E.zonedTime
16921702
1703+instance ToJSONKey ZonedTime where
1704+ toJSONKey = toJSONKeyTextEnc E.zonedTime
1705+
1706+
16931707instance ToJSON UTCTime where
16941708 toJSON = stringEncoding . E.utcTime
16951709 toEncoding = E.utcTime
16961710
1711+instance ToJSONKey UTCTime where
1712+ toJSONKey = toJSONKeyTextEnc E.utcTime
1713+
16971714-- | Encode something t a JSON string.
16981715stringEncoding :: Encoding -> Value
16991716stringEncoding = String
aeson.cabal+3
@@ -174,6 +174,9 @@ test-suite tests
174174 if !impl(ghc >= 7.10)
175175 build-depends: nats >=1 && <1.2
176176
177+ if impl(ghc >= 7.8)
178+ build-depends: hashable-time >= 0.2 && <0.3
179+
177180source-repository head
178181 type: git
179182 location: git://github.com/bos/aeson.git
tests/Instances.hs+3
@@ -28,6 +28,9 @@ import qualified Data.DList as DList
2828
2929import Data.Orphans ()
3030import Test.QuickCheck.Instances ()
31+#if MIN_VERSION_base(4,7,0)
32+import Data.Hashable.Time ()
33+#endif
3134
3235#if !MIN_VERSION_base(4,8,0) && !MIN_VERSION_QuickCheck(2,8,3)
3336import Numeric.Natural
tests/Properties.hs+7−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, RecordWildCards, ScopedTypeVariables #-}
1+{-# LANGUAGE CPP, OverloadedStrings, ScopedTypeVariables #-}
22
33module Properties where
44
@@ -186,6 +186,12 @@ tests = testGroup "properties" [
186186 , testProperty "Natural" $ roundTripKey (undefined :: Natural)
187187 , testProperty "Float" $ roundTripKey (undefined :: Float)
188188 , 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
189195 ]
190196 , testGroup "toFromJSON" [
191197 testProperty "Integer" (toFromJSON :: Integer -> Property)