Skip to content

Commit 42b690d

Browse files
author
Daniel Díaz
committed
FromJSON and ToJSON instances for CTime.
1 parent 475058c commit 42b690d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Data/Aeson/Types/FromJSON.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import Data.Vector (Vector)
109109
import Data.Version (Version, parseVersion)
110110
import Data.Word (Word16, Word32, Word64, Word8)
111111
import Foreign.Storable (Storable)
112+
import Foreign.C.Types (CTime (..))
112113
import GHC.Generics
113114
import Numeric.Natural (Natural)
114115
import Text.ParserCombinators.ReadP (readP_to_S)
@@ -1347,6 +1348,9 @@ instance FromJSON Word64 where
13471348
instance FromJSONKey Word64 where
13481349
fromJSONKey = FromJSONKeyTextParser $ parseBoundedIntegralText "Word64"
13491350

1351+
instance FromJSON CTime where
1352+
parseJSON = fmap CTime . parseJSON
1353+
{-# INLINE parseJSON #-}
13501354

13511355
instance FromJSON Text where
13521356
parseJSON = withText "Text" pure

Data/Aeson/Types/ToJSON.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import Data.Vector (Vector)
9191
import Data.Version (Version, showVersion)
9292
import Data.Word (Word16, Word32, Word64, Word8)
9393
import Foreign.Storable (Storable)
94+
import Foreign.C.Types (CTime (..))
9495
import GHC.Generics
9596
import Numeric.Natural (Natural)
9697
import qualified Data.Aeson.Encoding as E
@@ -1366,7 +1367,6 @@ instance ToJSONKey Int64 where
13661367
toJSONKey = toJSONKeyTextEnc E.int64Text
13671368
{-# INLINE toJSONKey #-}
13681369

1369-
13701370
instance ToJSON Word where
13711371
toJSON = Number . fromIntegral
13721372
{-# INLINE toJSON #-}
@@ -1426,6 +1426,12 @@ instance ToJSONKey Word64 where
14261426
toJSONKey = toJSONKeyTextEnc E.word64Text
14271427
{-# INLINE toJSONKey #-}
14281428

1429+
instance ToJSON CTime where
1430+
toJSON (CTime i) = toJSON i
1431+
{-# INLINE toJSON #-}
1432+
1433+
toEncoding (CTime i) = toEncoding i
1434+
{-# INLINE toEncoding #-}
14291435

14301436
instance ToJSON Text where
14311437
toJSON = String

0 commit comments

Comments
 (0)