@@ -1,27 +1,30 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE EmptyDataDecls #-}
4−{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
6−{-# LANGUAGE FunctionalDependencies #-}
7−{-# LANGUAGE GADTs #-}
8−{-# LANGUAGE NoImplicitPrelude #-}
9−{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PatternGuards #-}
11−{-# LANGUAGE PolyKinds #-}
12−{-# LANGUAGE RecordWildCards #-}
13−{-# LANGUAGE ScopedTypeVariables #-}
14−{-# LANGUAGE TypeOperators #-}
15−{-# LANGUAGE UndecidableInstances #-}
1+{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE DataKinds #-}
4+{-# LANGUAGE DefaultSignatures #-}
5+{-# LANGUAGE FlexibleContexts #-}
6+{-# LANGUAGE FlexibleInstances #-}
7+{-# LANGUAGE GADTs #-}
8+{-# LANGUAGE MultiParamTypeClasses #-}
9+{-# LANGUAGE NamedFieldPuns #-}
10+{-# LANGUAGE OverloadedStrings #-}
11+{-# LANGUAGE PatternGuards #-}
12+{-# LANGUAGE RecordWildCards #-}
13+{-# LANGUAGE ScopedTypeVariables #-}
14+{-# LANGUAGE TypeOperators #-}
15+{-# LANGUAGE TypeSynonymInstances #-}
16+{-# LANGUAGE UndecidableInstances #-}
17+
18+#if __GLASGOW_HASKELL__ >= 706
19+{-# LANGUAGE PolyKinds #-}
20+#endif
1621
1722#include "overlapping-compat.h"
18−#include "incoherent-compat.h"
1923
2024-- TODO: Drop this when we remove support for Data.Attoparsec.Number
2125{-# OPTIONS_GHC -fno-warn-deprecations #-}
2226
23−module Data.Aeson.Types.ToJSON
24− (
27+module Data.Aeson.Types.ToJSON (
2528 -- * Core JSON classes
2629 ToJSON(..)
2730 -- * Liftings to unary and binary type constructors
@@ -33,6 +36,7 @@ module Data.Aeson.Types.ToJSON
3336 , toEncoding2
3437 -- * Generic JSON classes
3538 , GToJSON(..)
39+ , GToEncoding(..)
3640 , ToArgs(..)
3741 , genericToJSON
3842 , genericToEncoding
@@ -43,101 +47,87 @@ module Data.Aeson.Types.ToJSON
4347 , ToJSONKeyFunction(..)
4448 , toJSONKeyText
4549 , contramapToJSONKeyFunction
46−
47− , GToJSONKey()
48− , genericToJSONKey
49−
5050 -- * Object key-value pairs
5151 , KeyValue(..)
52− , KeyValuePair(..)
53− , FromPairs(..)
5452 -- * Functions needed for documentation
5553 -- * Encoding functions
5654 , listEncoding
5755 , listValue
5856 ) where
5957
58+import Prelude ()
6059import Prelude.Compat
6160
62−import Control.Applicative (Const(..))
63−import Control.Monad.ST (ST)
64−import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
65−import Data.Aeson.Encoding.Internal ((>*<))
61+import Data.Aeson.Encoding.Internal (Encoding, Encoding', Series, dict,
62+ emptyArray_, tuple, (>*<), (><))
6663import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
67−import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
64+import Data.Aeson.Types.Generic
6865import Data.Aeson.Types.Internal
69−import Data.Attoparsec.Number (Number(..))
70−import Data.Bits (unsafeShiftR)
71−import Data.DList (DList)
72−import Data.Fixed (Fixed, HasResolution, Nano)
73−import Data.Foldable (toList)
74−import Data.Functor.Compose (Compose(..))
75−import Data.Functor.Contravariant (Contravariant (..))
76−import Data.Functor.Identity (Identity(..))
77−import Data.Functor.Product (Product(..))
78−import Data.Functor.Sum (Sum(..))
79−import Data.Int (Int16, Int32, Int64, Int8)
80−import Data.List (intersperse)
81−import Data.List.NonEmpty (NonEmpty(..))
82−import Data.Semigroup ((<>))
83−import Data.Proxy (Proxy(..))
84−import Data.Ratio (Ratio, denominator, numerator)
85−import Data.Scientific (Scientific)
86−import Data.Tagged (Tagged(..))
87−import Data.Text (Text, pack)
88−import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
89−import Data.Time.Calendar.Compat (CalendarDiffDays (..), DayOfWeek (..))
90−import Data.Time.LocalTime.Compat (CalendarDiffTime (..))
91−import Data.Time.Clock.System.Compat (SystemTime (..))
92−import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
93−import Data.Vector (Vector)
94−import Data.Version (Version, showVersion)
95−import Data.Void (Void, absurd)
96−import Data.Word (Word16, Word32, Word64, Word8)
97−import Foreign.Storable (Storable)
98−import Foreign.C.Types (CTime (..))
66+
67+-- We need internal here for generic deriving
68+import qualified Data.Aeson.Encoding.Internal as E
69+
70+import Control.Applicative (Const (..))
71+import Control.Monad.ST (ST)
72+import Data.Attoparsec.Number (Number (..))
73+import Data.Bits (unsafeShiftR)
74+import Data.DList (DList)
75+import Data.Fixed (Fixed, HasResolution)
76+import Data.Foldable (toList)
77+import Data.Functor.Compose (Compose (..))
78+import Data.Functor.Identity (Identity (..))
79+import Data.Functor.Product (Product (..))
80+import Data.Functor.Sum (Sum (..))
81+import Data.Int (Int16, Int32, Int64, Int8)
82+import Data.List (intersperse)
83+import Data.List.NonEmpty (NonEmpty (..))
84+import Data.Monoid (Dual (..), First (..), Last (..), (<>))
85+import Data.Proxy (Proxy (..))
86+import Data.Ratio (Ratio, denominator, numerator)
87+import Data.Scientific (Scientific)
88+import Data.Tagged (Tagged (..))
89+import Data.Text (Text, pack)
90+import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
91+ UTCTime, ZonedTime)
92+import Data.Time.Format (FormatTime, formatTime)
93+import Data.Time.Locale.Compat (defaultTimeLocale)
94+import Data.Vector (Vector)
95+import Data.Version (Version, showVersion)
96+import Data.Word (Word16, Word32, Word64, Word8)
97+import Foreign.Storable (Storable)
9998import GHC.Generics
100−import Numeric.Natural (Natural)
101−import qualified Data.Aeson.Encoding as E
102−import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
103−import qualified Data.ByteString.Lazy as L
104−import qualified Data.DList as DList
105−import qualified Data.HashMap.Strict as H
106−import qualified Data.HashSet as HashSet
107−import qualified Data.IntMap as IntMap
108−import qualified Data.IntSet as IntSet
109−import qualified Data.List.NonEmpty as NE
110−import qualified Data.Map as M
111−import qualified Data.Monoid as Monoid
112−import qualified Data.Scientific as Scientific
113−import qualified Data.Semigroup as Semigroup
114−import qualified Data.Sequence as Seq
115−import qualified Data.Set as Set
116−import qualified Data.Text as T
117−import qualified Data.Text.Encoding as T
118−import qualified Data.Text.Lazy as LT
119−import qualified Data.Tree as Tree
120−import qualified Data.UUID.Types as UUID
121−import qualified Data.Vector as V
122−import qualified Data.Vector.Generic as VG
123−import qualified Data.Vector.Mutable as VM
99+import Numeric.Natural (Natural)
100+
101+import qualified Data.ByteString as S
102+import qualified Data.ByteString.Lazy as L
103+import qualified Data.DList as DList
104+import qualified Data.HashMap.Strict as H
105+import qualified Data.HashSet as HashSet
106+import qualified Data.IntMap as IntMap
107+import qualified Data.IntSet as IntSet
108+import qualified Data.List.NonEmpty as NE
109+import qualified Data.Map as M
110+import qualified Data.Scientific as Scientific
111+import qualified Data.Sequence as Seq
112+import qualified Data.Set as Set
113+import qualified Data.Text as T
114+import qualified Data.Text.Encoding as T
115+import qualified Data.Text.Lazy as LT
116+import qualified Data.Tree as Tree
117+import qualified Data.Vector as V
118+import qualified Data.Vector.Generic as VG
119+import qualified Data.Vector.Mutable as VM
124120import qualified Data.Vector.Primitive as VP
125−import qualified Data.Vector.Storable as VS
126−import qualified Data.Vector.Unboxed as VU
127−
128−import qualified Data.Aeson.Encoding.Builder as EB
129−import qualified Data.ByteString.Builder as B
121+import qualified Data.Vector.Storable as VS
122+import qualified Data.Vector.Unboxed as VU
130123
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
124+#if !(MIN_VERSION_bytestring(0,10,0))
125+import Foreign.ForeignPtr (withForeignPtr)
126+import Foreign.Marshal.Utils (copyBytes)
127+import Foreign.Ptr (plusPtr)
135128
136−#if MIN_VERSION_primitive(0,6,4)
137−#if !MIN_VERSION_primitive(0,7,0)
138−import qualified Data.Primitive.UnliftedArray as PM
139−#endif
140−import qualified Data.Primitive.PrimArray as PM
129+import qualified Data.ByteString.Internal as S
130+import qualified Data.ByteString.Lazy.Internal as L
141131#endif
142132
143133toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
@@ -156,16 +146,19 @@ realFloatToJSON d
156146
157147-- | Class of generic representation types that can be converted to
158148-- JSON.
159−class GToJSON enc arity f where
149+class GToJSON arity f where
160150 -- | This method (applied to 'defaultOptions') is used as the
161− -- default generic implementation of 'toJSON'
162− -- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
163− -- and 'liftToJSON' (if the @arity@ is 'One').
164− --
165− -- It also provides a generic implementation of 'toEncoding'
166− -- (with @enc ~ 'Encoding'@ and @arity ~ 'Zero'@)
167− -- and 'liftToEncoding' (if the @arity@ is 'One').
168− gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
151+ -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
152+ -- or 'liftToJSON' (if the @arity@ is 'One').
153+ gToJSON :: Options -> ToArgs Value arity a -> f a -> Value
154+
155+-- | Class of generic representation types that can be converted to
156+-- a JSON 'Encoding'.
157+class GToEncoding arity f where
158+ -- | This method (applied to 'defaultOptions') can be used as the
159+ -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
160+ -- or 'liftToEncoding' (if the @arity@ is 'One').
161+ gToEncoding :: Options -> ToArgs Encoding arity a -> f a -> Encoding
169162
170163-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
171164-- function arguments that encode occurrences of the type parameter (for
@@ -177,14 +170,14 @@ data ToArgs res arity a where
177170-- | A configurable generic JSON creator. This function applied to
178171-- 'defaultOptions' is used as the default for 'toJSON' when the type
179172-- is an instance of 'Generic'.
180−genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
173+genericToJSON :: (Generic a, GToJSON Zero (Rep a))
181174 => Options -> a -> Value
182175genericToJSON opts = gToJSON opts NoToArgs . from
183176
184177-- | A configurable generic JSON creator. This function applied to
185178-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
186179-- is an instance of 'Generic1'.
187−genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
180+genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
188181 => Options -> (a -> Value) -> ([a] -> Value)
189182 -> f a -> Value
190183genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
@@ -192,17 +185,17 @@ genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
192185-- | A configurable generic JSON encoder. This function applied to
193186-- 'defaultOptions' is used as the default for 'toEncoding' when the type
194187-- is an instance of 'Generic'.
195−genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
188+genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
196189 => Options -> a -> Encoding
197−genericToEncoding opts = gToJSON opts NoToArgs . from
190+genericToEncoding opts = gToEncoding opts NoToArgs . from
198191
199192-- | A configurable generic JSON encoder. This function applied to
200193-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
201194-- is an instance of 'Generic1'.
202−genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
195+genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
203196 => Options -> (a -> Encoding) -> ([a] -> Encoding)
204197 -> f a -> Encoding
205−genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
198+genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
206199
207200-------------------------------------------------------------------------------
208201-- Class
@@ -210,9 +203,6 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
210203
211204-- | A type that can be converted to JSON.
212205--
213−-- Instances in general /must/ specify 'toJSON' and /should/ (but don't need
214−-- to) specify 'toEncoding'.
215−--
216206-- An example type and instance:
217207--
218208-- @
@@ -221,10 +211,10 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
221211--
222212-- data Coord = Coord { x :: Double, y :: Double }
223213--
224−-- instance 'ToJSON' Coord where
225−-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
214+-- instance ToJSON Coord where
215+-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
226216--
227−-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
217+-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
228218-- @
229219--
230220-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -232,15 +222,17 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
232222--
233223-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
234224-- instance at compile time. The generated instance is optimized for your type
235−-- so it will probably be more efficient than the following option.
225+-- so will probably be more efficient than the following two options:
236226--
237227-- * The compiler can provide a default generic implementation for
238228-- 'toJSON'.
239229--
240230-- 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
242−-- 'defaultOptions', it is sufficient to write (and this is the only
243−-- alternative where the default 'toJSON' implementation is sufficient):
231+-- datatype and declare a 'ToJSON' instance for your datatype without giving
232+-- definitions for 'toJSON' or 'toEncoding'.
233+--
234+-- For example, the previous example can be simplified to a more
235+-- minimal instance:
244236--
245237-- @
246238-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -249,46 +241,35 @@ genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
249241--
250242-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
251243--
252−-- instance 'ToJSON' Coord where
253−-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
244+-- instance ToJSON Coord where
245+-- toEncoding = 'genericToEncoding' 'defaultOptions'
254246-- @
255247--
256−-- If on the other hand you wish to customize the generic decoding, you have
257−-- to implement both methods:
248+-- Why do we provide an implementation for 'toEncoding' here? The
249+-- 'toEncoding' function is a relatively new addition to this class.
250+-- To allow users of older versions of this library to upgrade without
251+-- having to edit all of their instances or encounter surprising
252+-- incompatibilities, the default implementation of 'toEncoding' uses
253+-- 'toJSON'. This produces correct results, but since it performs an
254+-- intermediate conversion to a 'Value', it will be less efficient
255+-- than directly emitting an 'Encoding'. Our one-liner definition of
256+-- 'toEncoding' above bypasses the intermediate 'Value'.
257+--
258+-- If @DefaultSignatures@ doesn't give exactly the results you want,
259+-- you can customize the generic encoding with only a tiny amount of
260+-- effort, using 'genericToJSON' and 'genericToEncoding' with your
261+-- preferred 'Options':
258262--
259263-- @
260−-- customOptions = 'defaultOptions'
261−-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
262−-- }
263−--
264−-- instance 'ToJSON' Coord where
265−-- 'toJSON' = 'genericToJSON' customOptions
266−-- 'toEncoding' = 'genericToEncoding' customOptions
264+-- instance ToJSON Coord where
265+-- toJSON = 'genericToJSON' 'defaultOptions'
266+-- toEncoding = 'genericToEncoding' 'defaultOptions'
267267-- @
268−--
269−-- Previous versions of this library only had the 'toJSON' method. Adding
270−-- 'toEncoding' had two reasons:
271−--
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
275−-- non-optimal.
276−--
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.)
287268class ToJSON a where
288269 -- | Convert a Haskell value to a JSON-friendly intermediate type.
289270 toJSON :: a -> Value
290271
291− default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
272+ default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
292273 toJSON = genericToJSON defaultOptions
293274
294275 -- | Encode a Haskell value as JSON.
@@ -305,8 +286,8 @@ class ToJSON a where
305286 -- extension, and then have GHC generate a method body as follows.
306287 --
307288 -- @
308− -- instance 'ToJSON' Coord where
309− -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
289+ -- instance ToJSON Coord where
290+ -- toEncoding = 'genericToEncoding' 'defaultOptions'
310291 -- @
311292
312293 toEncoding :: a -> Encoding
@@ -338,13 +319,6 @@ instance KeyValue Pair where
338319 name .= value = (name, toJSON value)
339320 {-# INLINE (.=) #-}
340321
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)
346− {-# INLINE (.=) #-}
347−
348322-------------------------------------------------------------------------------
349323-- Classes and types for map keys
350324-------------------------------------------------------------------------------
@@ -402,7 +376,7 @@ instance KeyValue Object where
402376-- newtype wrapper around 'Text'. The recommended approach is to use
403377-- generalized newtype deriving:
404378--
405−-- > newtype RecordId = RecordId { getRecordId :: Text }
379+-- > newtype RecordId = RecordId { getRecordId :: Text}
406380-- > deriving (Eq,Ord,ToJSONKey)
407381--
408382-- Then we may write:
@@ -417,18 +391,8 @@ instance KeyValue Object where
417391--
418392-- It is possible to get the 'ToJSONKey' instance for free as we did
419393-- 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
421−-- 'ToJSONKeyText' can be used instead of 'ToJSONKeyValue' to encode maps
422−-- as objects instead of arrays of pairs. This instance may be
423−-- implemented using generics as follows:
424−--
425−-- @
426−-- instance 'ToJSONKey' Color where
427−-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
428−-- @
429−--
430−-- === __Low-level implementations__
431−--
394+-- and from 'Text' that does not require any escape sequences. So, in
395+-- this example, 'ToJSONKeyText' will be used instead of 'ToJSONKeyValue'.
432396-- The 'Show' instance can be used to help write 'ToJSONKey':
433397--
434398-- > instance ToJSONKey Color where
@@ -503,42 +467,16 @@ toJSONKeyTextEnc e = ToJSONKeyText tot e
503467 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
504468 tot = T.dropAround (== '"')
505469 . T.decodeLatin1
506− . L.toStrict
470+ . lazyToStrictByteString
507471 . E.encodingToLazyByteString
508472 . e
509473
510−instance Contravariant ToJSONKeyFunction where
511− contramap = contramapToJSONKeyFunction
512−
513474-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
514475contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
515476contramapToJSONKeyFunction h x = case x of
516477 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
517478 ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
518479
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
522−-- objects.
523−--
524−-- See also 'genericFromJSONKey'.
525−--
526−-- === __Example__
527−--
528−-- @
529−-- data Color = Red | Green | Blue
530−-- deriving 'Generic'
531−--
532−-- instance 'ToJSONKey' Color where
533−-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
534−-- @
535−genericToJSONKey :: (Generic a, GToJSONKey (Rep a))
536− => JSONKeyOptions -> ToJSONKeyFunction a
537−genericToJSONKey opts = toJSONKeyText (pack . keyModifier opts . getConName . from)
538−
539−class GetConName f => GToJSONKey f
540−instance GetConName f => GToJSONKey f
541−
542480-------------------------------------------------------------------------------
543481-- Lifings of FromJSON and ToJSON to unary and binary type constructors
544482-------------------------------------------------------------------------------
@@ -551,7 +489,7 @@ instance GetConName f => GToJSONKey f
551489--
552490-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
553491-- instance at compile time. The generated instance is optimized for your type
554−-- so it will probably be more efficient than the following option.
492+-- so will probably be more efficient than the following two options:
555493--
556494-- * The compiler can provide a default generic implementation for
557495-- 'toJSON1'.
@@ -569,29 +507,23 @@ instance GetConName f => GToJSONKey f
569507--
570508-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
571509--
572−-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
510+-- instance ToJSON a => ToJSON1 (Pair a)
573511-- @
574512--
575−-- If the default implementation doesn't give exactly the results you want,
513+-- If @DefaultSignatures@ doesn't give exactly the results you want,
576514-- you can customize the generic encoding with only a tiny amount of
577515-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
578516-- your preferred 'Options':
579517--
580518-- @
581−-- customOptions = 'defaultOptions'
582−-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
583−-- }
584−--
585−-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
586−-- 'liftToJSON' = 'genericLiftToJSON' customOptions
587−-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
519+-- instance ToJSON a => ToJSON1 (Pair a) where
520+-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
521+-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
588522-- @
589−--
590−-- See also 'ToJSON'.
591523class ToJSON1 f where
592524 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
593525
594− default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
526+ default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
595527 => (a -> Value) -> ([a] -> Value) -> f a -> Value
596528 liftToJSON = genericLiftToJSON defaultOptions
597529
@@ -600,7 +532,7 @@ class ToJSON1 f where
600532
601533 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
602534
603− default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
535+ default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
604536 => (a -> Encoding) -> ([a] -> Encoding)
605537 -> f a -> Encoding
606538 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -654,7 +586,7 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
654586-- @
655587-- newtype F a = F [a]
656588--
657−-- -- This instance encodes 'String' as an array of chars
589+-- -- This instance encodes String as an array of chars
658590-- instance 'ToJSON1' F where
659591-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
660592-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
@@ -685,11 +617,6 @@ instance ToJSON1 [] where
685617 {-# INLINE liftToEncoding #-}
686618
687619instance (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] #-}
692−
693620 toJSON = toJSON1
694621 {-# INLINE toJSON #-}
695622
@@ -700,80 +627,39 @@ instance (ToJSON a) => ToJSON [a] where
700627-- Generic toJSON / toEncoding
701628-------------------------------------------------------------------------------
702629
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 #-}
707−
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 #-}
713−
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.
720− gToJSON opts targs
721− | tagSingleConstructors opts = (unTagged :: Tagged allNullary enc -> enc)
722− . sumToJSON opts targs
723− . unM1
724− | otherwise = consToJSON opts targs . unM1 . unM1
725− {-# INLINE gToJSON #-}
726−
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 #-}
732−
733−instance ( AllNullary (a :+: b) allNullary
734− , SumToJSON enc arity (a :+: b) allNullary
735− ) => GToJSON enc arity (a :+: b)
736− where
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 #-}
743−
744630--------------------------------------------------------------------------------
745631-- Generic toJSON
746632
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
749−-- instances.
750−
751−instance GToJSON Value arity V1 where
752− -- Empty values do not exist, which makes the job of formatting them
753− -- rather easy:
754− gToJSON _ _ x = x `seq` error "case: V1"
755− {-# INLINE gToJSON #-}
633+instance OVERLAPPABLE_ (GToJSON arity a) => GToJSON arity (M1 i c a) where
634+ -- Meta-information, which is not handled elsewhere, is ignored:
635+ gToJSON opts targs = gToJSON opts targs . unM1
756636
757−instance ToJSON a => GToJSON Value arity (K1 i a) where
637+instance (ToJSON a) => GToJSON arity (K1 i a) where
758638 -- Constant values are encoded using their ToJSON instance:
759639 gToJSON _opts _ = toJSON . unK1
760− {-# INLINE gToJSON #-}
761640
762−instance ToJSON1 f => GToJSON Value One (Rec1 f) where
641+instance GToJSON One Par1 where
642+ -- Direct occurrences of the last type parameter are encoded with the
643+ -- function passed in as an argument:
644+ gToJSON _opts (To1Args tj _) = tj . unPar1
645+
646+instance (ToJSON1 f) => GToJSON One (Rec1 f) where
763647 -- Recursive occurrences of the last type parameter are encoded using their
764648 -- ToJSON1 instance:
765649 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766− {-# INLINE gToJSON #-}
767650
768−instance GToJSON Value arity U1 where
651+instance GToJSON arity U1 where
769652 -- Empty constructors are encoded to an empty array:
770653 gToJSON _opts _ _ = emptyArray
771− {-# INLINE gToJSON #-}
654+
655+instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
656+ -- Constructors need to be encoded differently depending on whether they're
657+ -- a record or not. This distinction is made by 'consToJSON':
658+ gToJSON opts targs = consToJSON opts targs . unM1
772659
773660instance ( WriteProduct arity a, WriteProduct arity b
774661 , ProductSize a, ProductSize b
775− ) => GToJSON Value arity (a :*: b)
776− where
662+ ) => GToJSON arity (a :*: b) where
777663 -- Products are encoded to an array. Here we allocate a mutable vector of
778664 -- the same size as the product and write the product's elements to it using
779665 -- 'writeProduct':
@@ -785,175 +671,238 @@ instance ( WriteProduct arity a, WriteProduct arity b
785671 where
786672 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
787673 productSize
788− {-# INLINE gToJSON #-}
789674
790−instance ( ToJSON1 f
791− , GToJSON Value One g
792− ) => GToJSON Value One (f :.: g)
793− where
675+instance ( AllNullary (a :+: b) allNullary
676+ , SumToJSON arity (a :+: b) allNullary
677+ ) => GToJSON arity (a :+: b) where
678+ -- If all constructors of a sum datatype are nullary and the
679+ -- 'allNullaryToStringTag' option is set they are encoded to
680+ -- strings. This distinction is made by 'sumToJSON':
681+ gToJSON opts targs = (unTagged :: Tagged allNullary Value -> Value)
682+ . sumToJSON opts targs
683+
684+instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
794685 -- If an occurrence of the last type parameter is nested inside two
795686 -- composed types, it is encoded by using the outermost type's ToJSON1
796687 -- instance to generically encode the innermost type:
797688 gToJSON opts targs =
798689 let gtj = gToJSON opts targs in
799690 liftToJSON gtj (listValue gtj) . unComp1
800− {-# INLINE gToJSON #-}
801691
802692--------------------------------------------------------------------------------
803693-- Generic toEncoding
804694
805−instance ToJSON a => GToJSON Encoding arity (K1 i a) where
695+instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
696+ -- Meta-information, which is not handled elsewhere, is ignored:
697+ gToEncoding opts targs = gToEncoding opts targs . unM1
698+
699+instance (ToJSON a) => GToEncoding arity (K1 i a) where
806700 -- Constant values are encoded using their ToJSON instance:
807− gToJSON _opts _ = toEncoding . unK1
808− {-# INLINE gToJSON #-}
701+ gToEncoding _opts _ = toEncoding . unK1
809702
810−instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
703+instance GToEncoding One Par1 where
704+ -- Direct occurrences of the last type parameter are encoded with the
705+ -- function passed in as an argument:
706+ gToEncoding _opts (To1Args te _) = te . unPar1
707+
708+instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
811709 -- Recursive occurrences of the last type parameter are encoded using their
812710 -- ToEncoding1 instance:
813− gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814− {-# INLINE gToJSON #-}
711+ gToEncoding _opts (To1Args te tel) = liftToEncoding te tel . unRec1
815712
816−instance GToJSON Encoding arity U1 where
713+instance GToEncoding arity U1 where
817714 -- Empty constructors are encoded to an empty array:
818− gToJSON _opts _ _ = E.emptyArray_
819− {-# INLINE gToJSON #-}
715+ gToEncoding _opts _ _ = E.emptyArray_
716+
717+instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
718+ -- Constructors need to be encoded differently depending on whether they're
719+ -- a record or not. This distinction is made by 'consToEncoding':
720+ gToEncoding opts targs = consToEncoding opts targs . unM1
820721
821722instance ( EncodeProduct arity a
822723 , EncodeProduct arity b
823− ) => GToJSON Encoding arity (a :*: b)
824− where
724+ ) => GToEncoding arity (a :*: b) where
825725 -- Products are encoded to an array. Here we allocate a mutable vector of
826726 -- the same size as the product and write the product's elements to it using
827727 -- 'encodeProduct':
828− gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
829− {-# INLINE gToJSON #-}
728+ gToEncoding opts targs p = E.tuple $ encodeProduct opts targs p
830729
831−instance ( ToJSON1 f
832− , GToJSON Encoding One g
833− ) => GToJSON Encoding One (f :.: g)
834− where
730+instance ( AllNullary (a :+: b) allNullary
731+ , SumToEncoding arity (a :+: b) allNullary
732+ ) => GToEncoding arity (a :+: b) where
733+ -- If all constructors of a sum datatype are nullary and the
734+ -- 'allNullaryToStringTag' option is set they are encoded to
735+ -- strings. This distinction is made by 'sumToEncoding':
736+ gToEncoding opts targs
737+ = (unTagged :: Tagged allNullary Encoding -> Encoding)
738+ . sumToEncoding opts targs
739+
740+instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
835741 -- If an occurrence of the last type parameter is nested inside two
836742 -- composed types, it is encoded by using the outermost type's ToJSON1
837743 -- instance to generically encode the innermost type:
838− gToJSON opts targs =
839− let gte = gToJSON opts targs in
744+ gToEncoding opts targs =
745+ let gte = gToEncoding opts targs in
840746 liftToEncoding gte (listEncoding gte) . unComp1
841− {-# INLINE gToJSON #-}
842747
843748--------------------------------------------------------------------------------
844749
845−class SumToJSON enc arity f allNullary where
846− sumToJSON :: Options -> ToArgs enc arity a
847− -> f a -> Tagged allNullary enc
848−
849−instance ( GetConName f
850− , FromString enc
851− , TaggedObject enc arity f
852− , SumToJSON' ObjectWithSingleField enc arity f
853− , SumToJSON' TwoElemArray enc arity f
854− , SumToJSON' UntaggedValue enc arity f
855− ) => SumToJSON enc arity f True
856− where
750+class SumToJSON arity f allNullary where
751+ sumToJSON :: Options -> ToArgs Value arity a
752+ -> f a -> Tagged allNullary Value
753+
754+instance ( GetConName f
755+ , TaggedObjectPairs arity f
756+ , ObjectWithSingleFieldObj arity f
757+ , TwoElemArrayObj arity f
758+ , UntaggedValueObj arity f
759+ ) => SumToJSON arity f True where
857760 sumToJSON opts targs
858− | allNullaryToStringTag opts = Tagged . fromString
761+ | allNullaryToStringTag opts = Tagged . String . pack
859762 . constructorTagModifier opts . getConName
860763 | otherwise = Tagged . nonAllNullarySumToJSON opts targs
861764
862−instance ( TaggedObject enc arity f
863− , SumToJSON' ObjectWithSingleField enc arity f
864− , SumToJSON' TwoElemArray enc arity f
865− , SumToJSON' UntaggedValue enc arity f
866− ) => SumToJSON enc arity f False
867− where
765+instance ( TwoElemArrayObj arity f
766+ , TaggedObjectPairs arity f
767+ , ObjectWithSingleFieldObj arity f
768+ , UntaggedValueObj arity f
769+ ) => SumToJSON arity f False where
868770 sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
869771
870−nonAllNullarySumToJSON :: ( TaggedObject enc arity f
871− , SumToJSON' ObjectWithSingleField enc arity f
872− , SumToJSON' TwoElemArray enc arity f
873− , SumToJSON' UntaggedValue enc arity f
874− ) => Options -> ToArgs enc arity a
875− -> f a -> enc
772+nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
773+ , TaggedObjectPairs arity f
774+ , ObjectWithSingleFieldObj arity f
775+ , UntaggedValueObj arity f
776+ ) => Options -> ToArgs Value arity a
777+ -> f a -> Value
876778nonAllNullarySumToJSON opts targs =
877779 case sumEncoding opts of
878−
879780 TaggedObject{..} ->
880− taggedObject opts targs tagFieldName contentsFieldName
781+ object . taggedObjectPairs opts targs tagFieldName contentsFieldName
782+ ObjectWithSingleField -> Object . objectWithSingleFieldObj opts targs
783+ TwoElemArray -> Array . twoElemArrayObj opts targs
784+ UntaggedValue -> untaggedValueObj opts targs
881785
882− ObjectWithSingleField ->
883− (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884− . sumToJSON' opts targs
885−
886− TwoElemArray ->
887− (unTagged :: Tagged TwoElemArray enc -> enc)
888− . sumToJSON' opts targs
786+--------------------------------------------------------------------------------
889787
890− UntaggedValue ->
891− (unTagged :: Tagged UntaggedValue enc -> enc)
892− . sumToJSON' opts targs
788+class SumToEncoding arity f allNullary where
789+ sumToEncoding :: Options -> ToArgs Encoding arity a
790+ -> f a -> Tagged allNullary Encoding
791+
792+instance ( GetConName f
793+ , TaggedObjectEnc arity f
794+ , ObjectWithSingleFieldEnc arity f
795+ , TwoElemArrayEnc arity f
796+ , UntaggedValueEnc arity f
797+ ) => SumToEncoding arity f True where
798+ sumToEncoding opts targs
799+ | allNullaryToStringTag opts = Tagged . toEncoding .
800+ constructorTagModifier opts . getConName
801+ | otherwise = Tagged . nonAllNullarySumToEncoding opts targs
802+
803+instance ( TwoElemArrayEnc arity f
804+ , TaggedObjectEnc arity f
805+ , ObjectWithSingleFieldEnc arity f
806+ , UntaggedValueEnc arity f
807+ ) => SumToEncoding arity f False where
808+ sumToEncoding opts targs = Tagged . nonAllNullarySumToEncoding opts targs
809+
810+nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
811+ , TaggedObjectEnc arity f
812+ , ObjectWithSingleFieldEnc arity f
813+ , UntaggedValueEnc arity f
814+ ) => Options -> ToArgs Encoding arity a
815+ -> f a -> Encoding
816+nonAllNullarySumToEncoding opts targs =
817+ case sumEncoding opts of
818+ TaggedObject{..} ->
819+ taggedObjectEnc opts targs tagFieldName contentsFieldName
820+ ObjectWithSingleField -> objectWithSingleFieldEnc opts targs
821+ TwoElemArray -> twoElemArrayEnc opts targs
822+ UntaggedValue -> untaggedValueEnc opts targs
893823
894824--------------------------------------------------------------------------------
895825
896−class FromString enc where
897− fromString :: String -> enc
826+class TaggedObjectPairs arity f where
827+ taggedObjectPairs :: Options -> ToArgs Value arity a
828+ -> String -> String
829+ -> f a -> [Pair]
830+
831+instance ( TaggedObjectPairs arity a
832+ , TaggedObjectPairs arity b
833+ ) => TaggedObjectPairs arity (a :+: b) where
834+ taggedObjectPairs opts targs tagFieldName contentsFieldName (L1 x) =
835+ taggedObjectPairs opts targs tagFieldName contentsFieldName x
836+ taggedObjectPairs opts targs tagFieldName contentsFieldName (R1 x) =
837+ taggedObjectPairs opts targs tagFieldName contentsFieldName x
838+
839+instance ( IsRecord a isRecord
840+ , TaggedObjectPairs' arity a isRecord
841+ , Constructor c
842+ ) => TaggedObjectPairs arity (C1 c a) where
843+ taggedObjectPairs opts targs tagFieldName contentsFieldName =
844+ (pack tagFieldName .= constructorTagModifier opts
845+ (conName (undefined :: t c a p)) :) .
846+ (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
847+ taggedObjectPairs' opts targs contentsFieldName . unM1
898848
899−instance FromString Encoding where
900− fromString = toEncoding
849+class TaggedObjectPairs' arity f isRecord where
850+ taggedObjectPairs' :: Options -> ToArgs Value arity a
851+ -> String -> f a -> Tagged isRecord [Pair]
901852
902−instance FromString Value where
903− fromString = String . pack
853+instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
854+ taggedObjectPairs' _ _ _ _ = Tagged []
904855
905−--------------------------------------------------------------------------------
856+instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
857+ taggedObjectPairs' opts targs _ =
858+ Tagged . toList . recordToPairs opts targs
906859
907−class TaggedObject enc arity f where
908− taggedObject :: Options -> ToArgs enc arity a
909− -> String -> String
910− -> f a -> enc
860+instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
861+ taggedObjectPairs' opts targs contentsFieldName =
862+ Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts targs
911863
912−instance ( TaggedObject enc arity a
913− , TaggedObject enc arity b
914− ) => TaggedObject enc arity (a :+: b)
915− where
916− taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
917− taggedObject opts targs tagFieldName contentsFieldName x
918− taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
919− taggedObject opts targs tagFieldName contentsFieldName x
920−
921−instance ( IsRecord a isRecord
922− , TaggedObject' enc pairs arity a isRecord
923− , FromPairs enc pairs
924− , FromString enc
925− , KeyValuePair enc pairs
926− , Constructor c
927− ) => TaggedObject enc arity (C1 c a)
928− where
929− taggedObject opts targs tagFieldName contentsFieldName =
930− fromPairs . mappend tag . contents
931− where
932− tag = tagFieldName `pair`
933− (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
934− :: enc)
935− contents =
936− (unTagged :: Tagged isRecord pairs -> pairs) .
937− taggedObject' opts targs contentsFieldName . unM1
938−
939−class TaggedObject' enc pairs arity f isRecord where
940− taggedObject' :: Options -> ToArgs enc arity a
941− -> String -> f a -> Tagged isRecord pairs
942−
943−instance ( GToJSON enc arity f
944− , KeyValuePair enc pairs
945− ) => TaggedObject' enc pairs arity f False
946− where
947− taggedObject' opts targs contentsFieldName =
948− Tagged . (contentsFieldName `pair`) . gToJSON opts targs
949−
950−instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
951− taggedObject' _ _ _ _ = Tagged mempty
864+--------------------------------------------------------------------------------
952865
953−instance ( RecordToPairs enc pairs arity f
954− ) => TaggedObject' enc pairs arity f True
955− where
956− taggedObject' opts targs _ = Tagged . recordToPairs opts targs
866+class TaggedObjectEnc arity f where
867+ taggedObjectEnc :: Options -> ToArgs Encoding arity a
868+ -> String -> String
869+ -> f a -> Encoding
870+
871+instance ( TaggedObjectEnc arity a
872+ , TaggedObjectEnc arity b
873+ ) => TaggedObjectEnc arity (a :+: b) where
874+ taggedObjectEnc opts targs tagFieldName contentsFieldName (L1 x) =
875+ taggedObjectEnc opts targs tagFieldName contentsFieldName x
876+ taggedObjectEnc opts targs tagFieldName contentsFieldName (R1 x) =
877+ taggedObjectEnc opts targs tagFieldName contentsFieldName x
878+
879+instance ( IsRecord a isRecord
880+ , TaggedObjectEnc' arity a isRecord
881+ , Constructor c
882+ ) => TaggedObjectEnc arity (C1 c a) where
883+ taggedObjectEnc opts targs tagFieldName contentsFieldName v =
884+ E.wrapObject $
885+ (E.string tagFieldName ><
886+ E.colon ><
887+ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) ><
888+ ((unTagged :: Tagged isRecord Encoding -> Encoding) .
889+ taggedObjectEnc' opts targs contentsFieldName . unM1 $ v)
890+
891+class TaggedObjectEnc' arity f isRecord where
892+ taggedObjectEnc' :: Options -> ToArgs Encoding arity a
893+ -> String -> f a -> Tagged isRecord Encoding
894+
895+instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
896+ taggedObjectEnc' _ _ _ _ = Tagged E.empty
897+
898+instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
899+ taggedObjectEnc' opts targs _ = Tagged . (E.comma ><) . fst
900+ . recordToEncoding opts targs
901+
902+instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
903+ taggedObjectEnc' opts targs contentsFieldName =
904+ Tagged . (\z -> E.comma >< toEncoding contentsFieldName >< E.colon >< z) .
905+ gToEncoding opts targs
957906
958907--------------------------------------------------------------------------------
959908
@@ -968,38 +917,23 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
968917instance (Constructor c) => GetConName (C1 c a) where
969918 getConName = conName
970919
971−-- For genericToJSONKey
972−instance GetConName a => GetConName (D1 d a) where
973− getConName (M1 x) = getConName x
974−
975920--------------------------------------------------------------------------------
976921
977−-- Reflection of SumEncoding variants
922+class TwoElemArrayObj arity f where
923+ twoElemArrayObj :: Options -> ToArgs Value arity a
924+ -> f a -> V.Vector Value
978925
979−data ObjectWithSingleField
980−data TwoElemArray
981−data UntaggedValue
926+instance ( TwoElemArrayObj arity a
927+ , TwoElemArrayObj arity b
928+ ) => TwoElemArrayObj arity (a :+: b) where
929+ twoElemArrayObj opts targs (L1 x) = twoElemArrayObj opts targs x
930+ twoElemArrayObj opts targs (R1 x) = twoElemArrayObj opts targs x
982931
983−--------------------------------------------------------------------------------
984−
985−class SumToJSON' s enc arity f where
986− sumToJSON' :: Options -> ToArgs enc arity a
987− -> f a -> Tagged s enc
988−
989−instance ( SumToJSON' s enc arity a
990− , SumToJSON' s enc arity b
991− ) => SumToJSON' s enc arity (a :+: b)
992− where
993− sumToJSON' opts targs (L1 x) = sumToJSON' opts targs x
994− sumToJSON' opts targs (R1 x) = sumToJSON' opts targs x
995−
996−--------------------------------------------------------------------------------
997−
998−instance ( GToJSON Value arity a
999− , ConsToJSON Value arity a
932+instance ( GToJSON arity a
933+ , ConsToJSON arity a
1000934 , Constructor c
1001− ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
1002− sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
935+ ) => TwoElemArrayObj arity (C1 c a) where
936+ twoElemArrayObj opts targs x = V.create $ do
1003937 mv <- VM.unsafeNew 2
1004938 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
1005939 $ conName (undefined :: t c a p)
@@ -1008,121 +942,148 @@ instance ( GToJSON Value arity a
1008942
1009943--------------------------------------------------------------------------------
1010944
1011−instance ( GToJSON Encoding arity a
1012− , ConsToJSON Encoding arity a
945+class TwoElemArrayEnc arity f where
946+ twoElemArrayEnc :: Options -> ToArgs Encoding arity a
947+ -> f a -> Encoding
948+
949+instance ( TwoElemArrayEnc arity a
950+ , TwoElemArrayEnc arity b
951+ ) => TwoElemArrayEnc arity (a :+: b) where
952+ twoElemArrayEnc opts targs (L1 x) = twoElemArrayEnc opts targs x
953+ twoElemArrayEnc opts targs (R1 x) = twoElemArrayEnc opts targs x
954+
955+instance ( GToEncoding arity a
956+ , ConsToEncoding arity a
1013957 , Constructor c
1014− ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
1015− where
1016− sumToJSON' opts targs x = Tagged $ E.list id
1017− [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
1018− , gToJSON opts targs x
1019− ]
958+ ) => TwoElemArrayEnc arity (C1 c a) where
959+ twoElemArrayEnc opts targs x = E.tuple $
960+ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
961+ gToEncoding opts targs x
1020962
1021963--------------------------------------------------------------------------------
1022964
1023−class ConsToJSON enc arity f where
1024− consToJSON :: Options -> ToArgs enc arity a
1025− -> f a -> enc
965+class ConsToJSON arity f where
966+ consToJSON :: Options -> ToArgs Value arity a
967+ -> f a -> Value
1026968
1027−class ConsToJSON' enc arity f isRecord where
1028− consToJSON' :: Options -> ToArgs enc arity a
1029− -> f a -> Tagged isRecord enc
969+class ConsToJSON' arity f isRecord where
970+ consToJSON' :: Options -> ToArgs Value arity a
971+ -> Bool -- ^ Are we a record with one field?
972+ -> f a -> Tagged isRecord Value
1030973
1031−instance ( IsRecord f isRecord
1032− , ConsToJSON' enc arity f isRecord
1033− ) => ConsToJSON enc arity f
1034− where
974+instance ( IsRecord f isRecord
975+ , ConsToJSON' arity f isRecord
976+ ) => ConsToJSON arity f where
1035977 consToJSON opts targs =
1036− (unTagged :: Tagged isRecord enc -> enc)
1037− . consToJSON' opts targs
1038− {-# INLINE consToJSON #-}
978+ (unTagged :: Tagged isRecord Value -> Value)
979+ . consToJSON' opts targs (isUnary (undefined :: f a))
1039980
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
1045− where
1046− consToJSON' opts targs
1047− | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
1048− | otherwise = Tagged . fromPairs . recordToPairs opts targs
1049− {-# INLINE consToJSON' #-}
1050−
1051−instance ( RecordToPairs enc pairs arity f
1052− , FromPairs enc pairs
1053− ) => ConsToJSON' enc arity f True
1054− where
1055− consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
1056− {-# INLINE consToJSON' #-}
981+instance (RecordToPairs arity f) => ConsToJSON' arity f True where
982+ consToJSON' opts targs isUn f = let
983+ vals = toList $ recordToPairs opts targs f
984+ in case (unwrapUnaryRecords opts,isUn,vals) of
985+ (True,True,[(_,val)]) -> Tagged val
986+ _ -> Tagged $ object vals
1057987
1058−instance GToJSON enc arity f => ConsToJSON' enc arity f False where
1059− consToJSON' opts targs = Tagged . gToJSON opts targs
1060− {-# INLINE consToJSON' #-}
988+instance GToJSON arity f => ConsToJSON' arity f False where
989+ consToJSON' opts targs _ = Tagged . gToJSON opts targs
1061990
1062991--------------------------------------------------------------------------------
1063992
1064−class RecordToPairs enc pairs arity f where
1065− -- 1st element: whole thing
1066− -- 2nd element: in case the record has only 1 field, just the value
1067− -- of the field (without the key); 'Nothing' otherwise
1068− recordToPairs :: Options -> ToArgs enc arity a
1069− -> f a -> pairs
993+class ConsToEncoding arity f where
994+ consToEncoding :: Options -> ToArgs Encoding arity a
995+ -> f a -> Encoding
1070996
1071−instance ( Monoid pairs
1072− , RecordToPairs enc pairs arity a
1073− , RecordToPairs enc pairs arity b
1074− ) => RecordToPairs enc pairs arity (a :*: b)
1075− where
1076− recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
1077− pairsOf a `mappend` pairsOf b
1078− where
1079− pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
1080− pairsOf = recordToPairs opts targs
1081− {-# INLINE recordToPairs #-}
1082−
1083−instance ( Selector s
1084− , GToJSON enc arity a
1085− , KeyValuePair enc pairs
1086− ) => RecordToPairs enc pairs arity (S1 s a)
1087− where
997+class ConsToEncoding' arity f isRecord where
998+ consToEncoding' :: Options -> ToArgs Encoding arity a
999+ -> Bool -- ^ Are we a record with one field?
1000+ -> f a -> Tagged isRecord Encoding
1001+
1002+instance ( IsRecord f isRecord
1003+ , ConsToEncoding' arity f isRecord
1004+ ) => ConsToEncoding arity f where
1005+ consToEncoding opts targs =
1006+ (unTagged :: Tagged isRecord Encoding -> Encoding)
1007+ . consToEncoding' opts targs (isUnary (undefined :: f a))
1008+
1009+instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
1010+ consToEncoding' opts targs isUn x =
1011+ let (enc, mbVal) = recordToEncoding opts targs x
1012+ in case (unwrapUnaryRecords opts, isUn, mbVal) of
1013+ (True, True, Just val) -> Tagged val
1014+ _ -> Tagged $ E.wrapObject enc
1015+
1016+instance GToEncoding arity f => ConsToEncoding' arity f False where
1017+ consToEncoding' opts targs _ = Tagged . gToEncoding opts targs
1018+
1019+--------------------------------------------------------------------------------
1020+
1021+class RecordToPairs arity f where
1022+ recordToPairs :: Options -> ToArgs Value arity a
1023+ -> f a -> DList Pair
1024+
1025+instance ( RecordToPairs arity a
1026+ , RecordToPairs arity b
1027+ ) => RecordToPairs arity (a :*: b) where
1028+ recordToPairs opts targs (a :*: b) = recordToPairs opts targs a <>
1029+ recordToPairs opts targs b
1030+
1031+instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
10881032 recordToPairs = fieldToPair
1089− {-# INLINE recordToPairs #-}
1090−
1091−instance INCOHERENT_
1092− ( Selector s
1093− , GToJSON enc arity (K1 i (Maybe a))
1094− , KeyValuePair enc pairs
1095− , Monoid pairs
1096− ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
1097− where
1033+
1034+instance OVERLAPPING_ (Selector s, ToJSON a) =>
1035+ RecordToPairs arity (S1 s (K1 i (Maybe a))) where
10981036 recordToPairs opts _ (M1 k1) | omitNothingFields opts
1099− , K1 Nothing <- k1 = mempty
1037+ , K1 Nothing <- k1 = DList.empty
11001038 recordToPairs opts targs m1 = fieldToPair opts targs m1
1101− {-# INLINE recordToPairs #-}
1102−
1103−instance INCOHERENT_
1104− ( Selector s
1105− , GToJSON enc arity (K1 i (Maybe a))
1106− , KeyValuePair enc pairs
1107− , Monoid pairs
1108− ) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup.Option a)))
1109− where
1110− recordToPairs opts targs = recordToPairs opts targs . unwrap
1111− where
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 #-}
1115−
1116−fieldToPair :: (Selector s
1117− , GToJSON enc arity a
1118− , KeyValuePair enc pairs)
1119− => Options -> ToArgs enc arity p
1120− -> S1 s a p -> pairs
1121−fieldToPair opts targs m1 =
1122− let key = fieldLabelModifier opts (selName m1)
1123− value = gToJSON opts targs (unM1 m1)
1124− in key `pair` value
1125−{-# INLINE fieldToPair #-}
1039+
1040+fieldToPair :: (Selector s, GToJSON arity a)
1041+ => Options -> ToArgs Value arity p
1042+ -> S1 s a p -> DList Pair
1043+fieldToPair opts targs m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
1044+ , gToJSON opts targs (unM1 m1)
1045+ )
1046+
1047+--------------------------------------------------------------------------------
1048+
1049+class RecordToEncoding arity f where
1050+ -- 1st element: whole thing
1051+ -- 2nd element: in case the record has only 1 field, just the value
1052+ -- of the field (without the key); 'Nothing' otherwise
1053+ recordToEncoding :: Options -> ToArgs Encoding arity a
1054+ -> f a -> (Encoding, Maybe Encoding)
1055+
1056+instance ( RecordToEncoding arity a
1057+ , RecordToEncoding arity b
1058+ ) => RecordToEncoding arity (a :*: b) where
1059+ recordToEncoding opts targs (a :*: b) | omitNothingFields opts =
1060+ (E.econcat $ intersperse E.comma $
1061+ filter (not . E.nullEncoding)
1062+ [ fst (recordToEncoding opts targs a)
1063+ , fst (recordToEncoding opts targs b) ]
1064+ , Nothing)
1065+ recordToEncoding opts targs (a :*: b) =
1066+ (fst (recordToEncoding opts targs a) >< E.comma ><
1067+ fst (recordToEncoding opts targs b),
1068+ Nothing)
1069+
1070+
1071+instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1072+ recordToEncoding = fieldToEncoding
1073+
1074+instance OVERLAPPING_ (Selector s, ToJSON a) =>
1075+ RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1076+ recordToEncoding opts _ (M1 k1) | omitNothingFields opts
1077+ , K1 Nothing <- k1 = (E.empty, Nothing)
1078+ recordToEncoding opts targs m1 = fieldToEncoding opts targs m1
1079+
1080+fieldToEncoding :: (Selector s, GToEncoding arity a)
1081+ => Options -> ToArgs Encoding arity p
1082+ -> S1 s a p -> (Encoding, Maybe Encoding)
1083+fieldToEncoding opts targs m1 =
1084+ let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1085+ valueBuilder = gToEncoding opts targs (unM1 m1)
1086+ in (keyBuilder >< E.colon >< valueBuilder, Just valueBuilder)
11261087
11271088--------------------------------------------------------------------------------
11281089
@@ -1145,12 +1106,10 @@ instance ( WriteProduct arity a
11451106 lenL = len `unsafeShiftR` 1
11461107 lenR = len - lenL
11471108 ixR = ix + lenL
1148− {-# INLINE writeProduct #-}
11491109
1150−instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1110+instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
11511111 writeProduct opts targs mv ix _ =
11521112 VM.unsafeWrite mv ix . gToJSON opts targs
1153− {-# INLINE writeProduct #-}
11541113
11551114--------------------------------------------------------------------------------
11561115
@@ -1165,44 +1124,114 @@ instance ( EncodeProduct arity a
11651124 E.econcat $ intersperse E.comma $
11661125 filter (not . E.nullEncoding)
11671126 [encodeProduct opts targs a, encodeProduct opts targs b]
1168− encodeProduct opts targs (a :*: b) =
1169− encodeProduct opts targs a >*<
1170− encodeProduct opts targs b
1171− {-# INLINE encodeProduct #-}
1127+ encodeProduct opts targs (a :*: b) = encodeProduct opts targs a >*<
1128+ encodeProduct opts targs b
11721129
1173−instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1174− encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1175− {-# INLINE encodeProduct #-}
1130+instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1131+ encodeProduct opts targs a = E.retagEncoding $ gToEncoding opts targs a
11761132
11771133--------------------------------------------------------------------------------
11781134
1179−instance ( GToJSON enc arity a
1180− , ConsToJSON enc arity a
1181− , FromPairs enc pairs
1182− , KeyValuePair enc pairs
1135+class ObjectWithSingleFieldObj arity f where
1136+ objectWithSingleFieldObj :: Options -> ToArgs Value arity a
1137+ -> f a -> Object
1138+
1139+instance ( ObjectWithSingleFieldObj arity a
1140+ , ObjectWithSingleFieldObj arity b
1141+ ) => ObjectWithSingleFieldObj arity (a :+: b) where
1142+ objectWithSingleFieldObj opts targs (L1 x) =
1143+ objectWithSingleFieldObj opts targs x
1144+ objectWithSingleFieldObj opts targs (R1 x) =
1145+ objectWithSingleFieldObj opts targs x
1146+
1147+instance ( GToJSON arity a
1148+ , ConsToJSON arity a
11831149 , Constructor c
1184− ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1185− where
1186− sumToJSON' opts targs =
1187− Tagged . fromPairs . (typ `pair`) . gToJSON opts targs
1150+ ) => ObjectWithSingleFieldObj arity (C1 c a) where
1151+ objectWithSingleFieldObj opts targs = H.singleton typ . gToJSON opts targs
11881152 where
1189− typ = constructorTagModifier opts $
1153+ typ = pack $ constructorTagModifier opts $
11901154 conName (undefined :: t c a p)
11911155
11921156--------------------------------------------------------------------------------
11931157
1158+class ObjectWithSingleFieldEnc arity f where
1159+ objectWithSingleFieldEnc :: Options -> ToArgs Encoding arity a
1160+ -> f a -> Encoding
1161+
1162+instance ( ObjectWithSingleFieldEnc arity a
1163+ , ObjectWithSingleFieldEnc arity b
1164+ ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1165+ objectWithSingleFieldEnc opts targs (L1 x) =
1166+ objectWithSingleFieldEnc opts targs x
1167+ objectWithSingleFieldEnc opts targs (R1 x) =
1168+ objectWithSingleFieldEnc opts targs x
1169+
1170+instance ( GToEncoding arity a
1171+ , ConsToEncoding arity a
1172+ , Constructor c
1173+ ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1174+ objectWithSingleFieldEnc opts targs v =
1175+ E.openCurly ><
1176+ toEncoding
1177+ (constructorTagModifier opts
1178+ (conName (undefined :: t c a p))) ><
1179+ E.colon ><
1180+ gToEncoding opts targs v ><
1181+ E.closeCurly
1182+
1183+--------------------------------------------------------------------------------
1184+
1185+class UntaggedValueObj arity f where
1186+ untaggedValueObj :: Options -> ToArgs Value arity a
1187+ -> f a -> Value
1188+
1189+instance
1190+ ( UntaggedValueObj arity a
1191+ , UntaggedValueObj arity b
1192+ ) => UntaggedValueObj arity (a :+: b)
1193+ where
1194+ untaggedValueObj opts targs (L1 x) = untaggedValueObj opts targs x
1195+ untaggedValueObj opts targs (R1 x) = untaggedValueObj opts targs x
1196+
11941197instance OVERLAPPABLE_
1195− ( ConsToJSON enc arity a
1196− ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1198+ ( GToJSON arity a
1199+ , ConsToJSON arity a
1200+ ) => UntaggedValueObj arity (C1 c a) where
1201+ untaggedValueObj = gToJSON
1202+
1203+instance OVERLAPPING_
1204+ ( Constructor c )
1205+ => UntaggedValueObj arity (C1 c U1)
1206+ where
1207+ untaggedValueObj opts _ _ = toJSON $
1208+ constructorTagModifier opts $ conName (undefined :: t c U1 p)
1209+
1210+--------------------------------------------------------------------------------
1211+
1212+class UntaggedValueEnc arity f where
1213+ untaggedValueEnc :: Options -> ToArgs Encoding arity a
1214+ -> f a -> Encoding
1215+instance
1216+ ( UntaggedValueEnc arity a
1217+ , UntaggedValueEnc arity b
1218+ ) => UntaggedValueEnc arity (a :+: b)
1219+ where
1220+ untaggedValueEnc opts targs (L1 x) = untaggedValueEnc opts targs x
1221+ untaggedValueEnc opts targs (R1 x) = untaggedValueEnc opts targs x
1222+
1223+instance OVERLAPPABLE_
1224+ ( GToEncoding arity a
1225+ , ConsToEncoding arity a
1226+ ) => UntaggedValueEnc arity (C1 c a)
11971227 where
1198− sumToJSON' opts targs = Tagged . gToJSON opts targs
1228+ untaggedValueEnc = gToEncoding
11991229
12001230instance OVERLAPPING_
1201− ( Constructor c
1202− , FromString enc
1203− ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1231+ ( Constructor c )
1232+ => UntaggedValueEnc arity (C1 c U1)
12041233 where
1205− sumToJSON' opts _ _ = Tagged . fromString $
1234+ untaggedValueEnc opts _ _ = toEncoding $
12061235 constructorTagModifier opts $ conName (undefined :: t c U1 p)
12071236
12081237-------------------------------------------------------------------------------
@@ -1276,13 +1305,6 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
12761305 toEncoding = toEncoding2
12771306 {-# INLINE toEncoding #-}
12781307
1279−instance ToJSON Void where
1280− toJSON = absurd
1281− {-# INLINE toJSON #-}
1282−
1283− toEncoding = absurd
1284− {-# INLINE toEncoding #-}
1285−
12861308
12871309instance ToJSON Bool where
12881310 toJSON = Bool
@@ -1468,6 +1490,7 @@ instance ToJSONKey Int64 where
14681490 toJSONKey = toJSONKeyTextEnc E.int64Text
14691491 {-# INLINE toJSONKey #-}
14701492
1493+
14711494instance ToJSON Word where
14721495 toJSON = Number . fromIntegral
14731496 {-# INLINE toJSON #-}
@@ -1527,12 +1550,6 @@ instance ToJSONKey Word64 where
15271550 toJSONKey = toJSONKeyTextEnc E.word64Text
15281551 {-# INLINE toJSONKey #-}
15291552
1530−instance ToJSON CTime where
1531− toJSON (CTime i) = toJSON i
1532− {-# INLINE toJSON #-}
1533−
1534− toEncoding (CTime i) = toEncoding i
1535− {-# INLINE toEncoding #-}
15361553
15371554instance ToJSON Text where
15381555 toJSON = String
@@ -1647,7 +1664,7 @@ instance (ToJSON a) => ToJSON (Identity a) where
16471664 toEncodingList = liftToEncodingList toEncoding toEncodingList
16481665 {-# INLINE toEncodingList #-}
16491666
1650−instance (ToJSONKey a) => ToJSONKey (Identity a) where
1667+instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
16511668 toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
16521669 toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
16531670
@@ -1796,11 +1813,13 @@ instance ToJSONKey k => ToJSON1 (M.Map k) where
17961813 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
17971814 {-# INLINE liftToJSON #-}
17981815
1816+ -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
17991817 liftToEncoding g _ = case toJSONKey of
18001818 ToJSONKeyText _ f -> dict f g M.foldrWithKey
18011819 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
18021820 where
1803− pairEncoding f (a, b) = E.list id [f a, g b]
1821+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1822+ pairEncoding f (a, b) = tuple $ f a >*< g b
18041823 {-# INLINE liftToEncoding #-}
18051824
18061825
@@ -1838,17 +1857,6 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
18381857 toEncoding = toEncoding1
18391858 {-# INLINE toEncoding #-}
18401859
1841−-------------------------------------------------------------------------------
1842−-- uuid
1843−-------------------------------------------------------------------------------
1844−
1845−instance ToJSON UUID.UUID where
1846− toJSON = toJSON . UUID.toText
1847− toEncoding = E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
1848−
1849−instance ToJSONKey UUID.UUID where
1850− toJSONKey = ToJSONKeyText UUID.toText $
1851− E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
18521860
18531861-------------------------------------------------------------------------------
18541862-- vector
@@ -1862,8 +1870,6 @@ instance ToJSON1 Vector where
18621870 {-# INLINE liftToEncoding #-}
18631871
18641872instance (ToJSON a) => ToJSON (Vector a) where
1865− {-# SPECIALIZE instance ToJSON Array #-}
1866−
18671873 toJSON = toJSON1
18681874 {-# INLINE toJSON #-}
18691875
@@ -1931,12 +1937,11 @@ instance ToJSONKey k => ToJSON1 (H.HashMap k) where
19311937 ToJSONKeyText _ f -> dict f g H.foldrWithKey
19321938 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
19331939 where
1934− pairEncoding f (a, b) = E.list id [f a, g b]
1940+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1941+ pairEncoding f (a, b) = tuple $ f a >*< g b
19351942 {-# INLINE liftToEncoding #-}
19361943
19371944instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1938− {-# SPECIALIZE instance ToJSON Object #-}
1939−
19401945 toJSON = toJSON1
19411946 {-# INLINE toJSON #-}
19421947
@@ -1966,32 +1971,6 @@ dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
19661971formatMillis :: (FormatTime t) => t -> String
19671972formatMillis = take 3 . formatTime defaultTimeLocale "%q"
19681973
1969−-------------------------------------------------------------------------------
1970−-- primitive
1971−-------------------------------------------------------------------------------
1972−
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
1978−
1979−instance ToJSON a => ToJSON (PM.SmallArray a) where
1980− toJSON = toJSON . Exts.toList
1981− toEncoding = toEncoding . Exts.toList
1982−
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
1987−
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
1992−#endif
1993−#endif
1994−
19951974-------------------------------------------------------------------------------
19961975-- time
19971976-------------------------------------------------------------------------------
@@ -2040,11 +2019,10 @@ stringEncoding :: Encoding' Text -> Value
20402019stringEncoding = String
20412020 . T.dropAround (== '"')
20422021 . T.decodeLatin1
2043− . L.toStrict
2022+ . lazyToStrictByteString
20442023 . E.encodingToLazyByteString
20452024{-# INLINE stringEncoding #-}
20462025
2047−
20482026instance ToJSON NominalDiffTime where
20492027 toJSON = Number . realToFrac
20502028 {-# INLINE toJSON #-}
@@ -2052,100 +2030,18 @@ instance ToJSON NominalDiffTime where
20522030 toEncoding = E.scientific . realToFrac
20532031 {-# INLINE toEncoding #-}
20542032
2055−
2056−instance ToJSON DiffTime where
2057− toJSON = Number . realToFrac
2058− {-# INLINE toJSON #-}
2059−
2060− toEncoding = E.scientific . realToFrac
2061− {-# INLINE toEncoding #-}
2062−
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)
2069−
2070−instance ToJSON CalendarDiffTime where
2071− toJSON (CalendarDiffTime m nt) = object
2072− [ "months" .= m
2073− , "time" .= nt
2074− ]
2075− toEncoding (CalendarDiffTime m nt) = E.pairs
2076− ("months" .= m <> "time" .= nt)
2077−
2078−instance ToJSON CalendarDiffDays where
2079− toJSON (CalendarDiffDays m d) = object
2080− [ "months" .= m
2081− , "days" .= d
2082− ]
2083− toEncoding (CalendarDiffDays m d) = E.pairs
2084− ("months" .= m <> "days" .= d)
2085−
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"
2094−
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\""
2103−
2104−instance ToJSONKey DayOfWeek where
2105− toJSONKey = toJSONKeyTextEnc toEncodingDayOfWeek
2106−
21072033-------------------------------------------------------------------------------
21082034-- base Monoid/Semigroup
21092035-------------------------------------------------------------------------------
21102036
2111−instance ToJSON1 Monoid.Dual where
2112− liftToJSON t _ = t . Monoid.getDual
2113− {-# INLINE liftToJSON #-}
2114−
2115− liftToEncoding t _ = t . Monoid.getDual
2116− {-# INLINE liftToEncoding #-}
2117−
2118−instance ToJSON a => ToJSON (Monoid.Dual a) where
2119− toJSON = toJSON1
2120− {-# INLINE toJSON #-}
2121−
2122− toEncoding = toEncoding1
2123− {-# INLINE toEncoding #-}
2124−
2125−
2126−instance ToJSON1 Monoid.First where
2127− liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
2128− {-# INLINE liftToJSON #-}
2129−
2130− liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
2131− {-# INLINE liftToEncoding #-}
2132−
2133−instance ToJSON a => ToJSON (Monoid.First a) where
2134− toJSON = toJSON1
2135− {-# INLINE toJSON #-}
2136−
2137− toEncoding = toEncoding1
2138− {-# INLINE toEncoding #-}
2139−
2140−
2141−instance ToJSON1 Monoid.Last where
2142− liftToJSON t to' = liftToJSON t to' . Monoid.getLast
2037+instance ToJSON1 Dual where
2038+ liftToJSON t _ = t . getDual
21432039 {-# INLINE liftToJSON #-}
21442040
2145− liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2041+ liftToEncoding t _ = t . getDual
21462042 {-# INLINE liftToEncoding #-}
21472043
2148−instance ToJSON a => ToJSON (Monoid.Last a) where
2044+instance ToJSON a => ToJSON (Dual a) where
21492045 toJSON = toJSON1
21502046 {-# INLINE toJSON #-}
21512047
@@ -2153,14 +2049,14 @@ instance ToJSON a => ToJSON (Monoid.Last a) where
21532049 {-# INLINE toEncoding #-}
21542050
21552051
2156−instance ToJSON1 Semigroup.Min where
2157− liftToJSON t _ (Semigroup.Min x) = t x
2052+instance ToJSON1 First where
2053+ liftToJSON t to' = liftToJSON t to' . getFirst
21582054 {-# INLINE liftToJSON #-}
21592055
2160− liftToEncoding t _ (Semigroup.Min x) = t x
2056+ liftToEncoding t to' = liftToEncoding t to' . getFirst
21612057 {-# INLINE liftToEncoding #-}
21622058
2163−instance ToJSON a => ToJSON (Semigroup.Min a) where
2059+instance ToJSON a => ToJSON (First a) where
21642060 toJSON = toJSON1
21652061 {-# INLINE toJSON #-}
21662062
@@ -2168,73 +2064,14 @@ instance ToJSON a => ToJSON (Semigroup.Min a) where
21682064 {-# INLINE toEncoding #-}
21692065
21702066
2171−instance ToJSON1 Semigroup.Max where
2172− liftToJSON t _ (Semigroup.Max x) = t x
2067+instance ToJSON1 Last where
2068+ liftToJSON t to' = liftToJSON t to' . getLast
21732069 {-# INLINE liftToJSON #-}
21742070
2175− liftToEncoding t _ (Semigroup.Max x) = t x
2071+ liftToEncoding t to' = liftToEncoding t to' . getLast
21762072 {-# INLINE liftToEncoding #-}
21772073
2178−instance ToJSON a => ToJSON (Semigroup.Max a) where
2179− toJSON = toJSON1
2180− {-# INLINE toJSON #-}
2181−
2182− toEncoding = toEncoding1
2183− {-# INLINE toEncoding #-}
2184−
2185−instance ToJSON1 Semigroup.First where
2186− liftToJSON t _ (Semigroup.First x) = t x
2187− {-# INLINE liftToJSON #-}
2188−
2189− liftToEncoding t _ (Semigroup.First x) = t x
2190− {-# INLINE liftToEncoding #-}
2191−
2192−instance ToJSON a => ToJSON (Semigroup.First a) where
2193− toJSON = toJSON1
2194− {-# INLINE toJSON #-}
2195−
2196− toEncoding = toEncoding1
2197− {-# INLINE toEncoding #-}
2198−
2199−
2200−instance ToJSON1 Semigroup.Last where
2201− liftToJSON t _ (Semigroup.Last x) = t x
2202− {-# INLINE liftToJSON #-}
2203−
2204− liftToEncoding t _ (Semigroup.Last x) = t x
2205− {-# INLINE liftToEncoding #-}
2206−
2207−instance ToJSON a => ToJSON (Semigroup.Last a) where
2208− toJSON = toJSON1
2209− {-# INLINE toJSON #-}
2210−
2211− toEncoding = toEncoding1
2212− {-# INLINE toEncoding #-}
2213−
2214−
2215−instance ToJSON1 Semigroup.WrappedMonoid where
2216− liftToJSON t _ (Semigroup.WrapMonoid x) = t x
2217− {-# INLINE liftToJSON #-}
2218−
2219− liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
2220− {-# INLINE liftToEncoding #-}
2221−
2222−instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
2223− toJSON = toJSON1
2224− {-# INLINE toJSON #-}
2225−
2226− toEncoding = toEncoding1
2227− {-# INLINE toEncoding #-}
2228−
2229−
2230−instance ToJSON1 Semigroup.Option where
2231− liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
2232− {-# INLINE liftToJSON #-}
2233−
2234− liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
2235− {-# INLINE liftToEncoding #-}
2236−
2237−instance ToJSON a => ToJSON (Semigroup.Option a) where
2074+instance ToJSON a => ToJSON (Last a) where
22382075 toJSON = toJSON1
22392076 {-# INLINE toJSON #-}
22402077
@@ -2245,13 +2082,6 @@ instance ToJSON a => ToJSON (Semigroup.Option a) where
22452082-- tagged
22462083-------------------------------------------------------------------------------
22472084
2248−instance ToJSON1 Proxy where
2249− liftToJSON _ _ _ = Null
2250− {-# INLINE liftToJSON #-}
2251−
2252− liftToEncoding _ _ _ = E.null_
2253− {-# INLINE liftToEncoding #-}
2254−
22552085instance ToJSON (Proxy a) where
22562086 toJSON _ = Null
22572087 {-# INLINE toJSON #-}
@@ -2260,13 +2090,6 @@ instance ToJSON (Proxy a) where
22602090 {-# INLINE toEncoding #-}
22612091
22622092
2263−instance ToJSON2 Tagged where
2264− liftToJSON2 _ _ t _ (Tagged x) = t x
2265− {-# INLINE liftToJSON2 #-}
2266−
2267− liftToEncoding2 _ _ t _ (Tagged x) = t x
2268− {-# INLINE liftToEncoding2 #-}
2269−
22702093instance ToJSON1 (Tagged a) where
22712094 liftToJSON t _ (Tagged x) = t x
22722095 {-# INLINE liftToJSON #-}
@@ -2281,6 +2104,7 @@ instance ToJSON b => ToJSON (Tagged a b) where
22812104 toEncoding = toEncoding1
22822105 {-# INLINE toEncoding #-}
22832106
2107+
22842108instance ToJSONKey b => ToJSONKey (Tagged a b) where
22852109 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
22862110 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -2301,7 +2125,7 @@ instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
23012125 toJSONKey = toJSONKeyList
23022126
23032127-------------------------------------------------------------------------------
2304−-- Tuple instances
2128+-- Tuple instances, see tuple-instances-to.hs
23052129-------------------------------------------------------------------------------
23062130
23072131instance ToJSON2 (,) where
@@ -2312,7 +2136,9 @@ instance ToJSON2 (,) where
23122136 return mv
23132137 {-# INLINE liftToJSON2 #-}
23142138
2315− liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
2139+ liftToEncoding2 toA _ toB _ (a, b) = tuple $
2140+ toA a >*<
2141+ toB b
23162142 {-# INLINE liftToEncoding2 #-}
23172143
23182144instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -2336,11 +2162,10 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
23362162 return mv
23372163 {-# INLINE liftToJSON2 #-}
23382164
2339− liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
2340− [ toEncoding a
2341− , toB b
2342− , toC c
2343− ]
2165+ liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2166+ toEncoding a >*<
2167+ toB b >*<
2168+ toC c
23442169 {-# INLINE liftToEncoding2 #-}
23452170
23462171instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -2365,12 +2190,11 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
23652190 return mv
23662191 {-# INLINE liftToJSON2 #-}
23672192
2368− liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
2369− [ toEncoding a
2370− , toEncoding b
2371− , toC c
2372− , toD d
2373− ]
2193+ liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2194+ toEncoding a >*<
2195+ toEncoding b >*<
2196+ toC c >*<
2197+ toD d
23742198 {-# INLINE liftToEncoding2 #-}
23752199
23762200instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -2396,13 +2220,12 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
23962220 return mv
23972221 {-# INLINE liftToJSON2 #-}
23982222
2399− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
2400− [ toEncoding a
2401− , toEncoding b
2402− , toEncoding c
2403− , toD d
2404− , toE e
2405− ]
2223+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2224+ toEncoding a >*<
2225+ toEncoding b >*<
2226+ toEncoding c >*<
2227+ toD d >*<
2228+ toE e
24062229 {-# INLINE liftToEncoding2 #-}
24072230
24082231instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
@@ -2429,14 +2252,13 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
24292252 return mv
24302253 {-# INLINE liftToJSON2 #-}
24312254
2432− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
2433− [ toEncoding a
2434− , toEncoding b
2435− , toEncoding c
2436− , toEncoding d
2437− , toE e
2438− , toF f
2439− ]
2255+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2256+ toEncoding a >*<
2257+ toEncoding b >*<
2258+ toEncoding c >*<
2259+ toEncoding d >*<
2260+ toE e >*<
2261+ toF f
24402262 {-# INLINE liftToEncoding2 #-}
24412263
24422264instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
@@ -2464,15 +2286,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
24642286 return mv
24652287 {-# INLINE liftToJSON2 #-}
24662288
2467− liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = E.list id
2468− [ toEncoding a
2469− , toEncoding b
2470− , toEncoding c
2471− , toEncoding d
2472− , toEncoding e
2473− , toF f
2474− , toG g
2475− ]
2289+ liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
2290+ toEncoding a >*<
2291+ toEncoding b >*<
2292+ toEncoding c >*<
2293+ toEncoding d >*<
2294+ toEncoding e >*<
2295+ toF f >*<
2296+ toG g
24762297 {-# INLINE liftToEncoding2 #-}
24772298
24782299instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
@@ -2501,16 +2322,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
25012322 return mv
25022323 {-# INLINE liftToJSON2 #-}
25032324
2504− liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = E.list id
2505− [ toEncoding a
2506− , toEncoding b
2507− , toEncoding c
2508− , toEncoding d
2509− , toEncoding e
2510− , toEncoding f
2511− , toG g
2512− , toH h
2513− ]
2325+ liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
2326+ toEncoding a >*<
2327+ toEncoding b >*<
2328+ toEncoding c >*<
2329+ toEncoding d >*<
2330+ toEncoding e >*<
2331+ toEncoding f >*<
2332+ toG g >*<
2333+ toH h
25142334 {-# INLINE liftToEncoding2 #-}
25152335
25162336instance (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 +2360,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
25402360 return mv
25412361 {-# INLINE liftToJSON2 #-}
25422362
2543− liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = E.list id
2544− [ toEncoding a
2545− , toEncoding b
2546− , toEncoding c
2547− , toEncoding d
2548− , toEncoding e
2549− , toEncoding f
2550− , toEncoding g
2551− , toH h
2552− , toI i
2553− ]
2363+ liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
2364+ toEncoding a >*<
2365+ toEncoding b >*<
2366+ toEncoding c >*<
2367+ toEncoding d >*<
2368+ toEncoding e >*<
2369+ toEncoding f >*<
2370+ toEncoding g >*<
2371+ toH h >*<
2372+ toI i
25542373 {-# INLINE liftToEncoding2 #-}
25552374
25562375instance (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 +2400,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
25812400 return mv
25822401 {-# INLINE liftToJSON2 #-}
25832402
2584− liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = E.list id
2585− [ toEncoding a
2586− , toEncoding b
2587− , toEncoding c
2588− , toEncoding d
2589− , toEncoding e
2590− , toEncoding f
2591− , toEncoding g
2592− , toEncoding h
2593− , toI i
2594− , toJ j
2595− ]
2403+ liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
2404+ toEncoding a >*<
2405+ toEncoding b >*<
2406+ toEncoding c >*<
2407+ toEncoding d >*<
2408+ toEncoding e >*<
2409+ toEncoding f >*<
2410+ toEncoding g >*<
2411+ toEncoding h >*<
2412+ toI i >*<
2413+ toJ j
25962414 {-# INLINE liftToEncoding2 #-}
25972415
25982416instance (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 +2442,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
26242442 return mv
26252443 {-# INLINE liftToJSON2 #-}
26262444
2627− liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = E.list id
2628− [ toEncoding a
2629− , toEncoding b
2630− , toEncoding c
2631− , toEncoding d
2632− , toEncoding e
2633− , toEncoding f
2634− , toEncoding g
2635− , toEncoding h
2636− , toEncoding i
2637− , toJ j
2638− , toK k
2639− ]
2445+ liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
2446+ toEncoding a >*<
2447+ toEncoding b >*<
2448+ toEncoding c >*<
2449+ toEncoding d >*<
2450+ toEncoding e >*<
2451+ toEncoding f >*<
2452+ toEncoding g >*<
2453+ toEncoding h >*<
2454+ toEncoding i >*<
2455+ toJ j >*<
2456+ toK k
26402457 {-# INLINE liftToEncoding2 #-}
26412458
26422459instance (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 +2486,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
26692486 return mv
26702487 {-# INLINE liftToJSON2 #-}
26712488
2672− liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = E.list id
2673− [ toEncoding a
2674− , toEncoding b
2675− , toEncoding c
2676− , toEncoding d
2677− , toEncoding e
2678− , toEncoding f
2679− , toEncoding g
2680− , toEncoding h
2681− , toEncoding i
2682− , toEncoding j
2683− , toK k
2684− , toL l
2685− ]
2489+ liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
2490+ toEncoding a >*<
2491+ toEncoding b >*<
2492+ toEncoding c >*<
2493+ toEncoding d >*<
2494+ toEncoding e >*<
2495+ toEncoding f >*<
2496+ toEncoding g >*<
2497+ toEncoding h >*<
2498+ toEncoding i >*<
2499+ toEncoding j >*<
2500+ toK k >*<
2501+ toL l
26862502 {-# INLINE liftToEncoding2 #-}
26872503
26882504instance (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 +2532,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
27162532 return mv
27172533 {-# INLINE liftToJSON2 #-}
27182534
2719− liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = E.list id
2720− [ toEncoding a
2721− , toEncoding b
2722− , toEncoding c
2723− , toEncoding d
2724− , toEncoding e
2725− , toEncoding f
2726− , toEncoding g
2727− , toEncoding h
2728− , toEncoding i
2729− , toEncoding j
2730− , toEncoding k
2731− , toL l
2732− , toM m
2733− ]
2535+ liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
2536+ toEncoding a >*<
2537+ toEncoding b >*<
2538+ toEncoding c >*<
2539+ toEncoding d >*<
2540+ toEncoding e >*<
2541+ toEncoding f >*<
2542+ toEncoding g >*<
2543+ toEncoding h >*<
2544+ toEncoding i >*<
2545+ toEncoding j >*<
2546+ toEncoding k >*<
2547+ toL l >*<
2548+ toM m
27342549 {-# INLINE liftToEncoding2 #-}
27352550
27362551instance (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 +2580,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
27652580 return mv
27662581 {-# INLINE liftToJSON2 #-}
27672582
2768− liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = E.list id
2769− [ toEncoding a
2770− , toEncoding b
2771− , toEncoding c
2772− , toEncoding d
2773− , toEncoding e
2774− , toEncoding f
2775− , toEncoding g
2776− , toEncoding h
2777− , toEncoding i
2778− , toEncoding j
2779− , toEncoding k
2780− , toEncoding l
2781− , toM m
2782− , toN n
2783− ]
2583+ liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
2584+ toEncoding a >*<
2585+ toEncoding b >*<
2586+ toEncoding c >*<
2587+ toEncoding d >*<
2588+ toEncoding e >*<
2589+ toEncoding f >*<
2590+ toEncoding g >*<
2591+ toEncoding h >*<
2592+ toEncoding i >*<
2593+ toEncoding j >*<
2594+ toEncoding k >*<
2595+ toEncoding l >*<
2596+ toM m >*<
2597+ toN n
27842598 {-# INLINE liftToEncoding2 #-}
27852599
27862600instance (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 +2630,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
28162630 return mv
28172631 {-# INLINE liftToJSON2 #-}
28182632
2819− liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = E.list id
2820− [ toEncoding a
2821− , toEncoding b
2822− , toEncoding c
2823− , toEncoding d
2824− , toEncoding e
2825− , toEncoding f
2826− , toEncoding g
2827− , toEncoding h
2828− , toEncoding i
2829− , toEncoding j
2830− , toEncoding k
2831− , toEncoding l
2832− , toEncoding m
2833− , toN n
2834− , toO o
2835− ]
2633+ liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
2634+ toEncoding a >*<
2635+ toEncoding b >*<
2636+ toEncoding c >*<
2637+ toEncoding d >*<
2638+ toEncoding e >*<
2639+ toEncoding f >*<
2640+ toEncoding g >*<
2641+ toEncoding h >*<
2642+ toEncoding i >*<
2643+ toEncoding j >*<
2644+ toEncoding k >*<
2645+ toEncoding l >*<
2646+ toEncoding m >*<
2647+ toN n >*<
2648+ toO o
28362649 {-# INLINE liftToEncoding2 #-}
28372650
28382651instance (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
@@ -2847,26 +2660,27 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
28472660 toEncoding = toEncoding2
28482661 {-# INLINE toEncoding #-}
28492662
2850−--------------------------------------------------------------------------------
2851−
2852−-- | Wrap a list of pairs as an object.
2853−class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2854− fromPairs :: pairs -> enc
2855−
2856−instance (a ~ Value) => FromPairs (Encoding' a) Series where
2857− fromPairs = E.pairs
2858−
2859−instance FromPairs Value (DList Pair) where
2860− fromPairs = object . toList
2663+-------------------------------------------------------------------------------
2664+-- pre-bytestring-0.10 compatibility
2665+-------------------------------------------------------------------------------
28612666
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
2667+{-# INLINE lazyToStrictByteString #-}
2668+lazyToStrictByteString :: L.ByteString -> S.ByteString
2669+#if MIN_VERSION_bytestring(0,10,0)
2670+lazyToStrictByteString = L.toStrict
2671+#else
2672+lazyToStrictByteString = packChunks
28672673
2868−instance (v ~ Value) => KeyValuePair v (DList Pair) where
2869− pair k v = DList.singleton (pack k .= v)
2674+-- packChunks is taken from the blaze-builder package.
28702675
2871−instance (e ~ Encoding) => KeyValuePair e Series where
2872− pair = E.pairStr
2676+-- | Pack the chunks of a lazy bytestring into a single strict bytestring.
2677+packChunks :: L.ByteString -> S.ByteString
2678+packChunks lbs = do
2679+ S.unsafeCreate (fromIntegral $ L.length lbs) (copyChunks lbs)
2680+ where
2681+ copyChunks !L.Empty !_pf = return ()
2682+ copyChunks !(L.Chunk (S.PS fpbuf o l) lbs') !pf = do
2683+ withForeignPtr fpbuf $ \pbuf ->
2684+ copyBytes pf (pbuf `plusPtr` o) l
2685+ copyChunks lbs' (pf `plusPtr` l)
2686+#endif