@@ -791,6 +791,19 @@ instance GFromJSON arity U1 where
791
791
| isEmptyArray v = pure U1
792
792
| otherwise = typeMismatch " unit constructor (U1)" v
793
793
794
+ instance ( ConsFromJSON arity a
795
+ , AllNullary (C1 c a ) allNullary
796
+ , ParseSum arity (C1 c a ) allNullary
797
+ ) => GFromJSON arity (D1 d (C1 c a )) where
798
+ -- The option 'tagSingleConstructors' determines whether to wrap
799
+ -- a single-constructor type.
800
+ gParseJSON opts fargs
801
+ | tagSingleConstructors opts
802
+ = fmap M1
803
+ . (unTagged :: Tagged allNullary (Parser (C1 c a p )) -> Parser (C1 c a p ))
804
+ . parseSum opts fargs
805
+ | otherwise = fmap M1 . fmap M1 . consParseJSON opts fargs
806
+
794
807
instance (ConsFromJSON arity a ) => GFromJSON arity (C1 c a ) where
795
808
-- Constructors need to be decoded differently depending on whether they're
796
809
-- a record or not. This distinction is made by consParseJSON:
@@ -837,19 +850,19 @@ class ParseSum arity f allNullary where
837
850
parseSum :: Options -> FromArgs arity a
838
851
-> Value -> Tagged allNullary (Parser (f a ))
839
852
840
- instance ( SumFromString ( a :+: b )
841
- , FromPair arity ( a :+: b )
842
- , FromTaggedObject arity ( a :+: b )
843
- , FromUntaggedValue arity ( a :+: b )
844
- ) => ParseSum arity ( a :+: b ) True where
853
+ instance ( SumFromString f
854
+ , FromPair arity f
855
+ , FromTaggedObject arity f
856
+ , FromUntaggedValue arity f
857
+ ) => ParseSum arity f True where
845
858
parseSum opts fargs
846
859
| allNullaryToStringTag opts = Tagged . parseAllNullarySum opts
847
860
| otherwise = Tagged . parseNonAllNullarySum opts fargs
848
861
849
- instance ( FromPair arity ( a :+: b )
850
- , FromTaggedObject arity ( a :+: b )
851
- , FromUntaggedValue arity ( a :+: b )
852
- ) => ParseSum arity ( a :+: b ) False where
862
+ instance ( FromPair arity f
863
+ , FromTaggedObject arity f
864
+ , FromUntaggedValue arity f
865
+ ) => ParseSum arity f False where
853
866
parseSum opts fargs = Tagged . parseNonAllNullarySum opts fargs
854
867
855
868
--------------------------------------------------------------------------------
@@ -875,11 +888,11 @@ instance (Constructor c) => SumFromString (C1 c U1) where
875
888
876
889
--------------------------------------------------------------------------------
877
890
878
- parseNonAllNullarySum :: ( FromPair arity ( a :+: b )
879
- , FromTaggedObject arity ( a :+: b )
880
- , FromUntaggedValue arity ( a :+: b )
891
+ parseNonAllNullarySum :: ( FromPair arity f
892
+ , FromTaggedObject arity f
893
+ , FromUntaggedValue arity f
881
894
) => Options -> FromArgs arity c
882
- -> Value -> Parser (( a :+: b ) c )
895
+ -> Value -> Parser (f c )
883
896
parseNonAllNullarySum opts fargs =
884
897
case sumEncoding opts of
885
898
TaggedObject {.. } ->
0 commit comments