@@ -1,27 +1,29 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE EmptyDataDecls #-}
4−{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE FlexibleInstances #-}
6−{-# LANGUAGE FunctionalDependencies #-}
7−{-# LANGUAGE GADTs #-}
6+{-# LANGUAGE GADTs #-}
8−{-# LANGUAGE NoImplicitPrelude #-}
7+{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE NamedFieldPuns #-}
9−{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PatternGuards #-}
10+{-# LANGUAGE PatternGuards #-}
11−{-# LANGUAGE PolyKinds #-}
12−{-# LANGUAGE RecordWildCards #-}
11+{-# LANGUAGE RecordWildCards #-}
13−{-# LANGUAGE ScopedTypeVariables #-}
12+{-# LANGUAGE ScopedTypeVariables #-}
14−{-# LANGUAGE TypeOperators #-}
13+{-# LANGUAGE TypeOperators #-}
14+{-# LANGUAGE TypeSynonymInstances #-}
15−{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE UndecidableInstances #-}
17+#if __GLASGOW_HASKELL__ >= 706
18+{-# LANGUAGE PolyKinds #-}
17#include "overlapping-compat.h"
21#include "overlapping-compat.h"
18−#include "incoherent-compat.h"
20-- TODO: Drop this when we remove support for Data.Attoparsec.Number
23-- TODO: Drop this when we remove support for Data.Attoparsec.Number
21{-# OPTIONS_GHC -fno-warn-deprecations #-}
24{-# OPTIONS_GHC -fno-warn-deprecations #-}
23−module Data.Aeson.Types.ToJSON
26+module Data.Aeson.Types.ToJSON (
25 -- * Core JSON classes
27 -- * Core JSON classes
26 ToJSON(..)
28 ToJSON(..)
27 -- * Liftings to unary and binary type constructors
29 -- * Liftings to unary and binary type constructors
@@ -33,6 +35,7 @@ module Data.Aeson.Types.ToJSON
33 , toEncoding2
35 , toEncoding2
34 -- * Generic JSON classes
36 -- * Generic JSON classes
35 , GToJSON(..)
37 , GToJSON(..)
36 , ToArgs(..)
39 , ToArgs(..)
37 , genericToJSON
40 , genericToJSON
38 , genericToEncoding
41 , genericToEncoding
@@ -43,102 +46,78 @@ module Data.Aeson.Types.ToJSON
43 , ToJSONKeyFunction(..)
46 , ToJSONKeyFunction(..)
44 , toJSONKeyText
47 , toJSONKeyText
45 , contramapToJSONKeyFunction
48 , contramapToJSONKeyFunction
50 -- * Object key-value pairs
49 -- * Object key-value pairs
51 , KeyValue(..)
50 , KeyValue(..)
54 -- * Functions needed for documentation
51 -- * Functions needed for documentation
55 -- * Encoding functions
52 -- * Encoding functions
56 , listEncoding
53 , listEncoding
57 , listValue
54 , listValue
60import Prelude.Compat
58import Prelude.Compat
62−import Control.Applicative (Const(..))
63−import Control.Monad.ST (ST)
64−import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
60+import Data.Aeson.Encoding.Internal (Encoding, Encoding', Series, dict,
65−import Data.Aeson.Encoding.Internal ((>*<))
61+ emptyArray_, tuple, (>*<), (><))
66import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
62import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
67−import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
63+import Data.Aeson.Types.Generic
68import Data.Aeson.Types.Internal
64import Data.Aeson.Types.Internal
66+-- We need internal here for generic deriving
67+import qualified Data.Aeson.Encoding.Internal as E
69+import Control.Applicative (Const (..))
70+import Control.Monad.ST (ST)
69−import Data.Attoparsec.Number (Number(..))
71+import Data.Attoparsec.Number (Number (..))
70−import Data.Bits (unsafeShiftR)
72+import Data.Bits (unsafeShiftR)
71−import Data.DList (DList)
73+import Data.DList (DList)
72−import Data.Fixed (Fixed, HasResolution, Nano)
74+import Data.Fixed (Fixed, HasResolution)
73−import Data.Foldable (toList)
75+import Data.Foldable (toList)
74−import Data.Functor.Compose (Compose(..))
76+import Data.Functor.Compose (Compose (..))
75−import Data.Functor.Contravariant (Contravariant (..))
76−import Data.Functor.Identity (Identity(..))
77+import Data.Functor.Identity (Identity (..))
77−import Data.Functor.Product (Product(..))
78+import Data.Functor.Product (Product (..))
78−import Data.Functor.Sum (Sum(..))
79+import Data.Functor.Sum (Sum (..))
79−import Data.Int (Int16, Int32, Int64, Int8)
80+import Data.Int (Int16, Int32, Int64, Int8)
80−import Data.List (intersperse)
81+import Data.List (intersperse)
81−import Data.List.NonEmpty (NonEmpty(..))
82+import Data.List.NonEmpty (NonEmpty (..))
82−import Data.Semigroup ((<>))
83+import Data.Monoid (Dual (..), First (..), Last (..), (<>))
83−import Data.Proxy (Proxy(..))
84+import Data.Proxy (Proxy (..))
84−import Data.Ratio (Ratio, denominator, numerator)
85+import Data.Ratio (Ratio, denominator, numerator)
85−import Data.Scientific (Scientific)
86+import Data.Scientific (Scientific)
86−import Data.Tagged (Tagged(..))
87+import Data.Tagged (Tagged (..))
87−import Data.Text (Text, pack)
88+import Data.Text (Text, pack)
88−import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
89+import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
89−import Data.Time.Calendar.Compat (CalendarDiffDays (..), DayOfWeek (..))
90+ UTCTime, ZonedTime)
90−import Data.Time.LocalTime.Compat (CalendarDiffTime (..))
91−import Data.Time.Clock.System.Compat (SystemTime (..))
92−import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
91+import Data.Time.Format (FormatTime, formatTime)
92+import Data.Time.Locale.Compat (defaultTimeLocale)
93−import Data.Vector (Vector)
93+import Data.Vector (Vector)
94−import Data.Version (Version, showVersion)
94+import Data.Version (Version, showVersion)
95−import Data.Void (Void, absurd)
96−import Data.Word (Word16, Word32, Word64, Word8)
95+import Data.Word (Word16, Word32, Word64, Word8)
97−import Foreign.Storable (Storable)
96+import Foreign.Storable (Storable)
98−import Foreign.C.Types (CTime (..))
99import GHC.Generics
97import GHC.Generics
100−import Numeric.Natural (Natural)
98+import Numeric.Natural (Natural)
101−import qualified Data.Aeson.Encoding as E
99+
102−import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
103−import qualified Data.ByteString.Lazy as L
100+import qualified Data.ByteString.Lazy as L
104−import qualified Data.DList as DList
101+import qualified Data.DList as DList
105−import qualified Data.HashMap.Strict as H
102+import qualified Data.HashMap.Strict as H
106−import qualified Data.HashSet as HashSet
103+import qualified Data.HashSet as HashSet
107−import qualified Data.IntMap as IntMap
104+import qualified Data.IntMap as IntMap
108−import qualified Data.IntSet as IntSet
105+import qualified Data.IntSet as IntSet
109−import qualified Data.List.NonEmpty as NE
106+import qualified Data.List.NonEmpty as NE
110−import qualified Data.Map as M
107+import qualified Data.Map as M
111−import qualified Data.Monoid as Monoid
112−import qualified Data.Scientific as Scientific
108+import qualified Data.Scientific as Scientific
113−import qualified Data.Semigroup as Semigroup
114−import qualified Data.Sequence as Seq
109+import qualified Data.Sequence as Seq
115−import qualified Data.Set as Set
110+import qualified Data.Set as Set
116−import qualified Data.Text as T
111+import qualified Data.Text as T
117−import qualified Data.Text.Encoding as T
112+import qualified Data.Text.Encoding as T
118−import qualified Data.Text.Lazy as LT
113+import qualified Data.Text.Lazy as LT
119−import qualified Data.Tree as Tree
114+import qualified Data.Tree as Tree
120−import qualified Data.UUID.Types as UUID
121−import qualified Data.Vector as V
115+import qualified Data.Vector as V
122−import qualified Data.Vector.Generic as VG
116+import qualified Data.Vector.Generic as VG
123−import qualified Data.Vector.Mutable as VM
117+import qualified Data.Vector.Mutable as VM
124import qualified Data.Vector.Primitive as VP
118import qualified Data.Vector.Primitive as VP
125−import qualified Data.Vector.Storable as VS
119+import qualified Data.Vector.Storable as VS
126−import qualified Data.Vector.Unboxed as VU
120+import qualified Data.Vector.Unboxed as VU
128−import qualified Data.Aeson.Encoding.Builder as EB
129−import qualified Data.ByteString.Builder as B
131−import qualified GHC.Exts as Exts
132−import qualified Data.Primitive.Array as PM
133−import qualified Data.Primitive.SmallArray as PM
134−import qualified Data.Primitive.Types as PM
136−#if MIN_VERSION_primitive(0,6,4)
137−#if !MIN_VERSION_primitive(0,7,0)
138−import qualified Data.Primitive.UnliftedArray as PM
140−import qualified Data.Primitive.PrimArray as PM
143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
122toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
144toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
123toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
@@ -156,16 +135,19 @@ realFloatToJSON d
157-- | Class of generic representation types that can be converted to
136-- | Class of generic representation types that can be converted to
159−class GToJSON enc arity f where
138+class GToJSON arity f where
160 -- | This method (applied to 'defaultOptions') is used as the
139 -- | This method (applied to 'defaultOptions') is used as the
161− -- default generic implementation of 'toJSON'
140+ -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
162− -- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
163− -- and 'liftToJSON' (if the @arity@ is 'One').
141+ -- or 'liftToJSON' (if the @arity@ is 'One').
164− --
142+ gToJSON :: Options -> ToArgs Value arity a -> f a -> Value
144+-- | Class of generic representation types that can be converted to
145+-- a JSON 'Encoding'.
146+class GToEncoding arity f where
147+ -- | This method (applied to 'defaultOptions') can be used as the
165− -- It also provides a generic implementation of 'toEncoding'
148+ -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
166− -- (with @enc ~ 'Encoding'@ and @arity ~ 'Zero'@)
167− -- and 'liftToEncoding' (if the @arity@ is 'One').
149+ -- or 'liftToEncoding' (if the @arity@ is 'One').
168− gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
150+ gToEncoding :: Options -> ToArgs Encoding arity a -> f a -> Encoding
170-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
152-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
171-- function arguments that encode occurrences of the type parameter (for
153-- function arguments that encode occurrences of the type parameter (for
@@ -177,14 +159,14 @@ data ToArgs res arity a where
177-- | A configurable generic JSON creator. This function applied to
159-- | A configurable generic JSON creator. This function applied to
178-- 'defaultOptions' is used as the default for 'toJSON' when the type
160-- 'defaultOptions' is used as the default for 'toJSON' when the type
179-- is an instance of 'Generic'.
161-- is an instance of 'Generic'.
180−genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
162+genericToJSON :: (Generic a, GToJSON Zero (Rep a))
181 => Options -> a -> Value
163 => Options -> a -> Value
182genericToJSON opts = gToJSON opts NoToArgs . from
164genericToJSON opts = gToJSON opts NoToArgs . from
184-- | A configurable generic JSON creator. This function applied to
166-- | A configurable generic JSON creator. This function applied to
185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
167-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
186-- is an instance of 'Generic1'.
168-- is an instance of 'Generic1'.
187−genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
169+genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
188 => Options -> (a -> Value) -> ([a] -> Value)
170 => Options -> (a -> Value) -> ([a] -> Value)
189 -> f a -> Value
171 -> f a -> Value
190genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
172genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
@@ -192,17 +174,17 @@ genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
192-- | A configurable generic JSON encoder. This function applied to
174-- | A configurable generic JSON encoder. This function applied to
193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
175-- 'defaultOptions' is used as the default for 'toEncoding' when the type
194-- is an instance of 'Generic'.
176-- is an instance of 'Generic'.
195−genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
177+genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
196 => Options -> a -> Encoding
178 => Options -> a -> Encoding
197−genericToEncoding opts = gToJSON opts NoToArgs . from
179+genericToEncoding opts = gToEncoding opts NoToArgs . from
199-- | A configurable generic JSON encoder. This function applied to
181-- | A configurable generic JSON encoder. This function applied to
200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
182-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
201-- is an instance of 'Generic1'.
183-- is an instance of 'Generic1'.
202−genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
184+genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
185 => Options -> (a -> Encoding) -> ([a] -> Encoding)
204 -> f a -> Encoding
186 -> f a -> Encoding
205−genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
187+genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
207-------------------------------------------------------------------------------
189-------------------------------------------------------------------------------
@@ -210,9 +192,6 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
211-- | A type that can be converted to JSON.
193-- | A type that can be converted to JSON.
213−-- Instances in general /must/ specify 'toJSON' and /should/ (but don't need
214−-- to) specify 'toEncoding'.
216-- An example type and instance:
195-- An example type and instance:
@@ -221,10 +200,10 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
222-- data Coord = Coord { x :: Double, y :: Double }
201-- data Coord = Coord { x :: Double, y :: Double }
224−-- instance 'ToJSON' Coord where
203+-- instance ToJSON Coord where
225−-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
204+-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
227−-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
206+-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
230-- Instead of manually writing your 'ToJSON' instance, there are two options
209-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -232,15 +211,17 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
212-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
234-- instance at compile time. The generated instance is optimized for your type
213-- instance at compile time. The generated instance is optimized for your type
235−-- so it will probably be more efficient than the following option.
214+-- so will probably be more efficient than the following two options:
237-- * The compiler can provide a default generic implementation for
216-- * The compiler can provide a default generic implementation for
238-- 'toJSON'.
217-- 'toJSON'.
240-- To use the second, simply add a @deriving 'Generic'@ clause to your
219-- To use the second, simply add a @deriving 'Generic'@ clause to your
241−-- datatype and declare a 'ToJSON' instance. If you require nothing other than
220+-- datatype and declare a 'ToJSON' instance for your datatype without giving
242−-- 'defaultOptions', it is sufficient to write (and this is the only
221+-- definitions for 'toJSON' or 'toEncoding'.
243−-- alternative where the default 'toJSON' implementation is sufficient):
222+--
223+-- For example, the previous example can be simplified to a more
246-- {-\# LANGUAGE DeriveGeneric \#-}
227-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -249,46 +230,35 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
231-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
252−-- instance 'ToJSON' Coord where
233+-- instance ToJSON Coord where
253−-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
234+-- toEncoding = 'genericToEncoding' 'defaultOptions'
256−-- If on the other hand you wish to customize the generic decoding, you have
237+-- Why do we provide an implementation for 'toEncoding' here? The
257−-- to implement both methods:
238+-- 'toEncoding' function is a relatively new addition to this class.
239+-- To allow users of older versions of this library to upgrade without
240+-- having to edit all of their instances or encounter surprising
241+-- incompatibilities, the default implementation of 'toEncoding' uses
242+-- 'toJSON'. This produces correct results, but since it performs an
243+-- intermediate conversion to a 'Value', it will be less efficient
244+-- than directly emitting an 'Encoding'. Our one-liner definition of
245+-- 'toEncoding' above bypasses the intermediate 'Value'.
247+-- If @DefaultSignatures@ doesn't give exactly the results you want,
248+-- you can customize the generic encoding with only a tiny amount of
249+-- effort, using 'genericToJSON' and 'genericToEncoding' with your
250+-- preferred 'Options':
260−-- customOptions = 'defaultOptions'
261−-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
264−-- instance 'ToJSON' Coord where
253+-- instance ToJSON Coord where
265−-- 'toJSON' = 'genericToJSON' customOptions
254+-- toJSON = 'genericToJSON' 'defaultOptions'
266−-- 'toEncoding' = 'genericToEncoding' customOptions
255+-- toEncoding = 'genericToEncoding' 'defaultOptions'
269−-- Previous versions of this library only had the 'toJSON' method. Adding
270−-- 'toEncoding' had two reasons:
272−-- 1. toEncoding is more efficient for the common case that the output of
273−-- 'toJSON' is directly serialized to a @ByteString@.
274−-- Further, expressing either method in terms of the other would be
277−-- 2. The choice of defaults allows a smooth transition for existing users:
278−-- Existing instances that do not define 'toEncoding' still
279−-- compile and have the correct semantics. This is ensured by making
280−-- the default implementation of 'toEncoding' use 'toJSON'. This produces
281−-- correct results, but since it performs an intermediate conversion to a
282−-- 'Value', it will be less efficient than directly emitting an 'Encoding'.
283−-- (this also means that specifying nothing more than
284−-- @instance ToJSON Coord@ would be sufficient as a generically decoding
285−-- instance, but there probably exists no good reason to not specify
286−-- 'toEncoding' in new instances.)
287class ToJSON a where
257class ToJSON a where
288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
258 -- | Convert a Haskell value to a JSON-friendly intermediate type.
289 toJSON :: a -> Value
259 toJSON :: a -> Value
291− default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
261+ default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
292 toJSON = genericToJSON defaultOptions
262 toJSON = genericToJSON defaultOptions
294 -- | Encode a Haskell value as JSON.
264 -- | Encode a Haskell value as JSON.
@@ -305,8 +275,8 @@ class ToJSON a where
305 -- extension, and then have GHC generate a method body as follows.
275 -- extension, and then have GHC generate a method body as follows.
308− -- instance 'ToJSON' Coord where
278+ -- instance ToJSON Coord where
309− -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
279+ -- toEncoding = 'genericToEncoding' 'defaultOptions'
312 toEncoding :: a -> Encoding
282 toEncoding :: a -> Encoding
@@ -338,13 +308,6 @@ instance KeyValue Pair where
338 name .= value = (name, toJSON value)
308 name .= value = (name, toJSON value)
339 {-# INLINE (.=) #-}
309 {-# INLINE (.=) #-}
341−-- | Constructs a singleton 'H.HashMap'. For calling functions that
342−-- demand an 'Object' for constructing objects. To be used in
343−-- conjunction with 'mconcat'. Prefer to use 'object' where possible.
344−instance KeyValue Object where
345− name .= value = H.singleton name (toJSON value)
348-------------------------------------------------------------------------------
311-------------------------------------------------------------------------------
349-- Classes and types for map keys
312-- Classes and types for map keys
350-------------------------------------------------------------------------------
313-------------------------------------------------------------------------------
@@ -402,7 +365,7 @@ instance KeyValue Object where
402-- newtype wrapper around 'Text'. The recommended approach is to use
365-- newtype wrapper around 'Text'. The recommended approach is to use
403-- generalized newtype deriving:
366-- generalized newtype deriving:
405−-- > newtype RecordId = RecordId { getRecordId :: Text }
368+-- > newtype RecordId = RecordId { getRecordId :: Text}
406-- > deriving (Eq,Ord,ToJSONKey)
369-- > deriving (Eq,Ord,ToJSONKey)
408-- Then we may write:
371-- Then we may write:
@@ -417,18 +380,8 @@ instance KeyValue Object where
418-- It is possible to get the 'ToJSONKey' instance for free as we did
381-- It is possible to get the 'ToJSONKey' instance for free as we did
419-- with 'Foo'. However, in this case, we have a natural way to go to
382-- with 'Foo'. However, in this case, we have a natural way to go to
420−-- and from 'Text' that does not require any escape sequences. So
383+-- and from 'Text' that does not require any escape sequences. So, in
421−-- 'ToJSONKeyText' can be used instead of 'ToJSONKeyValue' to encode maps
384+-- this example, 'ToJSONKeyText' will be used instead of 'ToJSONKeyValue'.
422−-- as objects instead of arrays of pairs. This instance may be
423−-- implemented using generics as follows:
426−-- instance 'ToJSONKey' Color where
427−-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
430−-- === __Low-level implementations__
432-- The 'Show' instance can be used to help write 'ToJSONKey':
385-- The 'Show' instance can be used to help write 'ToJSONKey':
434-- > instance ToJSONKey Color where
387-- > instance ToJSONKey Color where
@@ -501,14 +454,7 @@ toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
501toJSONKeyTextEnc e = ToJSONKeyText tot e
454toJSONKeyTextEnc e = ToJSONKeyText tot e
503 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
456 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
504− tot = T.dropAround (== '"')
507− . E.encodingToLazyByteString
457+ tot = T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . E.encodingToLazyByteString . e
510−instance Contravariant ToJSONKeyFunction where
511− contramap = contramapToJSONKeyFunction
513-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
459-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
460contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
@@ -516,29 +462,6 @@ contramapToJSONKeyFunction h x = case x of
516 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
462 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
517 ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
463 ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
519−-- 'toJSONKey' for 'Generic' types.
520−-- Deriving is supported for enumeration types, i.e. the sums of nullary
521−-- constructors. The names of constructors will be used as keys for JSON
524−-- See also 'genericFromJSONKey'.
529−-- data Color = Red | Green | Blue
530−-- deriving 'Generic'
532−-- instance 'ToJSONKey' Color where
533−-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
535−genericToJSONKey :: (Generic a, GToJSONKey (Rep a))
536− => JSONKeyOptions -> ToJSONKeyFunction a
537−genericToJSONKey opts = toJSONKeyText (pack . keyModifier opts . getConName . from)
539−class GetConName f => GToJSONKey f
540−instance GetConName f => GToJSONKey f
542-------------------------------------------------------------------------------
465-------------------------------------------------------------------------------
543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
466-- Lifings of FromJSON and ToJSON to unary and binary type constructors
544-------------------------------------------------------------------------------
467-------------------------------------------------------------------------------
@@ -551,7 +474,7 @@ instance GetConName f => GToJSONKey f
552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
475-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
553-- instance at compile time. The generated instance is optimized for your type
476-- instance at compile time. The generated instance is optimized for your type
554−-- so it will probably be more efficient than the following option.
477+-- so will probably be more efficient than the following two options:
556-- * The compiler can provide a default generic implementation for
479-- * The compiler can provide a default generic implementation for
557-- 'toJSON1'.
480-- 'toJSON1'.
@@ -569,29 +492,23 @@ instance GetConName f => GToJSONKey f
570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
493-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
572−-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
495+-- instance ToJSON a => ToJSON1 (Pair a)
575−-- If the default implementation doesn't give exactly the results you want,
498+-- If @DefaultSignatures@ doesn't give exactly the results you want,
576-- you can customize the generic encoding with only a tiny amount of
499-- you can customize the generic encoding with only a tiny amount of
577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
500-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
578-- your preferred 'Options':
501-- your preferred 'Options':
581−-- customOptions = 'defaultOptions'
582−-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
585−-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
504+-- instance ToJSON a => ToJSON1 (Pair a) where
586−-- 'liftToJSON' = 'genericLiftToJSON' customOptions
505+-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
587−-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
506+-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
590−-- See also 'ToJSON'.
591class ToJSON1 f where
508class ToJSON1 f where
592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
509 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
594− default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
511+ default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
512 => (a -> Value) -> ([a] -> Value) -> f a -> Value
596 liftToJSON = genericLiftToJSON defaultOptions
513 liftToJSON = genericLiftToJSON defaultOptions
@@ -600,7 +517,7 @@ class ToJSON1 f where
601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
518 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
603− default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
520+ default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
604 => (a -> Encoding) -> ([a] -> Encoding)
521 => (a -> Encoding) -> ([a] -> Encoding)
605 -> f a -> Encoding
522 -> f a -> Encoding
606 liftToEncoding = genericLiftToEncoding defaultOptions
523 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -648,25 +565,9 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
648-- Encoding functions
565-- Encoding functions
649-------------------------------------------------------------------------------
566-------------------------------------------------------------------------------
651−-- | Helper function to use with 'liftToEncoding'.
652−-- Useful when writing own 'ToJSON1' instances.
655−-- newtype F a = F [a]
657−-- -- This instance encodes 'String' as an array of chars
658−-- instance 'ToJSON1' F where
659−-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
660−-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
662−-- instance 'Data.Aeson.FromJSON.FromJSON1' F where
663−-- 'Data.Aeson.FromJSON.liftParseJSON' p _ v = F \<$\> 'Data.Aeson.FromJSON.liftParseJSON' p ('Data.Aeson.FromJSON.listParser' p) v
665listEncoding :: (a -> Encoding) -> [a] -> Encoding
568listEncoding :: (a -> Encoding) -> [a] -> Encoding
666listEncoding = E.list
569listEncoding = E.list
667−{-# INLINE listEncoding #-}
669−-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
670listValue :: (a -> Value) -> [a] -> Value
571listValue :: (a -> Value) -> [a] -> Value
671listValue f = Array . V.fromList . map f
572listValue f = Array . V.fromList . map f
672{-# INLINE listValue #-}
573{-# INLINE listValue #-}
@@ -685,11 +586,6 @@ instance ToJSON1 [] where
685 {-# INLINE liftToEncoding #-}
586 {-# INLINE liftToEncoding #-}
687instance (ToJSON a) => ToJSON [a] where
588instance (ToJSON a) => ToJSON [a] where
688− {-# SPECIALIZE instance ToJSON String #-}
689− {-# SPECIALIZE instance ToJSON [String] #-}
690− {-# SPECIALIZE instance ToJSON [Array] #-}
691− {-# SPECIALIZE instance ToJSON [Object] #-}
693 toJSON = toJSON1
589 toJSON = toJSON1
694 {-# INLINE toJSON #-}
590 {-# INLINE toJSON #-}
@@ -700,80 +596,39 @@ instance (ToJSON a) => ToJSON [a] where
700-- Generic toJSON / toEncoding
596-- Generic toJSON / toEncoding
701-------------------------------------------------------------------------------
597-------------------------------------------------------------------------------
703−instance OVERLAPPABLE_ (GToJSON enc arity a) => GToJSON enc arity (M1 i c a) where
704− -- Meta-information, which is not handled elsewhere, is ignored:
705− gToJSON opts targs = gToJSON opts targs . unM1
706− {-# INLINE gToJSON #-}
708−instance GToJSON enc One Par1 where
709− -- Direct occurrences of the last type parameter are encoded with the
710− -- function passed in as an argument:
711− gToJSON _opts (To1Args tj _) = tj . unPar1
712− {-# INLINE gToJSON #-}
714−instance ( ConsToJSON enc arity a
715− , AllNullary (C1 c a) allNullary
716− , SumToJSON enc arity (C1 c a) allNullary
717− ) => GToJSON enc arity (D1 d (C1 c a)) where
718− -- The option 'tagSingleConstructors' determines whether to wrap
719− -- a single-constructor type.
721− | tagSingleConstructors opts = (unTagged :: Tagged allNullary enc -> enc)
722− . sumToJSON opts targs
724− | otherwise = consToJSON opts targs . unM1 . unM1
725− {-# INLINE gToJSON #-}
727−instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
728− -- Constructors need to be encoded differently depending on whether they're
729− -- a record or not. This distinction is made by 'consToJSON':
730− gToJSON opts targs = consToJSON opts targs . unM1
731− {-# INLINE gToJSON #-}
733−instance ( AllNullary (a :+: b) allNullary
734− , SumToJSON enc arity (a :+: b) allNullary
735− ) => GToJSON enc arity (a :+: b)
737− -- If all constructors of a sum datatype are nullary and the
738− -- 'allNullaryToStringTag' option is set they are encoded to
739− -- strings. This distinction is made by 'sumToJSON':
740− gToJSON opts targs = (unTagged :: Tagged allNullary enc -> enc)
741− . sumToJSON opts targs
742− {-# INLINE gToJSON #-}
744--------------------------------------------------------------------------------
599--------------------------------------------------------------------------------
745-- Generic toJSON
600-- Generic toJSON
747−-- Note: Refactoring 'ToJSON a' to 'ToJSON enc a' (and 'ToJSON1' similarly) is
748−-- possible but makes error messages a bit harder to understand for missing
751−instance GToJSON Value arity V1 where
602+instance OVERLAPPABLE_ (GToJSON arity a) => GToJSON arity (M1 i c a) where
752− -- Empty values do not exist, which makes the job of formatting them
603+ -- Meta-information, which is not handled elsewhere, is ignored:
753− -- rather easy:
604+ gToJSON opts targs = gToJSON opts targs . unM1
754− gToJSON _ _ x = x `seq` error "case: V1"
755− {-# INLINE gToJSON #-}
757−instance ToJSON a => GToJSON Value arity (K1 i a) where
606+instance (ToJSON a) => GToJSON arity (K1 i a) where
758 -- Constant values are encoded using their ToJSON instance:
607 -- Constant values are encoded using their ToJSON instance:
759 gToJSON _opts _ = toJSON . unK1
608 gToJSON _opts _ = toJSON . unK1
760− {-# INLINE gToJSON #-}
610+instance GToJSON One Par1 where
611+ -- Direct occurrences of the last type parameter are encoded with the
612+ -- function passed in as an argument:
613+ gToJSON _opts (To1Args tj _) = tj . unPar1
762−instance ToJSON1 f => GToJSON Value One (Rec1 f) where
615+instance (ToJSON1 f) => GToJSON One (Rec1 f) where
763 -- Recursive occurrences of the last type parameter are encoded using their
616 -- Recursive occurrences of the last type parameter are encoded using their
764 -- ToJSON1 instance:
617 -- ToJSON1 instance:
765 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
618 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766− {-# INLINE gToJSON #-}
768−instance GToJSON Value arity U1 where
620+instance GToJSON arity U1 where
769 -- Empty constructors are encoded to an empty array:
621 -- Empty constructors are encoded to an empty array:
770 gToJSON _opts _ _ = emptyArray
622 gToJSON _opts _ _ = emptyArray
771− {-# INLINE gToJSON #-}
623+
624+instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
625+ -- Constructors need to be encoded differently depending on whether they're
626+ -- a record or not. This distinction is made by 'consToJSON':
627+ gToJSON opts targs = consToJSON opts targs . unM1
773instance ( WriteProduct arity a, WriteProduct arity b
629instance ( WriteProduct arity a, WriteProduct arity b
774 , ProductSize a, ProductSize b
630 , ProductSize a, ProductSize b
775− ) => GToJSON Value arity (a :*: b)
631+ ) => GToJSON arity (a :*: b) where
777 -- Products are encoded to an array. Here we allocate a mutable vector of
632 -- Products are encoded to an array. Here we allocate a mutable vector of
778 -- the same size as the product and write the product's elements to it using
633 -- the same size as the product and write the product's elements to it using
779 -- 'writeProduct':
634 -- 'writeProduct':
@@ -785,175 +640,230 @@ instance ( WriteProduct arity a, WriteProduct arity b
786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
641 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
787 productSize
642 productSize
788− {-# INLINE gToJSON #-}
790−instance ( ToJSON1 f
644+instance ( AllNullary (a :+: b) allNullary
791− , GToJSON Value One g
645+ , SumToJSON arity (a :+: b) allNullary
792− ) => GToJSON Value One (f :.: g)
646+ ) => GToJSON arity (a :+: b) where
793− where
647+ -- If all constructors of a sum datatype are nullary and the
648+ -- 'allNullaryToStringTag' option is set they are encoded to
649+ -- strings. This distinction is made by 'sumToJSON':
650+ gToJSON opts targs = (unTagged :: Tagged allNullary Value -> Value)
651+ . sumToJSON opts targs
653+instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
794 -- If an occurrence of the last type parameter is nested inside two
654 -- If an occurrence of the last type parameter is nested inside two
795 -- composed types, it is encoded by using the outermost type's ToJSON1
655 -- composed types, it is encoded by using the outermost type's ToJSON1
796 -- instance to generically encode the innermost type:
656 -- instance to generically encode the innermost type:
797 gToJSON opts targs =
657 gToJSON opts targs =
798 let gtj = gToJSON opts targs in
658 let gtj = gToJSON opts targs in
799 liftToJSON gtj (listValue gtj) . unComp1
659 liftToJSON gtj (listValue gtj) . unComp1
800− {-# INLINE gToJSON #-}
802--------------------------------------------------------------------------------
661--------------------------------------------------------------------------------
803-- Generic toEncoding
662-- Generic toEncoding
664+instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
665+ -- Meta-information, which is not handled elsewhere, is ignored:
666+ gToEncoding opts targs = gToEncoding opts targs . unM1
805−instance ToJSON a => GToJSON Encoding arity (K1 i a) where
668+instance (ToJSON a) => GToEncoding arity (K1 i a) where
806 -- Constant values are encoded using their ToJSON instance:
669 -- Constant values are encoded using their ToJSON instance:
807− gToJSON _opts _ = toEncoding . unK1
670+ gToEncoding _opts _ = toEncoding . unK1
808− {-# INLINE gToJSON #-}
672+instance GToEncoding One Par1 where
673+ -- Direct occurrences of the last type parameter are encoded with the
674+ -- function passed in as an argument:
675+ gToEncoding _opts (To1Args te _) = te . unPar1
810−instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
677+instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
811 -- Recursive occurrences of the last type parameter are encoded using their
678 -- Recursive occurrences of the last type parameter are encoded using their
812 -- ToEncoding1 instance:
679 -- ToEncoding1 instance:
813− gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
680+ gToEncoding _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814− {-# INLINE gToJSON #-}
816−instance GToJSON Encoding arity U1 where
682+instance GToEncoding arity U1 where
817 -- Empty constructors are encoded to an empty array:
683 -- Empty constructors are encoded to an empty array:
818− gToJSON _opts _ _ = E.emptyArray_
684+ gToEncoding _opts _ _ = E.emptyArray_
819− {-# INLINE gToJSON #-}
685+
686+instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
687+ -- Constructors need to be encoded differently depending on whether they're
688+ -- a record or not. This distinction is made by 'consToEncoding':
689+ gToEncoding opts targs = consToEncoding opts targs . unM1
821instance ( EncodeProduct arity a
691instance ( EncodeProduct arity a
822 , EncodeProduct arity b
692 , EncodeProduct arity b
823− ) => GToJSON Encoding arity (a :*: b)
693+ ) => GToEncoding arity (a :*: b) where
825 -- Products are encoded to an array. Here we allocate a mutable vector of
694 -- Products are encoded to an array. Here we allocate a mutable vector of
826 -- the same size as the product and write the product's elements to it using
695 -- the same size as the product and write the product's elements to it using
827 -- 'encodeProduct':
696 -- 'encodeProduct':
828− gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
697+ gToEncoding opts targs p = E.tuple $ encodeProduct opts targs p
829− {-# INLINE gToJSON #-}
831−instance ( ToJSON1 f
699+instance ( AllNullary (a :+: b) allNullary
832− , GToJSON Encoding One g
700+ , SumToEncoding arity (a :+: b) allNullary
833− ) => GToJSON Encoding One (f :.: g)
701+ ) => GToEncoding arity (a :+: b) where
834− where
702+ -- If all constructors of a sum datatype are nullary and the
703+ -- 'allNullaryToStringTag' option is set they are encoded to
704+ -- strings. This distinction is made by 'sumToEncoding':
705+ gToEncoding opts targs
706+ = (unTagged :: Tagged allNullary Encoding -> Encoding)
707+ . sumToEncoding opts targs
709+instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
835 -- If an occurrence of the last type parameter is nested inside two
710 -- If an occurrence of the last type parameter is nested inside two
836 -- composed types, it is encoded by using the outermost type's ToJSON1
711 -- composed types, it is encoded by using the outermost type's ToJSON1
837 -- instance to generically encode the innermost type:
712 -- instance to generically encode the innermost type:
838− gToJSON opts targs =
713+ gToEncoding opts targs =
839− let gte = gToJSON opts targs in
714+ let gte = gToEncoding opts targs in
840 liftToEncoding gte (listEncoding gte) . unComp1
715 liftToEncoding gte (listEncoding gte) . unComp1
841− {-# INLINE gToJSON #-}
843--------------------------------------------------------------------------------
717--------------------------------------------------------------------------------
845−class SumToJSON enc arity f allNullary where
719+class SumToJSON arity f allNullary where
846− sumToJSON :: Options -> ToArgs enc arity a
720+ sumToJSON :: Options -> ToArgs Value arity a
847− -> f a -> Tagged allNullary enc
721+ -> f a -> Tagged allNullary Value
849−instance ( GetConName f
723+instance ( GetConName f
850− , FromString enc
724+ , TaggedObjectPairs arity f
851− , TaggedObject enc arity f
725+ , ObjectWithSingleFieldObj arity f
852− , SumToJSON' ObjectWithSingleField enc arity f
726+ , TwoElemArrayObj arity f
853− , SumToJSON' TwoElemArray enc arity f
854− , SumToJSON' UntaggedValue enc arity f
855− ) => SumToJSON enc arity f True
727+ ) => SumToJSON arity f True where
857 sumToJSON opts targs
728 sumToJSON opts targs
858− | allNullaryToStringTag opts = Tagged . fromString
729+ | allNullaryToStringTag opts = Tagged . String . pack
859 . constructorTagModifier opts . getConName
730 . constructorTagModifier opts . getConName
860 | otherwise = Tagged . nonAllNullarySumToJSON opts targs
731 | otherwise = Tagged . nonAllNullarySumToJSON opts targs
862−instance ( TaggedObject enc arity f
733+instance ( TwoElemArrayObj arity f
863− , SumToJSON' ObjectWithSingleField enc arity f
734+ , TaggedObjectPairs arity f
864− , SumToJSON' TwoElemArray enc arity f
735+ , ObjectWithSingleFieldObj arity f
865− , SumToJSON' UntaggedValue enc arity f
866− ) => SumToJSON enc arity f False
736+ ) => SumToJSON arity f False where
868 sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
737 sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
870−nonAllNullarySumToJSON :: ( TaggedObject enc arity f
739+nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
871− , SumToJSON' ObjectWithSingleField enc arity f
740+ , TaggedObjectPairs arity f
872− , SumToJSON' TwoElemArray enc arity f
741+ , ObjectWithSingleFieldObj arity f
873− , SumToJSON' UntaggedValue enc arity f
874− ) => Options -> ToArgs enc arity a
742+ ) => Options -> ToArgs Value arity a
875− -> f a -> enc
743+ -> f a -> Value
876nonAllNullarySumToJSON opts targs =
744nonAllNullarySumToJSON opts targs =
877 case sumEncoding opts of
745 case sumEncoding opts of
879 TaggedObject{..} ->
746 TaggedObject{..} ->
880− taggedObject opts targs tagFieldName contentsFieldName
747+ object . taggedObjectPairs opts targs tagFieldName contentsFieldName
748+ ObjectWithSingleField -> Object . objectWithSingleFieldObj opts targs
749+ TwoElemArray -> Array . twoElemArrayObj opts targs
882− ObjectWithSingleField ->
751+--------------------------------------------------------------------------------
883− (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884− . sumToJSON' opts targs
887− (unTagged :: Tagged TwoElemArray enc -> enc)
888− . sumToJSON' opts targs
890− UntaggedValue ->
753+class SumToEncoding arity f allNullary where
891− (unTagged :: Tagged UntaggedValue enc -> enc)
754+ sumToEncoding :: Options -> ToArgs Encoding arity a
892− . sumToJSON' opts targs
755+ -> f a -> Tagged allNullary Encoding
757+instance ( GetConName f
758+ , TaggedObjectEnc arity f
759+ , ObjectWithSingleFieldEnc arity f
760+ , TwoElemArrayEnc arity f
761+ ) => SumToEncoding arity f True where
762+ sumToEncoding opts targs
763+ | allNullaryToStringTag opts = Tagged . toEncoding .
764+ constructorTagModifier opts . getConName
765+ | otherwise = Tagged . nonAllNullarySumToEncoding opts targs
767+instance ( TwoElemArrayEnc arity f
768+ , TaggedObjectEnc arity f
769+ , ObjectWithSingleFieldEnc arity f
770+ ) => SumToEncoding arity f False where
771+ sumToEncoding opts targs = Tagged . nonAllNullarySumToEncoding opts targs
773+nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
774+ , TaggedObjectEnc arity f
775+ , ObjectWithSingleFieldEnc arity f
776+ ) => Options -> ToArgs Encoding arity a
778+nonAllNullarySumToEncoding opts targs =
779+ case sumEncoding opts of
781+ taggedObjectEnc opts targs tagFieldName contentsFieldName
782+ ObjectWithSingleField -> objectWithSingleFieldEnc opts targs
783+ TwoElemArray -> twoElemArrayEnc opts targs
894--------------------------------------------------------------------------------
785--------------------------------------------------------------------------------
896−class FromString enc where
787+class TaggedObjectPairs arity f where
897− fromString :: String -> enc
788+ taggedObjectPairs :: Options -> ToArgs Value arity a
792+instance ( TaggedObjectPairs arity a
793+ , TaggedObjectPairs arity b
794+ ) => TaggedObjectPairs arity (a :+: b) where
795+ taggedObjectPairs opts targs tagFieldName contentsFieldName (L1 x) =
796+ taggedObjectPairs opts targs tagFieldName contentsFieldName x
797+ taggedObjectPairs opts targs tagFieldName contentsFieldName (R1 x) =
798+ taggedObjectPairs opts targs tagFieldName contentsFieldName x
800+instance ( IsRecord a isRecord
801+ , TaggedObjectPairs' arity a isRecord
803+ ) => TaggedObjectPairs arity (C1 c a) where
804+ taggedObjectPairs opts targs tagFieldName contentsFieldName =
805+ (pack tagFieldName .= constructorTagModifier opts
806+ (conName (undefined :: t c a p)) :) .
807+ (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
808+ taggedObjectPairs' opts targs contentsFieldName . unM1
810+class TaggedObjectPairs' arity f isRecord where
811+ taggedObjectPairs' :: Options -> ToArgs Value arity a
812+ -> String -> f a -> Tagged isRecord [Pair]
814+instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
815+ taggedObjectPairs' _ _ _ _ = Tagged []
899−instance FromString Encoding where
817+instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
900− fromString = toEncoding
818+ taggedObjectPairs' opts targs _ =
819+ Tagged . toList . recordToPairs opts targs
902−instance FromString Value where
821+instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
903− fromString = String . pack
822+ taggedObjectPairs' opts targs contentsFieldName =
823+ Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts targs
905--------------------------------------------------------------------------------
825--------------------------------------------------------------------------------
907−class TaggedObject enc arity f where
827+class TaggedObjectEnc arity f where
908− taggedObject :: Options -> ToArgs enc arity a
828+ taggedObjectEnc :: Options -> ToArgs Encoding arity a
909− -> String -> String
829+ -> String -> String
910− -> f a -> enc
830+ -> f a -> Encoding
912−instance ( TaggedObject enc arity a
832+instance ( TaggedObjectEnc arity a
913− , TaggedObject enc arity b
833+ , TaggedObjectEnc arity b
914− ) => TaggedObject enc arity (a :+: b)
834+ ) => TaggedObjectEnc arity (a :+: b) where
916− taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
835+ taggedObjectEnc opts targs tagFieldName contentsFieldName (L1 x) =
917− taggedObject opts targs tagFieldName contentsFieldName x
836+ taggedObjectEnc opts targs tagFieldName contentsFieldName x
918− taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
837+ taggedObjectEnc opts targs tagFieldName contentsFieldName (R1 x) =
919− taggedObject opts targs tagFieldName contentsFieldName x
838+ taggedObjectEnc opts targs tagFieldName contentsFieldName x
921−instance ( IsRecord a isRecord
840+instance ( IsRecord a isRecord
922− , TaggedObject' enc pairs arity a isRecord
841+ , TaggedObjectEnc' arity a isRecord
923− , FromPairs enc pairs
925− , KeyValuePair enc pairs
926 , Constructor c
842 , Constructor c
927− ) => TaggedObject enc arity (C1 c a)
843+ ) => TaggedObjectEnc arity (C1 c a) where
929− taggedObject opts targs tagFieldName contentsFieldName =
844+ taggedObjectEnc opts targs tagFieldName contentsFieldName v =
930− fromPairs . mappend tag . contents
845+ E.wrapObject $
931− where
846+ (E.string tagFieldName ><
932− tag = tagFieldName `pair`
847+ E.colon ><
933− (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
848+ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) ><
936− (unTagged :: Tagged isRecord pairs -> pairs) .
849+ ((unTagged :: Tagged isRecord Encoding -> Encoding) .
937− taggedObject' opts targs contentsFieldName . unM1
850+ taggedObjectEnc' opts targs contentsFieldName . unM1 $ v)
939−class TaggedObject' enc pairs arity f isRecord where
852+class TaggedObjectEnc' arity f isRecord where
940− taggedObject' :: Options -> ToArgs enc arity a
853+ taggedObjectEnc' :: Options -> ToArgs Encoding arity a
941− -> String -> f a -> Tagged isRecord pairs
854+ -> String -> f a -> Tagged isRecord Encoding
943−instance ( GToJSON enc arity f
944− , KeyValuePair enc pairs
945− ) => TaggedObject' enc pairs arity f False
947− taggedObject' opts targs contentsFieldName =
948− Tagged . (contentsFieldName `pair`) . gToJSON opts targs
950−instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
856+instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
951− taggedObject' _ _ _ _ = Tagged mempty
857+ taggedObjectEnc' _ _ _ _ = Tagged E.empty
953−instance ( RecordToPairs enc pairs arity f
859+instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
954− ) => TaggedObject' enc pairs arity f True
860+ taggedObjectEnc' opts targs _ = Tagged . (E.comma ><) . fst
955− where
861+ . recordToEncoding opts targs
956− taggedObject' opts targs _ = Tagged . recordToPairs opts targs
862+
863+instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
864+ taggedObjectEnc' opts targs contentsFieldName =
865+ Tagged . (\z -> E.comma >< toEncoding contentsFieldName >< E.colon >< z) .
866+ gToEncoding opts targs
958--------------------------------------------------------------------------------
868--------------------------------------------------------------------------------
@@ -968,38 +878,23 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
968instance (Constructor c) => GetConName (C1 c a) where
878instance (Constructor c) => GetConName (C1 c a) where
969 getConName = conName
879 getConName = conName
971−-- For genericToJSONKey
972−instance GetConName a => GetConName (D1 d a) where
973− getConName (M1 x) = getConName x
975−--------------------------------------------------------------------------------
977−-- Reflection of SumEncoding variants
979−data ObjectWithSingleField
983--------------------------------------------------------------------------------
881--------------------------------------------------------------------------------
985−class SumToJSON' s enc arity f where
883+class TwoElemArrayObj arity f where
986− sumToJSON' :: Options -> ToArgs enc arity a
884+ twoElemArrayObj :: Options -> ToArgs Value arity a
987− -> f a -> Tagged s enc
885+ -> f a -> V.Vector Value
989−instance ( SumToJSON' s enc arity a
887+instance ( TwoElemArrayObj arity a
990− , SumToJSON' s enc arity b
888+ , TwoElemArrayObj arity b
991− ) => SumToJSON' s enc arity (a :+: b)
889+ ) => TwoElemArrayObj arity (a :+: b) where
993− sumToJSON' opts targs (L1 x) = sumToJSON' opts targs x
890+ twoElemArrayObj opts targs (L1 x) = twoElemArrayObj opts targs x
994− sumToJSON' opts targs (R1 x) = sumToJSON' opts targs x
891+ twoElemArrayObj opts targs (R1 x) = twoElemArrayObj opts targs x
996−--------------------------------------------------------------------------------
998−instance ( GToJSON Value arity a
893+instance ( GToJSON arity a
999− , ConsToJSON Value arity a
894+ , ConsToJSON arity a
1000 , Constructor c
895 , Constructor c
1001− ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
896+ ) => TwoElemArrayObj arity (C1 c a) where
1002− sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
897+ twoElemArrayObj opts targs x = V.create $ do
1003 mv <- VM.unsafeNew 2
898 mv <- VM.unsafeNew 2
1004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
899 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
1005 $ conName (undefined :: t c a p)
900 $ conName (undefined :: t c a p)
@@ -1008,121 +903,148 @@ instance ( GToJSON Value arity a
1009--------------------------------------------------------------------------------
904--------------------------------------------------------------------------------
1011−instance ( GToJSON Encoding arity a
906+class TwoElemArrayEnc arity f where
1012− , ConsToJSON Encoding arity a
907+ twoElemArrayEnc :: Options -> ToArgs Encoding arity a
910+instance ( TwoElemArrayEnc arity a
911+ , TwoElemArrayEnc arity b
912+ ) => TwoElemArrayEnc arity (a :+: b) where
913+ twoElemArrayEnc opts targs (L1 x) = twoElemArrayEnc opts targs x
914+ twoElemArrayEnc opts targs (R1 x) = twoElemArrayEnc opts targs x
916+instance ( GToEncoding arity a
917+ , ConsToEncoding arity a
1013 , Constructor c
918 , Constructor c
1014− ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
919+ ) => TwoElemArrayEnc arity (C1 c a) where
1015− where
920+ twoElemArrayEnc opts targs x = E.tuple $
1016− sumToJSON' opts targs x = Tagged $ E.list id
1017− [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
921+ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
1018− , gToJSON opts targs x
922+ gToEncoding opts targs x
1021--------------------------------------------------------------------------------
924--------------------------------------------------------------------------------
1023−class ConsToJSON enc arity f where
926+class ConsToJSON arity f where
1024− consToJSON :: Options -> ToArgs enc arity a
927+ consToJSON :: Options -> ToArgs Value arity a
1025− -> f a -> enc
928+ -> f a -> Value
1027−class ConsToJSON' enc arity f isRecord where
930+class ConsToJSON' arity f isRecord where
1028− consToJSON' :: Options -> ToArgs enc arity a
931+ consToJSON' :: Options -> ToArgs Value arity a
932+ -> Bool -- ^ Are we a record with one field?
1029− -> f a -> Tagged isRecord enc
933+ -> f a -> Tagged isRecord Value
1031−instance ( IsRecord f isRecord
935+instance ( IsRecord f isRecord
1032− , ConsToJSON' enc arity f isRecord
936+ , ConsToJSON' arity f isRecord
1033− ) => ConsToJSON enc arity f
937+ ) => ConsToJSON arity f where
1035 consToJSON opts targs =
938 consToJSON opts targs =
1036− (unTagged :: Tagged isRecord enc -> enc)
939+ (unTagged :: Tagged isRecord Value -> Value)
1037− . consToJSON' opts targs
940+ . consToJSON' opts targs (isUnary (undefined :: f a))
1038− {-# INLINE consToJSON #-}
941+
1040−instance OVERLAPPING_
1041− ( RecordToPairs enc pairs arity (S1 s f)
1042− , FromPairs enc pairs
1043− , GToJSON enc arity f
1044− ) => ConsToJSON' enc arity (S1 s f) True
942+instance (RecordToPairs arity f) => ConsToJSON' arity f True where
1046− consToJSON' opts targs
943+ consToJSON' opts targs isUn f = let
944+ vals = toList $ recordToPairs opts targs f
1047− | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
945+ in case (unwrapUnaryRecords opts,isUn,vals) of
1048− | otherwise = Tagged . fromPairs . recordToPairs opts targs
946+ (True,True,[(_,val)]) -> Tagged val
1049− {-# INLINE consToJSON' #-}
947+ _ -> Tagged $ object vals
1051−instance ( RecordToPairs enc pairs arity f
1052− , FromPairs enc pairs
1053− ) => ConsToJSON' enc arity f True
1055− consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
1056− {-# INLINE consToJSON' #-}
1058−instance GToJSON enc arity f => ConsToJSON' enc arity f False where
949+instance GToJSON arity f => ConsToJSON' arity f False where
1059− consToJSON' opts targs = Tagged . gToJSON opts targs
950+ consToJSON' opts targs _ = Tagged . gToJSON opts targs
1060− {-# INLINE consToJSON' #-}
1062--------------------------------------------------------------------------------
952--------------------------------------------------------------------------------
1064−class RecordToPairs enc pairs arity f where
954+class ConsToEncoding arity f where
1065− -- 1st element: whole thing
955+ consToEncoding :: Options -> ToArgs Encoding arity a
1066− -- 2nd element: in case the record has only 1 field, just the value
956+ -> f a -> Encoding
1067− -- of the field (without the key); 'Nothing' otherwise
957+
958+class ConsToEncoding' arity f isRecord where
959+ consToEncoding' :: Options -> ToArgs Encoding arity a
960+ -> Bool -- ^ Are we a record with one field?
961+ -> f a -> Tagged isRecord Encoding
963+instance ( IsRecord f isRecord
964+ , ConsToEncoding' arity f isRecord
965+ ) => ConsToEncoding arity f where
966+ consToEncoding opts targs =
967+ (unTagged :: Tagged isRecord Encoding -> Encoding)
968+ . consToEncoding' opts targs (isUnary (undefined :: f a))
970+instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
971+ consToEncoding' opts targs isUn x =
972+ let (enc, mbVal) = recordToEncoding opts targs x
973+ in case (unwrapUnaryRecords opts, isUn, mbVal) of
974+ (True, True, Just val) -> Tagged val
975+ _ -> Tagged $ E.wrapObject enc
977+instance GToEncoding arity f => ConsToEncoding' arity f False where
978+ consToEncoding' opts targs _ = Tagged . gToEncoding opts targs
980+--------------------------------------------------------------------------------
982+class RecordToPairs arity f where
1068− recordToPairs :: Options -> ToArgs enc arity a
983+ recordToPairs :: Options -> ToArgs Value arity a
1069− -> f a -> pairs
984+ -> f a -> DList Pair
1071−instance ( Monoid pairs
1072− , RecordToPairs enc pairs arity a
986+instance ( RecordToPairs arity a
1073− , RecordToPairs enc pairs arity b
987+ , RecordToPairs arity b
1074− ) => RecordToPairs enc pairs arity (a :*: b)
988+ ) => RecordToPairs arity (a :*: b) where
1076− recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
989+ recordToPairs opts targs (a :*: b) = recordToPairs opts targs a <>
1077− pairsOf a `mappend` pairsOf b
1079− pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
1080− pairsOf = recordToPairs opts targs
990+ recordToPairs opts targs b
1081− {-# INLINE recordToPairs #-}
991+
1083−instance ( Selector s
1084− , GToJSON enc arity a
1085− , KeyValuePair enc pairs
1086− ) => RecordToPairs enc pairs arity (S1 s a)
992+instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
1088 recordToPairs = fieldToPair
993 recordToPairs = fieldToPair
1089− {-# INLINE recordToPairs #-}
994+
1091−instance INCOHERENT_
1092− ( Selector s
995+instance OVERLAPPING_ (Selector s, ToJSON a) =>
1093− , GToJSON enc arity (K1 i (Maybe a))
1094− , KeyValuePair enc pairs
1096− ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
996+ RecordToPairs arity (S1 s (K1 i (Maybe a))) where
1098 recordToPairs opts _ (M1 k1) | omitNothingFields opts
997 recordToPairs opts _ (M1 k1) | omitNothingFields opts
1099− , K1 Nothing <- k1 = mempty
998+ , K1 Nothing <- k1 = DList.empty
1100 recordToPairs opts targs m1 = fieldToPair opts targs m1
999 recordToPairs opts targs m1 = fieldToPair opts targs m1
1101− {-# INLINE recordToPairs #-}
1000+
1103−instance INCOHERENT_
1105− , GToJSON enc arity (K1 i (Maybe a))
1106− , KeyValuePair enc pairs
1108− ) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup.Option a)))
1110− recordToPairs opts targs = recordToPairs opts targs . unwrap
1112− unwrap :: S1 s (K1 i (Semigroup.Option a)) p -> S1 s (K1 i (Maybe a)) p
1113− unwrap (M1 (K1 (Semigroup.Option a))) = M1 (K1 a)
1114− {-# INLINE recordToPairs #-}
1116−fieldToPair :: (Selector s
1001+fieldToPair :: (Selector s, GToJSON arity a)
1117− , GToJSON enc arity a
1118− , KeyValuePair enc pairs)
1119− => Options -> ToArgs enc arity p
1002+ => Options -> ToArgs Value arity p
1120− -> S1 s a p -> pairs
1003+ -> S1 s a p -> DList Pair
1121−fieldToPair opts targs m1 =
1122− let key = fieldLabelModifier opts (selName m1)
1004+fieldToPair opts targs m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
1123− value = gToJSON opts targs (unM1 m1)
1005+ , gToJSON opts targs (unM1 m1)
1124− in key `pair` value
1006+ )
1125−{-# INLINE fieldToPair #-}
1007+
1008+--------------------------------------------------------------------------------
1010+class RecordToEncoding arity f where
1011+ -- 1st element: whole thing
1012+ -- 2nd element: in case the record has only 1 field, just the value
1013+ -- of the field (without the key); 'Nothing' otherwise
1014+ recordToEncoding :: Options -> ToArgs Encoding arity a
1015+ -> f a -> (Encoding, Maybe Encoding)
1017+instance ( RecordToEncoding arity a
1018+ , RecordToEncoding arity b
1019+ ) => RecordToEncoding arity (a :*: b) where
1020+ recordToEncoding opts targs (a :*: b) | omitNothingFields opts =
1021+ (E.econcat $ intersperse E.comma $
1022+ filter (not . E.nullEncoding)
1023+ [ fst (recordToEncoding opts targs a)
1024+ , fst (recordToEncoding opts targs b) ]
1026+ recordToEncoding opts targs (a :*: b) =
1027+ (fst (recordToEncoding opts targs a) >< E.comma ><
1028+ fst (recordToEncoding opts targs b),
1032+instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1033+ recordToEncoding = fieldToEncoding
1035+instance OVERLAPPING_ (Selector s, ToJSON a) =>
1036+ RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1037+ recordToEncoding opts _ (M1 k1) | omitNothingFields opts
1038+ , K1 Nothing <- k1 = (E.empty, Nothing)
1039+ recordToEncoding opts targs m1 = fieldToEncoding opts targs m1
1041+fieldToEncoding :: (Selector s, GToEncoding arity a)
1042+ => Options -> ToArgs Encoding arity p
1043+ -> S1 s a p -> (Encoding, Maybe Encoding)
1044+fieldToEncoding opts targs m1 =
1045+ let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1046+ valueBuilder = gToEncoding opts targs (unM1 m1)
1047+ in (keyBuilder >< E.colon >< valueBuilder, Just valueBuilder)
1127--------------------------------------------------------------------------------
1049--------------------------------------------------------------------------------
@@ -1145,12 +1067,10 @@ instance ( WriteProduct arity a
1145 lenL = len `unsafeShiftR` 1
1067 lenL = len `unsafeShiftR` 1
1146 lenR = len - lenL
1068 lenR = len - lenL
1147 ixR = ix + lenL
1069 ixR = ix + lenL
1148− {-# INLINE writeProduct #-}
1150−instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1071+instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
1151 writeProduct opts targs mv ix _ =
1072 writeProduct opts targs mv ix _ =
1152 VM.unsafeWrite mv ix . gToJSON opts targs
1073 VM.unsafeWrite mv ix . gToJSON opts targs
1153− {-# INLINE writeProduct #-}
1155--------------------------------------------------------------------------------
1075--------------------------------------------------------------------------------
@@ -1165,45 +1085,61 @@ instance ( EncodeProduct arity a
1165 E.econcat $ intersperse E.comma $
1085 E.econcat $ intersperse E.comma $
1166 filter (not . E.nullEncoding)
1086 filter (not . E.nullEncoding)
1167 [encodeProduct opts targs a, encodeProduct opts targs b]
1087 [encodeProduct opts targs a, encodeProduct opts targs b]
1168− encodeProduct opts targs (a :*: b) =
1088+ encodeProduct opts targs (a :*: b) = encodeProduct opts targs a >*<
1169− encodeProduct opts targs a >*<
1170− encodeProduct opts targs b
1089+ encodeProduct opts targs b
1171− {-# INLINE encodeProduct #-}
1173−instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1091+instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1174− encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1092+ encodeProduct opts targs a = E.retagEncoding $ gToEncoding opts targs a
1175− {-# INLINE encodeProduct #-}
1177--------------------------------------------------------------------------------
1094--------------------------------------------------------------------------------
1096+class ObjectWithSingleFieldObj arity f where
1097+ objectWithSingleFieldObj :: Options -> ToArgs Value arity a
1100+instance ( ObjectWithSingleFieldObj arity a
1101+ , ObjectWithSingleFieldObj arity b
1102+ ) => ObjectWithSingleFieldObj arity (a :+: b) where
1103+ objectWithSingleFieldObj opts targs (L1 x) =
1104+ objectWithSingleFieldObj opts targs x
1105+ objectWithSingleFieldObj opts targs (R1 x) =
1106+ objectWithSingleFieldObj opts targs x
1179−instance ( GToJSON enc arity a
1108+instance ( GToJSON arity a
1180− , ConsToJSON enc arity a
1109+ , ConsToJSON arity a
1181− , FromPairs enc pairs
1182− , KeyValuePair enc pairs
1183 , Constructor c
1110 , Constructor c
1184− ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1111+ ) => ObjectWithSingleFieldObj arity (C1 c a) where
1185− where
1112+ objectWithSingleFieldObj opts targs = H.singleton typ . gToJSON opts targs
1186− sumToJSON' opts targs =
1187− Tagged . fromPairs . (typ `pair`) . gToJSON opts targs
1189− typ = constructorTagModifier opts $
1114+ typ = pack $ constructorTagModifier opts $
1190 conName (undefined :: t c a p)
1115 conName (undefined :: t c a p)
1192--------------------------------------------------------------------------------
1117--------------------------------------------------------------------------------
1194−instance OVERLAPPABLE_
1119+class ObjectWithSingleFieldEnc arity f where
1195− ( ConsToJSON enc arity a
1120+ objectWithSingleFieldEnc :: Options -> ToArgs Encoding arity a
1196− ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1121+ -> f a -> Encoding
1198− sumToJSON' opts targs = Tagged . gToJSON opts targs
1200−instance OVERLAPPING_
1123+instance ( ObjectWithSingleFieldEnc arity a
1124+ , ObjectWithSingleFieldEnc arity b
1125+ ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1126+ objectWithSingleFieldEnc opts targs (L1 x) =
1127+ objectWithSingleFieldEnc opts targs x
1128+ objectWithSingleFieldEnc opts targs (R1 x) =
1129+ objectWithSingleFieldEnc opts targs x
1131+instance ( GToEncoding arity a
1132+ , ConsToEncoding arity a
1201− ( Constructor c
1133+ , Constructor c
1202− , FromString enc
1134+ ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1203− ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1135+ objectWithSingleFieldEnc opts targs v =
1204− where
1136+ E.openCurly ><
1205− sumToJSON' opts _ _ = Tagged . fromString $
1137+ toEncoding
1206− constructorTagModifier opts $ conName (undefined :: t c U1 p)
1138+ (constructorTagModifier opts
1139+ (conName (undefined :: t c a p))) ><
1141+ gToEncoding opts targs v ><
1208-------------------------------------------------------------------------------
1144-------------------------------------------------------------------------------
1209-- Instances
1145-- Instances
@@ -1276,13 +1212,6 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
1276 toEncoding = toEncoding2
1212 toEncoding = toEncoding2
1277 {-# INLINE toEncoding #-}
1213 {-# INLINE toEncoding #-}
1279−instance ToJSON Void where
1281− {-# INLINE toJSON #-}
1283− toEncoding = absurd
1284− {-# INLINE toEncoding #-}
1287instance ToJSON Bool where
1216instance ToJSON Bool where
1288 toJSON = Bool
1217 toJSON = Bool
@@ -1468,6 +1397,7 @@ instance ToJSONKey Int64 where
1468 toJSONKey = toJSONKeyTextEnc E.int64Text
1397 toJSONKey = toJSONKeyTextEnc E.int64Text
1469 {-# INLINE toJSONKey #-}
1398 {-# INLINE toJSONKey #-}
1471instance ToJSON Word where
1401instance ToJSON Word where
1472 toJSON = Number . fromIntegral
1402 toJSON = Number . fromIntegral
1473 {-# INLINE toJSON #-}
1403 {-# INLINE toJSON #-}
@@ -1527,12 +1457,6 @@ instance ToJSONKey Word64 where
1527 toJSONKey = toJSONKeyTextEnc E.word64Text
1457 toJSONKey = toJSONKeyTextEnc E.word64Text
1528 {-# INLINE toJSONKey #-}
1458 {-# INLINE toJSONKey #-}
1530−instance ToJSON CTime where
1531− toJSON (CTime i) = toJSON i
1532− {-# INLINE toJSON #-}
1534− toEncoding (CTime i) = toEncoding i
1535− {-# INLINE toEncoding #-}
1537instance ToJSON Text where
1461instance ToJSON Text where
1538 toJSON = String
1462 toJSON = String
@@ -1647,7 +1571,7 @@ instance (ToJSON a) => ToJSON (Identity a) where
1647 toEncodingList = liftToEncodingList toEncoding toEncodingList
1571 toEncodingList = liftToEncodingList toEncoding toEncodingList
1648 {-# INLINE toEncodingList #-}
1572 {-# INLINE toEncodingList #-}
1650−instance (ToJSONKey a) => ToJSONKey (Identity a) where
1574+instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1651 toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1575 toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1652 toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
1576 toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
@@ -1796,11 +1720,13 @@ instance ToJSONKey k => ToJSON1 (M.Map k) where
1796 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1720 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1797 {-# INLINE liftToJSON #-}
1721 {-# INLINE liftToJSON #-}
1723+ -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
1799 liftToEncoding g _ = case toJSONKey of
1724 liftToEncoding g _ = case toJSONKey of
1800 ToJSONKeyText _ f -> dict f g M.foldrWithKey
1725 ToJSONKeyText _ f -> dict f g M.foldrWithKey
1801 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1726 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1728+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1803− pairEncoding f (a, b) = E.list id [f a, g b]
1729+ pairEncoding f (a, b) = tuple $ f a >*< g b
1804 {-# INLINE liftToEncoding #-}
1730 {-# INLINE liftToEncoding #-}
@@ -1838,17 +1764,6 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
1838 toEncoding = toEncoding1
1764 toEncoding = toEncoding1
1839 {-# INLINE toEncoding #-}
1765 {-# INLINE toEncoding #-}
1841−-------------------------------------------------------------------------------
1843−-------------------------------------------------------------------------------
1845−instance ToJSON UUID.UUID where
1846− toJSON = toJSON . UUID.toText
1847− toEncoding = E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
1849−instance ToJSONKey UUID.UUID where
1850− toJSONKey = ToJSONKeyText UUID.toText $
1851− E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
1853-------------------------------------------------------------------------------
1768-------------------------------------------------------------------------------
1854-- vector
1769-- vector
@@ -1862,12 +1777,10 @@ instance ToJSON1 Vector where
1862 {-# INLINE liftToEncoding #-}
1777 {-# INLINE liftToEncoding #-}
1864instance (ToJSON a) => ToJSON (Vector a) where
1779instance (ToJSON a) => ToJSON (Vector a) where
1865− {-# SPECIALIZE instance ToJSON Array #-}
1780+ toJSON = Array . V.map toJSON
1868 {-# INLINE toJSON #-}
1781 {-# INLINE toJSON #-}
1870− toEncoding = toEncoding1
1783+ toEncoding = encodeVector
1871 {-# INLINE toEncoding #-}
1784 {-# INLINE toEncoding #-}
1873encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1786encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
@@ -1931,12 +1844,11 @@ instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1931 ToJSONKeyText _ f -> dict f g H.foldrWithKey
1844 ToJSONKeyText _ f -> dict f g H.foldrWithKey
1932 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1845 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1847+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1934− pairEncoding f (a, b) = E.list id [f a, g b]
1848+ pairEncoding f (a, b) = tuple $ f a >*< g b
1935 {-# INLINE liftToEncoding #-}
1849 {-# INLINE liftToEncoding #-}
1937instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1851instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1938− {-# SPECIALIZE instance ToJSON Object #-}
1940 toJSON = toJSON1
1852 toJSON = toJSON1
1941 {-# INLINE toJSON #-}
1853 {-# INLINE toJSON #-}
@@ -1966,32 +1878,6 @@ dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1966formatMillis :: (FormatTime t) => t -> String
1878formatMillis :: (FormatTime t) => t -> String
1967formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1879formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1969−-------------------------------------------------------------------------------
1971−-------------------------------------------------------------------------------
1973−instance ToJSON a => ToJSON (PM.Array a) where
1974− -- note: we could do better than this if vector exposed the data
1975− -- constructor in Data.Vector.
1976− toJSON = toJSON . Exts.toList
1977− toEncoding = toEncoding . Exts.toList
1979−instance ToJSON a => ToJSON (PM.SmallArray a) where
1980− toJSON = toJSON . Exts.toList
1981− toEncoding = toEncoding . Exts.toList
1983−#if (MIN_VERSION_primitive(0,6,4))
1984−instance (PM.Prim a,ToJSON a) => ToJSON (PM.PrimArray a) where
1985− toJSON = toJSON . Exts.toList
1986− toEncoding = toEncoding . Exts.toList
1988−#if !MIN_VERSION_primitive(0,7,0)
1989−instance (PM.PrimUnlifted a,ToJSON a) => ToJSON (PM.UnliftedArray a) where
1990− toJSON = toJSON . Exts.toList
1991− toEncoding = toEncoding . Exts.toList
1995-------------------------------------------------------------------------------
1881-------------------------------------------------------------------------------
1997-------------------------------------------------------------------------------
1883-------------------------------------------------------------------------------
@@ -2044,7 +1930,6 @@ stringEncoding = String
2044 . E.encodingToLazyByteString
1930 . E.encodingToLazyByteString
2045{-# INLINE stringEncoding #-}
1931{-# INLINE stringEncoding #-}
2048instance ToJSON NominalDiffTime where
1933instance ToJSON NominalDiffTime where
2049 toJSON = Number . realToFrac
1934 toJSON = Number . realToFrac
2050 {-# INLINE toJSON #-}
1935 {-# INLINE toJSON #-}
@@ -2052,159 +1937,18 @@ instance ToJSON NominalDiffTime where
2052 toEncoding = E.scientific . realToFrac
1937 toEncoding = E.scientific . realToFrac
2053 {-# INLINE toEncoding #-}
1938 {-# INLINE toEncoding #-}
2056−instance ToJSON DiffTime where
2057− toJSON = Number . realToFrac
2058− {-# INLINE toJSON #-}
2060− toEncoding = E.scientific . realToFrac
2061− {-# INLINE toEncoding #-}
2063−-- | Encoded as number
2064−instance ToJSON SystemTime where
2065− toJSON (MkSystemTime secs nsecs) =
2066− toJSON (fromIntegral secs + fromIntegral nsecs / 1000000000 :: Nano)
2067− toEncoding (MkSystemTime secs nsecs) =
2068− toEncoding (fromIntegral secs + fromIntegral nsecs / 1000000000 :: Nano)
2070−instance ToJSON CalendarDiffTime where
2071− toJSON (CalendarDiffTime m nt) = object
2075− toEncoding (CalendarDiffTime m nt) = E.pairs
2076− ("months" .= m <> "time" .= nt)
2078−instance ToJSON CalendarDiffDays where
2079− toJSON (CalendarDiffDays m d) = object
2083− toEncoding (CalendarDiffDays m d) = E.pairs
2084− ("months" .= m <> "days" .= d)
2086−instance ToJSON DayOfWeek where
2087− toJSON Monday = "monday"
2088− toJSON Tuesday = "tuesday"
2089− toJSON Wednesday = "wednesday"
2090− toJSON Thursday = "thursday"
2091− toJSON Friday = "friday"
2092− toJSON Saturday = "saturday"
2093− toJSON Sunday = "sunday"
2095−toEncodingDayOfWeek :: DayOfWeek -> E.Encoding' Text
2096−toEncodingDayOfWeek Monday = E.unsafeToEncoding "\"monday\""
2097−toEncodingDayOfWeek Tuesday = E.unsafeToEncoding "\"tuesday\""
2098−toEncodingDayOfWeek Wednesday = E.unsafeToEncoding "\"wednesday\""
2099−toEncodingDayOfWeek Thursday = E.unsafeToEncoding "\"thursday\""
2100−toEncodingDayOfWeek Friday = E.unsafeToEncoding "\"friday\""
2101−toEncodingDayOfWeek Saturday = E.unsafeToEncoding "\"saturday\""
2102−toEncodingDayOfWeek Sunday = E.unsafeToEncoding "\"sunday\""
2104−instance ToJSONKey DayOfWeek where
2105− toJSONKey = toJSONKeyTextEnc toEncodingDayOfWeek
2107-------------------------------------------------------------------------------
1940-------------------------------------------------------------------------------
2108-- base Monoid/Semigroup
1941-- base Monoid/Semigroup
2109-------------------------------------------------------------------------------
1942-------------------------------------------------------------------------------
2111−instance ToJSON1 Monoid.Dual where
1944+instance ToJSON1 Dual where
2112− liftToJSON t _ = t . Monoid.getDual
1945+ liftToJSON t _ = t . getDual
2113− {-# INLINE liftToJSON #-}
2115− liftToEncoding t _ = t . Monoid.getDual
2116− {-# INLINE liftToEncoding #-}
2118−instance ToJSON a => ToJSON (Monoid.Dual a) where
2120− {-# INLINE toJSON #-}
2122− toEncoding = toEncoding1
2123− {-# INLINE toEncoding #-}
2126−instance ToJSON1 Monoid.First where
2127− liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
2128− {-# INLINE liftToJSON #-}
2130− liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
2131− {-# INLINE liftToEncoding #-}
2133−instance ToJSON a => ToJSON (Monoid.First a) where
2135− {-# INLINE toJSON #-}
2137− toEncoding = toEncoding1
2138− {-# INLINE toEncoding #-}
2141−instance ToJSON1 Monoid.Last where
2142− liftToJSON t to' = liftToJSON t to' . Monoid.getLast
2143− {-# INLINE liftToJSON #-}
2145− liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2146− {-# INLINE liftToEncoding #-}
2148−instance ToJSON a => ToJSON (Monoid.Last a) where
2150− {-# INLINE toJSON #-}
2152− toEncoding = toEncoding1
2153− {-# INLINE toEncoding #-}
2156−instance ToJSON1 Semigroup.Min where
2157− liftToJSON t _ (Semigroup.Min x) = t x
2158− {-# INLINE liftToJSON #-}
2160− liftToEncoding t _ (Semigroup.Min x) = t x
2161− {-# INLINE liftToEncoding #-}
2163−instance ToJSON a => ToJSON (Semigroup.Min a) where
2165− {-# INLINE toJSON #-}
2167− toEncoding = toEncoding1
2168− {-# INLINE toEncoding #-}
2171−instance ToJSON1 Semigroup.Max where
2172− liftToJSON t _ (Semigroup.Max x) = t x
2173− {-# INLINE liftToJSON #-}
2175− liftToEncoding t _ (Semigroup.Max x) = t x
2176− {-# INLINE liftToEncoding #-}
2178−instance ToJSON a => ToJSON (Semigroup.Max a) where
2180− {-# INLINE toJSON #-}
2182− toEncoding = toEncoding1
2183− {-# INLINE toEncoding #-}
2185−instance ToJSON1 Semigroup.First where
2186− liftToJSON t _ (Semigroup.First x) = t x
2187− {-# INLINE liftToJSON #-}
2189− liftToEncoding t _ (Semigroup.First x) = t x
2190− {-# INLINE liftToEncoding #-}
2192−instance ToJSON a => ToJSON (Semigroup.First a) where
2194− {-# INLINE toJSON #-}
2196− toEncoding = toEncoding1
2197− {-# INLINE toEncoding #-}
2200−instance ToJSON1 Semigroup.Last where
2201− liftToJSON t _ (Semigroup.Last x) = t x
2202 {-# INLINE liftToJSON #-}
1946 {-# INLINE liftToJSON #-}
2204− liftToEncoding t _ (Semigroup.Last x) = t x
1948+ liftToEncoding t _ = t . getDual
2205 {-# INLINE liftToEncoding #-}
1949 {-# INLINE liftToEncoding #-}
2207−instance ToJSON a => ToJSON (Semigroup.Last a) where
1951+instance ToJSON a => ToJSON (Dual a) where
2208 toJSON = toJSON1
1952 toJSON = toJSON1
2209 {-# INLINE toJSON #-}
1953 {-# INLINE toJSON #-}
@@ -2212,14 +1956,14 @@ instance ToJSON a => ToJSON (Semigroup.Last a) where
2212 {-# INLINE toEncoding #-}
1956 {-# INLINE toEncoding #-}
2215−instance ToJSON1 Semigroup.WrappedMonoid where
1959+instance ToJSON1 First where
2216− liftToJSON t _ (Semigroup.WrapMonoid x) = t x
1960+ liftToJSON t to' = liftToJSON t to' . getFirst
2217 {-# INLINE liftToJSON #-}
1961 {-# INLINE liftToJSON #-}
2219− liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
1963+ liftToEncoding t to' = liftToEncoding t to' . getFirst
2220 {-# INLINE liftToEncoding #-}
1964 {-# INLINE liftToEncoding #-}
2222−instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
1966+instance ToJSON a => ToJSON (First a) where
2223 toJSON = toJSON1
1967 toJSON = toJSON1
2224 {-# INLINE toJSON #-}
1968 {-# INLINE toJSON #-}
@@ -2227,14 +1971,14 @@ instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
2227 {-# INLINE toEncoding #-}
1971 {-# INLINE toEncoding #-}
2230−instance ToJSON1 Semigroup.Option where
1974+instance ToJSON1 Last where
2231− liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
1975+ liftToJSON t to' = liftToJSON t to' . getLast
2232 {-# INLINE liftToJSON #-}
1976 {-# INLINE liftToJSON #-}
2234− liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
1978+ liftToEncoding t to' = liftToEncoding t to' . getLast
2235 {-# INLINE liftToEncoding #-}
1979 {-# INLINE liftToEncoding #-}
2237−instance ToJSON a => ToJSON (Semigroup.Option a) where
1981+instance ToJSON a => ToJSON (Last a) where
2238 toJSON = toJSON1
1982 toJSON = toJSON1
2239 {-# INLINE toJSON #-}
1983 {-# INLINE toJSON #-}
@@ -2245,13 +1989,6 @@ instance ToJSON a => ToJSON (Semigroup.Option a) where
2245-- tagged
1989-- tagged
2246-------------------------------------------------------------------------------
1990-------------------------------------------------------------------------------
2248−instance ToJSON1 Proxy where
2249− liftToJSON _ _ _ = Null
2250− {-# INLINE liftToJSON #-}
2252− liftToEncoding _ _ _ = E.null_
2253− {-# INLINE liftToEncoding #-}
2255instance ToJSON (Proxy a) where
1992instance ToJSON (Proxy a) where
2256 toJSON _ = Null
1993 toJSON _ = Null
2257 {-# INLINE toJSON #-}
1994 {-# INLINE toJSON #-}
@@ -2260,13 +1997,6 @@ instance ToJSON (Proxy a) where
2260 {-# INLINE toEncoding #-}
1997 {-# INLINE toEncoding #-}
2263−instance ToJSON2 Tagged where
2264− liftToJSON2 _ _ t _ (Tagged x) = t x
2265− {-# INLINE liftToJSON2 #-}
2267− liftToEncoding2 _ _ t _ (Tagged x) = t x
2268− {-# INLINE liftToEncoding2 #-}
2270instance ToJSON1 (Tagged a) where
2000instance ToJSON1 (Tagged a) where
2271 liftToJSON t _ (Tagged x) = t x
2001 liftToJSON t _ (Tagged x) = t x
2272 {-# INLINE liftToJSON #-}
2002 {-# INLINE liftToJSON #-}
@@ -2281,6 +2011,7 @@ instance ToJSON b => ToJSON (Tagged a b) where
2281 toEncoding = toEncoding1
2011 toEncoding = toEncoding1
2282 {-# INLINE toEncoding #-}
2012 {-# INLINE toEncoding #-}
2284instance ToJSONKey b => ToJSONKey (Tagged a b) where
2015instance ToJSONKey b => ToJSONKey (Tagged a b) where
2285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2016 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
2017 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -2301,7 +2032,7 @@ instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
2301 toJSONKey = toJSONKeyList
2032 toJSONKey = toJSONKeyList
2303-------------------------------------------------------------------------------
2034-------------------------------------------------------------------------------
2304−-- Tuple instances
2035+-- Tuple instances, see tuple-instances-to.hs
2305-------------------------------------------------------------------------------
2036-------------------------------------------------------------------------------
2307instance ToJSON2 (,) where
2038instance ToJSON2 (,) where
@@ -2312,7 +2043,9 @@ instance ToJSON2 (,) where
2312 return mv
2043 return mv
2313 {-# INLINE liftToJSON2 #-}
2044 {-# INLINE liftToJSON2 #-}
2315− liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
2046+ liftToEncoding2 toA _ toB _ (a, b) = tuple $
2316 {-# INLINE liftToEncoding2 #-}
2049 {-# INLINE liftToEncoding2 #-}
2318instance (ToJSON a) => ToJSON1 ((,) a) where
2051instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -2336,11 +2069,10 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
2336 return mv
2069 return mv
2337 {-# INLINE liftToJSON2 #-}
2070 {-# INLINE liftToJSON2 #-}
2339− liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
2072+ liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2340− [ toEncoding a
2073+ toEncoding a >*<
2341− , toB b
2074+ toB b >*<
2344 {-# INLINE liftToEncoding2 #-}
2076 {-# INLINE liftToEncoding2 #-}
2346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
2078instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -2365,12 +2097,11 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
2365 return mv
2097 return mv
2366 {-# INLINE liftToJSON2 #-}
2098 {-# INLINE liftToJSON2 #-}
2368− liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
2100+ liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2369− [ toEncoding a
2101+ toEncoding a >*<
2370− , toEncoding b
2102+ toEncoding b >*<
2371− , toC c
2103+ toC c >*<
2374 {-# INLINE liftToEncoding2 #-}
2105 {-# INLINE liftToEncoding2 #-}
2376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
2107instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -2396,13 +2127,12 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
2396 return mv
2127 return mv
2397 {-# INLINE liftToJSON2 #-}
2128 {-# INLINE liftToJSON2 #-}
2399− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
2130+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2400− [ toEncoding a
2131+ toEncoding a >*<
2401− , toEncoding b
2132+ toEncoding b >*<
2402− , toEncoding c
2133+ toEncoding c >*<
2403− , toD d
2134+ toD d >*<
2406 {-# INLINE liftToEncoding2 #-}
2136 {-# INLINE liftToEncoding2 #-}
2408instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
2138instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
@@ -2429,14 +2159,13 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
2429 return mv
2159 return mv
2430 {-# INLINE liftToJSON2 #-}
2160 {-# INLINE liftToJSON2 #-}
2432− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
2162+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2433− [ toEncoding a
2163+ toEncoding a >*<
2434− , toEncoding b
2164+ toEncoding b >*<
2435− , toEncoding c
2165+ toEncoding c >*<
2436− , toEncoding d
2166+ toEncoding d >*<
2437− , toE e
2167+ toE e >*<
2440 {-# INLINE liftToEncoding2 #-}
2169 {-# INLINE liftToEncoding2 #-}
2442instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
2171instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
@@ -2464,15 +2193,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
2464 return mv
2193 return mv
2465 {-# INLINE liftToJSON2 #-}
2194 {-# INLINE liftToJSON2 #-}
2467− liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = E.list id
2196+ liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
2468− [ toEncoding a
2197+ toEncoding a >*<
2469− , toEncoding b
2198+ toEncoding b >*<
2470− , toEncoding c
2199+ toEncoding c >*<
2471− , toEncoding d
2200+ toEncoding d >*<
2472− , toEncoding e
2201+ toEncoding e >*<
2473− , toF f
2202+ toF f >*<
2476 {-# INLINE liftToEncoding2 #-}
2204 {-# INLINE liftToEncoding2 #-}
2478instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
2206instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
@@ -2501,16 +2229,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
2501 return mv
2229 return mv
2502 {-# INLINE liftToJSON2 #-}
2230 {-# INLINE liftToJSON2 #-}
2504− liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = E.list id
2232+ liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
2505− [ toEncoding a
2233+ toEncoding a >*<
2506− , toEncoding b
2234+ toEncoding b >*<
2507− , toEncoding c
2235+ toEncoding c >*<
2508− , toEncoding d
2236+ toEncoding d >*<
2509− , toEncoding e
2237+ toEncoding e >*<
2510− , toEncoding f
2238+ toEncoding f >*<
2511− , toG g
2239+ toG g >*<
2514 {-# INLINE liftToEncoding2 #-}
2241 {-# INLINE liftToEncoding2 #-}
2516instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
2243instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
@@ -2540,17 +2267,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
2540 return mv
2267 return mv
2541 {-# INLINE liftToJSON2 #-}
2268 {-# INLINE liftToJSON2 #-}
2543− liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = E.list id
2270+ liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
2544− [ toEncoding a
2271+ toEncoding a >*<
2545− , toEncoding b
2272+ toEncoding b >*<
2546− , toEncoding c
2273+ toEncoding c >*<
2547− , toEncoding d
2274+ toEncoding d >*<
2548− , toEncoding e
2275+ toEncoding e >*<
2549− , toEncoding f
2276+ toEncoding f >*<
2550− , toEncoding g
2277+ toEncoding g >*<
2551− , toH h
2278+ toH h >*<
2554 {-# INLINE liftToEncoding2 #-}
2280 {-# INLINE liftToEncoding2 #-}
2556instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h) => ToJSON1 ((,,,,,,,,) a b c d e f g h) where
2282instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h) => ToJSON1 ((,,,,,,,,) a b c d e f g h) where
@@ -2581,18 +2307,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2581 return mv
2307 return mv
2582 {-# INLINE liftToJSON2 #-}
2308 {-# INLINE liftToJSON2 #-}
2584− liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = E.list id
2310+ liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
2585− [ toEncoding a
2311+ toEncoding a >*<
2586− , toEncoding b
2312+ toEncoding b >*<
2587− , toEncoding c
2313+ toEncoding c >*<
2588− , toEncoding d
2314+ toEncoding d >*<
2589− , toEncoding e
2315+ toEncoding e >*<
2590− , toEncoding f
2316+ toEncoding f >*<
2591− , toEncoding g
2317+ toEncoding g >*<
2592− , toEncoding h
2318+ toEncoding h >*<
2593− , toI i
2319+ toI i >*<
2596 {-# INLINE liftToEncoding2 #-}
2321 {-# INLINE liftToEncoding2 #-}
2598instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i) => ToJSON1 ((,,,,,,,,,) a b c d e f g h i) where
2323instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i) => ToJSON1 ((,,,,,,,,,) a b c d e f g h i) where
@@ -2624,19 +2349,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2624 return mv
2349 return mv
2625 {-# INLINE liftToJSON2 #-}
2350 {-# INLINE liftToJSON2 #-}
2627− liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = E.list id
2352+ liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
2628− [ toEncoding a
2353+ toEncoding a >*<
2629− , toEncoding b
2354+ toEncoding b >*<
2630− , toEncoding c
2355+ toEncoding c >*<
2631− , toEncoding d
2356+ toEncoding d >*<
2632− , toEncoding e
2357+ toEncoding e >*<
2633− , toEncoding f
2358+ toEncoding f >*<
2634− , toEncoding g
2359+ toEncoding g >*<
2635− , toEncoding h
2360+ toEncoding h >*<
2636− , toEncoding i
2361+ toEncoding i >*<
2637− , toJ j
2362+ toJ j >*<
2640 {-# INLINE liftToEncoding2 #-}
2364 {-# INLINE liftToEncoding2 #-}
2642instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j) => ToJSON1 ((,,,,,,,,,,) a b c d e f g h i j) where
2366instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j) => ToJSON1 ((,,,,,,,,,,) a b c d e f g h i j) where
@@ -2669,20 +2393,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2669 return mv
2393 return mv
2670 {-# INLINE liftToJSON2 #-}
2394 {-# INLINE liftToJSON2 #-}
2672− liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = E.list id
2396+ liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
2673− [ toEncoding a
2397+ toEncoding a >*<
2674− , toEncoding b
2398+ toEncoding b >*<
2675− , toEncoding c
2399+ toEncoding c >*<
2676− , toEncoding d
2400+ toEncoding d >*<
2677− , toEncoding e
2401+ toEncoding e >*<
2678− , toEncoding f
2402+ toEncoding f >*<
2679− , toEncoding g
2403+ toEncoding g >*<
2680− , toEncoding h
2404+ toEncoding h >*<
2681− , toEncoding i
2405+ toEncoding i >*<
2682− , toEncoding j
2406+ toEncoding j >*<
2683− , toK k
2407+ toK k >*<
2686 {-# INLINE liftToEncoding2 #-}
2409 {-# INLINE liftToEncoding2 #-}
2688instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) => ToJSON1 ((,,,,,,,,,,,) a b c d e f g h i j k) where
2411instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) => ToJSON1 ((,,,,,,,,,,,) a b c d e f g h i j k) where
@@ -2716,21 +2439,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2716 return mv
2439 return mv
2717 {-# INLINE liftToJSON2 #-}
2440 {-# INLINE liftToJSON2 #-}
2719− liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = E.list id
2442+ liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
2720− [ toEncoding a
2443+ toEncoding a >*<
2721− , toEncoding b
2444+ toEncoding b >*<
2722− , toEncoding c
2445+ toEncoding c >*<
2723− , toEncoding d
2446+ toEncoding d >*<
2724− , toEncoding e
2447+ toEncoding e >*<
2725− , toEncoding f
2448+ toEncoding f >*<
2726− , toEncoding g
2449+ toEncoding g >*<
2727− , toEncoding h
2450+ toEncoding h >*<
2728− , toEncoding i
2451+ toEncoding i >*<
2729− , toEncoding j
2452+ toEncoding j >*<
2730− , toEncoding k
2453+ toEncoding k >*<
2731− , toL l
2454+ toL l >*<
2734 {-# INLINE liftToEncoding2 #-}
2456 {-# INLINE liftToEncoding2 #-}
2736instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) => ToJSON1 ((,,,,,,,,,,,,) a b c d e f g h i j k l) where
2458instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) => ToJSON1 ((,,,,,,,,,,,,) a b c d e f g h i j k l) where
@@ -2765,22 +2487,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2765 return mv
2487 return mv
2766 {-# INLINE liftToJSON2 #-}
2488 {-# INLINE liftToJSON2 #-}
2768− liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = E.list id
2490+ liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
2769− [ toEncoding a
2491+ toEncoding a >*<
2770− , toEncoding b
2492+ toEncoding b >*<
2771− , toEncoding c
2493+ toEncoding c >*<
2772− , toEncoding d
2494+ toEncoding d >*<
2773− , toEncoding e
2495+ toEncoding e >*<
2774− , toEncoding f
2496+ toEncoding f >*<
2775− , toEncoding g
2497+ toEncoding g >*<
2776− , toEncoding h
2498+ toEncoding h >*<
2777− , toEncoding i
2499+ toEncoding i >*<
2778− , toEncoding j
2500+ toEncoding j >*<
2779− , toEncoding k
2501+ toEncoding k >*<
2780− , toEncoding l
2502+ toEncoding l >*<
2781− , toM m
2503+ toM m >*<
2784 {-# INLINE liftToEncoding2 #-}
2505 {-# INLINE liftToEncoding2 #-}
2786instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m) => ToJSON1 ((,,,,,,,,,,,,,) a b c d e f g h i j k l m) where
2507instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m) => ToJSON1 ((,,,,,,,,,,,,,) a b c d e f g h i j k l m) where
@@ -2816,23 +2537,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2816 return mv
2537 return mv
2817 {-# INLINE liftToJSON2 #-}
2538 {-# INLINE liftToJSON2 #-}
2819− liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = E.list id
2540+ liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
2820− [ toEncoding a
2541+ toEncoding a >*<
2821− , toEncoding b
2542+ toEncoding b >*<
2822− , toEncoding c
2543+ toEncoding c >*<
2823− , toEncoding d
2544+ toEncoding d >*<
2824− , toEncoding e
2545+ toEncoding e >*<
2825− , toEncoding f
2546+ toEncoding f >*<
2826− , toEncoding g
2547+ toEncoding g >*<
2827− , toEncoding h
2548+ toEncoding h >*<
2828− , toEncoding i
2549+ toEncoding i >*<
2829− , toEncoding j
2550+ toEncoding j >*<
2830− , toEncoding k
2551+ toEncoding k >*<
2831− , toEncoding l
2552+ toEncoding l >*<
2832− , toEncoding m
2553+ toEncoding m >*<
2833− , toN n
2554+ toN n >*<
2836 {-# INLINE liftToEncoding2 #-}
2556 {-# INLINE liftToEncoding2 #-}
2838instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m, ToJSON n) => ToJSON1 ((,,,,,,,,,,,,,,) a b c d e f g h i j k l m n) where
2558instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m, ToJSON n) => ToJSON1 ((,,,,,,,,,,,,,,) a b c d e f g h i j k l m n) where
@@ -2846,27 +2566,3 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2846 {-# INLINE toJSON #-}
2566 {-# INLINE toJSON #-}
2847 toEncoding = toEncoding2
2567 toEncoding = toEncoding2
2848 {-# INLINE toEncoding #-}
2568 {-# INLINE toEncoding #-}
2850−--------------------------------------------------------------------------------
2852−-- | Wrap a list of pairs as an object.
2853−class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2854− fromPairs :: pairs -> enc
2856−instance (a ~ Value) => FromPairs (Encoding' a) Series where
2857− fromPairs = E.pairs
2859−instance FromPairs Value (DList Pair) where
2860− fromPairs = object . toList
2862−-- | Like 'KeyValue' but the value is already converted to JSON
2863−-- ('Value' or 'Encoding'), and the result actually represents lists of pairs
2864−-- so it can be readily concatenated.
2865−class Monoid kv => KeyValuePair v kv where
2866− pair :: String -> v -> kv
2868−instance (v ~ Value) => KeyValuePair v (DList Pair) where
2869− pair k v = DList.singleton (pack k .= v)
2871−instance (e ~ Encoding) => KeyValuePair e Series where