@@ -74,6 +74,8 @@ import Data.Fixed (Fixed, HasResolution)
7474import Data.Foldable (toList)
7575import Data.Functor.Compose (Compose (..))
7676import Data.Functor.Identity (Identity (..))
77+import Data.Functor.Product (Product (..))
78+import Data.Functor.Sum (Sum (..))
7779import Data.Int (Int16, Int32, Int64, Int8)
7880import Data.List (intersperse)
7981import Data.List.NonEmpty (NonEmpty (..))
@@ -94,27 +96,27 @@ import Foreign.Storable (Storable)
9496import GHC.Generics
9597import Numeric.Natural (Natural)
9698
97−import qualified Data.ByteString.Lazy as L
98−import qualified Data.DList as DList
99−import qualified Data.HashMap.Strict as H
100−import qualified Data.HashSet as HashSet
101−import qualified Data.IntMap as IntMap
102−import qualified Data.IntSet as IntSet
103−import qualified Data.List.NonEmpty as NE
104−import qualified Data.Map as M
105−import qualified Data.Scientific as Scientific
106−import qualified Data.Sequence as Seq
107−import qualified Data.Set as Set
108−import qualified Data.Text as T
109−import qualified Data.Text.Encoding as T
110−import qualified Data.Text.Lazy as LT
111−import qualified Data.Tree as Tree
112−import qualified Data.Vector as V
113−import qualified Data.Vector.Generic as VG
114−import qualified Data.Vector.Mutable as VM
115−import qualified Data.Vector.Primitive as VP
116−import qualified Data.Vector.Storable as VS
117−import qualified Data.Vector.Unboxed as VU
99+import qualified Data.ByteString.Lazy as L
100+import qualified Data.DList as DList
101+import qualified Data.HashMap.Strict as H
102+import qualified Data.HashSet as HashSet
103+import qualified Data.IntMap as IntMap
104+import qualified Data.IntSet as IntSet
105+import qualified Data.List.NonEmpty as NE
106+import qualified Data.Map as M
107+import qualified Data.Scientific as Scientific
108+import qualified Data.Sequence as Seq
109+import qualified Data.Set as Set
110+import qualified Data.Text as T
111+import qualified Data.Text.Encoding as T
112+import qualified Data.Text.Lazy as LT
113+import qualified Data.Tree as Tree
114+import qualified Data.Vector as V
115+import qualified Data.Vector.Generic as VG
116+import qualified Data.Vector.Mutable as VM
117+import qualified Data.Vector.Primitive as VP
118+import qualified Data.Vector.Storable as VS
119+import qualified Data.Vector.Unboxed as VU
118120
119121toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
120122toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
@@ -1204,8 +1206,8 @@ instance (ToJSON a) => ToJSON (Maybe a) where
12041206
12051207
12061208instance ToJSON2 Either where
1207− liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton left (toA a)
1208− liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1209+ liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton "Left" (toA a)
1210+ liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton "Right" (toB b)
12091211 {-# INLINE liftToJSON2 #-}
12101212
12111213 liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
@@ -1227,10 +1229,6 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
12271229 toEncoding = toEncoding2
12281230 {-# INLINE toEncoding #-}
12291231
1230−left, right :: Text
1231−left = "Left"
1232−right = "Right"
1233−
12341232
12351233instance ToJSON Bool where
12361234 toJSON = Bool
@@ -1633,6 +1631,43 @@ instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Compose f g a) where
16331631 toEncodingList = liftToEncodingList toEncoding toEncodingList
16341632 {-# INLINE toEncodingList #-}
16351633
1634+
1635+instance (ToJSON1 f, ToJSON1 g) => ToJSON1 (Product f g) where
1636+ liftToJSON tv tvl (Pair x y) = liftToJSON2 tx txl ty tyl (x, y)
1637+ where
1638+ tx = liftToJSON tv tvl
1639+ txl = liftToJSONList tv tvl
1640+ ty = liftToJSON tv tvl
1641+ tyl = liftToJSONList tv tvl
1642+
1643+ liftToEncoding te tel (Pair x y) = liftToEncoding2 tx txl ty tyl (x, y)
1644+ where
1645+ tx = liftToEncoding te tel
1646+ txl = liftToEncodingList te tel
1647+ ty = liftToEncoding te tel
1648+ tyl = liftToEncodingList te tel
1649+
1650+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Product f g a) where
1651+ toJSON = toJSON1
1652+ {-# INLINE toJSON #-}
1653+
1654+ toEncoding = toEncoding1
1655+ {-# INLINE toEncoding #-}
1656+
1657+instance (ToJSON1 f, ToJSON1 g) => ToJSON1 (Sum f g) where
1658+ liftToJSON tv tvl (InL x) = Object $ H.singleton "InL" (liftToJSON tv tvl x)
1659+ liftToJSON tv tvl (InR y) = Object $ H.singleton "InR" (liftToJSON tv tvl y)
1660+
1661+ liftToEncoding te tel (InL x) = E.pairs $ E.pair "InL" $ liftToEncoding te tel x
1662+ liftToEncoding te tel (InR y) = E.pairs $ E.pair "InR" $ liftToEncoding te tel y
1663+
1664+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Sum f g a) where
1665+ toJSON = toJSON1
1666+ {-# INLINE toJSON #-}
1667+
1668+ toEncoding = toEncoding1
1669+ {-# INLINE toEncoding #-}
1670+
16361671-------------------------------------------------------------------------------
16371672-- containers
16381673-------------------------------------------------------------------------------