on master help sign in
Add ToJSON and FromJSON instances for Day

This fixes gh-252.

commit62541738d57090cbe0c127b7d0690cb96a1102a5 authorBryan O'Sullivan <bos@serpentine.com> dateTue 21 Jul 2015, 09:15 (11 years ago) parentf03650f
2 files changed+10−2
Data/Aeson/Types/Instances.hs+8−1 tests/Properties.hs+2−1
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 (UTCTime, ZonedTime)
80+import Data.Time (Day, UTCTime, ZonedTime)
8181import Data.Time.Format (FormatTime, formatTime, parseTime)
8282import Data.Traversable as Tr (sequence, traverse)
8383import Data.Vector (Vector)
@@ -711,6 +711,13 @@ instance FromJSON DotNetTime where
711711 _ -> fail "could not parse .NET time"
712712 {-# INLINE parseJSON #-}
713713
714+instance ToJSON Day where
715+ toJSON = stringEncoding
716+ toEncoding z = Encoding (E.day z)
717+
718+instance FromJSON Day where
719+ parseJSON = withText "Day" (Time.run Time.day)
720+
714721instance ToJSON ZonedTime where
715722 toJSON = stringEncoding
716723
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 (UTCTime, ZonedTime)
12+import Data.Time (Day, UTCTime, ZonedTime)
1313import Encoders
1414import Instances ()
1515import Test.Framework (Test, testGroup)
@@ -119,6 +119,7 @@ tests = testGroup "properties" [
119119 , testProperty "String" $ roundTripEq ("" :: String)
120120 , testProperty "Text" $ roundTripEq T.empty
121121 , testProperty "Foo" $ roundTripEq (undefined :: Foo)
122+ , testProperty "Day" $ roundTripEq (undefined :: Day)
122123 , testProperty "DotNetTime" $ roundTripEq (undefined :: DotNetTime)
123124 , testProperty "UTCTime" $ roundTripEq (undefined :: UTCTime)
124125 , testProperty "ZonedTime" $ roundTripEq (undefined :: ZonedTime)