@@ -407,11 +407,14 @@ 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} ->
410+ TaggedObject {tagFieldName, contentsFieldName, tagAsContentsFieldName } ->
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- content = pairs contentsFieldName
414+ contentsFieldName' = if tagAsContentsFieldName
415+ then conString opts conName
416+ else contentsFieldName
417+ content = pairs contentsFieldName'
415418 in fromPairsE target $
416419 if nullary then tag else infixApp tag [| (Monoid. <>) | ] content
417420 ObjectWithSingleField ->
@@ -715,8 +718,8 @@ consFromJSON jc tName opts instTys cons = do
715718
716719 mixedMatches tvMap =
717720 case sumEncoding opts of
718- TaggedObject {tagFieldName, contentsFieldName} ->
719- parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName
721+ TaggedObject {tagFieldName, contentsFieldName, tagAsContentsFieldName } ->
722+ parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName tagAsContentsFieldName
720723 UntaggedValue -> error " UntaggedValue: Should be handled already"
721724 ObjectWithSingleField ->
722725 parseObject $ parseObjectWithSingleField tvMap
@@ -758,13 +761,22 @@ consFromJSON jc tName opts instTys cons = do
758761 []
759762 ]
760763
761- parseTaggedObject tvMap typFieldName valFieldName obj = do
764+ parseTaggedObject tvMap typFieldName valFieldName tagAsContentsFieldName obj = do
762765 conKey <- newName " conKeyX"
766+ valField <- newName " valField"
763767 doE [ bindS (varP conKey)
764768 (infixApp (varE obj)
765769 [| (.:) | ]
766770 ([| Key. fromString| ] `appE` stringE typFieldName))
767- , noBindS $ parseContents tvMap conKey (Left (valFieldName, obj)) 'conNotFoundFailTaggedObject [| Key. fromString| ] [| Key. toString| ]
771+ , letS [ valD (varP valField)
772+ ( normalB
773+ $ if tagAsContentsFieldName
774+ then varE conKey
775+ else litE $ stringL valFieldName
776+ )
777+ []
778+ ]
779+ , noBindS $ parseContents tvMap conKey (Left (valField, obj)) 'conNotFoundFailTaggedObject [| Key. fromString| ] [| Key. toString| ]
768780 ]
769781
770782 parseUntaggedValue tvMap cons' conVal =
@@ -955,19 +967,19 @@ parseRecord jc tvMap argTys opts tName conName fields obj inTaggedObject =
955967 | (field, argTy) <- zip fields argTys
956968 ]
957969
958- getValField :: Name -> String -> [MatchQ ] -> Q Exp
959- getValField obj valFieldName matches = do
970+ getValField :: Name -> Name -> [MatchQ ] -> Q Exp
971+ getValField obj valField matches = do
960972 val <- newName " val"
961973 doE [ bindS (varP val) $ infixApp (varE obj)
962974 [| (.:) | ]
963975 ([| Key. fromString| ] `appE`
964- litE (stringL valFieldName) )
976+ varE valField )
965977 , noBindS $ caseE (varE val) matches
966978 ]
967979
968- matchCases :: Either (String , Name ) Name -> [MatchQ ] -> Q Exp
969- matchCases (Left (valFieldName , obj)) = getValField obj valFieldName
970- matchCases (Right valName) = caseE (varE valName)
980+ matchCases :: Either (Name , Name ) Name -> [MatchQ ] -> Q Exp
981+ matchCases (Left (valField , obj)) = getValField obj valField
982+ matchCases (Right valName) = caseE (varE valName)
971983
972984-- | Generates code to parse the JSON encoding of a single constructor.
973985parseArgs :: JSONClass -- ^ The FromJSON variant being derived.
@@ -976,8 +988,8 @@ parseArgs :: JSONClass -- ^ The FromJSON variant being derived.
976988 -> Name -- ^ Name of the type to which the constructor belongs.
977989 -> Options -- ^ Encoding options.
978990 -> ConstructorInfo -- ^ Constructor for which to generate JSON parsing code.
979- -> Either (String , Name ) Name -- ^ Left (valFieldName, objName) or
980- -- Right valName
991+ -> Either (Name , Name ) Name -- ^ Left (valFieldName, objName) or
992+ -- Right valName
981993 -> Q Exp
982994-- Nullary constructors.
983995parseArgs _ _ _ _
0 commit comments