@@ -1,25 +1,21 @@
1−{-# LANGUAGE BangPatterns #-}
21{-# LANGUAGE CPP #-}
3−{-# LANGUAGE DataKinds #-}
42{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE EmptyDataDecls #-}
54{-# LANGUAGE FlexibleContexts #-}
65{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE FunctionalDependencies #-}
77{-# LANGUAGE GADTs #-}
8−{-# LANGUAGE MultiParamTypeClasses #-}
9−{-# LANGUAGE NamedFieldPuns #-}
8+{-# LANGUAGE NoImplicitPrelude #-}
109{-# LANGUAGE OverloadedStrings #-}
1110{-# LANGUAGE PatternGuards #-}
11+{-# LANGUAGE PolyKinds #-}
1212{-# LANGUAGE RecordWildCards #-}
1313{-# LANGUAGE ScopedTypeVariables #-}
1414{-# LANGUAGE TypeOperators #-}
15−{-# LANGUAGE TypeSynonymInstances #-}
1615{-# LANGUAGE UndecidableInstances #-}
1716
18−#if __GLASGOW_HASKELL__ >= 706
19−{-# LANGUAGE PolyKinds #-}
20−#endif
21−
2217#include "overlapping-compat.h"
18+#include "incoherent-compat.h"
2319
2420-- TODO: Drop this when we remove support for Data.Attoparsec.Number
2521{-# OPTIONS_GHC -fno-warn-deprecations #-}
@@ -37,7 +33,6 @@ module Data.Aeson.Types.ToJSON
3733 , toEncoding2
3834 -- * Generic JSON classes
3935 , GToJSON(..)
40− , GToEncoding(..)
4136 , ToArgs(..)
4237 , genericToJSON
4338 , genericToEncoding
@@ -48,53 +43,63 @@ module Data.Aeson.Types.ToJSON
4843 , ToJSONKeyFunction(..)
4944 , toJSONKeyText
5045 , contramapToJSONKeyFunction
46+
47+ , GToJSONKey()
48+ , genericToJSONKey
49+
5150 -- * Object key-value pairs
5251 , KeyValue(..)
52+ , KeyValuePair(..)
53+ , FromPairs(..)
5354 -- * Functions needed for documentation
5455 -- * Encoding functions
5556 , listEncoding
5657 , listValue
5758 ) where
5859
59−import Prelude ()
6060import Prelude.Compat
6161
6262import Control.Applicative (Const(..))
6363import Control.Monad.ST (ST)
64−import Data.Aeson.Encoding.Internal ((>*<), (><), Encoding, Encoding', Series, dict, emptyArray_, tuple)
64+import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
65+import Data.Aeson.Encoding.Internal ((>*<))
6566import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
66−import Data.Aeson.Types.Generic
67+import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
6768import Data.Aeson.Types.Internal
6869import Data.Attoparsec.Number (Number(..))
6970import Data.Bits (unsafeShiftR)
7071import Data.DList (DList)
71−import Data.Fixed (Fixed, HasResolution)
72+import Data.Fixed (Fixed, HasResolution, Nano)
7273import Data.Foldable (toList)
7374import Data.Functor.Compose (Compose(..))
75+import Data.Functor.Contravariant (Contravariant (..))
7476import Data.Functor.Identity (Identity(..))
7577import Data.Functor.Product (Product(..))
7678import Data.Functor.Sum (Sum(..))
7779import Data.Int (Int16, Int32, Int64, Int8)
7880import Data.List (intersperse)
7981import Data.List.NonEmpty (NonEmpty(..))
80−import Data.Monoid ((<>), Dual(..), First(..), Last(..))
82+import Data.Semigroup ((<>))
8183import Data.Proxy (Proxy(..))
8284import Data.Ratio (Ratio, denominator, numerator)
8385import Data.Scientific (Scientific)
8486import Data.Tagged (Tagged(..))
8587import Data.Text (Text, pack)
86−import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
87−import Data.Time.Format (FormatTime, formatTime)
88−import Data.Time.Locale.Compat (defaultTimeLocale)
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)
8993import Data.Vector (Vector)
9094import Data.Version (Version, showVersion)
95+import Data.Void (Void, absurd)
9196import Data.Word (Word16, Word32, Word64, Word8)
9297import Foreign.Storable (Storable)
98+import Foreign.C.Types (CTime (..))
9399import GHC.Generics
94100import Numeric.Natural (Natural)
95−-- We need internal here for generic deriving
96−import qualified Data.Aeson.Encoding.Internal as E
97−import qualified Data.ByteString as S
101+import qualified Data.Aeson.Encoding as E
102+import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
98103import qualified Data.ByteString.Lazy as L
99104import qualified Data.DList as DList
100105import qualified Data.HashMap.Strict as H
@@ -103,13 +108,16 @@ import qualified Data.IntMap as IntMap
103108import qualified Data.IntSet as IntSet
104109import qualified Data.List.NonEmpty as NE
105110import qualified Data.Map as M
111+import qualified Data.Monoid as Monoid
106112import qualified Data.Scientific as Scientific
113+import qualified Data.Semigroup as Semigroup
107114import qualified Data.Sequence as Seq
108115import qualified Data.Set as Set
109116import qualified Data.Text as T
110117import qualified Data.Text.Encoding as T
111118import qualified Data.Text.Lazy as LT
112119import qualified Data.Tree as Tree
120+import qualified Data.UUID.Types as UUID
113121import qualified Data.Vector as V
114122import qualified Data.Vector.Generic as VG
115123import qualified Data.Vector.Mutable as VM
@@ -117,12 +125,19 @@ import qualified Data.Vector.Primitive as VP
117125import qualified Data.Vector.Storable as VS
118126import qualified Data.Vector.Unboxed as VU
119127
120−#if !(MIN_VERSION_bytestring(0,10,0))
121−import Foreign.ForeignPtr (withForeignPtr)
122−import Foreign.Marshal.Utils (copyBytes)
123−import Foreign.Ptr (plusPtr)
124−import qualified Data.ByteString.Internal as S
125−import qualified Data.ByteString.Lazy.Internal as L
128+import qualified Data.Aeson.Encoding.Builder as EB
129+import qualified Data.ByteString.Builder as B
130+
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
135+
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
126141#endif
127142
128143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
@@ -141,19 +156,16 @@ realFloatToJSON d
141156
142157-- | Class of generic representation types that can be converted to
143158-- JSON.
144−class GToJSON arity f where
159+class GToJSON enc arity f where
145160 -- | This method (applied to 'defaultOptions') is used as the
146− -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
147− -- or 'liftToJSON' (if the @arity@ is 'One').
148− gToJSON :: Options -> ToArgs Value arity a -> f a -> Value
149−
150−-- | Class of generic representation types that can be converted to
151−-- a JSON 'Encoding'.
152−class GToEncoding arity f where
153− -- | This method (applied to 'defaultOptions') can be used as the
154− -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
155− -- or 'liftToEncoding' (if the @arity@ is 'One').
156− gToEncoding :: Options -> ToArgs Encoding arity a -> f a -> Encoding
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
157169
158170-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
159171-- function arguments that encode occurrences of the type parameter (for
@@ -165,14 +177,14 @@ data ToArgs res arity a where
165177-- | A configurable generic JSON creator. This function applied to
166178-- 'defaultOptions' is used as the default for 'toJSON' when the type
167179-- is an instance of 'Generic'.
168−genericToJSON :: (Generic a, GToJSON Zero (Rep a))
180+genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
169181 => Options -> a -> Value
170182genericToJSON opts = gToJSON opts NoToArgs . from
171183
172184-- | A configurable generic JSON creator. This function applied to
173185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
174186-- is an instance of 'Generic1'.
175−genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
187+genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
176188 => Options -> (a -> Value) -> ([a] -> Value)
177189 -> f a -> Value
178190genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
@@ -180,17 +192,17 @@ genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
180192-- | A configurable generic JSON encoder. This function applied to
181193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
182194-- is an instance of 'Generic'.
183−genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
195+genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
184196 => Options -> a -> Encoding
185−genericToEncoding opts = gToEncoding opts NoToArgs . from
197+genericToEncoding opts = gToJSON opts NoToArgs . from
186198
187199-- | A configurable generic JSON encoder. This function applied to
188200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
189201-- is an instance of 'Generic1'.
190−genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
202+genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
191203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
192204 -> f a -> Encoding
193−genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
205+genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
194206
195207-------------------------------------------------------------------------------
196208-- Class
@@ -198,6 +210,9 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
198210
199211-- | A type that can be converted to JSON.
200212--
213+-- Instances in general /must/ specify 'toJSON' and /should/ (but don't need
214+-- to) specify 'toEncoding'.
215+--
201216-- An example type and instance:
202217--
203218-- @
@@ -206,10 +221,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
206221--
207222-- data Coord = Coord { x :: Double, y :: Double }
208223--
209−-- instance ToJSON Coord where
210−-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
224+-- instance 'ToJSON' Coord where
225+-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
211226--
212−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
227+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
213228-- @
214229--
215230-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -217,17 +232,15 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
217232--
218233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
219234-- instance at compile time. The generated instance is optimized for your type
220−-- so will probably be more efficient than the following two options:
235+-- so it will probably be more efficient than the following option.
221236--
222237-- * The compiler can provide a default generic implementation for
223238-- 'toJSON'.
224239--
225240-- To use the second, simply add a @deriving 'Generic'@ clause to your
226−-- datatype and declare a 'ToJSON' instance for your datatype without giving
227−-- definitions for 'toJSON' or 'toEncoding'.
228−--
229−-- For example, the previous example can be simplified to a more
230−-- minimal instance:
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):
231244--
232245-- @
233246-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -236,35 +249,46 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
236249--
237250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
238251--
239−-- instance ToJSON Coord where
240−-- toEncoding = 'genericToEncoding' 'defaultOptions'
252+-- instance 'ToJSON' Coord where
253+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
241254-- @
242255--
243−-- Why do we provide an implementation for 'toEncoding' here? The
244−-- 'toEncoding' function is a relatively new addition to this class.
245−-- To allow users of older versions of this library to upgrade without
246−-- having to edit all of their instances or encounter surprising
247−-- incompatibilities, the default implementation of 'toEncoding' uses
248−-- 'toJSON'. This produces correct results, but since it performs an
249−-- intermediate conversion to a 'Value', it will be less efficient
250−-- than directly emitting an 'Encoding'. Our one-liner definition of
251−-- 'toEncoding' above bypasses the intermediate 'Value'.
252−--
253−-- If @DefaultSignatures@ doesn't give exactly the results you want,
254−-- you can customize the generic encoding with only a tiny amount of
255−-- effort, using 'genericToJSON' and 'genericToEncoding' with your
256−-- preferred 'Options':
256+-- If on the other hand you wish to customize the generic decoding, you have
257+-- to implement both methods:
257258--
258259-- @
259−-- instance ToJSON Coord where
260−-- toJSON = 'genericToJSON' 'defaultOptions'
261−-- toEncoding = 'genericToEncoding' 'defaultOptions'
260+-- customOptions = 'defaultOptions'
261+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
262+-- }
263+--
264+-- instance 'ToJSON' Coord where
265+-- 'toJSON' = 'genericToJSON' customOptions
266+-- 'toEncoding' = 'genericToEncoding' customOptions
262267-- @
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.)
263287class ToJSON a where
264288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
265289 toJSON :: a -> Value
266290
267− default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
291+ default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
268292 toJSON = genericToJSON defaultOptions
269293
270294 -- | Encode a Haskell value as JSON.
@@ -281,8 +305,8 @@ class ToJSON a where
281305 -- extension, and then have GHC generate a method body as follows.
282306 --
283307 -- @
284− -- instance ToJSON Coord where
285− -- toEncoding = 'genericToEncoding' 'defaultOptions'
308+ -- instance 'ToJSON' Coord where
309+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
286310 -- @
287311
288312 toEncoding :: a -> Encoding
@@ -314,6 +338,13 @@ instance KeyValue Pair where
314338 name .= value = (name, toJSON value)
315339 {-# INLINE (.=) #-}
316340
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+
317348-------------------------------------------------------------------------------
318349-- Classes and types for map keys
319350-------------------------------------------------------------------------------
@@ -371,7 +402,7 @@ instance KeyValue Pair where
371402-- newtype wrapper around 'Text'. The recommended approach is to use
372403-- generalized newtype deriving:
373404--
374−-- > newtype RecordId = RecordId { getRecordId :: Text}
405+-- > newtype RecordId = RecordId { getRecordId :: Text }
375406-- > deriving (Eq,Ord,ToJSONKey)
376407--
377408-- Then we may write:
@@ -386,8 +417,18 @@ instance KeyValue Pair where
386417--
387418-- It is possible to get the 'ToJSONKey' instance for free as we did
388419-- with 'Foo'. However, in this case, we have a natural way to go to
389−-- and from 'Text' that does not require any escape sequences. So, in
390−-- this example, 'ToJSONKeyText' will be used instead of 'ToJSONKeyValue'.
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+--
391432-- The 'Show' instance can be used to help write 'ToJSONKey':
392433--
393434-- > instance ToJSONKey Color where
@@ -462,16 +503,42 @@ toJSONKeyTextEnc e = ToJSONKeyText tot e
462503 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
463504 tot = T.dropAround (== '"')
464505 . T.decodeLatin1
465− . lazyToStrictByteString
506+ . L.toStrict
466507 . E.encodingToLazyByteString
467508 . e
468509
510+instance Contravariant ToJSONKeyFunction where
511+ contramap = contramapToJSONKeyFunction
512+
469513-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
470514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
471515contramapToJSONKeyFunction h x = case x of
472516 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
473517 ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
474518
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+
475542-------------------------------------------------------------------------------
476543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
477544-------------------------------------------------------------------------------
@@ -484,7 +551,7 @@ contramapToJSONKeyFunction h x = case x of
484551--
485552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
486553-- instance at compile time. The generated instance is optimized for your type
487−-- so will probably be more efficient than the following two options:
554+-- so it will probably be more efficient than the following option.
488555--
489556-- * The compiler can provide a default generic implementation for
490557-- 'toJSON1'.
@@ -502,23 +569,29 @@ contramapToJSONKeyFunction h x = case x of
502569--
503570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
504571--
505−-- instance ToJSON a => ToJSON1 (Pair a)
572+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
506573-- @
507574--
508−-- If @DefaultSignatures@ doesn't give exactly the results you want,
575+-- If the default implementation doesn't give exactly the results you want,
509576-- you can customize the generic encoding with only a tiny amount of
510577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
511578-- your preferred 'Options':
512579--
513580-- @
514−-- instance ToJSON a => ToJSON1 (Pair a) where
515−-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
516−-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
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
517588-- @
589+--
590+-- See also 'ToJSON'.
518591class ToJSON1 f where
519592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
520593
521− default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
594+ default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
522595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
523596 liftToJSON = genericLiftToJSON defaultOptions
524597
@@ -527,7 +600,7 @@ class ToJSON1 f where
527600
528601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
529602
530− default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
603+ default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
531604 => (a -> Encoding) -> ([a] -> Encoding)
532605 -> f a -> Encoding
533606 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -581,7 +654,7 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
581654-- @
582655-- newtype F a = F [a]
583656--
584−-- -- This instance encodes String as an array of chars
657+-- -- This instance encodes 'String' as an array of chars
585658-- instance 'ToJSON1' F where
586659-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
587660-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
@@ -612,6 +685,11 @@ instance ToJSON1 [] where
612685 {-# INLINE liftToEncoding #-}
613686
614687instance (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+
615693 toJSON = toJSON1
616694 {-# INLINE toJSON #-}
617695
@@ -622,39 +700,80 @@ instance (ToJSON a) => ToJSON [a] where
622700-- Generic toJSON / toEncoding
623701-------------------------------------------------------------------------------
624702
625−--------------------------------------------------------------------------------
626−-- Generic toJSON
627−
628−instance OVERLAPPABLE_ (GToJSON arity a) => GToJSON arity (M1 i c a) where
703+instance OVERLAPPABLE_ (GToJSON enc arity a) => GToJSON enc arity (M1 i c a) where
629704 -- Meta-information, which is not handled elsewhere, is ignored:
630705 gToJSON opts targs = gToJSON opts targs . unM1
706+ {-# INLINE gToJSON #-}
631707
632−instance (ToJSON a) => GToJSON arity (K1 i a) where
633− -- Constant values are encoded using their ToJSON instance:
634− gToJSON _opts _ = toJSON . unK1
635−
636−instance GToJSON One Par1 where
708+instance GToJSON enc One Par1 where
637709 -- Direct occurrences of the last type parameter are encoded with the
638710 -- function passed in as an argument:
639711 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 #-}
640732
641−instance (ToJSON1 f) => GToJSON One (Rec1 f) where
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+
744+--------------------------------------------------------------------------------
745+-- Generic toJSON
746+
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 #-}
756+
757+instance ToJSON a => GToJSON Value arity (K1 i a) where
758+ -- Constant values are encoded using their ToJSON instance:
759+ gToJSON _opts _ = toJSON . unK1
760+ {-# INLINE gToJSON #-}
761+
762+instance ToJSON1 f => GToJSON Value One (Rec1 f) where
642763 -- Recursive occurrences of the last type parameter are encoded using their
643764 -- ToJSON1 instance:
644765 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766+ {-# INLINE gToJSON #-}
645767
646−instance GToJSON arity U1 where
768+instance GToJSON Value arity U1 where
647769 -- Empty constructors are encoded to an empty array:
648770 gToJSON _opts _ _ = emptyArray
649−
650−instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
651− -- Constructors need to be encoded differently depending on whether they're
652− -- a record or not. This distinction is made by 'consToJSON':
653− gToJSON opts targs = consToJSON opts targs . unM1
771+ {-# INLINE gToJSON #-}
654772
655773instance ( WriteProduct arity a, WriteProduct arity b
656774 , ProductSize a, ProductSize b
657− ) => GToJSON arity (a :*: b) where
775+ ) => GToJSON Value arity (a :*: b)
776+ where
658777 -- Products are encoded to an array. Here we allocate a mutable vector of
659778 -- the same size as the product and write the product's elements to it using
660779 -- 'writeProduct':
@@ -666,238 +785,175 @@ instance ( WriteProduct arity a, WriteProduct arity b
666785 where
667786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
668787 productSize
788+ {-# INLINE gToJSON #-}
669789
670−instance ( AllNullary (a :+: b) allNullary
671− , SumToJSON arity (a :+: b) allNullary
672− ) => GToJSON arity (a :+: b) where
673− -- If all constructors of a sum datatype are nullary and the
674− -- 'allNullaryToStringTag' option is set they are encoded to
675− -- strings. This distinction is made by 'sumToJSON':
676− gToJSON opts targs = (unTagged :: Tagged allNullary Value -> Value)
677− . sumToJSON opts targs
678−
679−instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
790+instance ( ToJSON1 f
791+ , GToJSON Value One g
792+ ) => GToJSON Value One (f :.: g)
793+ where
680794 -- If an occurrence of the last type parameter is nested inside two
681795 -- composed types, it is encoded by using the outermost type's ToJSON1
682796 -- instance to generically encode the innermost type:
683797 gToJSON opts targs =
684798 let gtj = gToJSON opts targs in
685799 liftToJSON gtj (listValue gtj) . unComp1
800+ {-# INLINE gToJSON #-}
686801
687802--------------------------------------------------------------------------------
688803-- Generic toEncoding
689804
690−instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
691− -- Meta-information, which is not handled elsewhere, is ignored:
692− gToEncoding opts targs = gToEncoding opts targs . unM1
693−
694−instance (ToJSON a) => GToEncoding arity (K1 i a) where
805+instance ToJSON a => GToJSON Encoding arity (K1 i a) where
695806 -- Constant values are encoded using their ToJSON instance:
696− gToEncoding _opts _ = toEncoding . unK1
697−
698−instance GToEncoding One Par1 where
699− -- Direct occurrences of the last type parameter are encoded with the
700− -- function passed in as an argument:
701− gToEncoding _opts (To1Args te _) = te . unPar1
807+ gToJSON _opts _ = toEncoding . unK1
808+ {-# INLINE gToJSON #-}
702809
703−instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
810+instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
704811 -- Recursive occurrences of the last type parameter are encoded using their
705812 -- ToEncoding1 instance:
706− gToEncoding _opts (To1Args te tel) = liftToEncoding te tel . unRec1
813+ gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814+ {-# INLINE gToJSON #-}
707815
708−instance GToEncoding arity U1 where
816+instance GToJSON Encoding arity U1 where
709817 -- Empty constructors are encoded to an empty array:
710− gToEncoding _opts _ _ = E.emptyArray_
711−
712−instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
713− -- Constructors need to be encoded differently depending on whether they're
714− -- a record or not. This distinction is made by 'consToEncoding':
715− gToEncoding opts targs = consToEncoding opts targs . unM1
818+ gToJSON _opts _ _ = E.emptyArray_
819+ {-# INLINE gToJSON #-}
716820
717821instance ( EncodeProduct arity a
718822 , EncodeProduct arity b
719− ) => GToEncoding arity (a :*: b) where
823+ ) => GToJSON Encoding arity (a :*: b)
824+ where
720825 -- Products are encoded to an array. Here we allocate a mutable vector of
721826 -- the same size as the product and write the product's elements to it using
722827 -- 'encodeProduct':
723− gToEncoding opts targs p = E.tuple $ encodeProduct opts targs p
828+ gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
829+ {-# INLINE gToJSON #-}
724830
725−instance ( AllNullary (a :+: b) allNullary
726− , SumToEncoding arity (a :+: b) allNullary
727− ) => GToEncoding arity (a :+: b) where
728− -- If all constructors of a sum datatype are nullary and the
729− -- 'allNullaryToStringTag' option is set they are encoded to
730− -- strings. This distinction is made by 'sumToEncoding':
731− gToEncoding opts targs
732− = (unTagged :: Tagged allNullary Encoding -> Encoding)
733− . sumToEncoding opts targs
734−
735−instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
831+instance ( ToJSON1 f
832+ , GToJSON Encoding One g
833+ ) => GToJSON Encoding One (f :.: g)
834+ where
736835 -- If an occurrence of the last type parameter is nested inside two
737836 -- composed types, it is encoded by using the outermost type's ToJSON1
738837 -- instance to generically encode the innermost type:
739− gToEncoding opts targs =
740− let gte = gToEncoding opts targs in
838+ gToJSON opts targs =
839+ let gte = gToJSON opts targs in
741840 liftToEncoding gte (listEncoding gte) . unComp1
841+ {-# INLINE gToJSON #-}
742842
743843--------------------------------------------------------------------------------
744844
745−class SumToJSON arity f allNullary where
746− sumToJSON :: Options -> ToArgs Value arity a
747− -> f a -> Tagged allNullary Value
748−
749−instance ( GetConName f
750− , TaggedObjectPairs arity f
751− , ObjectWithSingleFieldObj arity f
752− , TwoElemArrayObj arity f
753− , UntaggedValueObj arity f
754− ) => SumToJSON arity f True where
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
755857 sumToJSON opts targs
756− | allNullaryToStringTag opts = Tagged . String . pack
858+ | allNullaryToStringTag opts = Tagged . fromString
757859 . constructorTagModifier opts . getConName
758860 | otherwise = Tagged . nonAllNullarySumToJSON opts targs
759861
760−instance ( TwoElemArrayObj arity f
761− , TaggedObjectPairs arity f
762− , ObjectWithSingleFieldObj arity f
763− , UntaggedValueObj arity f
764− ) => SumToJSON arity f False where
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
765868 sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
766869
767−nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
768− , TaggedObjectPairs arity f
769− , ObjectWithSingleFieldObj arity f
770− , UntaggedValueObj arity f
771− ) => Options -> ToArgs Value arity a
772− -> f a -> Value
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
773876nonAllNullarySumToJSON opts targs =
774877 case sumEncoding opts of
775− TaggedObject{..} ->
776− object . taggedObjectPairs opts targs tagFieldName contentsFieldName
777− ObjectWithSingleField -> Object . objectWithSingleFieldObj opts targs
778− TwoElemArray -> Array . twoElemArrayObj opts targs
779− UntaggedValue -> untaggedValueObj opts targs
780−
781−--------------------------------------------------------------------------------
782878
783−class SumToEncoding arity f allNullary where
784− sumToEncoding :: Options -> ToArgs Encoding arity a
785− -> f a -> Tagged allNullary Encoding
786−
787−instance ( GetConName f
788− , TaggedObjectEnc arity f
789− , ObjectWithSingleFieldEnc arity f
790− , TwoElemArrayEnc arity f
791− , UntaggedValueEnc arity f
792− ) => SumToEncoding arity f True where
793− sumToEncoding opts targs
794− | allNullaryToStringTag opts = Tagged . toEncoding .
795− constructorTagModifier opts . getConName
796− | otherwise = Tagged . nonAllNullarySumToEncoding opts targs
797−
798−instance ( TwoElemArrayEnc arity f
799− , TaggedObjectEnc arity f
800− , ObjectWithSingleFieldEnc arity f
801− , UntaggedValueEnc arity f
802− ) => SumToEncoding arity f False where
803− sumToEncoding opts targs = Tagged . nonAllNullarySumToEncoding opts targs
804−
805−nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
806− , TaggedObjectEnc arity f
807− , ObjectWithSingleFieldEnc arity f
808− , UntaggedValueEnc arity f
809− ) => Options -> ToArgs Encoding arity a
810− -> f a -> Encoding
811−nonAllNullarySumToEncoding opts targs =
812− case sumEncoding opts of
813879 TaggedObject{..} ->
814− taggedObjectEnc opts targs tagFieldName contentsFieldName
815− ObjectWithSingleField -> objectWithSingleFieldEnc opts targs
816− TwoElemArray -> twoElemArrayEnc opts targs
817− UntaggedValue -> untaggedValueEnc opts targs
880+ taggedObject opts targs tagFieldName contentsFieldName
818881
819−--------------------------------------------------------------------------------
882+ ObjectWithSingleField ->
883+ (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884+ . sumToJSON' opts targs
820885
821−class TaggedObjectPairs arity f where
822− taggedObjectPairs :: Options -> ToArgs Value arity a
823− -> String -> String
824− -> f a -> [Pair]
825−
826−instance ( TaggedObjectPairs arity a
827− , TaggedObjectPairs arity b
828− ) => TaggedObjectPairs arity (a :+: b) where
829− taggedObjectPairs opts targs tagFieldName contentsFieldName (L1 x) =
830− taggedObjectPairs opts targs tagFieldName contentsFieldName x
831− taggedObjectPairs opts targs tagFieldName contentsFieldName (R1 x) =
832− taggedObjectPairs opts targs tagFieldName contentsFieldName x
833−
834−instance ( IsRecord a isRecord
835− , TaggedObjectPairs' arity a isRecord
836− , Constructor c
837− ) => TaggedObjectPairs arity (C1 c a) where
838− taggedObjectPairs opts targs tagFieldName contentsFieldName =
839− (pack tagFieldName .= constructorTagModifier opts
840− (conName (undefined :: t c a p)) :) .
841− (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
842− taggedObjectPairs' opts targs contentsFieldName . unM1
886+ TwoElemArray ->
887+ (unTagged :: Tagged TwoElemArray enc -> enc)
888+ . sumToJSON' opts targs
843889
844−class TaggedObjectPairs' arity f isRecord where
845− taggedObjectPairs' :: Options -> ToArgs Value arity a
846− -> String -> f a -> Tagged isRecord [Pair]
890+ UntaggedValue ->
891+ (unTagged :: Tagged UntaggedValue enc -> enc)
892+ . sumToJSON' opts targs
847893
848−instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
849− taggedObjectPairs' _ _ _ _ = Tagged []
894+--------------------------------------------------------------------------------
850895
851−instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
852− taggedObjectPairs' opts targs _ =
853− Tagged . toList . recordToPairs opts targs
896+class FromString enc where
897+ fromString :: String -> enc
854898
855−instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
856− taggedObjectPairs' opts targs contentsFieldName =
857− Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts targs
899+instance FromString Encoding where
900+ fromString = toEncoding
901+
902+instance FromString Value where
903+ fromString = String . pack
858904
859905--------------------------------------------------------------------------------
860906
861−class TaggedObjectEnc arity f where
862− taggedObjectEnc :: Options -> ToArgs Encoding arity a
863− -> String -> String
864− -> f a -> Encoding
865−
866−instance ( TaggedObjectEnc arity a
867− , TaggedObjectEnc arity b
868− ) => TaggedObjectEnc arity (a :+: b) where
869− taggedObjectEnc opts targs tagFieldName contentsFieldName (L1 x) =
870− taggedObjectEnc opts targs tagFieldName contentsFieldName x
871− taggedObjectEnc opts targs tagFieldName contentsFieldName (R1 x) =
872− taggedObjectEnc opts targs tagFieldName contentsFieldName x
873−
874−instance ( IsRecord a isRecord
875− , TaggedObjectEnc' arity a isRecord
907+class TaggedObject enc arity f where
908+ taggedObject :: Options -> ToArgs enc arity a
909+ -> String -> String
910+ -> f a -> enc
911+
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
876926 , Constructor c
877− ) => TaggedObjectEnc arity (C1 c a) where
878− taggedObjectEnc opts targs tagFieldName contentsFieldName v =
879− E.wrapObject $
880− (E.string tagFieldName ><
881− E.colon ><
882− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) ><
883− ((unTagged :: Tagged isRecord Encoding -> Encoding) .
884− taggedObjectEnc' opts targs contentsFieldName . unM1 $ v)
885−
886−class TaggedObjectEnc' arity f isRecord where
887− taggedObjectEnc' :: Options -> ToArgs Encoding arity a
888− -> String -> f a -> Tagged isRecord Encoding
889−
890−instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
891− taggedObjectEnc' _ _ _ _ = Tagged E.empty
892−
893−instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
894− taggedObjectEnc' opts targs _ = Tagged . (E.comma ><) . fst
895− . recordToEncoding opts targs
896−
897−instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
898− taggedObjectEnc' opts targs contentsFieldName =
899− Tagged . (\z -> E.comma >< toEncoding contentsFieldName >< E.colon >< z) .
900− gToEncoding opts targs
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
952+
953+instance ( RecordToPairs enc pairs arity f
954+ ) => TaggedObject' enc pairs arity f True
955+ where
956+ taggedObject' opts targs _ = Tagged . recordToPairs opts targs
901957
902958--------------------------------------------------------------------------------
903959
@@ -912,23 +968,38 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
912968instance (Constructor c) => GetConName (C1 c a) where
913969 getConName = conName
914970
971+-- For genericToJSONKey
972+instance GetConName a => GetConName (D1 d a) where
973+ getConName (M1 x) = getConName x
974+
915975--------------------------------------------------------------------------------
916976
917−class TwoElemArrayObj arity f where
918− twoElemArrayObj :: Options -> ToArgs Value arity a
919− -> f a -> V.Vector Value
977+-- Reflection of SumEncoding variants
978+
979+data ObjectWithSingleField
980+data TwoElemArray
981+data UntaggedValue
982+
983+--------------------------------------------------------------------------------
920984
921−instance ( TwoElemArrayObj arity a
922− , TwoElemArrayObj arity b
923− ) => TwoElemArrayObj arity (a :+: b) where
924− twoElemArrayObj opts targs (L1 x) = twoElemArrayObj opts targs x
925− twoElemArrayObj opts targs (R1 x) = twoElemArrayObj opts targs x
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+--------------------------------------------------------------------------------
926997
927−instance ( GToJSON arity a
928− , ConsToJSON arity a
998+instance ( GToJSON Value arity a
999+ , ConsToJSON Value arity a
9291000 , Constructor c
930− ) => TwoElemArrayObj arity (C1 c a) where
931− twoElemArrayObj opts targs x = V.create $ do
1001+ ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
1002+ sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
9321003 mv <- VM.unsafeNew 2
9331004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
9341005 $ conName (undefined :: t c a p)
@@ -937,148 +1008,121 @@ instance ( GToJSON arity a
9371008
9381009--------------------------------------------------------------------------------
9391010
940−class TwoElemArrayEnc arity f where
941− twoElemArrayEnc :: Options -> ToArgs Encoding arity a
942− -> f a -> Encoding
943−
944−instance ( TwoElemArrayEnc arity a
945− , TwoElemArrayEnc arity b
946− ) => TwoElemArrayEnc arity (a :+: b) where
947− twoElemArrayEnc opts targs (L1 x) = twoElemArrayEnc opts targs x
948− twoElemArrayEnc opts targs (R1 x) = twoElemArrayEnc opts targs x
949−
950−instance ( GToEncoding arity a
951− , ConsToEncoding arity a
1011+instance ( GToJSON Encoding arity a
1012+ , ConsToJSON Encoding arity a
9521013 , Constructor c
953− ) => TwoElemArrayEnc arity (C1 c a) where
954− twoElemArrayEnc opts targs x = E.tuple $
955− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
956− gToEncoding opts targs x
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+ ]
9571020
9581021--------------------------------------------------------------------------------
9591022
960−class ConsToJSON arity f where
961− consToJSON :: Options -> ToArgs Value arity a
962− -> f a -> Value
1023+class ConsToJSON enc arity f where
1024+ consToJSON :: Options -> ToArgs enc arity a
1025+ -> f a -> enc
9631026
964−class ConsToJSON' arity f isRecord where
965− consToJSON' :: Options -> ToArgs Value arity a
966− -> Bool -- ^ Are we a record with one field?
967− -> f a -> Tagged isRecord Value
1027+class ConsToJSON' enc arity f isRecord where
1028+ consToJSON' :: Options -> ToArgs enc arity a
1029+ -> f a -> Tagged isRecord enc
9681030
969−instance ( IsRecord f isRecord
970− , ConsToJSON' arity f isRecord
971− ) => ConsToJSON arity f where
1031+instance ( IsRecord f isRecord
1032+ , ConsToJSON' enc arity f isRecord
1033+ ) => ConsToJSON enc arity f
1034+ where
9721035 consToJSON opts targs =
973− (unTagged :: Tagged isRecord Value -> Value)
974− . consToJSON' opts targs (isUnary (undefined :: f a))
975−
976−instance (RecordToPairs arity f) => ConsToJSON' arity f True where
977− consToJSON' opts targs isUn f = let
978− vals = toList $ recordToPairs opts targs f
979− in case (unwrapUnaryRecords opts,isUn,vals) of
980− (True,True,[(_,val)]) -> Tagged val
981− _ -> Tagged $ object vals
982−
983−instance GToJSON arity f => ConsToJSON' arity f False where
984− consToJSON' opts targs _ = Tagged . gToJSON opts targs
985−
986−--------------------------------------------------------------------------------
1036+ (unTagged :: Tagged isRecord enc -> enc)
1037+ . consToJSON' opts targs
1038+ {-# INLINE consToJSON #-}
9871039
988−class ConsToEncoding arity f where
989− consToEncoding :: Options -> ToArgs Encoding arity a
990− -> f a -> Encoding
991−
992−class ConsToEncoding' arity f isRecord where
993− consToEncoding' :: Options -> ToArgs Encoding arity a
994− -> Bool -- ^ Are we a record with one field?
995− -> f a -> Tagged isRecord Encoding
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' #-}
9961057
997−instance ( IsRecord f isRecord
998− , ConsToEncoding' arity f isRecord
999− ) => ConsToEncoding arity f where
1000− consToEncoding opts targs =
1001− (unTagged :: Tagged isRecord Encoding -> Encoding)
1002− . consToEncoding' opts targs (isUnary (undefined :: f a))
1003−
1004−instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
1005− consToEncoding' opts targs isUn x =
1006− let (enc, mbVal) = recordToEncoding opts targs x
1007− in case (unwrapUnaryRecords opts, isUn, mbVal) of
1008− (True, True, Just val) -> Tagged val
1009− _ -> Tagged $ E.wrapObject enc
1010−
1011−instance GToEncoding arity f => ConsToEncoding' arity f False where
1012− consToEncoding' opts targs _ = Tagged . gToEncoding opts targs
1058+instance GToJSON enc arity f => ConsToJSON' enc arity f False where
1059+ consToJSON' opts targs = Tagged . gToJSON opts targs
1060+ {-# INLINE consToJSON' #-}
10131061
10141062--------------------------------------------------------------------------------
10151063
1016−class RecordToPairs arity f where
1017− recordToPairs :: Options -> ToArgs Value arity a
1018− -> f a -> DList Pair
1019−
1020−instance ( RecordToPairs arity a
1021− , RecordToPairs arity b
1022− ) => RecordToPairs arity (a :*: b) where
1023− recordToPairs opts targs (a :*: b) = recordToPairs opts targs a <>
1024− recordToPairs opts targs b
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
10251070
1026−instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
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
10271088 recordToPairs = fieldToPair
1028−
1029−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1030− RecordToPairs arity (S1 s (K1 i (Maybe a))) where
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
10311098 recordToPairs opts _ (M1 k1) | omitNothingFields opts
1032− , K1 Nothing <- k1 = DList.empty
1099+ , K1 Nothing <- k1 = mempty
10331100 recordToPairs opts targs m1 = fieldToPair opts targs m1
1034−
1035−fieldToPair :: (Selector s, GToJSON arity a)
1036− => Options -> ToArgs Value arity p
1037− -> S1 s a p -> DList Pair
1038−fieldToPair opts targs m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
1039− , gToJSON opts targs (unM1 m1)
1040− )
1041−
1042−--------------------------------------------------------------------------------
1043−
1044−class RecordToEncoding arity f where
1045− -- 1st element: whole thing
1046− -- 2nd element: in case the record has only 1 field, just the value
1047− -- of the field (without the key); 'Nothing' otherwise
1048− recordToEncoding :: Options -> ToArgs Encoding arity a
1049− -> f a -> (Encoding, Maybe Encoding)
1050−
1051−instance ( RecordToEncoding arity a
1052− , RecordToEncoding arity b
1053− ) => RecordToEncoding arity (a :*: b) where
1054− recordToEncoding opts targs (a :*: b) | omitNothingFields opts =
1055− (E.econcat $ intersperse E.comma $
1056− filter (not . E.nullEncoding)
1057− [ fst (recordToEncoding opts targs a)
1058− , fst (recordToEncoding opts targs b) ]
1059− , Nothing)
1060− recordToEncoding opts targs (a :*: b) =
1061− (fst (recordToEncoding opts targs a) >< E.comma ><
1062− fst (recordToEncoding opts targs b),
1063− Nothing)
1064−
1065−
1066−instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1067− recordToEncoding = fieldToEncoding
1068−
1069−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1070− RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1071− recordToEncoding opts _ (M1 k1) | omitNothingFields opts
1072− , K1 Nothing <- k1 = (E.empty, Nothing)
1073− recordToEncoding opts targs m1 = fieldToEncoding opts targs m1
1074−
1075−fieldToEncoding :: (Selector s, GToEncoding arity a)
1076− => Options -> ToArgs Encoding arity p
1077− -> S1 s a p -> (Encoding, Maybe Encoding)
1078−fieldToEncoding opts targs m1 =
1079− let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1080− valueBuilder = gToEncoding opts targs (unM1 m1)
1081− in (keyBuilder >< E.colon >< valueBuilder, Just valueBuilder)
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 #-}
10821126
10831127--------------------------------------------------------------------------------
10841128
@@ -1101,10 +1145,12 @@ instance ( WriteProduct arity a
11011145 lenL = len `unsafeShiftR` 1
11021146 lenR = len - lenL
11031147 ixR = ix + lenL
1148+ {-# INLINE writeProduct #-}
11041149
1105−instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
1150+instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
11061151 writeProduct opts targs mv ix _ =
11071152 VM.unsafeWrite mv ix . gToJSON opts targs
1153+ {-# INLINE writeProduct #-}
11081154
11091155--------------------------------------------------------------------------------
11101156
@@ -1119,114 +1165,44 @@ instance ( EncodeProduct arity a
11191165 E.econcat $ intersperse E.comma $
11201166 filter (not . E.nullEncoding)
11211167 [encodeProduct opts targs a, encodeProduct opts targs b]
1122− encodeProduct opts targs (a :*: b) = encodeProduct opts targs a >*<
1123− encodeProduct opts targs b
1168+ encodeProduct opts targs (a :*: b) =
1169+ encodeProduct opts targs a >*<
1170+ encodeProduct opts targs b
1171+ {-# INLINE encodeProduct #-}
11241172
1125−instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1126− encodeProduct opts targs a = E.retagEncoding $ gToEncoding opts targs a
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 #-}
11271176
11281177--------------------------------------------------------------------------------
11291178
1130−class ObjectWithSingleFieldObj arity f where
1131− objectWithSingleFieldObj :: Options -> ToArgs Value arity a
1132− -> f a -> Object
1133−
1134−instance ( ObjectWithSingleFieldObj arity a
1135− , ObjectWithSingleFieldObj arity b
1136− ) => ObjectWithSingleFieldObj arity (a :+: b) where
1137− objectWithSingleFieldObj opts targs (L1 x) =
1138− objectWithSingleFieldObj opts targs x
1139− objectWithSingleFieldObj opts targs (R1 x) =
1140− objectWithSingleFieldObj opts targs x
1141−
1142−instance ( GToJSON arity a
1143− , ConsToJSON arity a
1179+instance ( GToJSON enc arity a
1180+ , ConsToJSON enc arity a
1181+ , FromPairs enc pairs
1182+ , KeyValuePair enc pairs
11441183 , Constructor c
1145− ) => ObjectWithSingleFieldObj arity (C1 c a) where
1146− objectWithSingleFieldObj opts targs = H.singleton typ . gToJSON opts targs
1184+ ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1185+ where
1186+ sumToJSON' opts targs =
1187+ Tagged . fromPairs . (typ `pair`) . gToJSON opts targs
11471188 where
1148− typ = pack $ constructorTagModifier opts $
1189+ typ = constructorTagModifier opts $
11491190 conName (undefined :: t c a p)
11501191
11511192--------------------------------------------------------------------------------
11521193
1153−class ObjectWithSingleFieldEnc arity f where
1154− objectWithSingleFieldEnc :: Options -> ToArgs Encoding arity a
1155− -> f a -> Encoding
1156−
1157−instance ( ObjectWithSingleFieldEnc arity a
1158− , ObjectWithSingleFieldEnc arity b
1159− ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1160− objectWithSingleFieldEnc opts targs (L1 x) =
1161− objectWithSingleFieldEnc opts targs x
1162− objectWithSingleFieldEnc opts targs (R1 x) =
1163− objectWithSingleFieldEnc opts targs x
1164−
1165−instance ( GToEncoding arity a
1166− , ConsToEncoding arity a
1167− , Constructor c
1168− ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1169− objectWithSingleFieldEnc opts targs v =
1170− E.openCurly ><
1171− toEncoding
1172− (constructorTagModifier opts
1173− (conName (undefined :: t c a p))) ><
1174− E.colon ><
1175− gToEncoding opts targs v ><
1176− E.closeCurly
1177−
1178−--------------------------------------------------------------------------------
1179−
1180−class UntaggedValueObj arity f where
1181− untaggedValueObj :: Options -> ToArgs Value arity a
1182− -> f a -> Value
1183−
1184−instance
1185− ( UntaggedValueObj arity a
1186− , UntaggedValueObj arity b
1187− ) => UntaggedValueObj arity (a :+: b)
1188− where
1189− untaggedValueObj opts targs (L1 x) = untaggedValueObj opts targs x
1190− untaggedValueObj opts targs (R1 x) = untaggedValueObj opts targs x
1191−
11921194instance OVERLAPPABLE_
1193− ( GToJSON arity a
1194− , ConsToJSON arity a
1195− ) => UntaggedValueObj arity (C1 c a) where
1196− untaggedValueObj = gToJSON
1197−
1198−instance OVERLAPPING_
1199− ( Constructor c )
1200− => UntaggedValueObj arity (C1 c U1)
1195+ ( ConsToJSON enc arity a
1196+ ) => SumToJSON' UntaggedValue enc arity (C1 c a)
12011197 where
1202− untaggedValueObj opts _ _ = toJSON $
1203− constructorTagModifier opts $ conName (undefined :: t c U1 p)
1204−
1205−--------------------------------------------------------------------------------
1206−
1207−class UntaggedValueEnc arity f where
1208− untaggedValueEnc :: Options -> ToArgs Encoding arity a
1209− -> f a -> Encoding
1210−instance
1211− ( UntaggedValueEnc arity a
1212− , UntaggedValueEnc arity b
1213− ) => UntaggedValueEnc arity (a :+: b)
1214− where
1215− untaggedValueEnc opts targs (L1 x) = untaggedValueEnc opts targs x
1216− untaggedValueEnc opts targs (R1 x) = untaggedValueEnc opts targs x
1217−
1218−instance OVERLAPPABLE_
1219− ( GToEncoding arity a
1220− , ConsToEncoding arity a
1221− ) => UntaggedValueEnc arity (C1 c a)
1222− where
1223− untaggedValueEnc = gToEncoding
1198+ sumToJSON' opts targs = Tagged . gToJSON opts targs
12241199
12251200instance OVERLAPPING_
1226− ( Constructor c )
1227− => UntaggedValueEnc arity (C1 c U1)
1201+ ( Constructor c
1202+ , FromString enc
1203+ ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
12281204 where
1229− untaggedValueEnc opts _ _ = toEncoding $
1205+ sumToJSON' opts _ _ = Tagged . fromString $
12301206 constructorTagModifier opts $ conName (undefined :: t c U1 p)
12311207
12321208-------------------------------------------------------------------------------
@@ -1300,6 +1276,13 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
13001276 toEncoding = toEncoding2
13011277 {-# INLINE toEncoding #-}
13021278
1279+instance ToJSON Void where
1280+ toJSON = absurd
1281+ {-# INLINE toJSON #-}
1282+
1283+ toEncoding = absurd
1284+ {-# INLINE toEncoding #-}
1285+
13031286
13041287instance ToJSON Bool where
13051288 toJSON = Bool
@@ -1485,7 +1468,6 @@ instance ToJSONKey Int64 where
14851468 toJSONKey = toJSONKeyTextEnc E.int64Text
14861469 {-# INLINE toJSONKey #-}
14871470
1488−
14891471instance ToJSON Word where
14901472 toJSON = Number . fromIntegral
14911473 {-# INLINE toJSON #-}
@@ -1545,6 +1527,12 @@ instance ToJSONKey Word64 where
15451527 toJSONKey = toJSONKeyTextEnc E.word64Text
15461528 {-# INLINE toJSONKey #-}
15471529
1530+instance ToJSON CTime where
1531+ toJSON (CTime i) = toJSON i
1532+ {-# INLINE toJSON #-}
1533+
1534+ toEncoding (CTime i) = toEncoding i
1535+ {-# INLINE toEncoding #-}
15481536
15491537instance ToJSON Text where
15501538 toJSON = String
@@ -1659,7 +1647,7 @@ instance (ToJSON a) => ToJSON (Identity a) where
16591647 toEncodingList = liftToEncodingList toEncoding toEncodingList
16601648 {-# INLINE toEncodingList #-}
16611649
1662−instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1650+instance (ToJSONKey a) => ToJSONKey (Identity a) where
16631651 toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
16641652 toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
16651653
@@ -1808,13 +1796,11 @@ instance ToJSONKey k => ToJSON1 (M.Map k) where
18081796 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
18091797 {-# INLINE liftToJSON #-}
18101798
1811− -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
18121799 liftToEncoding g _ = case toJSONKey of
18131800 ToJSONKeyText _ f -> dict f g M.foldrWithKey
18141801 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
18151802 where
1816− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1817− pairEncoding f (a, b) = tuple $ f a >*< g b
1803+ pairEncoding f (a, b) = E.list id [f a, g b]
18181804 {-# INLINE liftToEncoding #-}
18191805
18201806
@@ -1852,6 +1838,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
18521838 toEncoding = toEncoding1
18531839 {-# INLINE toEncoding #-}
18541840
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
18551852
18561853-------------------------------------------------------------------------------
18571854-- vector
@@ -1865,6 +1862,8 @@ instance ToJSON1 Vector where
18651862 {-# INLINE liftToEncoding #-}
18661863
18671864instance (ToJSON a) => ToJSON (Vector a) where
1865+ {-# SPECIALIZE instance ToJSON Array #-}
1866+
18681867 toJSON = toJSON1
18691868 {-# INLINE toJSON #-}
18701869
@@ -1932,11 +1931,12 @@ instance ToJSONKey k => ToJSON1 (H.HashMap k) where
19321931 ToJSONKeyText _ f -> dict f g H.foldrWithKey
19331932 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
19341933 where
1935− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1936− pairEncoding f (a, b) = tuple $ f a >*< g b
1934+ pairEncoding f (a, b) = E.list id [f a, g b]
19371935 {-# INLINE liftToEncoding #-}
19381936
19391937instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1938+ {-# SPECIALIZE instance ToJSON Object #-}
1939+
19401940 toJSON = toJSON1
19411941 {-# INLINE toJSON #-}
19421942
@@ -1966,6 +1966,32 @@ dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
19661966formatMillis :: (FormatTime t) => t -> String
19671967formatMillis = take 3 . formatTime defaultTimeLocale "%q"
19681968
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+
19691995-------------------------------------------------------------------------------
19701996-- time
19711997-------------------------------------------------------------------------------
@@ -2014,10 +2040,11 @@ stringEncoding :: Encoding' Text -> Value
20142040stringEncoding = String
20152041 . T.dropAround (== '"')
20162042 . T.decodeLatin1
2017− . lazyToStrictByteString
2043+ . L.toStrict
20182044 . E.encodingToLazyByteString
20192045{-# INLINE stringEncoding #-}
20202046
2047+
20212048instance ToJSON NominalDiffTime where
20222049 toJSON = Number . realToFrac
20232050 {-# INLINE toJSON #-}
@@ -2025,18 +2052,159 @@ instance ToJSON NominalDiffTime where
20252052 toEncoding = E.scientific . realToFrac
20262053 {-# INLINE toEncoding #-}
20272054
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+
20282107-------------------------------------------------------------------------------
20292108-- base Monoid/Semigroup
20302109-------------------------------------------------------------------------------
20312110
2032−instance ToJSON1 Dual where
2033− liftToJSON t _ = t . getDual
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
2143+ {-# INLINE liftToJSON #-}
2144+
2145+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2146+ {-# INLINE liftToEncoding #-}
2147+
2148+instance ToJSON a => ToJSON (Monoid.Last a) where
2149+ toJSON = toJSON1
2150+ {-# INLINE toJSON #-}
2151+
2152+ toEncoding = toEncoding1
2153+ {-# INLINE toEncoding #-}
2154+
2155+
2156+instance ToJSON1 Semigroup.Min where
2157+ liftToJSON t _ (Semigroup.Min x) = t x
2158+ {-# INLINE liftToJSON #-}
2159+
2160+ liftToEncoding t _ (Semigroup.Min x) = t x
2161+ {-# INLINE liftToEncoding #-}
2162+
2163+instance ToJSON a => ToJSON (Semigroup.Min a) where
2164+ toJSON = toJSON1
2165+ {-# INLINE toJSON #-}
2166+
2167+ toEncoding = toEncoding1
2168+ {-# INLINE toEncoding #-}
2169+
2170+
2171+instance ToJSON1 Semigroup.Max where
2172+ liftToJSON t _ (Semigroup.Max x) = t x
2173+ {-# INLINE liftToJSON #-}
2174+
2175+ liftToEncoding t _ (Semigroup.Max x) = t x
2176+ {-# INLINE liftToEncoding #-}
2177+
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
20342202 {-# INLINE liftToJSON #-}
20352203
2036− liftToEncoding t _ = t . getDual
2204+ liftToEncoding t _ (Semigroup.Last x) = t x
20372205 {-# INLINE liftToEncoding #-}
20382206
2039−instance ToJSON a => ToJSON (Dual a) where
2207+instance ToJSON a => ToJSON (Semigroup.Last a) where
20402208 toJSON = toJSON1
20412209 {-# INLINE toJSON #-}
20422210
@@ -2044,14 +2212,14 @@ instance ToJSON a => ToJSON (Dual a) where
20442212 {-# INLINE toEncoding #-}
20452213
20462214
2047−instance ToJSON1 First where
2048− liftToJSON t to' = liftToJSON t to' . getFirst
2215+instance ToJSON1 Semigroup.WrappedMonoid where
2216+ liftToJSON t _ (Semigroup.WrapMonoid x) = t x
20492217 {-# INLINE liftToJSON #-}
20502218
2051− liftToEncoding t to' = liftToEncoding t to' . getFirst
2219+ liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
20522220 {-# INLINE liftToEncoding #-}
20532221
2054−instance ToJSON a => ToJSON (First a) where
2222+instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
20552223 toJSON = toJSON1
20562224 {-# INLINE toJSON #-}
20572225
@@ -2059,14 +2227,14 @@ instance ToJSON a => ToJSON (First a) where
20592227 {-# INLINE toEncoding #-}
20602228
20612229
2062−instance ToJSON1 Last where
2063− liftToJSON t to' = liftToJSON t to' . getLast
2230+instance ToJSON1 Semigroup.Option where
2231+ liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
20642232 {-# INLINE liftToJSON #-}
20652233
2066− liftToEncoding t to' = liftToEncoding t to' . getLast
2234+ liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
20672235 {-# INLINE liftToEncoding #-}
20682236
2069−instance ToJSON a => ToJSON (Last a) where
2237+instance ToJSON a => ToJSON (Semigroup.Option a) where
20702238 toJSON = toJSON1
20712239 {-# INLINE toJSON #-}
20722240
@@ -2077,6 +2245,13 @@ instance ToJSON a => ToJSON (Last a) where
20772245-- tagged
20782246-------------------------------------------------------------------------------
20792247
2248+instance ToJSON1 Proxy where
2249+ liftToJSON _ _ _ = Null
2250+ {-# INLINE liftToJSON #-}
2251+
2252+ liftToEncoding _ _ _ = E.null_
2253+ {-# INLINE liftToEncoding #-}
2254+
20802255instance ToJSON (Proxy a) where
20812256 toJSON _ = Null
20822257 {-# INLINE toJSON #-}
@@ -2085,6 +2260,13 @@ instance ToJSON (Proxy a) where
20852260 {-# INLINE toEncoding #-}
20862261
20872262
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+
20882270instance ToJSON1 (Tagged a) where
20892271 liftToJSON t _ (Tagged x) = t x
20902272 {-# INLINE liftToJSON #-}
@@ -2099,7 +2281,6 @@ instance ToJSON b => ToJSON (Tagged a b) where
20992281 toEncoding = toEncoding1
21002282 {-# INLINE toEncoding #-}
21012283
2102−
21032284instance ToJSONKey b => ToJSONKey (Tagged a b) where
21042285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
21052286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -2120,7 +2301,7 @@ instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
21202301 toJSONKey = toJSONKeyList
21212302
21222303-------------------------------------------------------------------------------
2123−-- Tuple instances, see tuple-instances-to.hs
2304+-- Tuple instances
21242305-------------------------------------------------------------------------------
21252306
21262307instance ToJSON2 (,) where
@@ -2131,9 +2312,7 @@ instance ToJSON2 (,) where
21312312 return mv
21322313 {-# INLINE liftToJSON2 #-}
21332314
2134− liftToEncoding2 toA _ toB _ (a, b) = tuple $
2135− toA a >*<
2136− toB b
2315+ liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
21372316 {-# INLINE liftToEncoding2 #-}
21382317
21392318instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -2157,10 +2336,11 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
21572336 return mv
21582337 {-# INLINE liftToJSON2 #-}
21592338
2160− liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2161− toEncoding a >*<
2162− toB b >*<
2163− toC c
2339+ liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
2340+ [ toEncoding a
2341+ , toB b
2342+ , toC c
2343+ ]
21642344 {-# INLINE liftToEncoding2 #-}
21652345
21662346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -2185,11 +2365,12 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
21852365 return mv
21862366 {-# INLINE liftToJSON2 #-}
21872367
2188− liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2189− toEncoding a >*<
2190− toEncoding b >*<
2191− toC c >*<
2192− toD d
2368+ liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
2369+ [ toEncoding a
2370+ , toEncoding b
2371+ , toC c
2372+ , toD d
2373+ ]
21932374 {-# INLINE liftToEncoding2 #-}
21942375
21952376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -2215,12 +2396,13 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
22152396 return mv
22162397 {-# INLINE liftToJSON2 #-}
22172398
2218− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2219− toEncoding a >*<
2220− toEncoding b >*<
2221− toEncoding c >*<
2222− toD d >*<
2223− toE e
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+ ]
22242406 {-# INLINE liftToEncoding2 #-}
22252407
22262408instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
@@ -2247,13 +2429,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
22472429 return mv
22482430 {-# INLINE liftToJSON2 #-}
22492431
2250− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2251− toEncoding a >*<
2252− toEncoding b >*<
2253− toEncoding c >*<
2254− toEncoding d >*<
2255− toE e >*<
2256− toF f
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+ ]
22572440 {-# INLINE liftToEncoding2 #-}
22582441
22592442instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
@@ -2281,14 +2464,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
22812464 return mv
22822465 {-# INLINE liftToJSON2 #-}
22832466
2284− liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
2285− toEncoding a >*<
2286− toEncoding b >*<
2287− toEncoding c >*<
2288− toEncoding d >*<
2289− toEncoding e >*<
2290− toF f >*<
2291− toG g
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+ ]
22922476 {-# INLINE liftToEncoding2 #-}
22932477
22942478instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
@@ -2317,15 +2501,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
23172501 return mv
23182502 {-# INLINE liftToJSON2 #-}
23192503
2320− liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
2321− toEncoding a >*<
2322− toEncoding b >*<
2323− toEncoding c >*<
2324− toEncoding d >*<
2325− toEncoding e >*<
2326− toEncoding f >*<
2327− toG g >*<
2328− toH h
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+ ]
23292514 {-# INLINE liftToEncoding2 #-}
23302515
23312516instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
@@ -2355,16 +2540,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
23552540 return mv
23562541 {-# INLINE liftToJSON2 #-}
23572542
2358− liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
2359− toEncoding a >*<
2360− toEncoding b >*<
2361− toEncoding c >*<
2362− toEncoding d >*<
2363− toEncoding e >*<
2364− toEncoding f >*<
2365− toEncoding g >*<
2366− toH h >*<
2367− toI i
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+ ]
23682554 {-# INLINE liftToEncoding2 #-}
23692555
23702556instance (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
@@ -2395,17 +2581,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
23952581 return mv
23962582 {-# INLINE liftToJSON2 #-}
23972583
2398− liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
2399− toEncoding a >*<
2400− toEncoding b >*<
2401− toEncoding c >*<
2402− toEncoding d >*<
2403− toEncoding e >*<
2404− toEncoding f >*<
2405− toEncoding g >*<
2406− toEncoding h >*<
2407− toI i >*<
2408− toJ j
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+ ]
24092596 {-# INLINE liftToEncoding2 #-}
24102597
24112598instance (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
@@ -2437,18 +2624,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
24372624 return mv
24382625 {-# INLINE liftToJSON2 #-}
24392626
2440− liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
2441− toEncoding a >*<
2442− toEncoding b >*<
2443− toEncoding c >*<
2444− toEncoding d >*<
2445− toEncoding e >*<
2446− toEncoding f >*<
2447− toEncoding g >*<
2448− toEncoding h >*<
2449− toEncoding i >*<
2450− toJ j >*<
2451− toK k
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+ ]
24522640 {-# INLINE liftToEncoding2 #-}
24532641
24542642instance (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
@@ -2481,19 +2669,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
24812669 return mv
24822670 {-# INLINE liftToJSON2 #-}
24832671
2484− liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
2485− toEncoding a >*<
2486− toEncoding b >*<
2487− toEncoding c >*<
2488− toEncoding d >*<
2489− toEncoding e >*<
2490− toEncoding f >*<
2491− toEncoding g >*<
2492− toEncoding h >*<
2493− toEncoding i >*<
2494− toEncoding j >*<
2495− toK k >*<
2496− toL l
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+ ]
24972686 {-# INLINE liftToEncoding2 #-}
24982687
24992688instance (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
@@ -2527,20 +2716,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
25272716 return mv
25282717 {-# INLINE liftToJSON2 #-}
25292718
2530− liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
2531− toEncoding a >*<
2532− toEncoding b >*<
2533− toEncoding c >*<
2534− toEncoding d >*<
2535− toEncoding e >*<
2536− toEncoding f >*<
2537− toEncoding g >*<
2538− toEncoding h >*<
2539− toEncoding i >*<
2540− toEncoding j >*<
2541− toEncoding k >*<
2542− toL l >*<
2543− toM m
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+ ]
25442734 {-# INLINE liftToEncoding2 #-}
25452735
25462736instance (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
@@ -2575,21 +2765,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
25752765 return mv
25762766 {-# INLINE liftToJSON2 #-}
25772767
2578− liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
2579− toEncoding a >*<
2580− toEncoding b >*<
2581− toEncoding c >*<
2582− toEncoding d >*<
2583− toEncoding e >*<
2584− toEncoding f >*<
2585− toEncoding g >*<
2586− toEncoding h >*<
2587− toEncoding i >*<
2588− toEncoding j >*<
2589− toEncoding k >*<
2590− toEncoding l >*<
2591− toM m >*<
2592− toN n
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+ ]
25932784 {-# INLINE liftToEncoding2 #-}
25942785
25952786instance (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
@@ -2625,22 +2816,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
26252816 return mv
26262817 {-# INLINE liftToJSON2 #-}
26272818
2628− liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
2629− toEncoding a >*<
2630− toEncoding b >*<
2631− toEncoding c >*<
2632− toEncoding d >*<
2633− toEncoding e >*<
2634− toEncoding f >*<
2635− toEncoding g >*<
2636− toEncoding h >*<
2637− toEncoding i >*<
2638− toEncoding j >*<
2639− toEncoding k >*<
2640− toEncoding l >*<
2641− toEncoding m >*<
2642− toN n >*<
2643− toO o
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+ ]
26442836 {-# INLINE liftToEncoding2 #-}
26452837
26462838instance (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
@@ -2655,27 +2847,26 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
26552847 toEncoding = toEncoding2
26562848 {-# INLINE toEncoding #-}
26572849
2658−-------------------------------------------------------------------------------
2659−-- pre-bytestring-0.10 compatibility
2660−-------------------------------------------------------------------------------
2850+--------------------------------------------------------------------------------
26612851
2662−{-# INLINE lazyToStrictByteString #-}
2663−lazyToStrictByteString :: L.ByteString -> S.ByteString
2664−#if MIN_VERSION_bytestring(0,10,0)
2665−lazyToStrictByteString = L.toStrict
2666−#else
2667−lazyToStrictByteString = packChunks
2852+-- | Wrap a list of pairs as an object.
2853+class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2854+ fromPairs :: pairs -> enc
26682855
2669−-- packChunks is taken from the blaze-builder package.
2856+instance (a ~ Value) => FromPairs (Encoding' a) Series where
2857+ fromPairs = E.pairs
26702858
2671−-- | Pack the chunks of a lazy bytestring into a single strict bytestring.
2672−packChunks :: L.ByteString -> S.ByteString
2673−packChunks lbs = do
2674− S.unsafeCreate (fromIntegral $ L.length lbs) (copyChunks lbs)
2675− where
2676− copyChunks !L.Empty !_pf = return ()
2677− copyChunks !(L.Chunk (S.PS fpbuf o l) lbs') !pf = do
2678− withForeignPtr fpbuf $ \pbuf ->
2679− copyBytes pf (pbuf `plusPtr` o) l
2680− copyChunks lbs' (pf `plusPtr` l)
2681−#endif
2859+instance FromPairs Value (DList Pair) where
2860+ fromPairs = object . toList
2861+
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
2867+
2868+instance (v ~ Value) => KeyValuePair v (DList Pair) where
2869+ pair k v = DList.singleton (pack k .= v)
2870+
2871+instance (e ~ Encoding) => KeyValuePair e Series where
2872+ pair = E.pairStr