Skip to content

Commit fd0fea8

Browse files
committed
empty contentsFieldName means use tag value as content field name
1 parent 55fe395 commit fd0fea8

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

src/Data/Aeson/TH.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ sumToValue letInsert target opts multiCons nullary conName value pairs
407407
case sumEncoding opts of
408408
TwoElemArray ->
409409
array target [conStr target opts conName, value]
410-
TaggedObject{tagFieldName, contentsFieldName, tagAsContentsFieldName} ->
410+
TaggedObject{tagFieldName, contentsFieldName} ->
411411
-- TODO: Maybe throw an error in case
412412
-- tagFieldName overwrites a field in pairs.
413413
let tag = pairE letInsert target tagFieldName (conStr target opts conName)
414-
contentsFieldName' = if tagAsContentsFieldName
414+
contentsFieldName' = if null contentsFieldName
415415
then conString opts conName
416416
else contentsFieldName
417417
content = pairs contentsFieldName'
@@ -718,8 +718,8 @@ consFromJSON jc tName opts instTys cons = do
718718

719719
mixedMatches tvMap =
720720
case sumEncoding opts of
721-
TaggedObject {tagFieldName, contentsFieldName, tagAsContentsFieldName} ->
722-
parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName tagAsContentsFieldName
721+
TaggedObject {tagFieldName, contentsFieldName} ->
722+
parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName
723723
UntaggedValue -> error "UntaggedValue: Should be handled already"
724724
ObjectWithSingleField ->
725725
parseObject $ parseObjectWithSingleField tvMap
@@ -761,7 +761,7 @@ consFromJSON jc tName opts instTys cons = do
761761
[]
762762
]
763763

