on master help sign in
Add ToJSON and FromJSON instances for LocalTime
commit684baea3976238acdbe416a362dd9e10e0d763bb authorBryan O'Sullivan <bos@serpentine.com> dateTue 21 Jul 2015, 09:22 (11 years ago) parent6254173
4 files changed+17−4
Data/Aeson/Encode/Builder.hs+6−2 Data/Aeson/Parser/Time.hs+1 Data/Aeson/Types/Instances.hs+8−1 tests/Properties.hs+2−1
Data/Aeson/Encode/Builder.hs+6−2
@@ -25,6 +25,7 @@ module Data.Aeson.Encode.Builder
2525 , unquoted
2626 , number
2727 , day
28+ , localTime
2829 , utcTime
2930 , zonedTime
3031 , ascii2
@@ -228,9 +229,12 @@ diffTimeOfDay64 t = TOD (fromIntegral h) (fromIntegral m) s
228229toTimeOfDay64 :: TimeOfDay -> TimeOfDay64
229230toTimeOfDay64 (TimeOfDay h m s) = TOD h m (fromIntegral (fromPico s))
230231
232+localTime :: LocalTime -> Builder
233+localTime (LocalTime d t) = dayTime d (toTimeOfDay64 t)
234+{-# INLINE localTime #-}
235+
231236zonedTime :: ZonedTime -> Builder
232−zonedTime (ZonedTime (LocalTime d t) z) =
233− dayTime d (toTimeOfDay64 t) <> timeZone z
237+zonedTime (ZonedTime t z) = localTime t <> timeZone z
234238{-# INLINE zonedTime #-}
235239
236240data T = T {-# UNPACK #-} !Char {-# UNPACK #-} !Char
Data/Aeson/Parser/Time.hs+1
@@ -14,6 +14,7 @@ module Data.Aeson.Parser.Time
1414 (
1515 run
1616 , day
17+ , localTime
1718 , timeOfDay
1819 , timeZone
1920 , utcTime
Data/Aeson/Types/Instances.hs+8−1
@@ -77,7 +77,7 @@ import Data.Maybe (fromMaybe)
7777import Data.Monoid (Dual(..), First(..), Last(..))
7878import Data.Ratio (Ratio, (%), numerator, denominator)
7979import Data.Text (Text, pack, unpack)
80−import Data.Time (Day, UTCTime, ZonedTime)
80+import Data.Time (Day, LocalTime, UTCTime, ZonedTime)
8181import Data.Time.Format (FormatTime, formatTime, parseTime)
8282import Data.Traversable as Tr (sequence, traverse)
8383import Data.Vector (Vector)
@@ -718,6 +718,13 @@ instance ToJSON Day where
718718instance FromJSON Day where
719719 parseJSON = withText "Day" (Time.run Time.day)
720720
721+instance ToJSON LocalTime where
722+ toJSON = stringEncoding
723+ toEncoding z = Encoding (E.localTime z)
724+
725+instance FromJSON LocalTime where
726+ parseJSON = withText "LocalTime" (Time.run Time.localTime)
727+
721728instance ToJSON ZonedTime where
722729 toJSON = stringEncoding
723730
tests/Properties.hs+2−1
@@ -9,7 +9,7 @@ import Data.Aeson.Parser (value)
99import Data.Aeson.Types
1010import Data.ByteString.Builder (toLazyByteString)
1111import Data.Int (Int8)
12−import Data.Time (Day, UTCTime, ZonedTime)
12+import Data.Time (Day, LocalTime, UTCTime, ZonedTime)
1313import Encoders
1414import Instances ()
1515import Test.Framework (Test, testGroup)
@@ -121,6 +121,7 @@ tests = testGroup "properties" [
121121 , testProperty "Foo" $ roundTripEq (undefined :: Foo)
122122 , testProperty "Day" $ roundTripEq (undefined :: Day)
123123 , testProperty "DotNetTime" $ roundTripEq (undefined :: DotNetTime)
124+ , testProperty "LocalTime" $ roundTripEq (undefined :: LocalTime)
124125 , testProperty "UTCTime" $ roundTripEq (undefined :: UTCTime)
125126 , testProperty "ZonedTime" $ roundTripEq (undefined :: ZonedTime)
126127 , testGroup "ghcGenerics" [