on master help sign in
Merge pull request #663 from wyager/master

Instances for Void/V1

commit7faaa74d1c8c689e2ff6328ec330471f64f56fac authorAdam Bergmark <adam@bergmark.nl> dateTue 4 Sep 2018, 18:46 (8 years ago) parent1a322a1cf7d1f2
3 files changed+27−3
Data/Aeson/Types/FromJSON.hs+9−1 Data/Aeson/Types/ToJSON.hs+14 aeson.cabal+4−2
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+14
@@ -91,6 +91,7 @@ import Data.Time.Format (FormatTime, formatTime)
9191import Data.Time.Locale.Compat (defaultTimeLocale)
9292import Data.Vector (Vector)
9393import Data.Version (Version, showVersion)
94+import Data.Void (Void, absurd)
9495import Data.Word (Word16, Word32, Word64, Word8)
9596import Foreign.Storable (Storable)
9697import Foreign.C.Types (CTime (..))
@@ -702,6 +703,12 @@ instance ( AllNullary (a :+: b) allNullary
702703-- possible but makes error messages a bit harder to understand for missing
703704-- instances.
704705
706+instance GToJSON Value arity V1 where
707+ -- Empty values do not exist, which makes the job of formatting them
708+ -- rather easy:
709+ gToJSON _ _ x = x `seq` error "case: V1"
710+ {-# INLINE gToJSON #-}
711+
705712instance ToJSON a => GToJSON Value arity (K1 i a) where
706713 -- Constant values are encoded using their ToJSON instance:
707714 gToJSON _opts _ = toJSON . unK1
@@ -1221,6 +1228,13 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
12211228 toEncoding = toEncoding2
12221229 {-# INLINE toEncoding #-}
12231230
1231+instance ToJSON Void where
1232+ toJSON = absurd
1233+ {-# INLINE toJSON #-}
1234+
1235+ toEncoding = absurd
1236+ {-# INLINE toEncoding #-}
1237+
12241238
12251239instance ToJSON Bool where
12261240 toJSON = Bool
aeson.cabal+4−2
@@ -135,7 +135,8 @@ library
135135
136136 if !impl(ghc >= 7.10)
137137 -- `Numeric.Natural` is available in base only since GHC 7.10 / base 4.8
138− build-depends: nats >= 1 && < 1.2
138+ build-depends: nats >= 1 && < 1.2,
139+ void >= 0.7.2 && < 0.8
139140
140141 ghc-options: -Wall
141142
@@ -227,7 +228,8 @@ test-suite tests
227228 transformers-compat >= 0.3
228229
229230 if !impl(ghc >= 7.10)
230− build-depends: nats >=1 && <1.2
231+ build-depends: nats >=1 && <1.2,
232+ void >=0.7.2 && <0.8
231233
232234 if impl(ghc >= 7.8)
233235 build-depends: hashable-time >= 0.2 && <0.3