@@ -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,21 @@ 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
440
+ , multiCons = do
441
+ let tag = (tagFieldName, conStr target opts conName)
442
+ argTys' <- mapM resolveTypeSynonyms argTys
443
+ let len = length argTys'
444
+ args <- newNameList " arg" len
445
+ let os = zipWith (\ arg argTy -> dispatchToJSON target jc conName tvMap argTy `appE` varE arg) args argTys'
446
+ pairs = zip (fmap (show :: Int -> String ) [1 .. ]) os
447
+ obj = objectE (tag : pairs)
448
+ match (conP conName $ map varP args)
449
+ (normalB obj)
450
+ []
451
+ | otherwise =
452
+ do
438
453
argTys' <- mapM resolveTypeSynonyms argTys
439
454
let len = length argTys'
440
455
args <- newNameList " arg" len
@@ -491,14 +506,33 @@ argsToValue target jc tvMap opts multiCons
491
506
else e arg
492
507
493
508
match (conP conName $ map varP args)
494
- (normalB $ recordSumToValue target opts multiCons (null argTys) conName pairs)
509
+ (normalB $ case () of
510
+ ()
511
+ | TaggedFlatObject {tagFieldName} <- sumEncoding opts -> do
512
+ let tag = pairE tagFieldName (conStr target opts conName)
513
+ fromPairsE $ infixApp tag [| (<>) | ] pairs
514
+ | otherwise -> recordSumToValue target opts multiCons (null argTys) conName pairs)
495
515
[]
496
516
497
517
-- Infix constructors.
498
518
argsToValue target jc tvMap opts multiCons
499
519
ConstructorInfo { constructorName = conName
500
520
, constructorVariant = InfixConstructor
501
- , constructorFields = argTys } = do
521
+ , constructorFields = argTys }
522
+ | TaggedFlatObject {tagFieldName} <- sumEncoding opts
523
+ , multiCons = do
524
+ [alTy, arTy] <- mapM resolveTypeSynonyms argTys
525
+ al <- newName " argL"
526
+ ar <- newName " argR"
527
+ let tag = (tagFieldName, conStr target opts conName)
528
+ os = zipWith (\ arg argTy -> dispatchToJSON target jc conName tvMap argTy `appE` varE arg) [al, ar] [alTy, arTy]
529
+ pairs = zip (fmap (show :: Int -> String ) [1 .. ]) os
530
+ obj = objectE (tag : pairs)
531
+ match (infixP (varP al) conName (varP ar))
532
+ (normalB obj)
533
+ []
534
+ | otherwise =
535
+ do
502
536
[alTy, arTy] <- mapM resolveTypeSynonyms argTys
503
537
al <- newName " argL"
504
538
ar <- newName " argR"
@@ -729,6 +763,7 @@ consFromJSON jc tName opts instTys cons = do
729
763
case sumEncoding opts of
730
764
TaggedObject {tagFieldName, contentsFieldName} ->
731
765
parseObject $ parseTaggedObject tvMap tagFieldName contentsFieldName
766
+ TaggedFlatObject {tagFieldName} -> error " unsupported"
732
767
UntaggedValue -> error " UntaggedValue: Should be handled already"
733
768
ObjectWithSingleField ->
734
769
parseObject $ parseObjectWithSingleField tvMap
@@ -779,6 +814,13 @@ consFromJSON jc tName opts instTys cons = do
779
814
, noBindS $ parseContents tvMap conKey (Left (valFieldName, obj)) 'conNotFoundFailTaggedObject
780
815
]
781
816
817
+ parseTaggedFlatObject tvMap typFieldName obj = do
818
+ conKey <- newName " conKey"
819
+ doE [ bindS (varP conKey)
820
+ (infixApp (varE obj) [| (.:) | ] ([| T. pack| ] `appE` stringE typFieldName))
821
+ , noBindS $ parseContents tvMap conKey (Right obj) 'conNotFoundFailTaggedObject
822
+ ]
823
+
782
824
parseUntaggedValue tvMap cons' conVal =
783
825
foldr1 (\ e e' -> infixApp e [| (<|>) | ] e')
784
826
(map (\ x -> parseValue tvMap x conVal) cons')
0 commit comments