on master help sign in
Instances for Void/V1
commit551f191178f32ce41c0293d14fffa672579d22a2 authorWill Yager <will.yager@gmail.com> dateSat 18 Aug 2018, 19:51 (8 years ago) parent1a322a1
2 files changed+24−1
Data/Aeson/Types/FromJSON.hs+9−1 Data/Aeson/Types/ToJSON.hs+15
Data/Aeson/Types/FromJSON.hs+9−1
@@ -107,6 +107,7 @@ import Data.Time.Locale.Compat (defaultTimeLocale)
107107import Data.Traversable as Tr (sequence)
108108import Data.Vector (Vector)
109109import Data.Version (Version, parseVersion)
110+import Data.Void (Void)
110111import Data.Word (Word16, Word32, Word64, Word8)
111112import Foreign.Storable (Storable)
112113import Foreign.C.Types (CTime (..))
@@ -786,6 +787,11 @@ pmval .!= val = fromMaybe val <$> pmval
786787-- Generic parseJSON
787788-------------------------------------------------------------------------------
788789
790+instance GFromJSON arity V1 where
791+ -- Whereof we cannot format, thereof we cannot parse:
792+ gParseJSON _ _ _ = fail "Attempted to parse empty type"
793+
794+
789795instance OVERLAPPABLE_ (GFromJSON arity a) => GFromJSON arity (M1 i c a) where
790796 -- Meta-information, which is not handled elsewhere, is just added to the
791797 -- parsed value:
@@ -1198,7 +1204,9 @@ instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
11981204 parseJSON = parseJSON2
11991205 {-# INLINE parseJSON #-}
12001206
1201−
1207+instance FromJSON Void where
1208+ parseJSON _ = fail "Cannot parse Void"
1209+ {-# INLINE parseJSON #-}
12021210
12031211instance FromJSON Bool where
12041212 parseJSON = withBool "Bool" pure
Data/Aeson/Types/ToJSON.hs+15
@@ -14,6 +14,7 @@
1414{-# LANGUAGE TypeOperators #-}
1515{-# LANGUAGE TypeSynonymInstances #-}
1616{-# LANGUAGE UndecidableInstances #-}
17+{-# LANGUAGE EmptyCase #-}
1718
1819#if __GLASGOW_HASKELL__ >= 706
1920{-# LANGUAGE PolyKinds #-}
@@ -91,6 +92,7 @@ import Data.Time.Format (FormatTime, formatTime)
9192import Data.Time.Locale.Compat (defaultTimeLocale)
9293import Data.Vector (Vector)
9394import Data.Version (Version, showVersion)
95+import Data.Void (Void)
9496import Data.Word (Word16, Word32, Word64, Word8)
9597import Foreign.Storable (Storable)
9698import Foreign.C.Types (CTime (..))
@@ -702,6 +704,12 @@ instance ( AllNullary (a :+: b) allNullary
702704-- possible but makes error messages a bit harder to understand for missing
703705-- instances.
704706
707+instance GToJSON Value arity V1 where
708+ -- Empty values do not exist, which makes the job of formatting them
709+ -- rather easy:
710+ gToJSON _ _ x = case x of {}
711+ {-# INLINE gToJSON #-}
712+
705713instance ToJSON a => GToJSON Value arity (K1 i a) where
706714 -- Constant values are encoded using their ToJSON instance:
707715 gToJSON _opts _ = toJSON . unK1
@@ -1221,6 +1229,13 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
12211229 toEncoding = toEncoding2
12221230 {-# INLINE toEncoding #-}
12231231
1232+instance ToJSON Void where
1233+ toJSON x = case x of {}
1234+ {-# INLINE toJSON #-}
1235+
1236+ toEncoding x = case x of {}
1237+ {-# INLINE toEncoding #-}
1238+
12241239
12251240instance ToJSON Bool where
12261241 toJSON = Bool