Skip to content

Commit 5834a26

Browse files
committed
allNullaryConstructorTagModifier
1 parent cb75115 commit 5834a26

File tree

7 files changed

+24
-14
lines changed

7 files changed

+24
-14
lines changed

src/Data/Aeson.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ module Data.Aeson
131131
, fieldLabelModifier
132132
, constructorTagModifier
133133
, allNullaryToStringTag
134+
, allNullaryConstructorTagModifier
134135
, omitNothingFields
135136
, allowOmittedFields
136137
, sumEncoding

src/Data/Aeson/TH.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ consToValue target jc opts instTys cons = autoletE liftSBS $ \letInsert -> do
335335
-- forgotten.
336336
[con] | not (tagSingleConstructors opts) -> [argsToValue letInsert target jc tvMap opts False con]
337337
_ | allNullaryToStringTag opts && all isNullary cons ->
338-
[ match (conP conName []) (normalB $ conStr target opts conName) []
338+
let opts' = opts { constructorTagModifier = allNullaryConstructorTagModifier opts } in
339+
[ match (conP conName []) (normalB $ conStr target opts' conName) []
339340
| con <- cons
340341
, let conName = constructorName con
341342
]
@@ -682,13 +683,14 @@ consFromJSON jc tName opts instTys cons = do
682683
else mixedMatches tvMap
683684

684685
allNullaryMatches =
686+
let opts' = opts { constructorTagModifier = allNullaryConstructorTagModifier opts } in
685687
[ do txt <- newName "txtX"
686688
match (conP 'String [varP txt])
687689
(guardedB $
688690
[ liftM2 (,) (normalG $
689691
infixApp (varE txt)
690692
[|(==)|]
691-
(conTxt opts conName)
693+
(conTxt opts' conName)
692694
)
693695
([|pure|] `appE` conE conName)
694696
| con <- cons

src/Data/Aeson/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ module Data.Aeson.Types
142142
, fieldLabelModifier
143143
, constructorTagModifier
144144
, allNullaryToStringTag
145+
, allNullaryConstructorTagModifier
145146
, omitNothingFields
146147
, allowOmittedFields
147148
, sumEncoding

src/Data/Aeson/Types/FromJSON.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ parseAllNullarySum tname opts =
11281128
badTag tag = failWithCTags tname modifier $ \cnames ->
11291129
"expected one of the tags " ++ show cnames ++
11301130
", but found tag " ++ show tag
1131-
modifier = constructorTagModifier opts
1131+
modifier = allNullaryConstructorTagModifier opts
11321132

11331133
-- | Fail with an informative error message about a mismatched tag.
11341134
-- The error message is parameterized by the list of expected tags,

src/Data/Aeson/Types/Internal.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module Data.Aeson.Types.Internal
5656
fieldLabelModifier
5757
, constructorTagModifier
5858
, allNullaryToStringTag
59+
, allNullaryConstructorTagModifier
5960
, omitNothingFields
6061
, allowOmittedFields
6162
, sumEncoding
@@ -714,6 +715,7 @@ data Options = Options
714715
-- nullary constructors, will be encoded to just a string with
715716
-- the constructor tag. If 'False' the encoding will always
716717
-- follow the `sumEncoding`.
718+
, allNullaryConstructorTagModifier :: String -> String
717719
, omitNothingFields :: Bool
718720
-- ^ If 'True', record fields with a 'Nothing' value will be
719721
-- omitted from the resulting object. If 'False', the resulting
@@ -744,12 +746,13 @@ data Options = Options
744746
}
745747

746748
instance Show Options where
747-
show (Options f c a o q s u t r) =
749+
show (Options f c a ac o q s u t r) =
748750
"Options {"
749751
++ intercalate ", "
750752
[ "fieldLabelModifier =~ " ++ show (f "exampleField")
751753
, "constructorTagModifier =~ " ++ show (c "ExampleConstructor")
752754
, "allNullaryToStringTag = " ++ show a
755+
, "allNullaryonstructorTagModifier =~ " ++ show (ac "ExampleConstructor")
753756
, "omitNothingFields = " ++ show o
754757
, "allowOmittedFields = " ++ show q
755758
, "sumEncoding = " ++ show s
@@ -843,15 +846,16 @@ data JSONKeyOptions = JSONKeyOptions
843846
-- @
844847
defaultOptions :: Options
845848
defaultOptions = Options
846-
{ fieldLabelModifier = id
847-
, constructorTagModifier = id
848-
, allNullaryToStringTag = True
849-
, omitNothingFields = False
850-
, allowOmittedFields = True
851-
, sumEncoding = defaultTaggedObject
852-
, unwrapUnaryRecords = False
853-
, tagSingleConstructors = False
854-
, rejectUnknownFields = False
849+
{ fieldLabelModifier = id
850+
, constructorTagModifier = id
851+
, allNullaryToStringTag = True
852+
, allNullaryConstructorTagModifier = id
853+
, omitNothingFields = False
854+
, allowOmittedFields = True
855+
, sumEncoding = defaultTaggedObject
856+
, unwrapUnaryRecords = False
857+
, tagSingleConstructors = False
858+
, rejectUnknownFields = False
855859
}
856860

857861
-- | Default 'TaggedObject' 'SumEncoding' options:

src/Data/Aeson/Types/ToJSON.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,8 @@ instance ( GetConName f
938938
where
939939
sumToJSON opts targs
940940
| allNullaryToStringTag opts = Tagged . fromString
941-
. constructorTagModifier opts . getConName
941+
. allNullaryConstructorTagModifier opts
942+
. getConName
942943
| otherwise = Tagged . nonAllNullarySumToJSON opts targs
943944
{-# INLINE sumToJSON #-}
944945

tests/UnitTests.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ showOptions =
278278
++ "fieldLabelModifier =~ \"exampleField\""
279279
++ ", constructorTagModifier =~ \"ExampleConstructor\""
280280
++ ", allNullaryToStringTag = True"
281+
++ ", allNullaryConstructorTagModifier =~ \"ExampleConstructor\""
281282
++ ", omitNothingFields = False"
282283
++ ", allowOmittedFields = True"
283284
++ ", sumEncoding = TaggedObject {tagFieldName = \"tag\", contentsFieldName = \"contents\"}"

0 commit comments

Comments
 (0)