@@ -50,6 +50,8 @@ module Data.Aeson.Types.ToJSON
50
50
, contramapToJSONKeyFunction
51
51
-- * Object key-value pairs
52
52
, KeyValue (.. )
53
+ , KeyValuePair (.. )
54
+ , FromPairs (.. )
53
55
-- * Functions needed for documentation
54
56
-- * Encoding functions
55
57
, listEncoding
@@ -853,14 +855,14 @@ instance ( IsRecord a isRecord
853
855
, TaggedObject' enc pairs arity a isRecord
854
856
, FromPairs enc pairs
855
857
, FromString enc
856
- , GKeyValue enc pairs
858
+ , KeyValuePair enc pairs
857
859
, Constructor c
858
860
) => TaggedObject enc arity (C1 c a )
859
861
where
860
862
taggedObject opts targs tagFieldName contentsFieldName =
861
863
fromPairs . (tag <> ) . contents
862
864
where
863
- tag = tagFieldName `gPair `
865
+ tag = tagFieldName `pair `
864
866
(fromString (constructorTagModifier opts (conName (undefined :: t c a p )))
865
867
:: enc )
866
868
contents =
@@ -872,11 +874,11 @@ class TaggedObject' enc pairs arity f isRecord where
872
874
-> String -> f a -> Tagged isRecord pairs
873
875
874
876
instance ( GToJSON enc arity f
875
- , GKeyValue enc pairs
877
+ , KeyValuePair enc pairs
876
878
) => TaggedObject' enc pairs arity f False
877
879
where
878
880
taggedObject' opts targs contentsFieldName =
879
- Tagged . (contentsFieldName `gPair ` ) . gToJSON opts targs
881
+ Tagged . (contentsFieldName `pair ` ) . gToJSON opts targs
880
882
881
883
instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
882
884
taggedObject' _ _ _ _ = Tagged mempty
@@ -1005,7 +1007,7 @@ instance ( Monoid pairs
1005
1007
1006
1008
instance ( Selector s
1007
1009
, GToJSON enc arity a
1008
- , GKeyValue enc pairs
1010
+ , KeyValuePair enc pairs
1009
1011
) => RecordToPairs enc pairs arity (S1 s a )
1010
1012
where
1011
1013
recordToPairs = fieldToPair
@@ -1014,7 +1016,7 @@ instance ( Selector s
1014
1016
instance INCOHERENT_
1015
1017
( Selector s
1016
1018
, GToJSON enc arity (K1 i (Maybe a ))
1017
- , GKeyValue enc pairs
1019
+ , KeyValuePair enc pairs
1018
1020
, Monoid pairs
1019
1021
) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a )))
1020
1022
where
@@ -1026,7 +1028,7 @@ instance INCOHERENT_
1026
1028
instance INCOHERENT_
1027
1029
( Selector s
1028
1030
, GToJSON enc arity (K1 i (Maybe a ))
1029
- , GKeyValue enc pairs
1031
+ , KeyValuePair enc pairs
1030
1032
, Monoid pairs
1031
1033
) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup. Option a )))
1032
1034
where
@@ -1038,13 +1040,13 @@ instance INCOHERENT_
1038
1040
1039
1041
fieldToPair :: (Selector s
1040
1042
, GToJSON enc arity a
1041
- , GKeyValue enc pairs )
1043
+ , KeyValuePair enc pairs )
1042
1044
=> Options -> ToArgs enc arity p
1043
1045
-> S1 s a p -> pairs
1044
1046
fieldToPair opts targs m1 =
1045
1047
let key = fieldLabelModifier opts (selName m1)
1046
1048
value = gToJSON opts targs (unM1 m1)
1047
- in key `gPair ` value
1049
+ in key `pair ` value
1048
1050
{-# INLINE fieldToPair #-}
1049
1051
1050
1052
--------------------------------------------------------------------------------
@@ -1098,12 +1100,12 @@ instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1098
1100
instance ( GToJSON enc arity a
1099
1101
, ConsToJSON enc arity a
1100
1102
, FromPairs enc pairs
1101
- , GKeyValue enc pairs
1103
+ , KeyValuePair enc pairs
1102
1104
, Constructor c
1103
1105
) => SumToJSON' ObjectWithSingleField enc arity (C1 c a )
1104
1106
where
1105
1107
sumToJSON' opts targs =
1106
- Tagged . fromPairs . (typ `gPair ` ) . gToJSON opts targs
1108
+ Tagged . fromPairs . (typ `pair ` ) . gToJSON opts targs
1107
1109
where
1108
1110
typ = constructorTagModifier opts $
1109
1111
conName (undefined :: t c a p )
@@ -2716,20 +2718,24 @@ packChunks lbs =
2716
2718
2717
2719
--------------------------------------------------------------------------------
2718
2720
2721
+ -- | Wrap a list of pairs as an object.
2719
2722
class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2720
2723
fromPairs :: pairs -> enc
2721
2724
2722
- instance FromPairs Encoding Series where
2725
+ instance ( a ~ Value ) => FromPairs ( Encoding' a ) Series where
2723
2726
fromPairs = E. pairs
2724
2727
2725
2728
instance FromPairs Value (DList Pair ) where
2726
2729
fromPairs = object . toList
2727
2730
2728
- class Monoid kv => GKeyValue v kv where
2729
- gPair :: String -> v -> kv
2731
+ -- | Like 'KeyValue' but the value is already converted to JSON
2732
+ -- ('Value' or 'Encoding'), and the result actually represents lists of pairs
2733
+ -- so it can be readily concatenated.
2734
+ class Monoid kv => KeyValuePair v kv where
2735
+ pair :: String -> v -> kv
2730
2736
2731
- instance ToJSON v => GKeyValue v (DList Pair ) where
2732
- gPair k v = DList. singleton (pack k .= v)
2737
+ instance ( v ~ Value ) => KeyValuePair v (DList Pair ) where
2738
+ pair k v = DList. singleton (pack k .= v)
2733
2739
2734
- instance GKeyValue Encoding Series where
2735
- gPair = E. pairStr
2740
+ instance ( e ~ Encoding ) => KeyValuePair e Series where
2741
+ pair = E. pairStr
0 commit comments