Skip to content

Commit aa1ce47

Browse files
author
Poscat
committed
WIP
1 parent af0897f commit aa1ce47

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Data/Aeson/TH.hs

+23-1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ sumToValue target opts multiCons nullary conName value pairs
421421
content = pairs contentsFieldName
422422
in fromPairsE $
423423
if nullary then tag else infixApp tag [|(Monoid.<>)|] content
424+
TaggedFlatObject {} -> error "impossible"
424425
ObjectWithSingleField ->
425426
objectE [(conString opts conName, value)]
426427
UntaggedValue | nullary -> conStr target opts conName
@@ -434,7 +435,20 @@ argsToValue :: ToJSONFun -> JSONClass -> TyVarMap -> Options -> Bool -> Construc
434435
argsToValue target jc tvMap opts multiCons
435436
ConstructorInfo { constructorName = conName
436437
, constructorVariant = NormalConstructor
437-
, constructorFields = argTys } = do
438+
, constructorFields = argTys }
439+
| TaggedFlatObject{tagFieldName} <- sumEncoding opts = do
440+
let tag = (tagFieldName, conStr target opts conName)
441+
argTys' <- mapM resolveTypeSynonyms argTys
442+
let len = length argTys'
443+
args <- newNameList "arg" len
444+
let os = zipWith (\arg argTy -> dispatchToJSON target jc conName tvMap argTy `appE` varE arg) args argTys'
445+
pairs = zip (fmap (show :: Int -> String) [1..]) os
446+
obj = objectE (tag : pairs)
447+
match (conP conName $ map varP args)
448+
(normalB obj)
449+
[]
450+
| otherwise =
451+
do
438452
argTys' <- mapM resolveTypeSynonyms argTys
439453
let len = length argTys'
440454
args <- newNameList "arg" len
@@ -729,6 +743,7 @@ consFromJSON jc tName opts instTys cons = do
729743
case sumEncoding opts of
730744
TaggedObject {tagFieldName, contentsFieldName} ->
731745
parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName
746+
TaggedFlatObject {tagFieldName} -> error "unsupported"
732747
UntaggedValue -> error "UntaggedValue: Should be handled already"
733748
ObjectWithSingleField ->
734749
parseObject $ parseObjectWithSingleField tvMap
@@ -778,6 +793,13 @@ consFromJSON jc tName opts instTys cons = do
778793
([|T.pack|] `appE` stringE typFieldName))
779794
, noBindS $ parseContents tvMap conKey (Left (valFieldName, obj)) 'conNotFoundFailTaggedObject
780795
]
796+
797+
parseTaggedFlatObject tvMap typFieldName obj = do
798+
conKey <- newName "conKey"
799+
doE [ bindS (varP conKey)
800+
(infixApp (varE obj) [|(.:)|] ([|T.pack|] `appE` stringE typFieldName))
801+
, noBindS $ parseContents tvMap conKey (Right obj) 'conNotFoundFailTaggedObject
802+
]
781803

782804
parseUntaggedValue tvMap cons' conVal =
783805
foldr1 (\e e' -> infixApp e [|(<|>)|] e')

0 commit comments

Comments
 (0)