764-
parseTaggedObject tvMap typFieldName valFieldName tagAsContentsFieldName obj = do
764+
parseTaggedObject tvMap typFieldName valFieldName obj = do
765765
conKey <- newName "conKeyX"
766766
valField <- newName "valField"
767767
doE [ bindS (varP conKey)
@@ -770,7 +770,7 @@ consFromJSON jc tName opts instTys cons = do
770770
([|Key.fromString|] `appE` stringE typFieldName))
771771
, letS [ valD (varP valField)
772772
( normalB
773-
$ if tagAsContentsFieldName
773+
$ if null valFieldName
774774
then varE conKey
775775
else litE $ stringL valFieldName
776776
)

src/Data/Aeson/Types/FromJSON.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
12091209
TaggedObject{..} ->
12101210
withObject tname $ \obj -> do
12111211
tag <- contextType tname . contextTag tagKey cnames_ $ obj .: tagKey
1212-
let contentsFieldName' = if tagAsContentsFieldName
1212+
let contentsFieldName' = if null contentsFieldName
12131213
then unpack tag
12141214
else contentsFieldName
12151215
fromMaybe (badTag tag <?> Key tagKey) $

src/Data/Aeson/Types/Internal.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,8 @@ instance Show Options where
761761

762762
-- | Specifies how to encode constructors of a sum datatype.
763763
data SumEncoding =
764-
TaggedObject { tagFieldName :: String
765-
, contentsFieldName :: String
766-
, tagAsContentsFieldName :: Bool
764+
TaggedObject { tagFieldName :: String
765+
, contentsFieldName :: String
767766
}
768767
-- ^ A constructor will be encoded to an object with a field
769768
-- 'tagFieldName' which specifies the constructor tag (modified by
@@ -775,7 +774,7 @@ data SumEncoding =
775774
-- record the encoded constructor contents will be stored under
776775
-- the 'contentsFieldName' field.
777776
--
778-
-- If 'tagAsContentsFieldName' is True, then the value of
777+
-- If 'contentsFieldName' is the empty string, then the value of
779778
-- 'tagFieldName' will be used as the 'contentsFieldName' instead.
780779
| UntaggedValue
781780
-- ^ Constructor names won't be encoded. Instead only the contents of the
@@ -868,9 +867,8 @@ defaultOptions = Options
868867
-- @
869868
defaultTaggedObject :: SumEncoding
870869
defaultTaggedObject = TaggedObject
871-
{ tagFieldName = "tag"
872-
, contentsFieldName = "contents"
873-
, tagAsContentsFieldName = False
870+
{ tagFieldName = "tag"
871+
, contentsFieldName = "contents"
874872
}
875873

876874
-- | Default 'JSONKeyOptions':

src/Data/Aeson/Types/ToJSON.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ nonAllNullarySumToJSON opts targs =
961961
case sumEncoding opts of
962962

963963
TaggedObject{..} ->
964-
taggedObject opts targs (Key.fromString tagFieldName) (Key.fromString contentsFieldName) tagAsContentsFieldName
964+
taggedObject opts targs (Key.fromString tagFieldName) (Key.fromString contentsFieldName)
965965

966966
ObjectWithSingleField ->
967967
(unTagged :: Tagged ObjectWithSingleField enc -> enc)
@@ -984,17 +984,17 @@ nonAllNullarySumToJSON opts targs =
984984

985985
class TaggedObject enc arity f where
986986
taggedObject :: Options -> ToArgs enc arity a
987-
-> Key -> Key -> Bool
987+
-> Key -> Key
988988
-> f a -> enc
989989

990990
instance ( TaggedObject enc arity a
991991
, TaggedObject enc arity b
992992
) => TaggedObject enc arity (a :+: b)
993993
where
994-
taggedObject opts targs tagFieldName contentsFieldName tagAsContentsFieldName (L1 x) =
995-
taggedObject opts targs tagFieldName contentsFieldName tagAsContentsFieldName x
996-
taggedObject opts targs tagFieldName contentsFieldName tagAsContentsFieldName (R1 x) =
997-
taggedObject opts targs tagFieldName contentsFieldName tagAsContentsFieldName x
994+
taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
995+
taggedObject opts targs tagFieldName contentsFieldName x
996+
taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
997+
taggedObject opts targs tagFieldName contentsFieldName x
998998
{-# INLINE taggedObject #-}
999999

10001000
instance ( IsRecord a isRecord
@@ -1005,14 +1005,14 @@ instance ( IsRecord a isRecord
10051005
, Constructor c
10061006
) => TaggedObject enc arity (C1 c a)
10071007
where
1008-
taggedObject opts targs tagFieldName contentsFieldName tagAsContentsFieldName =
1008+
taggedObject opts targs tagFieldName contentsFieldName =
10091009
fromPairs . mappend tag . contents
10101010
where
10111011
constructorTagString = constructorTagModifier opts (conName (undefined :: t c a p))
10121012
tag = tagFieldName `pair` (fromString constructorTagString :: enc)
1013-
contentsFieldName' = if tagAsContentsFieldName
1014-
then Key.fromString constructorTagString
1015-
else contentsFieldName
1013+
contentsFieldName' = if null $ Key.toString contentsFieldName
1014+
then Key.fromString constructorTagString
1015+
else contentsFieldName
10161016
contents =
10171017
(unTagged :: Tagged isRecord pairs -> pairs) .
10181018
taggedObject' opts targs contentsFieldName' . unM1

tests/UnitTests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ showOptions =
280280
++ ", allNullaryToStringTag = True"
281281
++ ", omitNothingFields = False"
282282
++ ", allowOmittedFields = True"
283-
++ ", sumEncoding = TaggedObject {tagFieldName = \"tag\", contentsFieldName = \"contents\", tagAsContentsFieldName = False}"
283+
++ ", sumEncoding = TaggedObject {tagFieldName = \"tag\", contentsFieldName = \"contents\"}"
284284
++ ", unwrapUnaryRecords = False"
285285
++ ", tagSingleConstructors = False"
286286
++ ", rejectUnknownFields = False"

0 commit comments

Comments
 (0)