@@ -421,6 +421,7 @@ sumToValue target opts multiCons nullary conName value pairs
421
421
content = pairs contentsFieldName
422
422
in fromPairsE $
423
423
if nullary then tag else infixApp tag [| (Monoid. <>) | ] content
424
+ TaggedFlatObject {} -> error " impossible"
424
425
ObjectWithSingleField ->
425
426
objectE [(conString opts conName, value)]
426
427
UntaggedValue | nullary -> conStr target opts conName
@@ -434,7 +435,20 @@ argsToValue :: ToJSONFun -> JSONClass -> TyVarMap -> Options -> Bool -> Construc
434
435
argsToValue target jc tvMap opts multiCons
435
436
ConstructorInfo { constructorName = conName
436
437
, 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
438
452
argTys' <- mapM resolveTypeSynonyms argTys
439
453
let len = length argTys'
440
454
args <- newNameList " arg" len
@@ -729,6 +743,7 @@ consFromJSON jc tName opts instTys cons = do
729
743
case sumEncoding opts of
730
744
TaggedObject {tagFieldName, contentsFieldName} ->
731
745
parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName
746
+ TaggedFlatObject {tagFieldName} -> error " unsupported"
732
747
UntaggedValue -> error " UntaggedValue: Should be handled already"
733
748
ObjectWithSingleField ->
734
749
parseObject $ parseObjectWithSingleField tvMap
@@ -778,6 +793,13 @@ consFromJSON jc tName opts instTys cons = do
778
793
([| T. pack| ] `appE` stringE typFieldName))
779
794
, noBindS $ parseContents tvMap conKey (Left (valFieldName, obj)) 'conNotFoundFailTaggedObject
780
795
]
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
+ ]
781
803
782
804
parseUntaggedValue tvMap cons' conVal =
783
805
foldr1 (\ e e' -> infixApp e [| (<|>) | ] e')
0 commit comments