@@ -1,30 +1,27 @@
1−{-# LANGUAGE BangPatterns #-}
1+{-# LANGUAGE CPP #-}
3−{-# LANGUAGE DataKinds #-}
4−{-# LANGUAGE DefaultSignatures #-}
2+{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE EmptyDataDecls #-}
5−{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleContexts #-}
6−{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE FunctionalDependencies #-}
7−{-# LANGUAGE GADTs #-}
7+{-# LANGUAGE GADTs #-}
8−{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE NoImplicitPrelude #-}
9−{-# LANGUAGE NamedFieldPuns #-}
10−{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE OverloadedStrings #-}
11−{-# LANGUAGE PatternGuards #-}
10+{-# LANGUAGE PatternGuards #-}
11+{-# LANGUAGE PolyKinds #-}
12−{-# LANGUAGE RecordWildCards #-}
12+{-# LANGUAGE RecordWildCards #-}
13−{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE ScopedTypeVariables #-}
14−{-# LANGUAGE TypeOperators #-}
14+{-# LANGUAGE TypeOperators #-}
15−{-# LANGUAGE TypeSynonymInstances #-}
16−{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE UndecidableInstances #-}
18−#if __GLASGOW_HASKELL__ >= 706
19−{-# LANGUAGE PolyKinds #-}
22#include "overlapping-compat.h"
17#include "overlapping-compat.h"
18+#include "incoherent-compat.h"
24-- TODO: Drop this when we remove support for Data.Attoparsec.Number
20-- TODO: Drop this when we remove support for Data.Attoparsec.Number
25{-# OPTIONS_GHC -fno-warn-deprecations #-}
21{-# OPTIONS_GHC -fno-warn-deprecations #-}
27−module Data.Aeson.Types.ToJSON (
23+module Data.Aeson.Types.ToJSON
28 -- * Core JSON classes
25 -- * Core JSON classes
29 ToJSON(..)
26 ToJSON(..)
30 -- * Liftings to unary and binary type constructors
27 -- * Liftings to unary and binary type constructors
@@ -36,7 +33,6 @@ module Data.Aeson.Types.ToJSON (
36 , toEncoding2
33 , toEncoding2
37 -- * Generic JSON classes
34 -- * Generic JSON classes
38 , GToJSON(..)
35 , GToJSON(..)
40 , ToArgs(..)
36 , ToArgs(..)
41 , genericToJSON
37 , genericToJSON
42 , genericToEncoding
38 , genericToEncoding
@@ -47,87 +43,101 @@ module Data.Aeson.Types.ToJSON (
47 , ToJSONKeyFunction(..)
43 , ToJSONKeyFunction(..)
48 , toJSONKeyText
44 , toJSONKeyText
49 , contramapToJSONKeyFunction
45 , contramapToJSONKeyFunction
50 -- * Object key-value pairs
50 -- * Object key-value pairs
51 , KeyValue(..)
51 , KeyValue(..)
52 -- * Functions needed for documentation
54 -- * Functions needed for documentation
53 -- * Encoding functions
55 -- * Encoding functions
54 , listEncoding
56 , listEncoding
55 , listValue
57 , listValue
59import Prelude.Compat
60import Prelude.Compat
62+import Control.Applicative (Const(..))
63+import Control.Monad.ST (ST)
61−import Data.Aeson.Encoding.Internal (Encoding, Encoding', Series, dict,
64+import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
62− emptyArray_, tuple, (>*<), (><))
65+import Data.Aeson.Encoding.Internal ((>*<))
63import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
66import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
64−import Data.Aeson.Types.Generic
67+import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
65import Data.Aeson.Types.Internal
68import Data.Aeson.Types.Internal
67−-- We need internal here for generic deriving
68−import qualified Data.Aeson.Encoding.Internal as E
70−import Control.Applicative (Const (..))
71−import Control.Monad.ST (ST)
72−import Data.Attoparsec.Number (Number (..))
69+import Data.Attoparsec.Number (Number(..))
73−import Data.Bits (unsafeShiftR)
70+import Data.Bits (unsafeShiftR)
74−import Data.DList (DList)
71+import Data.DList (DList)
75−import Data.Fixed (Fixed, HasResolution)
72+import Data.Fixed (Fixed, HasResolution, Nano)
76−import Data.Foldable (toList)
73+import Data.Foldable (toList)
77−import Data.Functor.Compose (Compose (..))
74+import Data.Functor.Compose (Compose(..))
75+import Data.Functor.Contravariant (Contravariant (..))
78−import Data.Functor.Identity (Identity (..))
76+import Data.Functor.Identity (Identity(..))
79−import Data.Functor.Product (Product (..))
77+import Data.Functor.Product (Product(..))
80−import Data.Functor.Sum (Sum (..))
78+import Data.Functor.Sum (Sum(..))
81−import Data.Int (Int16, Int32, Int64, Int8)
79+import Data.Int (Int16, Int32, Int64, Int8)
82−import Data.List (intersperse)
80+import Data.List (intersperse)
83−import Data.List.NonEmpty (NonEmpty (..))
81+import Data.List.NonEmpty (NonEmpty(..))
84−import Data.Monoid (Dual (..), First (..), Last (..), (<>))
82+import Data.Semigroup ((<>))
85−import Data.Proxy (Proxy (..))
83+import Data.Proxy (Proxy(..))
86−import Data.Ratio (Ratio, denominator, numerator)
84+import Data.Ratio (Ratio, denominator, numerator)
87−import Data.Scientific (Scientific)
85+import Data.Scientific (Scientific)
88−import Data.Tagged (Tagged (..))
86+import Data.Tagged (Tagged(..))
89−import Data.Text (Text, pack)
87+import Data.Text (Text, pack)
90−import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
88+import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
91− 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 (FormatTime, formatTime)
92+import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
93−import Data.Time.Locale.Compat (defaultTimeLocale)
94−import Data.Vector (Vector)
93+import Data.Vector (Vector)
95−import Data.Version (Version, showVersion)
94+import Data.Version (Version, showVersion)
95+import Data.Void (Void, absurd)
96−import Data.Word (Word16, Word32, Word64, Word8)
96+import Data.Word (Word16, Word32, Word64, Word8)
97−import Foreign.Storable (Storable)
97+import Foreign.Storable (Storable)
98+import Foreign.C.Types (CTime (..))
98import GHC.Generics
99import GHC.Generics
99−import Numeric.Natural (Natural)
100+import Numeric.Natural (Natural)
100−
101+import qualified Data.Aeson.Encoding as E
101−import qualified Data.ByteString as S
102+import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
102−import qualified Data.ByteString.Lazy as L
103+import qualified Data.ByteString.Lazy as L
103−import qualified Data.DList as DList
104+import qualified Data.DList as DList
104−import qualified Data.HashMap.Strict as H
105+import qualified Data.HashMap.Strict as H
105−import qualified Data.HashSet as HashSet
106+import qualified Data.HashSet as HashSet
106−import qualified Data.IntMap as IntMap
107+import qualified Data.IntMap as IntMap
107−import qualified Data.IntSet as IntSet
108+import qualified Data.IntSet as IntSet
108−import qualified Data.List.NonEmpty as NE
109+import qualified Data.List.NonEmpty as NE
109−import qualified Data.Map as M
110+import qualified Data.Map as M
111+import qualified Data.Monoid as Monoid
110−import qualified Data.Scientific as Scientific
112+import qualified Data.Scientific as Scientific
111−import qualified Data.Sequence as Seq
113+import qualified Data.Semigroup as Semigroup
114+import qualified Data.Sequence as Seq
112−import qualified Data.Set as Set
115+import qualified Data.Set as Set
113−import qualified Data.Text as T
116+import qualified Data.Text as T
114−import qualified Data.Text.Encoding as T
117+import qualified Data.Text.Encoding as T
115−import qualified Data.Text.Lazy as LT
118+import qualified Data.Text.Lazy as LT
116−import qualified Data.Tree as Tree
119+import qualified Data.Tree as Tree
117−import qualified Data.Vector as V
120+import qualified Data.UUID.Types as UUID
121+import qualified Data.Vector as V
118−import qualified Data.Vector.Generic as VG
122+import qualified Data.Vector.Generic as VG
119−import qualified Data.Vector.Mutable as VM
123+import qualified Data.Vector.Mutable as VM
120import qualified Data.Vector.Primitive as VP
124import qualified Data.Vector.Primitive as VP
121−import qualified Data.Vector.Storable as VS
125+import qualified Data.Vector.Storable as VS
122−import qualified Data.Vector.Unboxed as VU
126+import qualified Data.Vector.Unboxed as VU
128+import qualified Data.Aeson.Encoding.Builder as EB
129+import qualified Data.ByteString.Builder as B
124−#if !(MIN_VERSION_bytestring(0,10,0))
131+import qualified GHC.Exts as Exts
125−import Foreign.ForeignPtr (withForeignPtr)
132+import qualified Data.Primitive.Array as PM
126−import Foreign.Marshal.Utils (copyBytes)
133+import qualified Data.Primitive.SmallArray as PM
127−import Foreign.Ptr (plusPtr)
134+import qualified Data.Primitive.Types as PM
136+#if MIN_VERSION_primitive(0,6,4)
137+#if !MIN_VERSION_primitive(0,7,0)
129−import qualified Data.ByteString.Internal as S
138+import qualified Data.Primitive.UnliftedArray as PM
130−import qualified Data.ByteString.Lazy.Internal as L
140+import qualified Data.Primitive.PrimArray as PM
133toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
@@ -146,19 +156,16 @@ realFloatToJSON d
147-- | Class of generic representation types that can be converted to
157-- | Class of generic representation types that can be converted to
149−class GToJSON arity f where
159+class GToJSON enc arity f where
150 -- | This method (applied to 'defaultOptions') is used as the
160 -- | This method (applied to 'defaultOptions') is used as the
151− -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
161+ -- default generic implementation of 'toJSON'
162+ -- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
152− -- or 'liftToJSON' (if the @arity@ is 'One').
163+ -- and 'liftToJSON' (if the @arity@ is 'One').
153− gToJSON :: Options -> ToArgs Value arity a -> f a -> Value
164+ --
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')
165+ -- It also provides a generic implementation of 'toEncoding'
166+ -- (with @enc ~ 'Encoding'@ and @arity ~ 'Zero'@)
160− -- or 'liftToEncoding' (if the @arity@ is 'One').
167+ -- and 'liftToEncoding' (if the @arity@ is 'One').
161− gToEncoding :: Options -> ToArgs Encoding arity a -> f a -> Encoding
168+ gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
163-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
170-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
164-- function arguments that encode occurrences of the type parameter (for
171-- function arguments that encode occurrences of the type parameter (for
@@ -170,14 +177,14 @@ data ToArgs res arity a where
170-- | A configurable generic JSON creator. This function applied to
177-- | A configurable generic JSON creator. This function applied to
171-- 'defaultOptions' is used as the default for 'toJSON' when the type
178-- 'defaultOptions' is used as the default for 'toJSON' when the type
172-- is an instance of 'Generic'.
179-- is an instance of 'Generic'.
173−genericToJSON :: (Generic a, GToJSON Zero (Rep a))
180+genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
174 => Options -> a -> Value
181 => Options -> a -> Value
175genericToJSON opts = gToJSON opts NoToArgs . from
182genericToJSON opts = gToJSON opts NoToArgs . from
177-- | A configurable generic JSON creator. This function applied to
184-- | A configurable generic JSON creator. This function applied to
178-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
179-- is an instance of 'Generic1'.
186-- is an instance of 'Generic1'.
180−genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
187+genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
181 => Options -> (a -> Value) -> ([a] -> Value)
188 => Options -> (a -> Value) -> ([a] -> Value)
182 -> f a -> Value
189 -> f a -> Value
183genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
190genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
@@ -185,17 +192,17 @@ genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
185-- | A configurable generic JSON encoder. This function applied to
192-- | A configurable generic JSON encoder. This function applied to
186-- 'defaultOptions' is used as the default for 'toEncoding' when the type
193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
187-- is an instance of 'Generic'.
194-- is an instance of 'Generic'.
188−genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
195+genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
189 => Options -> a -> Encoding
196 => Options -> a -> Encoding
190−genericToEncoding opts = gToEncoding opts NoToArgs . from
197+genericToEncoding opts = gToJSON opts NoToArgs . from
192-- | A configurable generic JSON encoder. This function applied to
199-- | A configurable generic JSON encoder. This function applied to
193-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
194-- is an instance of 'Generic1'.
201-- is an instance of 'Generic1'.
195−genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
202+genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
196 => Options -> (a -> Encoding) -> ([a] -> Encoding)
203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
197 -> f a -> Encoding
204 -> f a -> Encoding
198−genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
205+genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
200-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
@@ -203,6 +210,9 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
204-- | A type that can be converted to JSON.
211-- | A type that can be converted to JSON.
213+-- Instances in general /must/ specify 'toJSON' and /should/ (but don't need
214+-- to) specify 'toEncoding'.
206-- An example type and instance:
216-- An example type and instance:
@@ -211,10 +221,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
212-- data Coord = Coord { x :: Double, y :: Double }
222-- data Coord = Coord { x :: Double, y :: Double }
214−-- instance ToJSON Coord where
224+-- instance 'ToJSON' Coord where
215−-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
225+-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
217−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
227+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
220-- Instead of manually writing your 'ToJSON' instance, there are two options
230-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -222,17 +232,15 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
223-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
224-- instance at compile time. The generated instance is optimized for your type
234-- instance at compile time. The generated instance is optimized for your type
225−-- so will probably be more efficient than the following two options:
235+-- so it will probably be more efficient than the following option.
227-- * The compiler can provide a default generic implementation for
237-- * The compiler can provide a default generic implementation for
228-- 'toJSON'.
238-- 'toJSON'.
230-- To use the second, simply add a @deriving 'Generic'@ clause to your
240-- To use the second, simply add a @deriving 'Generic'@ clause to your
231−-- datatype and declare a 'ToJSON' instance for your datatype without giving
241+-- datatype and declare a 'ToJSON' instance. If you require nothing other than
232−-- definitions for 'toJSON' or 'toEncoding'.
242+-- 'defaultOptions', it is sufficient to write (and this is the only
233−--
243+-- alternative where the default 'toJSON' implementation is sufficient):
234−-- For example, the previous example can be simplified to a more
238-- {-\# LANGUAGE DeriveGeneric \#-}
246-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -241,35 +249,46 @@ genericLiftToEncoding opts te tel = gToEncoding opts (To1Args te tel) . from1
242-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
244−-- instance ToJSON Coord where
252+-- instance 'ToJSON' Coord where
245−-- toEncoding = 'genericToEncoding' 'defaultOptions'
253+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
248−-- Why do we provide an implementation for 'toEncoding' here? The
256+-- If on the other hand you wish to customize the generic decoding, you have
249−-- 'toEncoding' function is a relatively new addition to this class.
257+-- to implement both methods:
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'.
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':
260+-- customOptions = 'defaultOptions'
261+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
264−-- instance ToJSON Coord where
264+-- instance 'ToJSON' Coord where
265−-- toJSON = 'genericToJSON' 'defaultOptions'
265+-- 'toJSON' = 'genericToJSON' customOptions
266−-- toEncoding = 'genericToEncoding' 'defaultOptions'
266+-- 'toEncoding' = 'genericToEncoding' customOptions
269+-- Previous versions of this library only had the 'toJSON' method. Adding
270+-- 'toEncoding' had two reasons:
272+-- 1. toEncoding is more efficient for the common case that the output of
273+-- 'toJSON' is directly serialized to a @ByteString@.
274+-- Further, expressing either method in terms of the other would be
277+-- 2. The choice of defaults allows a smooth transition for existing users:
278+-- Existing instances that do not define 'toEncoding' still
279+-- compile and have the correct semantics. This is ensured by making
280+-- the default implementation of 'toEncoding' use 'toJSON'. This produces
281+-- correct results, but since it performs an intermediate conversion to a
282+-- 'Value', it will be less efficient than directly emitting an 'Encoding'.
283+-- (this also means that specifying nothing more than
284+-- @instance ToJSON Coord@ would be sufficient as a generically decoding
285+-- instance, but there probably exists no good reason to not specify
286+-- 'toEncoding' in new instances.)
268class ToJSON a where
287class ToJSON a where
269 -- | Convert a Haskell value to a JSON-friendly intermediate type.
288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
270 toJSON :: a -> Value
289 toJSON :: a -> Value
272− default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
291+ default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
273 toJSON = genericToJSON defaultOptions
292 toJSON = genericToJSON defaultOptions
275 -- | Encode a Haskell value as JSON.
294 -- | Encode a Haskell value as JSON.
@@ -286,8 +305,8 @@ class ToJSON a where
286 -- extension, and then have GHC generate a method body as follows.
305 -- extension, and then have GHC generate a method body as follows.
289− -- instance ToJSON Coord where
308+ -- instance 'ToJSON' Coord where
290− -- toEncoding = 'genericToEncoding' 'defaultOptions'
309+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
293 toEncoding :: a -> Encoding
312 toEncoding :: a -> Encoding
@@ -319,6 +338,13 @@ instance KeyValue Pair where
319 name .= value = (name, toJSON value)
338 name .= value = (name, toJSON value)
320 {-# INLINE (.=) #-}
339 {-# INLINE (.=) #-}
341+-- | Constructs a singleton 'H.HashMap'. For calling functions that
342+-- demand an 'Object' for constructing objects. To be used in
343+-- conjunction with 'mconcat'. Prefer to use 'object' where possible.
344+instance KeyValue Object where
345+ name .= value = H.singleton name (toJSON value)
322-------------------------------------------------------------------------------
348-------------------------------------------------------------------------------
323-- Classes and types for map keys
349-- Classes and types for map keys
324-------------------------------------------------------------------------------
350-------------------------------------------------------------------------------
@@ -376,7 +402,7 @@ instance KeyValue Pair where
376-- newtype wrapper around 'Text'. The recommended approach is to use
402-- newtype wrapper around 'Text'. The recommended approach is to use
377-- generalized newtype deriving:
403-- generalized newtype deriving:
379−-- > newtype RecordId = RecordId { getRecordId :: Text}
405+-- > newtype RecordId = RecordId { getRecordId :: Text }
380-- > deriving (Eq,Ord,ToJSONKey)
406-- > deriving (Eq,Ord,ToJSONKey)
382-- Then we may write:
408-- Then we may write:
@@ -391,8 +417,18 @@ instance KeyValue Pair where
392-- It is possible to get the 'ToJSONKey' instance for free as we did
418-- It is possible to get the 'ToJSONKey' instance for free as we did
393-- with 'Foo'. However, in this case, we have a natural way to go to
419-- with 'Foo'. However, in this case, we have a natural way to go to
394−-- and from 'Text' that does not require any escape sequences. So, in
420+-- and from 'Text' that does not require any escape sequences. So
395−-- this example, 'ToJSONKeyText' will be used instead of 'ToJSONKeyValue'.
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:
426+-- instance 'ToJSONKey' Color where
427+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
430+-- === __Low-level implementations__
396-- The 'Show' instance can be used to help write 'ToJSONKey':
432-- The 'Show' instance can be used to help write 'ToJSONKey':
398-- > instance ToJSONKey Color where
434-- > instance ToJSONKey Color where
@@ -467,16 +503,42 @@ toJSONKeyTextEnc e = ToJSONKeyText tot e
467 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
503 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
468 tot = T.dropAround (== '"')
504 tot = T.dropAround (== '"')
469 . T.decodeLatin1
505 . T.decodeLatin1
470− . lazyToStrictByteString
506+ . L.toStrict
471 . E.encodingToLazyByteString
507 . E.encodingToLazyByteString
510+instance Contravariant ToJSONKeyFunction where
511+ contramap = contramapToJSONKeyFunction
474-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
513-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
475contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
476contramapToJSONKeyFunction h x = case x of
515contramapToJSONKeyFunction h x = case x of
477 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
516 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
478 ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
517 ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
519+-- 'toJSONKey' for 'Generic' types.
520+-- Deriving is supported for enumeration types, i.e. the sums of nullary
521+-- constructors. The names of constructors will be used as keys for JSON
524+-- See also 'genericFromJSONKey'.
529+-- data Color = Red | Green | Blue
530+-- deriving 'Generic'
532+-- instance 'ToJSONKey' Color where
533+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
535+genericToJSONKey :: (Generic a, GToJSONKey (Rep a))
536+ => JSONKeyOptions -> ToJSONKeyFunction a
537+genericToJSONKey opts = toJSONKeyText (pack . keyModifier opts . getConName . from)
539+class GetConName f => GToJSONKey f
540+instance GetConName f => GToJSONKey f
480-------------------------------------------------------------------------------
542-------------------------------------------------------------------------------
481-- Lifings of FromJSON and ToJSON to unary and binary type constructors
543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
482-------------------------------------------------------------------------------
544-------------------------------------------------------------------------------
@@ -489,7 +551,7 @@ contramapToJSONKeyFunction h x = case x of
490-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
491-- instance at compile time. The generated instance is optimized for your type
553-- instance at compile time. The generated instance is optimized for your type
492−-- so will probably be more efficient than the following two options:
554+-- so it will probably be more efficient than the following option.
494-- * The compiler can provide a default generic implementation for
556-- * The compiler can provide a default generic implementation for
495-- 'toJSON1'.
557-- 'toJSON1'.
@@ -507,23 +569,29 @@ contramapToJSONKeyFunction h x = case x of
508-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
510−-- instance ToJSON a => ToJSON1 (Pair a)
572+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
513−-- If @DefaultSignatures@ doesn't give exactly the results you want,
575+-- If the default implementation doesn't give exactly the results you want,
514-- you can customize the generic encoding with only a tiny amount of
576-- you can customize the generic encoding with only a tiny amount of
515-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
516-- your preferred 'Options':
578-- your preferred 'Options':
581+-- customOptions = 'defaultOptions'
582+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
519−-- instance ToJSON a => ToJSON1 (Pair a) where
585+-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
520−-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
586+-- 'liftToJSON' = 'genericLiftToJSON' customOptions
521−-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
587+-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
590+-- See also 'ToJSON'.
523class ToJSON1 f where
591class ToJSON1 f where
524 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
526− default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
594+ default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
527 => (a -> Value) -> ([a] -> Value) -> f a -> Value
595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
528 liftToJSON = genericLiftToJSON defaultOptions
596 liftToJSON = genericLiftToJSON defaultOptions
@@ -532,7 +600,7 @@ class ToJSON1 f where
533 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
535− default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
603+ default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
536 => (a -> Encoding) -> ([a] -> Encoding)
604 => (a -> Encoding) -> ([a] -> Encoding)
537 -> f a -> Encoding
605 -> f a -> Encoding
538 liftToEncoding = genericLiftToEncoding defaultOptions
606 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -586,7 +654,7 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
587-- newtype F a = F [a]
655-- newtype F a = F [a]
589−-- -- This instance encodes String as an array of chars
657+-- -- This instance encodes 'String' as an array of chars
590-- instance 'ToJSON1' F where
658-- instance 'ToJSON1' F where
591-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
659-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
592-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
660-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
@@ -617,6 +685,11 @@ instance ToJSON1 [] where
617 {-# INLINE liftToEncoding #-}
685 {-# INLINE liftToEncoding #-}
619instance (ToJSON a) => ToJSON [a] where
687instance (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] #-}
620 toJSON = toJSON1
693 toJSON = toJSON1
621 {-# INLINE toJSON #-}
694 {-# INLINE toJSON #-}
@@ -627,39 +700,80 @@ instance (ToJSON a) => ToJSON [a] where
627-- Generic toJSON / toEncoding
700-- Generic toJSON / toEncoding
628-------------------------------------------------------------------------------
701-------------------------------------------------------------------------------
630−--------------------------------------------------------------------------------
633−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
634 -- Meta-information, which is not handled elsewhere, is ignored:
704 -- Meta-information, which is not handled elsewhere, is ignored:
635 gToJSON opts targs = gToJSON opts targs . unM1
705 gToJSON opts targs = gToJSON opts targs . unM1
706+ {-# INLINE gToJSON #-}
637−instance (ToJSON a) => GToJSON arity (K1 i a) where
708+instance GToJSON enc One Par1 where
638− -- Constant values are encoded using their ToJSON instance:
639− gToJSON _opts _ = toJSON . unK1
641−instance GToJSON One Par1 where
642 -- Direct occurrences of the last type parameter are encoded with the
709 -- Direct occurrences of the last type parameter are encoded with the
643 -- function passed in as an argument:
710 -- function passed in as an argument:
644 gToJSON _opts (To1Args tj _) = tj . unPar1
711 gToJSON _opts (To1Args tj _) = tj . unPar1
712+ {-# INLINE gToJSON #-}
714+instance ( ConsToJSON enc arity a
715+ , AllNullary (C1 c a) allNullary
716+ , SumToJSON enc arity (C1 c a) allNullary
717+ ) => GToJSON enc arity (D1 d (C1 c a)) where
718+ -- The option 'tagSingleConstructors' determines whether to wrap
719+ -- a single-constructor type.
721+ | tagSingleConstructors opts = (unTagged :: Tagged allNullary enc -> enc)
722+ . sumToJSON opts targs
724+ | otherwise = consToJSON opts targs . unM1 . unM1
725+ {-# INLINE gToJSON #-}
727+instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
728+ -- Constructors need to be encoded differently depending on whether they're
729+ -- a record or not. This distinction is made by 'consToJSON':
730+ gToJSON opts targs = consToJSON opts targs . unM1
731+ {-# INLINE gToJSON #-}
733+instance ( AllNullary (a :+: b) allNullary
734+ , SumToJSON enc arity (a :+: b) allNullary
735+ ) => GToJSON enc arity (a :+: b)
737+ -- If all constructors of a sum datatype are nullary and the
738+ -- 'allNullaryToStringTag' option is set they are encoded to
739+ -- strings. This distinction is made by 'sumToJSON':
740+ gToJSON opts targs = (unTagged :: Tagged allNullary enc -> enc)
741+ . sumToJSON opts targs
742+ {-# INLINE gToJSON #-}
744+--------------------------------------------------------------------------------
747+-- Note: Refactoring 'ToJSON a' to 'ToJSON enc a' (and 'ToJSON1' similarly) is
748+-- possible but makes error messages a bit harder to understand for missing
751+instance GToJSON Value arity V1 where
752+ -- Empty values do not exist, which makes the job of formatting them
754+ gToJSON _ _ x = x `seq` error "case: V1"
755+ {-# INLINE gToJSON #-}
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 #-}
646−instance (ToJSON1 f) => GToJSON One (Rec1 f) where
762+instance ToJSON1 f => GToJSON Value One (Rec1 f) where
647 -- Recursive occurrences of the last type parameter are encoded using their
763 -- Recursive occurrences of the last type parameter are encoded using their
648 -- ToJSON1 instance:
764 -- ToJSON1 instance:
649 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
765 gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766+ {-# INLINE gToJSON #-}
651−instance GToJSON arity U1 where
768+instance GToJSON Value arity U1 where
652 -- Empty constructors are encoded to an empty array:
769 -- Empty constructors are encoded to an empty array:
653 gToJSON _opts _ _ = emptyArray
770 gToJSON _opts _ _ = emptyArray
654−
771+ {-# INLINE gToJSON #-}
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
660instance ( WriteProduct arity a, WriteProduct arity b
773instance ( WriteProduct arity a, WriteProduct arity b
661 , ProductSize a, ProductSize b
774 , ProductSize a, ProductSize b
662− ) => GToJSON arity (a :*: b) where
775+ ) => GToJSON Value arity (a :*: b)
663 -- Products are encoded to an array. Here we allocate a mutable vector of
777 -- Products are encoded to an array. Here we allocate a mutable vector of
664 -- the same size as the product and write the product's elements to it using
778 -- the same size as the product and write the product's elements to it using
665 -- 'writeProduct':
779 -- 'writeProduct':
@@ -671,230 +785,175 @@ instance ( WriteProduct arity a, WriteProduct arity b
672 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
673 productSize
787 productSize
788+ {-# INLINE gToJSON #-}
675−instance ( AllNullary (a :+: b) allNullary
790+instance ( ToJSON1 f
676− , SumToJSON arity (a :+: b) allNullary
791+ , GToJSON Value One g
677− ) => GToJSON arity (a :+: b) where
792+ ) => GToJSON Value One (f :.: g)
678− -- If all constructors of a sum datatype are nullary and the
793+ where
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
684−instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
685 -- If an occurrence of the last type parameter is nested inside two
794 -- If an occurrence of the last type parameter is nested inside two
686 -- composed types, it is encoded by using the outermost type's ToJSON1
795 -- composed types, it is encoded by using the outermost type's ToJSON1
687 -- instance to generically encode the innermost type:
796 -- instance to generically encode the innermost type:
688 gToJSON opts targs =
797 gToJSON opts targs =
689 let gtj = gToJSON opts targs in
798 let gtj = gToJSON opts targs in
690 liftToJSON gtj (listValue gtj) . unComp1
799 liftToJSON gtj (listValue gtj) . unComp1
800+ {-# INLINE gToJSON #-}
692--------------------------------------------------------------------------------
802--------------------------------------------------------------------------------
693-- Generic toEncoding
803-- Generic toEncoding
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
699−instance (ToJSON a) => GToEncoding arity (K1 i a) where
805+instance ToJSON a => GToJSON Encoding arity (K1 i a) where
700 -- Constant values are encoded using their ToJSON instance:
806 -- Constant values are encoded using their ToJSON instance:
701− gToEncoding _opts _ = toEncoding . unK1
807+ gToJSON _opts _ = toEncoding . unK1
808+ {-# INLINE gToJSON #-}
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
708−instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
810+instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
709 -- Recursive occurrences of the last type parameter are encoded using their
811 -- Recursive occurrences of the last type parameter are encoded using their
710 -- ToEncoding1 instance:
812 -- ToEncoding1 instance:
711− gToEncoding _opts (To1Args te tel) = liftToEncoding te tel . unRec1
813+ gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814+ {-# INLINE gToJSON #-}
713−instance GToEncoding arity U1 where
816+instance GToJSON Encoding arity U1 where
714 -- Empty constructors are encoded to an empty array:
817 -- Empty constructors are encoded to an empty array:
715− gToEncoding _opts _ _ = E.emptyArray_
818+ gToJSON _opts _ _ = E.emptyArray_
716−
819+ {-# INLINE gToJSON #-}
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
722instance ( EncodeProduct arity a
821instance ( EncodeProduct arity a
723 , EncodeProduct arity b
822 , EncodeProduct arity b
724− ) => GToEncoding arity (a :*: b) where
823+ ) => GToJSON Encoding arity (a :*: b)
725 -- Products are encoded to an array. Here we allocate a mutable vector of
825 -- Products are encoded to an array. Here we allocate a mutable vector of
726 -- the same size as the product and write the product's elements to it using
826 -- the same size as the product and write the product's elements to it using
727 -- 'encodeProduct':
827 -- 'encodeProduct':
728− 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 #-}
730−instance ( AllNullary (a :+: b) allNullary
831+instance ( ToJSON1 f
731− , SumToEncoding arity (a :+: b) allNullary
832+ , GToJSON Encoding One g
732− ) => GToEncoding arity (a :+: b) where
833+ ) => GToJSON Encoding One (f :.: g)
733− -- If all constructors of a sum datatype are nullary and the
834+ where
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
740−instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
741 -- If an occurrence of the last type parameter is nested inside two
835 -- If an occurrence of the last type parameter is nested inside two
742 -- composed types, it is encoded by using the outermost type's ToJSON1
836 -- composed types, it is encoded by using the outermost type's ToJSON1
743 -- instance to generically encode the innermost type:
837 -- instance to generically encode the innermost type:
744− gToEncoding opts targs =
838+ gToJSON opts targs =
745− let gte = gToEncoding opts targs in
839+ let gte = gToJSON opts targs in
746 liftToEncoding gte (listEncoding gte) . unComp1
840 liftToEncoding gte (listEncoding gte) . unComp1
841+ {-# INLINE gToJSON #-}
748--------------------------------------------------------------------------------
843--------------------------------------------------------------------------------
750−class SumToJSON arity f allNullary where
845+class SumToJSON enc arity f allNullary where
751− sumToJSON :: Options -> ToArgs Value arity a
846+ sumToJSON :: Options -> ToArgs enc arity a
752− -> f a -> Tagged allNullary Value
847+ -> f a -> Tagged allNullary enc
754−instance ( GetConName f
849+instance ( GetConName f
755− , TaggedObjectPairs arity f
850+ , FromString enc
756− , ObjectWithSingleFieldObj arity f
851+ , TaggedObject enc arity f
757− , TwoElemArrayObj arity f
852+ , SumToJSON' ObjectWithSingleField enc arity f
853+ , SumToJSON' TwoElemArray enc arity f
854+ , SumToJSON' UntaggedValue enc arity f
758− ) => SumToJSON arity f True where
855+ ) => SumToJSON enc arity f True
759 sumToJSON opts targs
857 sumToJSON opts targs
760− | allNullaryToStringTag opts = Tagged . String . pack
858+ | allNullaryToStringTag opts = Tagged . fromString
761 . constructorTagModifier opts . getConName
859 . constructorTagModifier opts . getConName
762 | otherwise = Tagged . nonAllNullarySumToJSON opts targs
860 | otherwise = Tagged . nonAllNullarySumToJSON opts targs
764−instance ( TwoElemArrayObj arity f
862+instance ( TaggedObject enc arity f
765− , TaggedObjectPairs arity f
863+ , SumToJSON' ObjectWithSingleField enc arity f
766− , ObjectWithSingleFieldObj arity f
864+ , SumToJSON' TwoElemArray enc arity f
865+ , SumToJSON' UntaggedValue enc arity f
767− ) => SumToJSON arity f False where
866+ ) => SumToJSON enc arity f False
768 sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
868 sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
770−nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
870+nonAllNullarySumToJSON :: ( TaggedObject enc arity f
771− , TaggedObjectPairs arity f
871+ , SumToJSON' ObjectWithSingleField enc arity f
772− , ObjectWithSingleFieldObj arity f
872+ , SumToJSON' TwoElemArray enc arity f
873+ , SumToJSON' UntaggedValue enc arity f
773− ) => Options -> ToArgs Value arity a
874+ ) => Options -> ToArgs enc arity a
774− -> f a -> Value
875+ -> f a -> enc
775nonAllNullarySumToJSON opts targs =
876nonAllNullarySumToJSON opts targs =
776 case sumEncoding opts of
877 case sumEncoding opts of
778− object . taggedObjectPairs opts targs tagFieldName contentsFieldName
779− ObjectWithSingleField -> Object . objectWithSingleFieldObj opts targs
780− TwoElemArray -> Array . twoElemArrayObj opts targs
782−--------------------------------------------------------------------------------
784−class SumToEncoding arity f allNullary where
785− sumToEncoding :: Options -> ToArgs Encoding arity a
786− -> f a -> Tagged allNullary Encoding
788−instance ( GetConName f
789− , TaggedObjectEnc arity f
790− , ObjectWithSingleFieldEnc arity f
791− , TwoElemArrayEnc 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
798−instance ( TwoElemArrayEnc arity f
799− , TaggedObjectEnc arity f
800− , ObjectWithSingleFieldEnc arity f
801− ) => SumToEncoding arity f False where
802− sumToEncoding opts targs = Tagged . nonAllNullarySumToEncoding opts targs
804−nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
805− , TaggedObjectEnc arity f
806− , ObjectWithSingleFieldEnc arity f
807− ) => Options -> ToArgs Encoding arity a
809−nonAllNullarySumToEncoding opts targs =
810− case sumEncoding opts of
811 TaggedObject{..} ->
879 TaggedObject{..} ->
812− taggedObjectEnc opts targs tagFieldName contentsFieldName
880+ taggedObject opts targs tagFieldName contentsFieldName
813− ObjectWithSingleField -> objectWithSingleFieldEnc opts targs
814− TwoElemArray -> twoElemArrayEnc opts targs
816−--------------------------------------------------------------------------------
882+ ObjectWithSingleField ->
883+ (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884+ . sumToJSON' opts targs
818−class TaggedObjectPairs arity f where
886+ TwoElemArray ->
819− taggedObjectPairs :: Options -> ToArgs Value arity a
823−instance ( TaggedObjectPairs arity a
824− , TaggedObjectPairs arity b
825− ) => TaggedObjectPairs arity (a :+: b) where
826− taggedObjectPairs opts targs tagFieldName contentsFieldName (L1 x) =
827− taggedObjectPairs opts targs tagFieldName contentsFieldName x
828− taggedObjectPairs opts targs tagFieldName contentsFieldName (R1 x) =
829− taggedObjectPairs opts targs tagFieldName contentsFieldName x
831−instance ( IsRecord a isRecord
832− , TaggedObjectPairs' arity a isRecord
834− ) => TaggedObjectPairs arity (C1 c a) where
835− taggedObjectPairs opts targs tagFieldName contentsFieldName =
836− (pack tagFieldName .= constructorTagModifier opts
837− (conName (undefined :: t c a p)) :) .
838− (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
887+ (unTagged :: Tagged TwoElemArray enc -> enc)
839− taggedObjectPairs' opts targs contentsFieldName . unM1
888+ . sumToJSON' opts targs
841−class TaggedObjectPairs' arity f isRecord where
890+ UntaggedValue ->
842− taggedObjectPairs' :: Options -> ToArgs Value arity a
891+ (unTagged :: Tagged UntaggedValue enc -> enc)
843− -> String -> f a -> Tagged isRecord [Pair]
892+ . sumToJSON' opts targs
894+--------------------------------------------------------------------------------
845−instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
896+class FromString enc where
846− taggedObjectPairs' _ _ _ _ = Tagged []
897+ fromString :: String -> enc
848−instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
899+instance FromString Encoding where
849− taggedObjectPairs' opts targs _ =
900+ fromString = toEncoding
850− Tagged . toList . recordToPairs opts targs
852−instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
902+instance FromString Value where
853− taggedObjectPairs' opts targs contentsFieldName =
903+ fromString = String . pack
854− Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts targs
856--------------------------------------------------------------------------------
905--------------------------------------------------------------------------------
858−class TaggedObjectEnc arity f where
907+class TaggedObject enc arity f where
859− taggedObjectEnc :: Options -> ToArgs Encoding arity a
908+ taggedObject :: Options -> ToArgs enc arity a
860− -> String -> String
909+ -> String -> String
861− -> f a -> Encoding
910+ -> f a -> enc
863−instance ( TaggedObjectEnc arity a
912+instance ( TaggedObject enc arity a
864− , TaggedObjectEnc arity b
913+ , TaggedObject enc arity b
865− ) => TaggedObjectEnc arity (a :+: b) where
914+ ) => TaggedObject enc arity (a :+: b)
866− taggedObjectEnc opts targs tagFieldName contentsFieldName (L1 x) =
916+ taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
867− taggedObjectEnc opts targs tagFieldName contentsFieldName x
917+ taggedObject opts targs tagFieldName contentsFieldName x
868− taggedObjectEnc opts targs tagFieldName contentsFieldName (R1 x) =
918+ taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
869− taggedObjectEnc opts targs tagFieldName contentsFieldName x
919+ taggedObject opts targs tagFieldName contentsFieldName x
871−instance ( IsRecord a isRecord
921+instance ( IsRecord a isRecord
872− , TaggedObjectEnc' arity a isRecord
922+ , TaggedObject' enc pairs arity a isRecord
923+ , FromPairs enc pairs
925+ , KeyValuePair enc pairs
873 , Constructor c
926 , Constructor c
874− ) => TaggedObjectEnc arity (C1 c a) where
927+ ) => TaggedObject enc arity (C1 c a)
875− taggedObjectEnc opts targs tagFieldName contentsFieldName v =
929+ taggedObject opts targs tagFieldName contentsFieldName =
876− E.wrapObject $
930+ fromPairs . mappend tag . contents
877− (E.string tagFieldName ><
931+ where
878− E.colon ><
932+ tag = tagFieldName `pair`
879− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) ><
933+ (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
880− ((unTagged :: Tagged isRecord Encoding -> Encoding) .
936+ (unTagged :: Tagged isRecord pairs -> pairs) .
881− taggedObjectEnc' opts targs contentsFieldName . unM1 $ v)
937+ taggedObject' opts targs contentsFieldName . unM1
883−class TaggedObjectEnc' arity f isRecord where
939+class TaggedObject' enc pairs arity f isRecord where
884− taggedObjectEnc' :: Options -> ToArgs Encoding arity a
940+ taggedObject' :: Options -> ToArgs enc arity a
885− -> String -> f a -> Tagged isRecord Encoding
941+ -> String -> f a -> Tagged isRecord pairs
943+instance ( GToJSON enc arity f
944+ , KeyValuePair enc pairs
945+ ) => TaggedObject' enc pairs arity f False
947+ taggedObject' opts targs contentsFieldName =
948+ Tagged . (contentsFieldName `pair`) . gToJSON opts targs
887−instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
950+instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
888− taggedObjectEnc' _ _ _ _ = Tagged E.empty
951+ taggedObject' _ _ _ _ = Tagged mempty
890−instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
953+instance ( RecordToPairs enc pairs arity f
891− taggedObjectEnc' opts targs _ = Tagged . (E.comma ><) . fst
954+ ) => TaggedObject' enc pairs arity f True
892− . recordToEncoding opts targs
955+ where
893−
956+ taggedObject' opts targs _ = Tagged . recordToPairs opts targs
894−instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
895− taggedObjectEnc' opts targs contentsFieldName =
896− Tagged . (\z -> E.comma >< toEncoding contentsFieldName >< E.colon >< z) .
897− gToEncoding opts targs
899--------------------------------------------------------------------------------
958--------------------------------------------------------------------------------
@@ -909,23 +968,38 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
909instance (Constructor c) => GetConName (C1 c a) where
968instance (Constructor c) => GetConName (C1 c a) where
910 getConName = conName
969 getConName = conName
971+-- For genericToJSONKey
972+instance GetConName a => GetConName (D1 d a) where
973+ getConName (M1 x) = getConName x
975+--------------------------------------------------------------------------------
977+-- Reflection of SumEncoding variants
979+data ObjectWithSingleField
912--------------------------------------------------------------------------------
983--------------------------------------------------------------------------------
914−class TwoElemArrayObj arity f where
985+class SumToJSON' s enc arity f where
915− twoElemArrayObj :: Options -> ToArgs Value arity a
986+ sumToJSON' :: Options -> ToArgs enc arity a
916− -> f a -> V.Vector Value
987+ -> f a -> Tagged s enc
989+instance ( SumToJSON' s enc arity a
990+ , SumToJSON' s enc arity b
991+ ) => SumToJSON' s enc arity (a :+: b)
993+ sumToJSON' opts targs (L1 x) = sumToJSON' opts targs x
994+ sumToJSON' opts targs (R1 x) = sumToJSON' opts targs x
918−instance ( TwoElemArrayObj arity a
996+--------------------------------------------------------------------------------
919− , TwoElemArrayObj arity b
920− ) => TwoElemArrayObj arity (a :+: b) where
921− twoElemArrayObj opts targs (L1 x) = twoElemArrayObj opts targs x
922− twoElemArrayObj opts targs (R1 x) = twoElemArrayObj opts targs x
924−instance ( GToJSON arity a
998+instance ( GToJSON Value arity a
925− , ConsToJSON arity a
999+ , ConsToJSON Value arity a
926 , Constructor c
1000 , Constructor c
927− ) => TwoElemArrayObj arity (C1 c a) where
1001+ ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
928− twoElemArrayObj opts targs x = V.create $ do
1002+ sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
929 mv <- VM.unsafeNew 2
1003 mv <- VM.unsafeNew 2
930 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
1004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
931 $ conName (undefined :: t c a p)
1005 $ conName (undefined :: t c a p)
@@ -934,148 +1008,121 @@ instance ( GToJSON arity a
935--------------------------------------------------------------------------------
1009--------------------------------------------------------------------------------
937−class TwoElemArrayEnc arity f where
938− twoElemArrayEnc :: Options -> ToArgs Encoding arity a
941−instance ( TwoElemArrayEnc arity a
1011+instance ( GToJSON Encoding arity a
942− , TwoElemArrayEnc arity b
1012+ , ConsToJSON Encoding arity a
943− ) => TwoElemArrayEnc arity (a :+: b) where
944− twoElemArrayEnc opts targs (L1 x) = twoElemArrayEnc opts targs x
945− twoElemArrayEnc opts targs (R1 x) = twoElemArrayEnc opts targs x
947−instance ( GToEncoding arity a
948− , ConsToEncoding arity a
949 , Constructor c
1013 , Constructor c
950− ) => TwoElemArrayEnc arity (C1 c a) where
1014+ ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
951− twoElemArrayEnc opts targs x = E.tuple $
1015+ where
1016+ sumToJSON' opts targs x = Tagged $ E.list id
952− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
1017+ [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
953− gToEncoding opts targs x
1018+ , gToJSON opts targs x
955--------------------------------------------------------------------------------
1021--------------------------------------------------------------------------------
957−class ConsToJSON arity f where
1023+class ConsToJSON enc arity f where
958− consToJSON :: Options -> ToArgs Value arity a
1024+ consToJSON :: Options -> ToArgs enc arity a
959− -> f a -> Value
1025+ -> f a -> enc
961−class ConsToJSON' arity f isRecord where
1027+class ConsToJSON' enc arity f isRecord where
962− consToJSON' :: Options -> ToArgs Value arity a
1028+ consToJSON' :: Options -> ToArgs enc arity a
963− -> Bool -- ^ Are we a record with one field?
964− -> f a -> Tagged isRecord Value
1029+ -> f a -> Tagged isRecord enc
966−instance ( IsRecord f isRecord
1031+instance ( IsRecord f isRecord
967− , ConsToJSON' arity f isRecord
1032+ , ConsToJSON' enc arity f isRecord
968− ) => ConsToJSON arity f where
1033+ ) => ConsToJSON enc arity f
969 consToJSON opts targs =
1035 consToJSON opts targs =
970− (unTagged :: Tagged isRecord Value -> Value)
1036+ (unTagged :: Tagged isRecord enc -> enc)
971− . consToJSON' opts targs (isUnary (undefined :: f a))
1037+ . consToJSON' opts targs
972−
1038+ {-# INLINE consToJSON #-}
973−instance (RecordToPairs arity f) => ConsToJSON' arity f True where
1039+
974− consToJSON' opts targs isUn f = let
1040+instance OVERLAPPING_
975− vals = toList $ recordToPairs opts targs f
1041+ ( RecordToPairs enc pairs arity (S1 s f)
976− in case (unwrapUnaryRecords opts,isUn,vals) of
1042+ , FromPairs enc pairs
977− (True,True,[(_,val)]) -> Tagged val
978− _ -> Tagged $ object vals
980−instance GToJSON arity f => ConsToJSON' arity f False where
1043+ , GToJSON enc arity f
1044+ ) => ConsToJSON' enc arity (S1 s f) True
981− consToJSON' opts targs _ = Tagged . gToJSON opts targs
1046+ consToJSON' opts targs
982−
1047+ | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
983−--------------------------------------------------------------------------------
1048+ | otherwise = Tagged . fromPairs . recordToPairs opts targs
984−
1049+ {-# INLINE consToJSON' #-}
985−class ConsToEncoding arity f where
1050+
986− consToEncoding :: Options -> ToArgs Encoding arity a
1051+instance ( RecordToPairs enc pairs arity f
987− -> f a -> Encoding
1052+ , FromPairs enc pairs
988−
1053+ ) => ConsToJSON' enc arity f True
989−class ConsToEncoding' arity f isRecord where
1054+ where
990− consToEncoding' :: Options -> ToArgs Encoding arity a
1055+ consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
991− -> Bool -- ^ Are we a record with one field?
1056+ {-# INLINE consToJSON' #-}
992− -> f a -> Tagged isRecord Encoding
994−instance ( IsRecord f isRecord
1058+instance GToJSON enc arity f => ConsToJSON' enc arity f False where
995− , ConsToEncoding' arity f isRecord
996− ) => ConsToEncoding arity f where
997− consToEncoding opts targs =
998− (unTagged :: Tagged isRecord Encoding -> Encoding)
999− . consToEncoding' opts targs (isUnary (undefined :: f a))
1001−instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
1002− consToEncoding' opts targs isUn x =
1003− let (enc, mbVal) = recordToEncoding opts targs x
1004− in case (unwrapUnaryRecords opts, isUn, mbVal) of
1005− (True, True, Just val) -> Tagged val
1006− _ -> Tagged $ E.wrapObject enc
1008−instance GToEncoding arity f => ConsToEncoding' arity f False where
1009− consToEncoding' opts targs _ = Tagged . gToEncoding opts targs
1059+ consToJSON' opts targs = Tagged . gToJSON opts targs
1060+ {-# INLINE consToJSON' #-}
1011--------------------------------------------------------------------------------
1062--------------------------------------------------------------------------------
1013−class RecordToPairs arity f where
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
1014− recordToPairs :: Options -> ToArgs Value arity a
1068+ recordToPairs :: Options -> ToArgs enc arity a
1015− -> f a -> DList Pair
1069+ -> f a -> pairs
1017−instance ( RecordToPairs arity a
1018− , RecordToPairs arity b
1019− ) => RecordToPairs arity (a :*: b) where
1020− recordToPairs opts targs (a :*: b) = recordToPairs opts targs a <>
1021− recordToPairs opts targs b
1071+instance ( Monoid pairs
1072+ , RecordToPairs enc pairs arity a
1073+ , RecordToPairs enc pairs arity b
1074+ ) => RecordToPairs enc pairs arity (a :*: b)
1076+ recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
1077+ pairsOf a `mappend` pairsOf b
1079+ pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
1080+ pairsOf = recordToPairs opts targs
1081+ {-# INLINE recordToPairs #-}
1083+instance ( Selector s
1084+ , GToJSON enc arity a
1085+ , KeyValuePair enc pairs
1023−instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
1086+ ) => RecordToPairs enc pairs arity (S1 s a)
1024 recordToPairs = fieldToPair
1088 recordToPairs = fieldToPair
1025−
1089+ {-# INLINE recordToPairs #-}
1091+instance INCOHERENT_
1026−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1092+ ( Selector s
1093+ , GToJSON enc arity (K1 i (Maybe a))
1094+ , KeyValuePair enc pairs
1027− RecordToPairs arity (S1 s (K1 i (Maybe a))) where
1096+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
1028 recordToPairs opts _ (M1 k1) | omitNothingFields opts
1098 recordToPairs opts _ (M1 k1) | omitNothingFields opts
1029− , K1 Nothing <- k1 = DList.empty
1099+ , K1 Nothing <- k1 = mempty
1030 recordToPairs opts targs m1 = fieldToPair opts targs m1
1100 recordToPairs opts targs m1 = fieldToPair opts targs m1
1031−
1101+ {-# INLINE recordToPairs #-}
1032−fieldToPair :: (Selector s, GToJSON arity a)
1102+
1033− => Options -> ToArgs Value arity p
1103+instance INCOHERENT_
1034− -> S1 s a p -> DList Pair
1104+ ( Selector s
1035−fieldToPair opts targs m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
1105+ , GToJSON enc arity (K1 i (Maybe a))
1036− , gToJSON opts targs (unM1 m1)
1106+ , KeyValuePair enc pairs
1037− )
1107+ , Monoid pairs
1038−
1108+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup.Option a)))
1039−--------------------------------------------------------------------------------
1109+ where
1040−
1110+ recordToPairs opts targs = recordToPairs opts targs . unwrap
1041−class RecordToEncoding arity f where
1111+ where
1042− -- 1st element: whole thing
1112+ unwrap :: S1 s (K1 i (Semigroup.Option a)) p -> S1 s (K1 i (Maybe a)) p
1043− -- 2nd element: in case the record has only 1 field, just the value
1113+ unwrap (M1 (K1 (Semigroup.Option a))) = M1 (K1 a)
1044− -- of the field (without the key); 'Nothing' otherwise
1114+ {-# INLINE recordToPairs #-}
1045− recordToEncoding :: Options -> ToArgs Encoding arity a
1115+
1046− -> f a -> (Encoding, Maybe Encoding)
1116+fieldToPair :: (Selector s
1047−
1117+ , GToJSON enc arity a
1048−instance ( RecordToEncoding arity a
1118+ , KeyValuePair enc pairs)
1049− , RecordToEncoding arity b
1050− ) => RecordToEncoding arity (a :*: b) where
1051− recordToEncoding opts targs (a :*: b) | omitNothingFields opts =
1052− (E.econcat $ intersperse E.comma $
1053− filter (not . E.nullEncoding)
1054− [ fst (recordToEncoding opts targs a)
1055− , fst (recordToEncoding opts targs b) ]
1057− recordToEncoding opts targs (a :*: b) =
1058− (fst (recordToEncoding opts targs a) >< E.comma ><
1059− fst (recordToEncoding opts targs b),
1063−instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1064− recordToEncoding = fieldToEncoding
1066−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1067− RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1068− recordToEncoding opts _ (M1 k1) | omitNothingFields opts
1069− , K1 Nothing <- k1 = (E.empty, Nothing)
1070− recordToEncoding opts targs m1 = fieldToEncoding opts targs m1
1072−fieldToEncoding :: (Selector s, GToEncoding arity a)
1073− => Options -> ToArgs Encoding arity p
1119+ => Options -> ToArgs enc arity p
1074− -> S1 s a p -> (Encoding, Maybe Encoding)
1120+ -> S1 s a p -> pairs
1075−fieldToEncoding opts targs m1 =
1121+fieldToPair opts targs m1 =
1076− let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1122+ let key = fieldLabelModifier opts (selName m1)
1077− valueBuilder = gToEncoding opts targs (unM1 m1)
1123+ value = gToJSON opts targs (unM1 m1)
1078− in (keyBuilder >< E.colon >< valueBuilder, Just valueBuilder)
1124+ in key `pair` value
1125+{-# INLINE fieldToPair #-}
1080--------------------------------------------------------------------------------
1127--------------------------------------------------------------------------------
@@ -1098,10 +1145,12 @@ instance ( WriteProduct arity a
1098 lenL = len `unsafeShiftR` 1
1145 lenL = len `unsafeShiftR` 1
1099 lenR = len - lenL
1146 lenR = len - lenL
1100 ixR = ix + lenL
1147 ixR = ix + lenL
1148+ {-# INLINE writeProduct #-}
1102−instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
1150+instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1103 writeProduct opts targs mv ix _ =
1151 writeProduct opts targs mv ix _ =
1104 VM.unsafeWrite mv ix . gToJSON opts targs
1152 VM.unsafeWrite mv ix . gToJSON opts targs
1153+ {-# INLINE writeProduct #-}
1106--------------------------------------------------------------------------------
1155--------------------------------------------------------------------------------
@@ -1116,61 +1165,45 @@ instance ( EncodeProduct arity a
1116 E.econcat $ intersperse E.comma $
1165 E.econcat $ intersperse E.comma $
1117 filter (not . E.nullEncoding)
1166 filter (not . E.nullEncoding)
1118 [encodeProduct opts targs a, encodeProduct opts targs b]
1167 [encodeProduct opts targs a, encodeProduct opts targs b]
1119− encodeProduct opts targs (a :*: b) = encodeProduct opts targs a >*<
1168+ encodeProduct opts targs (a :*: b) =
1169+ encodeProduct opts targs a >*<
1120− encodeProduct opts targs b
1170+ encodeProduct opts targs b
1171+ {-# INLINE encodeProduct #-}
1122−instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1173+instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1123− encodeProduct opts targs a = E.retagEncoding $ gToEncoding opts targs a
1174+ encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1175+ {-# INLINE encodeProduct #-}
1125--------------------------------------------------------------------------------
1177--------------------------------------------------------------------------------
1127−class ObjectWithSingleFieldObj arity f where
1128− objectWithSingleFieldObj :: Options -> ToArgs Value arity a
1131−instance ( ObjectWithSingleFieldObj arity a
1132− , ObjectWithSingleFieldObj arity b
1133− ) => ObjectWithSingleFieldObj arity (a :+: b) where
1134− objectWithSingleFieldObj opts targs (L1 x) =
1135− objectWithSingleFieldObj opts targs x
1136− objectWithSingleFieldObj opts targs (R1 x) =
1137− objectWithSingleFieldObj opts targs x
1139−instance ( GToJSON arity a
1179+instance ( GToJSON enc arity a
1140− , ConsToJSON arity a
1180+ , ConsToJSON enc arity a
1181+ , FromPairs enc pairs
1182+ , KeyValuePair enc pairs
1141 , Constructor c
1183 , Constructor c
1142− ) => ObjectWithSingleFieldObj arity (C1 c a) where
1184+ ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1143− objectWithSingleFieldObj opts targs = H.singleton typ . gToJSON opts targs
1185+ where
1186+ sumToJSON' opts targs =
1187+ Tagged . fromPairs . (typ `pair`) . gToJSON opts targs
1145− typ = pack $ constructorTagModifier opts $
1189+ typ = constructorTagModifier opts $
1146 conName (undefined :: t c a p)
1190 conName (undefined :: t c a p)
1148--------------------------------------------------------------------------------
1192--------------------------------------------------------------------------------
1150−class ObjectWithSingleFieldEnc arity f where
1194+instance OVERLAPPABLE_
1151− objectWithSingleFieldEnc :: Options -> ToArgs Encoding arity a
1195+ ( ConsToJSON enc arity a
1152− -> f a -> Encoding
1196+ ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1154−instance ( ObjectWithSingleFieldEnc arity a
1198+ sumToJSON' opts targs = Tagged . gToJSON opts targs
1155− , ObjectWithSingleFieldEnc arity b
1156− ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1157− objectWithSingleFieldEnc opts targs (L1 x) =
1158− objectWithSingleFieldEnc opts targs x
1159− objectWithSingleFieldEnc opts targs (R1 x) =
1160− objectWithSingleFieldEnc opts targs x
1162−instance ( GToEncoding arity a
1200+instance OVERLAPPING_
1163− , ConsToEncoding arity a
1164− , Constructor c
1201+ ( Constructor c
1165− ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1202+ , FromString enc
1166− objectWithSingleFieldEnc opts targs v =
1203+ ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1167− E.openCurly ><
1204+ where
1168− toEncoding
1205+ sumToJSON' opts _ _ = Tagged . fromString $
1169− (constructorTagModifier opts
1206+ constructorTagModifier opts $ conName (undefined :: t c U1 p)
1170− (conName (undefined :: t c a p))) ><
1172− gToEncoding opts targs v ><
1175-------------------------------------------------------------------------------
1208-------------------------------------------------------------------------------
1176-- Instances
1209-- Instances
@@ -1243,6 +1276,13 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
1243 toEncoding = toEncoding2
1276 toEncoding = toEncoding2
1244 {-# INLINE toEncoding #-}
1277 {-# INLINE toEncoding #-}
1279+instance ToJSON Void where
1281+ {-# INLINE toJSON #-}
1283+ toEncoding = absurd
1284+ {-# INLINE toEncoding #-}
1247instance ToJSON Bool where
1287instance ToJSON Bool where
1248 toJSON = Bool
1288 toJSON = Bool
@@ -1428,7 +1468,6 @@ instance ToJSONKey Int64 where
1428 toJSONKey = toJSONKeyTextEnc E.int64Text
1468 toJSONKey = toJSONKeyTextEnc E.int64Text
1429 {-# INLINE toJSONKey #-}
1469 {-# INLINE toJSONKey #-}
1432instance ToJSON Word where
1471instance ToJSON Word where
1433 toJSON = Number . fromIntegral
1472 toJSON = Number . fromIntegral
1434 {-# INLINE toJSON #-}
1473 {-# INLINE toJSON #-}
@@ -1488,6 +1527,12 @@ instance ToJSONKey Word64 where
1488 toJSONKey = toJSONKeyTextEnc E.word64Text
1527 toJSONKey = toJSONKeyTextEnc E.word64Text
1489 {-# INLINE toJSONKey #-}
1528 {-# INLINE toJSONKey #-}
1530+instance ToJSON CTime where
1531+ toJSON (CTime i) = toJSON i
1532+ {-# INLINE toJSON #-}
1534+ toEncoding (CTime i) = toEncoding i
1535+ {-# INLINE toEncoding #-}
1492instance ToJSON Text where
1537instance ToJSON Text where
1493 toJSON = String
1538 toJSON = String
@@ -1602,7 +1647,7 @@ instance (ToJSON a) => ToJSON (Identity a) where
1602 toEncodingList = liftToEncodingList toEncoding toEncodingList
1647 toEncodingList = liftToEncodingList toEncoding toEncodingList
1603 {-# INLINE toEncodingList #-}
1648 {-# INLINE toEncodingList #-}
1605−instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1650+instance (ToJSONKey a) => ToJSONKey (Identity a) where
1606 toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1651 toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1607 toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
1652 toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
@@ -1751,13 +1796,11 @@ instance ToJSONKey k => ToJSON1 (M.Map k) where
1751 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1796 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1752 {-# INLINE liftToJSON #-}
1797 {-# INLINE liftToJSON #-}
1754− -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
1755 liftToEncoding g _ = case toJSONKey of
1799 liftToEncoding g _ = case toJSONKey of
1756 ToJSONKeyText _ f -> dict f g M.foldrWithKey
1800 ToJSONKeyText _ f -> dict f g M.foldrWithKey
1757 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1801 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1759− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1760− pairEncoding f (a, b) = tuple $ f a >*< g b
1803+ pairEncoding f (a, b) = E.list id [f a, g b]
1761 {-# INLINE liftToEncoding #-}
1804 {-# INLINE liftToEncoding #-}
@@ -1795,6 +1838,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
1795 toEncoding = toEncoding1
1838 toEncoding = toEncoding1
1796 {-# INLINE toEncoding #-}
1839 {-# INLINE toEncoding #-}
1841+-------------------------------------------------------------------------------
1843+-------------------------------------------------------------------------------
1845+instance ToJSON UUID.UUID where
1846+ toJSON = toJSON . UUID.toText
1847+ toEncoding = E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
1849+instance ToJSONKey UUID.UUID where
1850+ toJSONKey = ToJSONKeyText UUID.toText $
1851+ E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
1799-------------------------------------------------------------------------------
1853-------------------------------------------------------------------------------
1800-- vector
1854-- vector
@@ -1808,6 +1862,8 @@ instance ToJSON1 Vector where
1808 {-# INLINE liftToEncoding #-}
1862 {-# INLINE liftToEncoding #-}
1810instance (ToJSON a) => ToJSON (Vector a) where
1864instance (ToJSON a) => ToJSON (Vector a) where
1865+ {-# SPECIALIZE instance ToJSON Array #-}
1811 toJSON = toJSON1
1867 toJSON = toJSON1
1812 {-# INLINE toJSON #-}
1868 {-# INLINE toJSON #-}
@@ -1875,11 +1931,12 @@ instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1875 ToJSONKeyText _ f -> dict f g H.foldrWithKey
1931 ToJSONKeyText _ f -> dict f g H.foldrWithKey
1876 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1932 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1878− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1879− pairEncoding f (a, b) = tuple $ f a >*< g b
1934+ pairEncoding f (a, b) = E.list id [f a, g b]
1880 {-# INLINE liftToEncoding #-}
1935 {-# INLINE liftToEncoding #-}
1882instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1937instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1938+ {-# SPECIALIZE instance ToJSON Object #-}
1883 toJSON = toJSON1
1940 toJSON = toJSON1
1884 {-# INLINE toJSON #-}
1941 {-# INLINE toJSON #-}
@@ -1909,6 +1966,32 @@ dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1909formatMillis :: (FormatTime t) => t -> String
1966formatMillis :: (FormatTime t) => t -> String
1910formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1967formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1969+-------------------------------------------------------------------------------
1971+-------------------------------------------------------------------------------
1973+instance ToJSON a => ToJSON (PM.Array a) where
1974+ -- note: we could do better than this if vector exposed the data
1975+ -- constructor in Data.Vector.
1976+ toJSON = toJSON . Exts.toList
1977+ toEncoding = toEncoding . Exts.toList
1979+instance ToJSON a => ToJSON (PM.SmallArray a) where
1980+ toJSON = toJSON . Exts.toList
1981+ toEncoding = toEncoding . Exts.toList
1983+#if (MIN_VERSION_primitive(0,6,4))
1984+instance (PM.Prim a,ToJSON a) => ToJSON (PM.PrimArray a) where
1985+ toJSON = toJSON . Exts.toList
1986+ toEncoding = toEncoding . Exts.toList
1988+#if !MIN_VERSION_primitive(0,7,0)
1989+instance (PM.PrimUnlifted a,ToJSON a) => ToJSON (PM.UnliftedArray a) where
1990+ toJSON = toJSON . Exts.toList
1991+ toEncoding = toEncoding . Exts.toList
1912-------------------------------------------------------------------------------
1995-------------------------------------------------------------------------------
1914-------------------------------------------------------------------------------
1997-------------------------------------------------------------------------------
@@ -1957,10 +2040,11 @@ stringEncoding :: Encoding' Text -> Value
1957stringEncoding = String
2040stringEncoding = String
1958 . T.dropAround (== '"')
2041 . T.dropAround (== '"')
1959 . T.decodeLatin1
2042 . T.decodeLatin1
1960− . lazyToStrictByteString
2043+ . L.toStrict
1961 . E.encodingToLazyByteString
2044 . E.encodingToLazyByteString
1962{-# INLINE stringEncoding #-}
2045{-# INLINE stringEncoding #-}
1964instance ToJSON NominalDiffTime where
2048instance ToJSON NominalDiffTime where
1965 toJSON = Number . realToFrac
2049 toJSON = Number . realToFrac
1966 {-# INLINE toJSON #-}
2050 {-# INLINE toJSON #-}
@@ -1968,18 +2052,70 @@ instance ToJSON NominalDiffTime where
1968 toEncoding = E.scientific . realToFrac
2052 toEncoding = E.scientific . realToFrac
1969 {-# INLINE toEncoding #-}
2053 {-# INLINE toEncoding #-}
2056+instance ToJSON DiffTime where
2057+ toJSON = Number . realToFrac
2058+ {-# INLINE toJSON #-}
2060+ toEncoding = E.scientific . realToFrac
2061+ {-# INLINE toEncoding #-}
2063+-- | Encoded as number
2064+instance ToJSON SystemTime where
2065+ toJSON (MkSystemTime secs nsecs) =
2066+ toJSON (fromIntegral secs + fromIntegral nsecs / 1000000000 :: Nano)
2067+ toEncoding (MkSystemTime secs nsecs) =
2068+ toEncoding (fromIntegral secs + fromIntegral nsecs / 1000000000 :: Nano)
2070+instance ToJSON CalendarDiffTime where
2071+ toJSON (CalendarDiffTime m nt) = object
2075+ toEncoding (CalendarDiffTime m nt) = E.pairs
2076+ ("months" .= m <> "time" .= nt)
2078+instance ToJSON CalendarDiffDays where
2079+ toJSON (CalendarDiffDays m d) = object
2083+ toEncoding (CalendarDiffDays m d) = E.pairs
2084+ ("months" .= m <> "days" .= d)
2086+instance ToJSON DayOfWeek where
2087+ toJSON Monday = "monday"
2088+ toJSON Tuesday = "tuesday"
2089+ toJSON Wednesday = "wednesday"
2090+ toJSON Thursday = "thursday"
2091+ toJSON Friday = "friday"
2092+ toJSON Saturday = "saturday"
2093+ toJSON Sunday = "sunday"
2095+toEncodingDayOfWeek :: DayOfWeek -> E.Encoding' Text
2096+toEncodingDayOfWeek Monday = E.unsafeToEncoding "\"monday\""
2097+toEncodingDayOfWeek Tuesday = E.unsafeToEncoding "\"tuesday\""
2098+toEncodingDayOfWeek Wednesday = E.unsafeToEncoding "\"wednesday\""
2099+toEncodingDayOfWeek Thursday = E.unsafeToEncoding "\"thursday\""
2100+toEncodingDayOfWeek Friday = E.unsafeToEncoding "\"friday\""
2101+toEncodingDayOfWeek Saturday = E.unsafeToEncoding "\"saturday\""
2102+toEncodingDayOfWeek Sunday = E.unsafeToEncoding "\"sunday\""
2104+instance ToJSONKey DayOfWeek where
2105+ toJSONKey = toJSONKeyTextEnc toEncodingDayOfWeek
1971-------------------------------------------------------------------------------
2107-------------------------------------------------------------------------------
1972-- base Monoid/Semigroup
2108-- base Monoid/Semigroup
1973-------------------------------------------------------------------------------
2109-------------------------------------------------------------------------------
1975−instance ToJSON1 Dual where
2111+instance ToJSON1 Monoid.Dual where
1976− liftToJSON t _ = t . getDual
2112+ liftToJSON t _ = t . Monoid.getDual
1977 {-# INLINE liftToJSON #-}
2113 {-# INLINE liftToJSON #-}
1979− liftToEncoding t _ = t . getDual
2115+ liftToEncoding t _ = t . Monoid.getDual
1980 {-# INLINE liftToEncoding #-}
2116 {-# INLINE liftToEncoding #-}
1982−instance ToJSON a => ToJSON (Dual a) where
2118+instance ToJSON a => ToJSON (Monoid.Dual a) where
1983 toJSON = toJSON1
2119 toJSON = toJSON1
1984 {-# INLINE toJSON #-}
2120 {-# INLINE toJSON #-}
@@ -1987,14 +2123,14 @@ instance ToJSON a => ToJSON (Dual a) where
1987 {-# INLINE toEncoding #-}
2123 {-# INLINE toEncoding #-}
1990−instance ToJSON1 First where
2126+instance ToJSON1 Monoid.First where
1991− liftToJSON t to' = liftToJSON t to' . getFirst
2127+ liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
1992 {-# INLINE liftToJSON #-}
2128 {-# INLINE liftToJSON #-}
1994− liftToEncoding t to' = liftToEncoding t to' . getFirst
2130+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
1995 {-# INLINE liftToEncoding #-}
2131 {-# INLINE liftToEncoding #-}
1997−instance ToJSON a => ToJSON (First a) where
2133+instance ToJSON a => ToJSON (Monoid.First a) where
1998 toJSON = toJSON1
2134 toJSON = toJSON1
1999 {-# INLINE toJSON #-}
2135 {-# INLINE toJSON #-}
@@ -2002,14 +2138,103 @@ instance ToJSON a => ToJSON (First a) where
2002 {-# INLINE toEncoding #-}
2138 {-# INLINE toEncoding #-}
2005−instance ToJSON1 Last where
2141+instance ToJSON1 Monoid.Last where
2006− liftToJSON t to' = liftToJSON t to' . getLast
2142+ liftToJSON t to' = liftToJSON t to' . Monoid.getLast
2007 {-# INLINE liftToJSON #-}
2143 {-# INLINE liftToJSON #-}
2009− liftToEncoding t to' = liftToEncoding t to' . getLast
2145+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2010 {-# INLINE liftToEncoding #-}
2146 {-# INLINE liftToEncoding #-}
2012−instance ToJSON a => ToJSON (Last a) where
2148+instance ToJSON a => ToJSON (Monoid.Last a) where
2150+ {-# INLINE toJSON #-}
2152+ toEncoding = toEncoding1
2153+ {-# INLINE toEncoding #-}
2156+instance ToJSON1 Semigroup.Min where
2157+ liftToJSON t _ (Semigroup.Min x) = t x
2158+ {-# INLINE liftToJSON #-}
2160+ liftToEncoding t _ (Semigroup.Min x) = t x
2161+ {-# INLINE liftToEncoding #-}
2163+instance ToJSON a => ToJSON (Semigroup.Min a) where
2165+ {-# INLINE toJSON #-}
2167+ toEncoding = toEncoding1
2168+ {-# INLINE toEncoding #-}
2171+instance ToJSON1 Semigroup.Max where
2172+ liftToJSON t _ (Semigroup.Max x) = t x
2173+ {-# INLINE liftToJSON #-}
2175+ liftToEncoding t _ (Semigroup.Max x) = t x
2176+ {-# INLINE liftToEncoding #-}
2178+instance ToJSON a => ToJSON (Semigroup.Max a) where
2180+ {-# INLINE toJSON #-}
2182+ toEncoding = toEncoding1
2183+ {-# INLINE toEncoding #-}
2185+instance ToJSON1 Semigroup.First where
2186+ liftToJSON t _ (Semigroup.First x) = t x
2187+ {-# INLINE liftToJSON #-}
2189+ liftToEncoding t _ (Semigroup.First x) = t x
2190+ {-# INLINE liftToEncoding #-}
2192+instance ToJSON a => ToJSON (Semigroup.First a) where
2194+ {-# INLINE toJSON #-}
2196+ toEncoding = toEncoding1
2197+ {-# INLINE toEncoding #-}
2200+instance ToJSON1 Semigroup.Last where
2201+ liftToJSON t _ (Semigroup.Last x) = t x
2202+ {-# INLINE liftToJSON #-}
2204+ liftToEncoding t _ (Semigroup.Last x) = t x
2205+ {-# INLINE liftToEncoding #-}
2207+instance ToJSON a => ToJSON (Semigroup.Last a) where
2209+ {-# INLINE toJSON #-}
2211+ toEncoding = toEncoding1
2212+ {-# INLINE toEncoding #-}
2215+instance ToJSON1 Semigroup.WrappedMonoid where
2216+ liftToJSON t _ (Semigroup.WrapMonoid x) = t x
2217+ {-# INLINE liftToJSON #-}
2219+ liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
2220+ {-# INLINE liftToEncoding #-}
2222+instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
2224+ {-# INLINE toJSON #-}
2226+ toEncoding = toEncoding1
2227+ {-# INLINE toEncoding #-}
2230+instance ToJSON1 Semigroup.Option where
2231+ liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
2232+ {-# INLINE liftToJSON #-}
2234+ liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
2235+ {-# INLINE liftToEncoding #-}
2237+instance ToJSON a => ToJSON (Semigroup.Option a) where
2013 toJSON = toJSON1
2238 toJSON = toJSON1
2014 {-# INLINE toJSON #-}
2239 {-# INLINE toJSON #-}
@@ -2020,6 +2245,13 @@ instance ToJSON a => ToJSON (Last a) where
2020-- tagged
2245-- tagged
2021-------------------------------------------------------------------------------
2246-------------------------------------------------------------------------------
2248+instance ToJSON1 Proxy where
2249+ liftToJSON _ _ _ = Null
2250+ {-# INLINE liftToJSON #-}
2252+ liftToEncoding _ _ _ = E.null_
2253+ {-# INLINE liftToEncoding #-}
2023instance ToJSON (Proxy a) where
2255instance ToJSON (Proxy a) where
2024 toJSON _ = Null
2256 toJSON _ = Null
2025 {-# INLINE toJSON #-}
2257 {-# INLINE toJSON #-}
@@ -2028,6 +2260,13 @@ instance ToJSON (Proxy a) where
2028 {-# INLINE toEncoding #-}
2260 {-# INLINE toEncoding #-}
2263+instance ToJSON2 Tagged where
2264+ liftToJSON2 _ _ t _ (Tagged x) = t x
2265+ {-# INLINE liftToJSON2 #-}
2267+ liftToEncoding2 _ _ t _ (Tagged x) = t x
2268+ {-# INLINE liftToEncoding2 #-}
2031instance ToJSON1 (Tagged a) where
2270instance ToJSON1 (Tagged a) where
2032 liftToJSON t _ (Tagged x) = t x
2271 liftToJSON t _ (Tagged x) = t x
2033 {-# INLINE liftToJSON #-}
2272 {-# INLINE liftToJSON #-}
@@ -2042,7 +2281,6 @@ instance ToJSON b => ToJSON (Tagged a b) where
2042 toEncoding = toEncoding1
2281 toEncoding = toEncoding1
2043 {-# INLINE toEncoding #-}
2282 {-# INLINE toEncoding #-}
2046instance ToJSONKey b => ToJSONKey (Tagged a b) where
2284instance ToJSONKey b => ToJSONKey (Tagged a b) where
2047 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2048 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
2286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -2063,7 +2301,7 @@ instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
2063 toJSONKey = toJSONKeyList
2301 toJSONKey = toJSONKeyList
2065-------------------------------------------------------------------------------
2303-------------------------------------------------------------------------------
2066−-- Tuple instances, see tuple-instances-to.hs
2304+-- Tuple instances
2067-------------------------------------------------------------------------------
2305-------------------------------------------------------------------------------
2069instance ToJSON2 (,) where
2307instance ToJSON2 (,) where
@@ -2074,9 +2312,7 @@ instance ToJSON2 (,) where
2074 return mv
2312 return mv
2075 {-# INLINE liftToJSON2 #-}
2313 {-# INLINE liftToJSON2 #-}
2077− liftToEncoding2 toA _ toB _ (a, b) = tuple $
2315+ liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
2080 {-# INLINE liftToEncoding2 #-}
2316 {-# INLINE liftToEncoding2 #-}
2082instance (ToJSON a) => ToJSON1 ((,) a) where
2318instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -2100,10 +2336,11 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
2100 return mv
2336 return mv
2101 {-# INLINE liftToJSON2 #-}
2337 {-# INLINE liftToJSON2 #-}
2103− liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2339+ liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
2104− toEncoding a >*<
2340+ [ toEncoding a
2105− toB b >*<
2341+ , toB b
2107 {-# INLINE liftToEncoding2 #-}
2344 {-# INLINE liftToEncoding2 #-}
2109instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
2346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -2128,11 +2365,12 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
2128 return mv
2365 return mv
2129 {-# INLINE liftToJSON2 #-}
2366 {-# INLINE liftToJSON2 #-}
2131− liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2368+ liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
2132− toEncoding a >*<
2369+ [ toEncoding a
2133− toEncoding b >*<
2370+ , toEncoding b
2134− toC c >*<
2371+ , toC c
2136 {-# INLINE liftToEncoding2 #-}
2374 {-# INLINE liftToEncoding2 #-}
2138instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
2376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -2158,12 +2396,13 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
2158 return mv
2396 return mv
2159 {-# INLINE liftToJSON2 #-}
2397 {-# INLINE liftToJSON2 #-}
2161− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2399+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
2162− toEncoding a >*<
2400+ [ toEncoding a
2163− toEncoding b >*<
2401+ , toEncoding b
2164− toEncoding c >*<
2402+ , toEncoding c
2165− toD d >*<
2403+ , toD d
2167 {-# INLINE liftToEncoding2 #-}
2406 {-# INLINE liftToEncoding2 #-}
2169instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
2408instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
@@ -2190,13 +2429,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
2190 return mv
2429 return mv
2191 {-# INLINE liftToJSON2 #-}
2430 {-# INLINE liftToJSON2 #-}
2193− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2432+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
2194− toEncoding a >*<
2433+ [ toEncoding a
2195− toEncoding b >*<
2434+ , toEncoding b
2196− toEncoding c >*<
2435+ , toEncoding c
2197− toEncoding d >*<
2436+ , toEncoding d
2198− toE e >*<
2437+ , toE e
2200 {-# INLINE liftToEncoding2 #-}
2440 {-# INLINE liftToEncoding2 #-}
2202instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
2442instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
@@ -2224,14 +2464,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
2224 return mv
2464 return mv
2225 {-# INLINE liftToJSON2 #-}
2465 {-# INLINE liftToJSON2 #-}
2227− liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
2467+ liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = E.list id
2228− toEncoding a >*<
2468+ [ toEncoding a
2229− toEncoding b >*<
2469+ , toEncoding b
2230− toEncoding c >*<
2470+ , toEncoding c
2231− toEncoding d >*<
2471+ , toEncoding d
2232− toEncoding e >*<
2472+ , toEncoding e
2233− toF f >*<
2473+ , toF f
2235 {-# INLINE liftToEncoding2 #-}
2476 {-# INLINE liftToEncoding2 #-}
2237instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
2478instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
@@ -2260,15 +2501,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
2260 return mv
2501 return mv
2261 {-# INLINE liftToJSON2 #-}
2502 {-# INLINE liftToJSON2 #-}
2263− liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
2504+ liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = E.list id
2264− toEncoding a >*<
2505+ [ toEncoding a
2265− toEncoding b >*<
2506+ , toEncoding b
2266− toEncoding c >*<
2507+ , toEncoding c
2267− toEncoding d >*<
2508+ , toEncoding d
2268− toEncoding e >*<
2509+ , toEncoding e
2269− toEncoding f >*<
2510+ , toEncoding f
2270− toG g >*<
2511+ , toG g
2272 {-# INLINE liftToEncoding2 #-}
2514 {-# INLINE liftToEncoding2 #-}
2274instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
2516instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
@@ -2298,16 +2540,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
2298 return mv
2540 return mv
2299 {-# INLINE liftToJSON2 #-}
2541 {-# INLINE liftToJSON2 #-}
2301− liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
2543+ liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = E.list id
2302− toEncoding a >*<
2544+ [ toEncoding a
2303− toEncoding b >*<
2545+ , toEncoding b
2304− toEncoding c >*<
2546+ , toEncoding c
2305− toEncoding d >*<
2547+ , toEncoding d
2306− toEncoding e >*<
2548+ , toEncoding e
2307− toEncoding f >*<
2549+ , toEncoding f
2308− toEncoding g >*<
2550+ , toEncoding g
2309− toH h >*<
2551+ , toH h
2311 {-# INLINE liftToEncoding2 #-}
2554 {-# INLINE liftToEncoding2 #-}
2313instance (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
2556instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h) => ToJSON1 ((,,,,,,,,) a b c d e f g h) where
@@ -2338,17 +2581,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2338 return mv
2581 return mv
2339 {-# INLINE liftToJSON2 #-}
2582 {-# INLINE liftToJSON2 #-}
2341− liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
2584+ liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = E.list id
2342− toEncoding a >*<
2585+ [ toEncoding a
2343− toEncoding b >*<
2586+ , toEncoding b
2344− toEncoding c >*<
2587+ , toEncoding c
2345− toEncoding d >*<
2588+ , toEncoding d
2346− toEncoding e >*<
2589+ , toEncoding e
2347− toEncoding f >*<
2590+ , toEncoding f
2348− toEncoding g >*<
2591+ , toEncoding g
2349− toEncoding h >*<
2592+ , toEncoding h
2350− toI i >*<
2593+ , toI i
2352 {-# INLINE liftToEncoding2 #-}
2596 {-# INLINE liftToEncoding2 #-}
2354instance (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
2598instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i) => ToJSON1 ((,,,,,,,,,) a b c d e f g h i) where
@@ -2380,18 +2624,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2380 return mv
2624 return mv
2381 {-# INLINE liftToJSON2 #-}
2625 {-# INLINE liftToJSON2 #-}
2383− liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
2627+ liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = E.list id
2384− toEncoding a >*<
2628+ [ toEncoding a
2385− toEncoding b >*<
2629+ , toEncoding b
2386− toEncoding c >*<
2630+ , toEncoding c
2387− toEncoding d >*<
2631+ , toEncoding d
2388− toEncoding e >*<
2632+ , toEncoding e
2389− toEncoding f >*<
2633+ , toEncoding f
2390− toEncoding g >*<
2634+ , toEncoding g
2391− toEncoding h >*<
2635+ , toEncoding h
2392− toEncoding i >*<
2636+ , toEncoding i
2393− toJ j >*<
2637+ , toJ j
2395 {-# INLINE liftToEncoding2 #-}
2640 {-# INLINE liftToEncoding2 #-}
2397instance (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
2642instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j) => ToJSON1 ((,,,,,,,,,,) a b c d e f g h i j) where
@@ -2424,19 +2669,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2424 return mv
2669 return mv
2425 {-# INLINE liftToJSON2 #-}
2670 {-# INLINE liftToJSON2 #-}
2427− liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
2672+ liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = E.list id
2428− toEncoding a >*<
2673+ [ toEncoding a
2429− toEncoding b >*<
2674+ , toEncoding b
2430− toEncoding c >*<
2675+ , toEncoding c
2431− toEncoding d >*<
2676+ , toEncoding d
2432− toEncoding e >*<
2677+ , toEncoding e
2433− toEncoding f >*<
2678+ , toEncoding f
2434− toEncoding g >*<
2679+ , toEncoding g
2435− toEncoding h >*<
2680+ , toEncoding h
2436− toEncoding i >*<
2681+ , toEncoding i
2437− toEncoding j >*<
2682+ , toEncoding j
2438− toK k >*<
2683+ , toK k
2440 {-# INLINE liftToEncoding2 #-}
2686 {-# INLINE liftToEncoding2 #-}
2442instance (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
2688instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) => ToJSON1 ((,,,,,,,,,,,) a b c d e f g h i j k) where
@@ -2470,20 +2716,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2470 return mv
2716 return mv
2471 {-# INLINE liftToJSON2 #-}
2717 {-# INLINE liftToJSON2 #-}
2473− liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
2719+ liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = E.list id
2474− toEncoding a >*<
2720+ [ toEncoding a
2475− toEncoding b >*<
2721+ , toEncoding b
2476− toEncoding c >*<
2722+ , toEncoding c
2477− toEncoding d >*<
2723+ , toEncoding d
2478− toEncoding e >*<
2724+ , toEncoding e
2479− toEncoding f >*<
2725+ , toEncoding f
2480− toEncoding g >*<
2726+ , toEncoding g
2481− toEncoding h >*<
2727+ , toEncoding h
2482− toEncoding i >*<
2728+ , toEncoding i
2483− toEncoding j >*<
2729+ , toEncoding j
2484− toEncoding k >*<
2730+ , toEncoding k
2485− toL l >*<
2731+ , toL l
2487 {-# INLINE liftToEncoding2 #-}
2734 {-# INLINE liftToEncoding2 #-}
2489instance (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
2736instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) => ToJSON1 ((,,,,,,,,,,,,) a b c d e f g h i j k l) where
@@ -2518,21 +2765,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2518 return mv
2765 return mv
2519 {-# INLINE liftToJSON2 #-}
2766 {-# INLINE liftToJSON2 #-}
2521− liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
2768+ liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = E.list id
2522− toEncoding a >*<
2769+ [ toEncoding a
2523− toEncoding b >*<
2770+ , toEncoding b
2524− toEncoding c >*<
2771+ , toEncoding c
2525− toEncoding d >*<
2772+ , toEncoding d
2526− toEncoding e >*<
2773+ , toEncoding e
2527− toEncoding f >*<
2774+ , toEncoding f
2528− toEncoding g >*<
2775+ , toEncoding g
2529− toEncoding h >*<
2776+ , toEncoding h
2530− toEncoding i >*<
2777+ , toEncoding i
2531− toEncoding j >*<
2778+ , toEncoding j
2532− toEncoding k >*<
2779+ , toEncoding k
2533− toEncoding l >*<
2780+ , toEncoding l
2534− toM m >*<
2781+ , toM m
2536 {-# INLINE liftToEncoding2 #-}
2784 {-# INLINE liftToEncoding2 #-}
2538instance (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
2786instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m) => ToJSON1 ((,,,,,,,,,,,,,) a b c d e f g h i j k l m) where
@@ -2568,22 +2816,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2568 return mv
2816 return mv
2569 {-# INLINE liftToJSON2 #-}
2817 {-# INLINE liftToJSON2 #-}
2571− liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
2819+ liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = E.list id
2572− toEncoding a >*<
2820+ [ toEncoding a
2573− toEncoding b >*<
2821+ , toEncoding b
2574− toEncoding c >*<
2822+ , toEncoding c
2575− toEncoding d >*<
2823+ , toEncoding d
2576− toEncoding e >*<
2824+ , toEncoding e
2577− toEncoding f >*<
2825+ , toEncoding f
2578− toEncoding g >*<
2826+ , toEncoding g
2579− toEncoding h >*<
2827+ , toEncoding h
2580− toEncoding i >*<
2828+ , toEncoding i
2581− toEncoding j >*<
2829+ , toEncoding j
2582− toEncoding k >*<
2830+ , toEncoding k
2583− toEncoding l >*<
2831+ , toEncoding l
2584− toEncoding m >*<
2832+ , toEncoding m
2585− toN n >*<
2833+ , toN n
2587 {-# INLINE liftToEncoding2 #-}
2836 {-# INLINE liftToEncoding2 #-}
2589instance (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
2838instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m, ToJSON n) => ToJSON1 ((,,,,,,,,,,,,,,) a b c d e f g h i j k l m n) where
@@ -2598,27 +2847,26 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2598 toEncoding = toEncoding2
2847 toEncoding = toEncoding2
2599 {-# INLINE toEncoding #-}
2848 {-# INLINE toEncoding #-}
2601−-------------------------------------------------------------------------------
2850+--------------------------------------------------------------------------------
2602−-- pre-bytestring-0.10 compatibility
2603−-------------------------------------------------------------------------------
2605−{-# INLINE lazyToStrictByteString #-}
2852+-- | Wrap a list of pairs as an object.
2606−lazyToStrictByteString :: L.ByteString -> S.ByteString
2853+class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2607−#if MIN_VERSION_bytestring(0,10,0)
2854+ fromPairs :: pairs -> enc
2608−lazyToStrictByteString = L.toStrict
2610−lazyToStrictByteString = packChunks
2612−-- packChunks is taken from the blaze-builder package.
2856+instance (a ~ Value) => FromPairs (Encoding' a) Series where
2857+ fromPairs = E.pairs
2614−-- | Pack the chunks of a lazy bytestring into a single strict bytestring.
2859+instance FromPairs Value (DList Pair) where
2615−packChunks :: L.ByteString -> S.ByteString
2860+ fromPairs = object . toList
2616−packChunks lbs = do
2861+
2617− S.unsafeCreate (fromIntegral $ L.length lbs) (copyChunks lbs)
2862+-- | Like 'KeyValue' but the value is already converted to JSON
2618− where
2863+-- ('Value' or 'Encoding'), and the result actually represents lists of pairs
2619− copyChunks !L.Empty !_pf = return ()
2864+-- so it can be readily concatenated.
2620− copyChunks !(L.Chunk (S.PS fpbuf o l) lbs') !pf = do
2865+class Monoid kv => KeyValuePair v kv where
2621− withForeignPtr fpbuf $ \pbuf ->
2866+ pair :: String -> v -> kv
2622− copyBytes pf (pbuf `plusPtr` o) l
2867+
2623− copyChunks lbs' (pf `plusPtr` l)
2868+instance (v ~ Value) => KeyValuePair v (DList Pair) where
2624−#endif
2869+ pair k v = DList.singleton (pack k .= v)
2871+instance (e ~ Encoding) => KeyValuePair e Series where