@@ -312,6 +312,15 @@ sumToValue opts multiCons conName exp
313 | otherwise = exp
313 | otherwise = exp
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)
322+ _ -> sumToValue opts multiCons conName [e|toJSON ([] :: [()])|]
315-- | Generates code to generate the JSON encoding of a single constructor.
324-- | Generates code to generate the JSON encoding of a single constructor.
316argsToValue :: Options -> Bool -> Con -> Q Match
325argsToValue :: Options -> Bool -> Con -> Q Match
317-- Nullary constructors. Generates code that explicitly matches against the
326-- Nullary constructors. Generates code that explicitly matches against the
@@ -319,7 +328,7 @@ argsToValue :: Options -> Bool -> Con -> Q Match
319-- type errors.
328-- type errors.
320argsToValue opts multiCons (NormalC conName []) =
329argsToValue opts multiCons (NormalC conName []) =
321 match (conP conName [])
330 match (conP conName [])
322− (normalB (sumToValue opts multiCons conName [e|toJSON ([] :: [()])|]))
331+ (normalB (nullarySumToValue opts multiCons conName))
325-- Polyadic constructors with special case for unary constructors.
334-- Polyadic constructors with special case for unary constructors.
@@ -458,6 +467,14 @@ sumToEncoding opts multiCons conName exp
459 | otherwise = exp
468 | otherwise = exp
470+nullarySumToEncoding :: Options -> Bool -> Name -> Q Exp
471+nullarySumToEncoding opts multiCons conName =
472+ case sumEncoding opts of
473+ TaggedObject{tagFieldName} ->
475+ ([|E.text (T.pack tagFieldName)|] <:> encStr opts conName)
476+ _ -> sumToEncoding opts multiCons conName [e|toEncoding ([] :: [()])|]
461-- | Generates code to generate the JSON encoding of a single constructor.
478-- | Generates code to generate the JSON encoding of a single constructor.
462argsToEncoding :: Options -> Bool -> Con -> Q Match
479argsToEncoding :: Options -> Bool -> Con -> Q Match
463-- Nullary constructors. Generates code that explicitly matches against the
480-- Nullary constructors. Generates code that explicitly matches against the
@@ -465,7 +482,7 @@ argsToEncoding :: Options -> Bool -> Con -> Q Match
465-- type errors.
482-- type errors.
466argsToEncoding opts multiCons (NormalC conName []) =
483argsToEncoding opts multiCons (NormalC conName []) =
467 match (conP conName [])
484 match (conP conName [])
468− (normalB (sumToEncoding opts multiCons conName [e|toEncoding ([] :: [()])|]))
485+ (normalB (nullarySumToEncoding opts multiCons conName))
471-- Polyadic constructors with special case for unary constructors.
488-- Polyadic constructors with special case for unary constructors.
@@ -847,8 +864,8 @@ parseArgs :: Name -- ^ Name of the type to which the constructor belongs.
847 -- Right valName
864 -- Right valName
849-- Nullary constructors.
866-- Nullary constructors.
850−parseArgs tName _ (NormalC conName []) (Left (valFieldName, obj)) =
867+parseArgs _ _ (NormalC conName []) (Left _) =
851− getValField obj valFieldName $ parseNullaryMatches tName conName
868+ [|pure|] `appE` conE conName
852parseArgs tName _ (NormalC conName []) (Right valName) =
869parseArgs tName _ (NormalC conName []) (Right valName) =
853 caseE (varE valName) $ parseNullaryMatches tName conName
870 caseE (varE valName) $ parseNullaryMatches tName conName