@@ -312,6 +312,15 @@ sumToValue opts multiCons conName exp
312312
313313 | otherwise = exp
314314
315+nullarySumToValue :: Options -> Bool -> Name -> Q Exp
316+nullarySumToValue opts multiCons conName =
317+ case sumEncoding opts of
318+ TaggedObject{tagFieldName} ->
319+ [|A.object|] `appE` listE
320+ [ infixApp [|T.pack tagFieldName|] [|(.=)|] (conStr opts conName)
321+ ]
322+ _ -> sumToValue opts multiCons conName [e|toJSON ([] :: [()])|]
323+
315324-- | Generates code to generate the JSON encoding of a single constructor.
316325argsToValue :: Options -> Bool -> Con -> Q Match
317326-- Nullary constructors. Generates code that explicitly matches against the
@@ -319,7 +328,7 @@ argsToValue :: Options -> Bool -> Con -> Q Match
319328-- type errors.
320329argsToValue opts multiCons (NormalC conName []) =
321330 match (conP conName [])
322− (normalB (sumToValue opts multiCons conName [e|toJSON ([] :: [()])|]))
331+ (normalB (nullarySumToValue opts multiCons conName))
323332 []
324333
325334-- Polyadic constructors with special case for unary constructors.
@@ -458,6 +467,14 @@ sumToEncoding opts multiCons conName exp
458467
459468 | otherwise = exp
460469
470+nullarySumToEncoding :: Options -> Bool -> Name -> Q Exp
471+nullarySumToEncoding opts multiCons conName =
472+ case sumEncoding opts of
473+ TaggedObject{tagFieldName} ->
474+ object $
475+ ([|E.text (T.pack tagFieldName)|] <:> encStr opts conName)
476+ _ -> sumToEncoding opts multiCons conName [e|toEncoding ([] :: [()])|]
477+
461478-- | Generates code to generate the JSON encoding of a single constructor.
462479argsToEncoding :: Options -> Bool -> Con -> Q Match
463480-- Nullary constructors. Generates code that explicitly matches against the
@@ -465,7 +482,7 @@ argsToEncoding :: Options -> Bool -> Con -> Q Match
465482-- type errors.
466483argsToEncoding opts multiCons (NormalC conName []) =
467484 match (conP conName [])
468− (normalB (sumToEncoding opts multiCons conName [e|toEncoding ([] :: [()])|]))
485+ (normalB (nullarySumToEncoding opts multiCons conName))
469486 []
470487
471488-- Polyadic constructors with special case for unary constructors.
@@ -847,8 +864,8 @@ parseArgs :: Name -- ^ Name of the type to which the constructor belongs.
847864 -- Right valName
848865 -> Q Exp
849866-- Nullary constructors.
850−parseArgs tName _ (NormalC conName []) (Left (valFieldName, obj)) =
851− getValField obj valFieldName $ parseNullaryMatches tName conName
867+parseArgs _ _ (NormalC conName []) (Left _) =
868+ [|pure|] `appE` conE conName
852869parseArgs tName _ (NormalC conName []) (Right valName) =
853870 caseE (varE valName) $ parseNullaryMatches tName conName
854871