@@ -1,29 +1,27 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DataKinds #-}
3−{-# LANGUAGE DefaultSignatures #-}
2+{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE EmptyDataDecls #-}
4−{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE FunctionalDependencies #-}
6−{-# LANGUAGE GADTs #-}
7+{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE NoImplicitPrelude #-}
8−{-# LANGUAGE NamedFieldPuns #-}
9−{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PatternGuards #-}
10+{-# LANGUAGE PatternGuards #-}
11+{-# LANGUAGE PolyKinds #-}
11−{-# LANGUAGE RecordWildCards #-}
12+{-# LANGUAGE RecordWildCards #-}
12−{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE ScopedTypeVariables #-}
13−{-# LANGUAGE TypeOperators #-}
14+{-# LANGUAGE TypeOperators #-}
14−{-# LANGUAGE TypeSynonymInstances #-}
15−{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE UndecidableInstances #-}
17−#if __GLASGOW_HASKELL__ >= 706
18−{-# LANGUAGE PolyKinds #-}
21#include "overlapping-compat.h"
17#include "overlapping-compat.h"
18+#include "incoherent-compat.h"
23-- TODO: Drop this when we remove support for Data.Attoparsec.Number
20-- TODO: Drop this when we remove support for Data.Attoparsec.Number
24{-# OPTIONS_GHC -fno-warn-deprecations #-}
21{-# OPTIONS_GHC -fno-warn-deprecations #-}
26−module Data.Aeson.Types.ToJSON (
23+module Data.Aeson.Types.ToJSON
27 -- * Core JSON classes
25 -- * Core JSON classes
28 ToJSON(..)
26 ToJSON(..)
29 -- * Liftings to unary and binary type constructors
27 -- * Liftings to unary and binary type constructors
@@ -35,7 +33,7 @@ module Data.Aeson.Types.ToJSON (
35 , toEncoding2
33 , toEncoding2
36 -- * Generic JSON classes
34 -- * Generic JSON classes
37 , GToJSON(..)
35 , GToJSON(..)
38− , GToEncoding(..)
36+ , ToArgs(..)
39 , genericToJSON
37 , genericToJSON
40 , genericToEncoding
38 , genericToEncoding
41 , genericLiftToJSON
39 , genericLiftToJSON
@@ -43,89 +41,107 @@ module Data.Aeson.Types.ToJSON (
43 -- * Classes and types for map keys
41 -- * Classes and types for map keys
44 , ToJSONKey(..)
42 , ToJSONKey(..)
45 , ToJSONKeyFunction(..)
43 , ToJSONKeyFunction(..)
46 , contramapToJSONKeyFunction
45 , contramapToJSONKeyFunction
47 -- * Object key-value pairs
50 -- * Object key-value pairs
48 , KeyValue(..)
51 , KeyValue(..)
49 -- * Functions needed for documentation
54 -- * Functions needed for documentation
50 -- * Encoding functions
55 -- * Encoding functions
51 , listEncoding
56 , listEncoding
52 , listValue
57 , listValue
56import Prelude.Compat
60import Prelude.Compat
62+import Control.Applicative (Const(..))
63+import Control.Monad.ST (ST)
58−import Data.Aeson.Encoding.Internal (Encoding (..), Series, dict, emptyArray_,
64+import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
59− tuple, (>*<))
65+import Data.Aeson.Encoding.Internal ((>*<))
60−import Data.Aeson.Functions (mapHashKeyVal, mapKeyVal)
66+import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
61−import Data.Aeson.Types.Generic
67+import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
62import Data.Aeson.Types.Internal
68import Data.Aeson.Types.Internal
64−import qualified Data.Aeson.Encode.Builder as EB
65−import qualified Data.Aeson.Encoding.Internal as E
67−import Control.Applicative (Const (..))
68−import Control.Monad.ST (ST)
69−import Data.Attoparsec.Number (Number (..))
69+import Data.Attoparsec.Number (Number(..))
70−import Data.Bits (unsafeShiftR)
70+import Data.Bits (unsafeShiftR)
71−import Data.DList (DList)
71+import Data.DList (DList)
72−import Data.Fixed (Fixed, HasResolution)
72+import Data.Fixed (Fixed, HasResolution, Nano)
73−import Data.Foldable (toList)
73+import Data.Foldable (toList)
74−import Data.Functor.Identity (Identity (..))
74+import Data.Functor.Compose (Compose(..))
75+import Data.Functor.Contravariant (Contravariant (..))
76+import Data.Functor.Identity (Identity(..))
77+import Data.Functor.Product (Product(..))
78+import Data.Functor.Sum (Sum(..))
75−import Data.Int (Int16, Int32, Int64, Int8)
79+import Data.Int (Int16, Int32, Int64, Int8)
76−import Data.List (intersperse)
80+import Data.List (intersperse)
77−import Data.List.NonEmpty (NonEmpty (..))
81+import Data.List.NonEmpty (NonEmpty(..))
78−import Data.Monoid (Dual (..), First (..), Last (..), (<>))
82+import Data.Semigroup ((<>))
79−import Data.Proxy (Proxy (..))
83+import Data.Proxy (Proxy(..))
80−import Data.Ratio (Ratio, denominator, numerator)
84+import Data.Ratio (Ratio, denominator, numerator)
81−import Data.Scientific (Scientific)
85+import Data.Scientific (Scientific)
82−import Data.Tagged (Tagged (..))
86+import Data.Tagged (Tagged(..))
83−import Data.Text (Text, pack)
87+import Data.Text (Text, pack)
84−import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
88+import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
85− 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 (..))
86−import Data.Time.Format (FormatTime, formatTime)
92+import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
87−import Data.Time.Locale.Compat (defaultTimeLocale)
88−import Data.Vector (Vector)
93+import Data.Vector (Vector)
89−import Data.Version (Version, showVersion)
94+import Data.Version (Version, showVersion)
95+import Data.Void (Void, absurd)
90−import Data.Word (Word16, Word32, Word64, Word8)
96+import Data.Word (Word16, Word32, Word64, Word8)
91−import Foreign.Storable (Storable)
97+import Foreign.Storable (Storable)
98+import Foreign.C.Types (CTime (..))
92import GHC.Generics
99import GHC.Generics
93−import Numeric.Natural (Natural)
100+import Numeric.Natural (Natural)
94−
101+import qualified Data.Aeson.Encoding as E
95−import qualified Data.ByteString.Builder as B
102+import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
96−import qualified Data.ByteString.Lazy as L
103+import qualified Data.ByteString.Lazy as L
97−import qualified Data.DList as DList
104+import qualified Data.DList as DList
98−import qualified Data.HashMap.Strict as H
105+import qualified Data.HashMap.Strict as H
99−import qualified Data.HashSet as HashSet
106+import qualified Data.HashSet as HashSet
100−import qualified Data.IntMap as IntMap
107+import qualified Data.IntMap as IntMap
101−import qualified Data.IntSet as IntSet
108+import qualified Data.IntSet as IntSet
102−import qualified Data.List.NonEmpty as NE
109+import qualified Data.List.NonEmpty as NE
103−import qualified Data.Map as M
110+import qualified Data.Map as M
111+import qualified Data.Monoid as Monoid
104−import qualified Data.Scientific as Scientific
112+import qualified Data.Scientific as Scientific
105−import qualified Data.Sequence as Seq
113+import qualified Data.Semigroup as Semigroup
114+import qualified Data.Sequence as Seq
106−import qualified Data.Set as Set
115+import qualified Data.Set as Set
107−import qualified Data.Text as T
116+import qualified Data.Text as T
108−import qualified Data.Text.Encoding as T
117+import qualified Data.Text.Encoding as T
109−import qualified Data.Text.Lazy as LT
118+import qualified Data.Text.Lazy as LT
110−import qualified Data.Text.Lazy.Builder as LTB
119+import qualified Data.Tree as Tree
111−import qualified Data.Text.Lazy.Builder.Int as LTBI
120+import qualified Data.UUID.Types as UUID
112−import qualified Data.Tree as Tree
113−import qualified Data.Vector as V
121+import qualified Data.Vector as V
114−import qualified Data.Vector.Generic as VG
122+import qualified Data.Vector.Generic as VG
115−import qualified Data.Vector.Mutable as VM
123+import qualified Data.Vector.Mutable as VM
116−import qualified Data.Vector.Primitive as VP
124+import qualified Data.Vector.Primitive as VP
117−import qualified Data.Vector.Storable as VS
125+import qualified Data.Vector.Storable as VS
118−import qualified Data.Vector.Unboxed as VU
126+import qualified Data.Vector.Unboxed as VU
120−builder :: ToJSON a => a -> B.Builder
128+import qualified Data.Aeson.Encoding.Builder as EB
121−builder = fromEncoding . toEncoding
129+import qualified Data.ByteString.Builder as B
131+import qualified GHC.Exts as Exts
132+import qualified Data.Primitive.Array as PM
133+import qualified Data.Primitive.SmallArray as PM
134+import qualified Data.Primitive.Types as PM
136+#if MIN_VERSION_primitive(0,6,4)
137+#if !MIN_VERSION_primitive(0,7,0)
138+import qualified Data.Primitive.UnliftedArray as PM
140+import qualified Data.Primitive.PrimArray as PM
123toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
124−toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
144+toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
125− mv <- VM.unsafeNew 2
126− VM.unsafeWrite mv 0 (keySerialiser a)
127− VM.unsafeWrite mv 1 (valSerializer b)
129{-# INLINE toJSONPair #-}
145{-# INLINE toJSONPair #-}
131realFloatToJSON :: RealFloat a => a -> Value
147realFloatToJSON :: RealFloat a => a -> Value
@@ -134,63 +150,59 @@ realFloatToJSON d
134 | otherwise = Number $ Scientific.fromFloatDigits d
150 | otherwise = Number $ Scientific.fromFloatDigits d
135{-# INLINE realFloatToJSON #-}
151{-# INLINE realFloatToJSON #-}
137−realFloatToEncoding :: RealFloat a => a -> Encoding
138−realFloatToEncoding d
139− | isNaN d || isInfinite d = Encoding EB.null_
140− | otherwise = toEncoding (Scientific.fromFloatDigits d)
141−{-# INLINE realFloatToEncoding #-}
143-------------------------------------------------------------------------------
153-------------------------------------------------------------------------------
144-- Generics
154-- Generics
145-------------------------------------------------------------------------------
155-------------------------------------------------------------------------------
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').
165+ -- It also provides a generic implementation of 'toEncoding'
166+ -- (with @enc ~ 'Encoding'@ and @arity ~ 'Zero'@)
167+ -- and 'liftToEncoding' (if the @arity@ is 'One').
153− gToJSON :: Options -> Proxy arity
168+ gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
154− -> (a -> Value) -> ([a] -> Value) -> f a -> Value
169+
155−
170+-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
156−-- | Class of generic representation types that can be converted to
171+-- function arguments that encode occurrences of the type parameter (for
157−-- a JSON 'Encoding'.
172+-- 'ToJSON1').
158−class GToEncoding arity f where
173+data ToArgs res arity a where
159− -- | This method (applied to 'defaultOptions') can be used as the
174+ NoToArgs :: ToArgs res Zero a
160− -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
175+ To1Args :: (a -> res) -> ([a] -> res) -> ToArgs res One a
161− -- or 'liftToEncoding' (if the @arity@ is 'One').
162− gToEncoding :: Options -> Proxy arity
163− -> (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
165-- | A configurable generic JSON creator. This function applied to
177-- | A configurable generic JSON creator. This function applied to
166-- 'defaultOptions' is used as the default for 'toJSON' when the type
178-- 'defaultOptions' is used as the default for 'toJSON' when the type
167-- is an instance of 'Generic'.
179-- is an instance of 'Generic'.
168−genericToJSON :: (Generic a, GToJSON Zero (Rep a))
180+genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
169 => Options -> a -> Value
181 => Options -> a -> Value
170−genericToJSON opts = gToJSON opts proxyZero undefined undefined . from
182+genericToJSON opts = gToJSON opts NoToArgs . from
172-- | A configurable generic JSON creator. This function applied to
184-- | A configurable generic JSON creator. This function applied to
173-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
174-- is an instance of 'Generic1'.
186-- is an instance of 'Generic1'.
175−genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
187+genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
176 => Options -> (a -> Value) -> ([a] -> Value)
188 => Options -> (a -> Value) -> ([a] -> Value)
177 -> f a -> Value
189 -> f a -> Value
178−genericLiftToJSON opts tj tjl = gToJSON opts proxyOne tj tjl . from1
190+genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
180-- | A configurable generic JSON encoder. This function applied to
192-- | A configurable generic JSON encoder. This function applied to
181-- 'defaultOptions' is used as the default for 'toEncoding' when the type
193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
182-- is an instance of 'Generic'.
194-- is an instance of 'Generic'.
183−genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
195+genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
184 => Options -> a -> Encoding
196 => Options -> a -> Encoding
185−genericToEncoding opts = gToEncoding opts proxyZero undefined undefined . from
197+genericToEncoding opts = gToJSON opts NoToArgs . from
187-- | A configurable generic JSON encoder. This function applied to
199-- | A configurable generic JSON encoder. This function applied to
188-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
189-- is an instance of 'Generic1'.
201-- is an instance of 'Generic1'.
190−genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
202+genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
191 => Options -> (a -> Encoding) -> ([a] -> Encoding)
203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
192 -> f a -> Encoding
204 -> f a -> Encoding
193−genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
205+genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
195-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
@@ -198,6 +210,9 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
199-- | 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'.
201-- An example type and instance:
216-- An example type and instance:
@@ -206,10 +221,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
207-- data Coord = Coord { x :: Double, y :: Double }
222-- data Coord = Coord { x :: Double, y :: Double }
209−-- instance ToJSON Coord where
224+-- instance 'ToJSON' Coord where
210−-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
225+-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
212−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
227+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
215-- Instead of manually writing your 'ToJSON' instance, there are two options
230-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -217,17 +232,15 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
218-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
219-- 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
220−-- so will probably be more efficient than the following two options:
235+-- so it will probably be more efficient than the following option.
222-- * The compiler can provide a default generic implementation for
237-- * The compiler can provide a default generic implementation for
223-- 'toJSON'.
238-- 'toJSON'.
225-- 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
226−-- datatype and declare a 'ToJSON' instance for your datatype without giving
241+-- datatype and declare a 'ToJSON' instance. If you require nothing other than
227−-- definitions for 'toJSON' or 'toEncoding'.
242+-- 'defaultOptions', it is sufficient to write (and this is the only
228−--
243+-- alternative where the default 'toJSON' implementation is sufficient):
229−-- For example, the previous example can be simplified to a more
233-- {-\# LANGUAGE DeriveGeneric \#-}
246-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -236,35 +249,46 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
237-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
239−-- instance ToJSON Coord where
252+-- instance 'ToJSON' Coord where
240−-- toEncoding = 'genericToEncoding' 'defaultOptions'
253+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
243−-- 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
244−-- 'toEncoding' function is a relatively new addition to this class.
257+-- to implement both methods:
245−-- To allow users of older versions of this library to upgrade without
246−-- having to edit all of their instances or encounter surprising
247−-- incompatibilities, the default implementation of 'toEncoding' uses
248−-- 'toJSON'. This produces correct results, but since it performs an
249−-- intermediate conversion to a 'Value', it will be less efficient
250−-- than directly emitting an 'Encoding'. Our one-liner definition of
251−-- 'toEncoding' above bypasses the intermediate 'Value'.
253−-- If @DefaultSignatures@ doesn't give exactly the results you want,
254−-- you can customize the generic encoding with only a tiny amount of
255−-- effort, using 'genericToJSON' and 'genericToEncoding' with your
256−-- preferred 'Options':
260+-- customOptions = 'defaultOptions'
261+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
259−-- instance ToJSON Coord where
264+-- instance 'ToJSON' Coord where
260−-- toJSON = 'genericToJSON' 'defaultOptions'
265+-- 'toJSON' = 'genericToJSON' customOptions
261−-- 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.)
263class ToJSON a where
287class ToJSON a where
264 -- | Convert a Haskell value to a JSON-friendly intermediate type.
288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
265 toJSON :: a -> Value
289 toJSON :: a -> Value
267− default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
291+ default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
268 toJSON = genericToJSON defaultOptions
292 toJSON = genericToJSON defaultOptions
270 -- | Encode a Haskell value as JSON.
294 -- | Encode a Haskell value as JSON.
@@ -281,12 +305,12 @@ class ToJSON a where
281 -- extension, and then have GHC generate a method body as follows.
305 -- extension, and then have GHC generate a method body as follows.
284− -- instance ToJSON Coord where
308+ -- instance 'ToJSON' Coord where
285− -- toEncoding = 'genericToEncoding' 'defaultOptions'
309+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
288 toEncoding :: a -> Encoding
312 toEncoding :: a -> Encoding
289− toEncoding = E.Encoding . EB.encodeToBuilder . toJSON
313+ toEncoding = E.value . toJSON
290 {-# INLINE toEncoding #-}
314 {-# INLINE toEncoding #-}
292 toJSONList :: [a] -> Value
316 toJSONList :: [a] -> Value
@@ -307,20 +331,27 @@ class KeyValue kv where
307 infixr 8 .=
331 infixr 8 .=
309instance KeyValue Series where
333instance KeyValue Series where
310− name .= value = E.Value $ E.text name <> E.colon <> toEncoding value
334+ name .= value = E.pair name (toEncoding value)
311 {-# INLINE (.=) #-}
335 {-# INLINE (.=) #-}
313instance KeyValue Pair where
337instance KeyValue Pair where
314 name .= value = (name, toJSON value)
338 name .= value = (name, toJSON value)
315 {-# 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)
317-------------------------------------------------------------------------------
348-------------------------------------------------------------------------------
318-- Classes and types for map keys
349-- Classes and types for map keys
319-------------------------------------------------------------------------------
350-------------------------------------------------------------------------------
321-- | Typeclass for types that can be used as the key of a map-like container
352-- | Typeclass for types that can be used as the key of a map-like container
322-- (like 'Map' or 'HashMap'). For example, since 'Text' has a 'ToJSONKey'
353-- (like 'Map' or 'HashMap'). For example, since 'Text' has a 'ToJSONKey'
323−-- instance and 'Char' has a 'ToJSON' instance, we can encode a value of
354+-- instance and 'Char' has a 'ToJSON' instance, we can encode a value of
324-- type 'Map' 'Text' 'Char':
355-- type 'Map' 'Text' 'Char':
326-- >>> LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]
357-- >>> LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]
@@ -346,29 +377,167 @@ instance KeyValue Pair where
346-- >>> LBC8.putStrLn $ encode $ Map.fromList [(["foo","bar","baz" :: Text], 'a')]
377-- >>> LBC8.putStrLn $ encode $ Map.fromList [(["foo","bar","baz" :: Text], 'a')]
347-- [[["foo","bar","baz"],"a"]]
378-- [[["foo","bar","baz"],"a"]]
349−-- The default implementation of 'ToJSONKey' will choose this method of
380+-- The default implementation of 'ToJSONKey' chooses this method of
350-- encoding a key, using the 'ToJSON' instance of the type.
381-- encoding a key, using the 'ToJSON' instance of the type.
383+-- To use your own data type as the key in a map, all that is needed
384+-- is to write a 'ToJSONKey' (and possibly a 'FromJSONKey') instance
385+-- for it. If the type cannot be trivially converted to and from 'Text',
386+-- it is recommended that 'ToJSONKeyValue' is used. Since the default
387+-- implementations of the typeclass methods can build this from a
388+-- 'ToJSON' instance, there is nothing that needs to be written:
390+-- > data Foo = Foo { fooAge :: Int, fooName :: Text }
391+-- > deriving (Eq,Ord,Generic)
392+-- > instance ToJSON Foo
393+-- > instance ToJSONKey Foo
395+-- That's it. We can now write:
397+-- >>> let m = Map.fromList [(Foo 4 "bar",'a'),(Foo 6 "arg",'b')]
398+-- >>> LBC8.putStrLn $ encode m
399+-- [[{"fooName":"bar","fooAge":4},"a"],[{"fooName":"arg","fooAge":6},"b"]]
401+-- The next case to consider is if we have a type that is a
402+-- newtype wrapper around 'Text'. The recommended approach is to use
403+-- generalized newtype deriving:
405+-- > newtype RecordId = RecordId { getRecordId :: Text }
406+-- > deriving (Eq,Ord,ToJSONKey)
408+-- Then we may write:
410+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(RecordId "abc",'a')]
413+-- Simple sum types are a final case worth considering. Suppose we have:
415+-- > data Color = Red | Green | Blue
416+-- > deriving (Show,Read,Eq,Ord)
418+-- It is possible to get the 'ToJSONKey' instance for free as we did
419+-- with 'Foo'. However, in this case, we have a natural way to go to
420+-- and from 'Text' that does not require any escape sequences. So
421+-- 'ToJSONKeyText' can be used instead of 'ToJSONKeyValue' to encode maps
422+-- as objects instead of arrays of pairs. This instance may be
423+-- implemented using generics as follows:
426+-- instance 'ToJSONKey' Color where
427+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
430+-- === __Low-level implementations__
432+-- The 'Show' instance can be used to help write 'ToJSONKey':
434+-- > instance ToJSONKey Color where
435+-- > toJSONKey = ToJSONKeyText f g
436+-- > where f = Text.pack . show
437+-- > g = text . Text.pack . show
438+-- > -- text function is from Data.Aeson.Encoding
440+-- The situation of needing to turning function @a -> Text@ into
441+-- a 'ToJSONKeyFunction' is common enough that a special combinator
442+-- is provided for it. The above instance can be rewritten as:
444+-- > instance ToJSONKey Color where
445+-- > toJSONKey = toJSONKeyText (Text.pack . show)
447+-- The performance of the above instance can be improved by
448+-- not using 'String' as an intermediate step when converting to
449+-- 'Text'. One option for improving performance would be to use
450+-- template haskell machinery from the @text-show@ package. However,
451+-- even with the approach, the 'Encoding' (a wrapper around a bytestring
452+-- builder) is generated by encoding the 'Text' to a 'ByteString',
453+-- an intermediate step that could be avoided. The fastest possible
454+-- implementation would be:
456+-- > -- Assuming that OverloadedStrings is enabled
457+-- > instance ToJSONKey Color where
458+-- > toJSONKey = ToJSONKeyText f g
459+-- > where f x = case x of {Red -> "Red";Green ->"Green";Blue -> "Blue"}
460+-- > g x = case x of {Red -> text "Red";Green -> text "Green";Blue -> text "Blue"}
461+-- > -- text function is from Data.Aeson.Encoding
463+-- This works because GHC can lift the encoded values out of the case
464+-- statements, which means that they are only evaluated once. This
465+-- approach should only be used when there is a serious need to
466+-- maximize performance.
351class ToJSONKey a where
468class ToJSONKey a where
352− -- | Provides a way to use a data type as the key for a map-like
469+ -- | Strategy for rendering the key for a map-like container.
354 toJSONKey :: ToJSONKeyFunction a
470 toJSONKey :: ToJSONKeyFunction a
355 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
471 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
356− toJSONKey = ToJSONKeyValue (toJSON, toEncoding)
472+ toJSONKey = ToJSONKeyValue toJSON toEncoding
357 -- | This is similar in spirit to the 'showsList' method of 'Show'.
474 -- | This is similar in spirit to the 'showsList' method of 'Show'.
358 -- It makes it possible to give 'String' keys special treatment
475 -- It makes it possible to give 'String' keys special treatment
359− -- without using @OverlappingInstances@.
476+ -- without using @OverlappingInstances@. End users should always
477+ -- be able to use the default implementation of this method.
360 toJSONKeyList :: ToJSONKeyFunction [a]
478 toJSONKeyList :: ToJSONKeyFunction [a]
361 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
479 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
362− toJSONKeyList = ToJSONKeyValue (toJSON, toEncoding)
480+ toJSONKeyList = ToJSONKeyValue toJSON toEncoding
364data ToJSONKeyFunction a
482data ToJSONKeyFunction a
483+ = ToJSONKeyText !(a -> Text) !(a -> Encoding' Text)
365− = ToJSONKeyText (a -> Text, a -> Encoding) -- ^ key is encoded to string, produces object
484+ -- ^ key is encoded to string, produces object
485+ | ToJSONKeyValue !(a -> Value) !(a -> Encoding)
366− | ToJSONKeyValue (a -> Value, a -> Encoding) -- ^ key is encoded to value, produces array
486+ -- ^ key is encoded to value, produces array
488+-- | Helper for creating textual keys.
491+-- instance 'ToJSONKey' MyKey where
492+-- 'toJSONKey' = 'toJSONKeyText' myKeyToText
494+-- myKeyToText = Text.pack . show -- or showt from text-show
496+toJSONKeyText :: (a -> Text) -> ToJSONKeyFunction a
497+toJSONKeyText f = ToJSONKeyText f (E.text . f)
499+-- | TODO: should this be exported?
500+toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
501+toJSONKeyTextEnc e = ToJSONKeyText tot e
503+ -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
504+ tot = T.dropAround (== '"')
507+ . E.encodingToLazyByteString
510+instance Contravariant ToJSONKeyFunction where
511+ contramap = contramapToJSONKeyFunction
513+-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
368contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
369contramapToJSONKeyFunction h x = case x of
515contramapToJSONKeyFunction h x = case x of
370− ToJSONKeyText (f,g) -> ToJSONKeyText (f . h, g . h)
516+ ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
371− 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
373-------------------------------------------------------------------------------
542-------------------------------------------------------------------------------
374-- Lifings of FromJSON and ToJSON to unary and binary type constructors
543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
@@ -382,7 +551,7 @@ contramapToJSONKeyFunction h x = case x of
383-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
384-- 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
385−-- so will probably be more efficient than the following two options:
554+-- so it will probably be more efficient than the following option.
387-- * The compiler can provide a default generic implementation for
556-- * The compiler can provide a default generic implementation for
388-- 'toJSON1'.
557-- 'toJSON1'.
@@ -400,23 +569,29 @@ contramapToJSONKeyFunction h x = case x of
401-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
403−-- instance ToJSON a => ToJSON1 (Pair a)
572+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
406−-- If @DefaultSignatures@ doesn't give exactly the results you want,
575+-- If the default implementation doesn't give exactly the results you want,
407-- 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
408-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
409-- your preferred 'Options':
578-- your preferred 'Options':
581+-- customOptions = 'defaultOptions'
582+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
412−-- instance ToJSON a => ToJSON1 (Pair a) where
585+-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
413−-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
586+-- 'liftToJSON' = 'genericLiftToJSON' customOptions
414−-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
587+-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
590+-- See also 'ToJSON'.
416class ToJSON1 f where
591class ToJSON1 f where
417 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
419− default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
594+ default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
420 => (a -> Value) -> ([a] -> Value) -> f a -> Value
595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
421 liftToJSON = genericLiftToJSON defaultOptions
596 liftToJSON = genericLiftToJSON defaultOptions
@@ -425,7 +600,7 @@ class ToJSON1 f where
426 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
428− default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
603+ default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
429 => (a -> Encoding) -> ([a] -> Encoding)
604 => (a -> Encoding) -> ([a] -> Encoding)
430 -> f a -> Encoding
605 -> f a -> Encoding
431 liftToEncoding = genericLiftToEncoding defaultOptions
606 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -473,9 +648,25 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
473-- Encoding functions
648-- Encoding functions
474-------------------------------------------------------------------------------
649-------------------------------------------------------------------------------
651+-- | Helper function to use with 'liftToEncoding'.
652+-- Useful when writing own 'ToJSON1' instances.
655+-- newtype F a = F [a]
657+-- -- This instance encodes 'String' as an array of chars
658+-- instance 'ToJSON1' F where
659+-- 'liftToJSON' tj _ (F xs) = 'liftToJSON' tj ('listValue' tj) xs
660+-- 'liftToEncoding' te _ (F xs) = 'liftToEncoding' te ('listEncoding' te) xs
662+-- instance 'Data.Aeson.FromJSON.FromJSON1' F where
663+-- 'Data.Aeson.FromJSON.liftParseJSON' p _ v = F \<$\> 'Data.Aeson.FromJSON.liftParseJSON' p ('Data.Aeson.FromJSON.listParser' p) v
476listEncoding :: (a -> Encoding) -> [a] -> Encoding
665listEncoding :: (a -> Encoding) -> [a] -> Encoding
477listEncoding = E.list
666listEncoding = E.list
667+{-# INLINE listEncoding #-}
669+-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
479listValue :: (a -> Value) -> [a] -> Value
670listValue :: (a -> Value) -> [a] -> Value
480listValue f = Array . V.fromList . map f
671listValue f = Array . V.fromList . map f
481{-# INLINE listValue #-}
672{-# INLINE listValue #-}
@@ -494,6 +685,11 @@ instance ToJSON1 [] where
494 {-# INLINE liftToEncoding #-}
685 {-# INLINE liftToEncoding #-}
496instance (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] #-}
497 toJSON = toJSON1
693 toJSON = toJSON1
498 {-# INLINE toJSON #-}
694 {-# INLINE toJSON #-}
@@ -504,283 +700,260 @@ instance (ToJSON a) => ToJSON [a] where
504-- Generic toJSON / toEncoding
700-- Generic toJSON / toEncoding
505-------------------------------------------------------------------------------
701-------------------------------------------------------------------------------
703+instance OVERLAPPABLE_ (GToJSON enc arity a) => GToJSON enc arity (M1 i c a) where
704+ -- Meta-information, which is not handled elsewhere, is ignored:
705+ gToJSON opts targs = gToJSON opts targs . unM1
706+ {-# INLINE gToJSON #-}
708+instance GToJSON enc One Par1 where
709+ -- Direct occurrences of the last type parameter are encoded with the
710+ -- function passed in as an argument:
711+ gToJSON _opts (To1Args tj _) = tj . unPar1
712+ {-# INLINE gToJSON #-}
714+instance ( ConsToJSON enc arity a
715+ , AllNullary (C1 c a) allNullary
716+ , SumToJSON enc arity (C1 c a) allNullary
717+ ) => GToJSON enc arity (D1 d (C1 c a)) where
718+ -- The option 'tagSingleConstructors' determines whether to wrap
719+ -- a single-constructor type.
721+ | tagSingleConstructors opts = (unTagged :: Tagged allNullary enc -> enc)
722+ . sumToJSON opts targs
724+ | otherwise = consToJSON opts targs . unM1 . unM1
725+ {-# INLINE gToJSON #-}
727+instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
728+ -- Constructors need to be encoded differently depending on whether they're
729+ -- a record or not. This distinction is made by 'consToJSON':
730+ gToJSON opts targs = consToJSON opts targs . unM1
731+ {-# INLINE gToJSON #-}
733+instance ( AllNullary (a :+: b) allNullary
734+ , SumToJSON enc arity (a :+: b) allNullary
735+ ) => GToJSON enc arity (a :+: b)
737+ -- If all constructors of a sum datatype are nullary and the
738+ -- 'allNullaryToStringTag' option is set they are encoded to
739+ -- strings. This distinction is made by 'sumToJSON':
740+ gToJSON opts targs = (unTagged :: Tagged allNullary enc -> enc)
741+ . sumToJSON opts targs
742+ {-# INLINE gToJSON #-}
507--------------------------------------------------------------------------------
744--------------------------------------------------------------------------------
508-- Generic toJSON
745-- Generic toJSON
510−instance OVERLAPPABLE_ (GToJSON arity a) => GToJSON arity (M1 i c a) where
747+-- Note: Refactoring 'ToJSON a' to 'ToJSON enc a' (and 'ToJSON1' similarly) is
511− -- Meta-information, which is not handled elsewhere, is ignored:
748+-- possible but makes error messages a bit harder to understand for missing
512− gToJSON opts pa tj tjl = gToJSON opts pa tj tjl . unM1
749+-- instances.
514−instance (ToJSON a) => GToJSON arity (K1 i a) where
751+instance GToJSON Value arity V1 where
515− -- Constant values are encoded using their ToJSON instance:
752+ -- Empty values do not exist, which makes the job of formatting them
516− gToJSON _opts _ _ _ = toJSON . unK1
753+ -- rather easy:
754+ gToJSON _ _ x = x `seq` error "case: V1"
755+ {-# INLINE gToJSON #-}
518−instance GToJSON One Par1 where
757+instance ToJSON a => GToJSON Value arity (K1 i a) where
519− -- Direct occurrences of the last type parameter are encoded with the
758+ -- Constant values are encoded using their ToJSON instance:
520− -- function passed in as an argument:
521− gToJSON _opts _ tj _ = tj . unPar1
759+ gToJSON _opts _ = toJSON . unK1
760+ {-# INLINE gToJSON #-}
523−instance (ToJSON1 f) => GToJSON One (Rec1 f) where
762+instance ToJSON1 f => GToJSON Value One (Rec1 f) where
524 -- Recursive occurrences of the last type parameter are encoded using their
763 -- Recursive occurrences of the last type parameter are encoded using their
525 -- ToJSON1 instance:
764 -- ToJSON1 instance:
526− gToJSON _opts _ tj tjl = liftToJSON tj tjl . unRec1
765+ gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766+ {-# INLINE gToJSON #-}
528−instance GToJSON arity U1 where
768+instance GToJSON Value arity U1 where
529 -- Empty constructors are encoded to an empty array:
769 -- Empty constructors are encoded to an empty array:
530− gToJSON _opts _ _ _ _ = emptyArray
770+ gToJSON _opts _ _ = emptyArray
531−
771+ {-# INLINE gToJSON #-}
532−instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
533− -- Constructors need to be encoded differently depending on whether they're
534− -- a record or not. This distinction is made by 'consToJSON':
535− gToJSON opts pa tj tjl = consToJSON opts pa tj tjl . unM1
537instance ( WriteProduct arity a, WriteProduct arity b
773instance ( WriteProduct arity a, WriteProduct arity b
538 , ProductSize a, ProductSize b
774 , ProductSize a, ProductSize b
539− ) => GToJSON arity (a :*: b) where
775+ ) => GToJSON Value arity (a :*: b)
540 -- 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
541 -- 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
542 -- 'writeProduct':
779 -- 'writeProduct':
543− gToJSON opts pa tj tjl p =
780+ gToJSON opts targs p =
544 Array $ V.create $ do
781 Array $ V.create $ do
545 mv <- VM.unsafeNew lenProduct
782 mv <- VM.unsafeNew lenProduct
546− writeProduct opts pa mv 0 lenProduct tj tjl p
783+ writeProduct opts targs mv 0 lenProduct p
547 return mv
784 return mv
549 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
550 productSize
787 productSize
788+ {-# INLINE gToJSON #-}
552−instance ( AllNullary (a :+: b) allNullary
790+instance ( ToJSON1 f
553− , SumToJSON arity (a :+: b) allNullary
791+ , GToJSON Value One g
554− ) => GToJSON arity (a :+: b) where
792+ ) => GToJSON Value One (f :.: g)
555− -- If all constructors of a sum datatype are nullary and the
793+ where
556− -- 'allNullaryToStringTag' option is set they are encoded to
557− -- strings. This distinction is made by 'sumToJSON':
558− gToJSON opts pa tj tjl = (unTagged :: Tagged allNullary Value -> Value)
559− . sumToJSON opts pa tj tjl
561−instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
562 -- 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
563 -- 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
564 -- instance to generically encode the innermost type:
796 -- instance to generically encode the innermost type:
565− gToJSON opts pa tj tjl =
797+ gToJSON opts targs =
566− let gtj = gToJSON opts pa tj tjl in
798+ let gtj = gToJSON opts targs in
567 liftToJSON gtj (listValue gtj) . unComp1
799 liftToJSON gtj (listValue gtj) . unComp1
800+ {-# INLINE gToJSON #-}
569--------------------------------------------------------------------------------
802--------------------------------------------------------------------------------
570-- Generic toEncoding
803-- Generic toEncoding
572−instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
573− -- Meta-information, which is not handled elsewhere, is ignored:
574− gToEncoding opts pa te tel = gToEncoding opts pa te tel . unM1
576−instance (ToJSON a) => GToEncoding arity (K1 i a) where
805+instance ToJSON a => GToJSON Encoding arity (K1 i a) where
577 -- Constant values are encoded using their ToJSON instance:
806 -- Constant values are encoded using their ToJSON instance:
578− gToEncoding _opts _ _ _ = toEncoding . unK1
807+ gToJSON _opts _ = toEncoding . unK1
579−
808+ {-# INLINE gToJSON #-}
580−instance GToEncoding One Par1 where
581− -- Direct occurrences of the last type parameter are encoded with the
582− -- function passed in as an argument:
583− gToEncoding _opts _ te _ = te . unPar1
585−instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
810+instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
586 -- Recursive occurrences of the last type parameter are encoded using their
811 -- Recursive occurrences of the last type parameter are encoded using their
587 -- ToEncoding1 instance:
812 -- ToEncoding1 instance:
588− gToEncoding _opts _ te tel = liftToEncoding te tel . unRec1
813+ gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814+ {-# INLINE gToJSON #-}
590−instance GToEncoding arity U1 where
816+instance GToJSON Encoding arity U1 where
591 -- Empty constructors are encoded to an empty array:
817 -- Empty constructors are encoded to an empty array:
592− gToEncoding _opts _ _ _ _ = E.emptyArray_
818+ gToJSON _opts _ _ = E.emptyArray_
593−
819+ {-# INLINE gToJSON #-}
594−instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
595− -- Constructors need to be encoded differently depending on whether they're
596− -- a record or not. This distinction is made by 'consToEncoding':
597− gToEncoding opts pa te tel = consToEncoding opts pa te tel . unM1
599instance ( EncodeProduct arity a
821instance ( EncodeProduct arity a
600 , EncodeProduct arity b
822 , EncodeProduct arity b
601− ) => GToEncoding arity (a :*: b) where
823+ ) => GToJSON Encoding arity (a :*: b)
602 -- 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
603 -- 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
604 -- 'encodeProduct':
827 -- 'encodeProduct':
605− gToEncoding opts pa te tel p = E.tuple $ encodeProduct opts pa te tel p
828+ gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
829+ {-# INLINE gToJSON #-}
607−instance ( AllNullary (a :+: b) allNullary
831+instance ( ToJSON1 f
608− , SumToEncoding arity (a :+: b) allNullary
832+ , GToJSON Encoding One g
609− ) => GToEncoding arity (a :+: b) where
833+ ) => GToJSON Encoding One (f :.: g)
610− -- If all constructors of a sum datatype are nullary and the
834+ where
611− -- 'allNullaryToStringTag' option is set they are encoded to
612− -- strings. This distinction is made by 'sumToEncoding':
613− gToEncoding opts pa te tel
614− = (unTagged :: Tagged allNullary Encoding -> Encoding)
615− . sumToEncoding opts pa te tel
617−instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
618 -- 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
619 -- 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
620 -- instance to generically encode the innermost type:
837 -- instance to generically encode the innermost type:
621− gToEncoding opts pa te tel =
838+ gToJSON opts targs =
622− let gte = gToEncoding opts pa te tel in
839+ let gte = gToJSON opts targs in
623 liftToEncoding gte (listEncoding gte) . unComp1
840 liftToEncoding gte (listEncoding gte) . unComp1
841+ {-# INLINE gToJSON #-}
625--------------------------------------------------------------------------------
843--------------------------------------------------------------------------------
627−class SumToJSON arity f allNullary where
845+class SumToJSON enc arity f allNullary where
628− sumToJSON :: Options -> Proxy arity
846+ sumToJSON :: Options -> ToArgs enc arity a
629− -> (a -> Value) -> ([a] -> Value)
630− -> f a -> Tagged allNullary Value
847+ -> f a -> Tagged allNullary enc
632−instance ( GetConName f
849+instance ( GetConName f
633− , TaggedObjectPairs arity f
850+ , FromString enc
634− , ObjectWithSingleFieldObj arity f
851+ , TaggedObject enc arity f
635− , TwoElemArrayObj arity f
852+ , SumToJSON' ObjectWithSingleField enc arity f
853+ , SumToJSON' TwoElemArray enc arity f
854+ , SumToJSON' UntaggedValue enc arity f
636− ) => SumToJSON arity f True where
855+ ) => SumToJSON enc arity f True
637− sumToJSON opts pa tj tjl
857+ sumToJSON opts targs
638− | allNullaryToStringTag opts = Tagged . String . pack
858+ | allNullaryToStringTag opts = Tagged . fromString
639 . constructorTagModifier opts . getConName
859 . constructorTagModifier opts . getConName
640− | otherwise = Tagged . nonAllNullarySumToJSON opts pa tj tjl
860+ | otherwise = Tagged . nonAllNullarySumToJSON opts targs
642−instance ( TwoElemArrayObj arity f
862+instance ( TaggedObject enc arity f
643− , TaggedObjectPairs arity f
863+ , SumToJSON' ObjectWithSingleField enc arity f
644− , ObjectWithSingleFieldObj arity f
864+ , SumToJSON' TwoElemArray enc arity f
865+ , SumToJSON' UntaggedValue enc arity f
645− ) => SumToJSON arity f False where
866+ ) => SumToJSON enc arity f False
646− sumToJSON opts pa tj tjl = Tagged . nonAllNullarySumToJSON opts pa tj tjl
868+ sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
648−nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
870+nonAllNullarySumToJSON :: ( TaggedObject enc arity f
649− , TaggedObjectPairs arity f
871+ , SumToJSON' ObjectWithSingleField enc arity f
650− , ObjectWithSingleFieldObj arity f
872+ , SumToJSON' TwoElemArray enc arity f
873+ , SumToJSON' UntaggedValue enc arity f
651− ) => Options -> Proxy arity
874+ ) => Options -> ToArgs enc arity a
652− -> (a -> Value) -> ([a] -> Value)
875+ -> f a -> enc
654−nonAllNullarySumToJSON opts pa tj tjl =
876+nonAllNullarySumToJSON opts targs =
655 case sumEncoding opts of
877 case sumEncoding opts of
657− object . taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl
658− ObjectWithSingleField -> Object . objectWithSingleFieldObj opts pa tj tjl
659− TwoElemArray -> Array . twoElemArrayObj opts pa tj tjl
661−--------------------------------------------------------------------------------
663−class SumToEncoding arity f allNullary where
664− sumToEncoding :: Options -> Proxy arity
665− -> (a -> Encoding) -> ([a] -> Encoding)
666− -> f a -> Tagged allNullary Encoding
668−instance ( GetConName f
669− , TaggedObjectEnc arity f
670− , ObjectWithSingleFieldEnc arity f
671− , TwoElemArrayEnc arity f
672− ) => SumToEncoding arity f True where
673− sumToEncoding opts pa te tel
674− | allNullaryToStringTag opts = Tagged . toEncoding .
675− constructorTagModifier opts . getConName
676− | otherwise = Tagged . nonAllNullarySumToEncoding opts pa te tel
678−instance ( TwoElemArrayEnc arity f
679− , TaggedObjectEnc arity f
680− , ObjectWithSingleFieldEnc arity f
681− ) => SumToEncoding arity f False where
682− sumToEncoding opts pa te tel = Tagged . nonAllNullarySumToEncoding opts pa te tel
684−nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
685− , TaggedObjectEnc arity f
686− , ObjectWithSingleFieldEnc arity f
687− ) => Options -> Proxy arity
688− -> (a -> Encoding) -> ([a] -> Encoding)
690−nonAllNullarySumToEncoding opts pa te tel =
691− case sumEncoding opts of
692 TaggedObject{..} ->
879 TaggedObject{..} ->
693− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel
880+ taggedObject opts targs tagFieldName contentsFieldName
694− ObjectWithSingleField -> objectWithSingleFieldEnc opts pa te tel
695− TwoElemArray -> twoElemArrayEnc opts pa te tel
697−--------------------------------------------------------------------------------
882+ ObjectWithSingleField ->
883+ (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884+ . sumToJSON' opts targs
699−class TaggedObjectPairs arity f where
886+ TwoElemArray ->
700− taggedObjectPairs :: Options -> Proxy arity
887+ (unTagged :: Tagged TwoElemArray enc -> enc)
701− -> String -> String
888+ . sumToJSON' opts targs
702− -> (a -> Value) -> ([a] -> Value)
889+
703− -> f a -> [Pair]
890+ UntaggedValue ->
705−instance ( TaggedObjectPairs arity a
706− , TaggedObjectPairs arity b
707− ) => TaggedObjectPairs arity (a :+: b) where
708− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (L1 x) =
709− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
710− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (R1 x) =
711− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
713−instance ( IsRecord a isRecord
714− , TaggedObjectPairs' arity a isRecord
716− ) => TaggedObjectPairs arity (C1 c a) where
717− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl =
718− (pack tagFieldName .= constructorTagModifier opts
719− (conName (undefined :: t c a p)) :) .
720− (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
891+ (unTagged :: Tagged UntaggedValue enc -> enc)
721− taggedObjectPairs' opts pa contentsFieldName tj tjl . unM1
892+ . sumToJSON' opts targs
723−class TaggedObjectPairs' arity f isRecord where
894+--------------------------------------------------------------------------------
724− taggedObjectPairs' :: Options -> Proxy arity
725− -> String -> (a -> Value) -> ([a] -> Value)
726− -> f a -> Tagged isRecord [Pair]
728−instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
896+class FromString enc where
729− taggedObjectPairs' _ _ _ _ _ _ = Tagged []
897+ fromString :: String -> enc
731−instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
899+instance FromString Encoding where
732− taggedObjectPairs' opts pa _ tj tjl =
900+ fromString = toEncoding
733− Tagged . toList . recordToPairs opts pa tj tjl
735−instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
902+instance FromString Value where
736− taggedObjectPairs' opts pa contentsFieldName tj tjl =
903+ fromString = String . pack
737− Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts pa tj tjl
739--------------------------------------------------------------------------------
905--------------------------------------------------------------------------------
741−class TaggedObjectEnc arity f where
907+class TaggedObject enc arity f where
742− taggedObjectEnc :: Options -> Proxy arity
908+ taggedObject :: Options -> ToArgs enc arity a
743− -> String -> String
909+ -> String -> String
744− -> (a -> Encoding) -> ([a] -> Encoding)
910+ -> f a -> enc
745− -> f a -> Encoding
911+
747−instance ( TaggedObjectEnc arity a
912+instance ( TaggedObject enc arity a
748− , TaggedObjectEnc arity b
913+ , TaggedObject enc arity b
749− ) => TaggedObjectEnc arity (a :+: b) where
914+ ) => TaggedObject enc arity (a :+: b)
750− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (L1 x) =
916+ taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
751− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
917+ taggedObject opts targs tagFieldName contentsFieldName x
752− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (R1 x) =
918+ taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
753− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
919+ taggedObject opts targs tagFieldName contentsFieldName x
755−instance ( IsRecord a isRecord
921+instance ( IsRecord a isRecord
756− , TaggedObjectEnc' arity a isRecord
922+ , TaggedObject' enc pairs arity a isRecord
923+ , FromPairs enc pairs
925+ , KeyValuePair enc pairs
757 , Constructor c
926 , Constructor c
758− ) => TaggedObjectEnc arity (C1 c a) where
927+ ) => TaggedObject enc arity (C1 c a)
759− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel v =
929+ taggedObject opts targs tagFieldName contentsFieldName =
760− E.openCurly <>
930+ fromPairs . mappend tag . contents
761− (toEncoding tagFieldName <>
932+ tag = tagFieldName `pair`
763− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) <>
933+ (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
764− ((unTagged :: Tagged isRecord Encoding -> Encoding) .
936+ (unTagged :: Tagged isRecord pairs -> pairs) .
765− taggedObjectEnc' opts pa contentsFieldName te tel . unM1 $ v) <>
937+ taggedObject' opts targs contentsFieldName . unM1
768−class TaggedObjectEnc' arity f isRecord where
939+class TaggedObject' enc pairs arity f isRecord where
769− taggedObjectEnc' :: Options -> Proxy arity
940+ taggedObject' :: Options -> ToArgs enc arity a
770− -> String -> (a -> Encoding) -> ([a] -> Encoding)
941+ -> String -> f a -> Tagged isRecord pairs
771− -> f a -> Tagged isRecord Encoding
942+
772−
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
773−instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
950+instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
774− taggedObjectEnc' _ _ _ _ _ _ = Tagged mempty
951+ taggedObject' _ _ _ _ = Tagged mempty
776−instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
953+instance ( RecordToPairs enc pairs arity f
777− taggedObjectEnc' opts pa _ te tel = Tagged . (E.comma <>) . fst
954+ ) => TaggedObject' enc pairs arity f True
778− . recordToEncoding opts pa te tel
955+ where
779−
956+ taggedObject' opts targs _ = Tagged . recordToPairs opts targs
780−instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
781− taggedObjectEnc' opts pa contentsFieldName te tel =
782− Tagged . (\z -> E.comma <> toEncoding contentsFieldName <> E.colon <> z) .
783− gToEncoding opts pa te tel
785--------------------------------------------------------------------------------
958--------------------------------------------------------------------------------
@@ -795,283 +968,242 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
795instance (Constructor c) => GetConName (C1 c a) where
968instance (Constructor c) => GetConName (C1 c a) where
796 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
798--------------------------------------------------------------------------------
983--------------------------------------------------------------------------------
800−class TwoElemArrayObj arity f where
985+class SumToJSON' s enc arity f where
801− twoElemArrayObj :: Options -> Proxy arity
986+ sumToJSON' :: Options -> ToArgs enc arity a
802− -> (a -> Value) -> ([a] -> Value)
987+ -> f a -> Tagged s enc
803− -> f a -> V.Vector Value
805−instance ( TwoElemArrayObj arity a
989+instance ( SumToJSON' s enc arity a
806− , TwoElemArrayObj arity b
990+ , SumToJSON' s enc arity b
807− ) => TwoElemArrayObj arity (a :+: b) where
991+ ) => SumToJSON' s enc arity (a :+: b)
808− twoElemArrayObj opts pa tj tjl (L1 x) = twoElemArrayObj opts pa tj tjl x
992+ where
809− twoElemArrayObj opts pa tj tjl (R1 x) = twoElemArrayObj opts pa tj tjl x
993+ sumToJSON' opts targs (L1 x) = sumToJSON' opts targs x
994+ sumToJSON' opts targs (R1 x) = sumToJSON' opts targs x
996+--------------------------------------------------------------------------------
811−instance ( GToJSON arity a
998+instance ( GToJSON Value arity a
812− , ConsToJSON arity a
999+ , ConsToJSON Value arity a
813 , Constructor c
1000 , Constructor c
814− ) => TwoElemArrayObj arity (C1 c a) where
1001+ ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
815− twoElemArrayObj opts pa tj tjl x = V.create $ do
1002+ sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
816 mv <- VM.unsafeNew 2
1003 mv <- VM.unsafeNew 2
817 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
1004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
818 $ conName (undefined :: t c a p)
1005 $ conName (undefined :: t c a p)
819− VM.unsafeWrite mv 1 $ gToJSON opts pa tj tjl x
1006+ VM.unsafeWrite mv 1 $ gToJSON opts targs x
820 return mv
1007 return mv
822--------------------------------------------------------------------------------
1009--------------------------------------------------------------------------------
824−class TwoElemArrayEnc arity f where
1011+instance ( GToJSON Encoding arity a
825− twoElemArrayEnc :: Options -> Proxy arity
1012+ , ConsToJSON Encoding arity a
826− -> (a -> Encoding) -> ([a] -> Encoding)
829−instance ( TwoElemArrayEnc arity a
830− , TwoElemArrayEnc arity b
831− ) => TwoElemArrayEnc arity (a :+: b) where
832− twoElemArrayEnc opts pa te tel (L1 x) = twoElemArrayEnc opts pa te tel x
833− twoElemArrayEnc opts pa te tel (R1 x) = twoElemArrayEnc opts pa te tel x
835−instance ( GToEncoding arity a
836− , ConsToEncoding arity a
837 , Constructor c
1013 , Constructor c
838− ) => TwoElemArrayEnc arity (C1 c a) where
1014+ ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
839− twoElemArrayEnc opts pa te tel x = E.tuple $
1015+ where
1016+ sumToJSON' opts targs x = Tagged $ E.list id
840− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
1017+ [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
841− gToEncoding opts pa te tel x
1018+ , gToJSON opts targs x
843−--------------------------------------------------------------------------------
845−class ConsToJSON arity f where
846− consToJSON :: Options -> Proxy arity
847− -> (a -> Value) -> ([a] -> Value)
850−class ConsToJSON' arity f isRecord where
851− consToJSON' :: Options -> Proxy arity
852− -> Bool -- ^ Are we a record with one field?
853− -> (a -> Value) -> ([a] -> Value)
854− -> f a -> Tagged isRecord Value
856−instance ( IsRecord f isRecord
857− , ConsToJSON' arity f isRecord
858− ) => ConsToJSON arity f where
859− consToJSON opts pa tj tjl =
860− (unTagged :: Tagged isRecord Value -> Value)
861− . consToJSON' opts pa (isUnary (undefined :: f a)) tj tjl
863−instance (RecordToPairs arity f) => ConsToJSON' arity f True where
864− consToJSON' opts pa isUn tj tjl f = let
865− vals = toList $ recordToPairs opts pa tj tjl f
866− in case (unwrapUnaryRecords opts,isUn,vals) of
867− (True,True,[(_,val)]) -> Tagged val
868− _ -> Tagged $ object vals
870−instance GToJSON arity f => ConsToJSON' arity f False where
871− consToJSON' opts pa _ tj tjl = Tagged . gToJSON opts pa tj tjl
873--------------------------------------------------------------------------------
1021--------------------------------------------------------------------------------
875−class ConsToEncoding arity f where
1023+class ConsToJSON enc arity f where
876− consToEncoding :: Options -> Proxy arity
1024+ consToJSON :: Options -> ToArgs enc arity a
877− -> (a -> Encoding) -> ([a] -> Encoding)
1025+ -> f a -> enc
880−class ConsToEncoding' arity f isRecord where
1027+class ConsToJSON' enc arity f isRecord where
881− consToEncoding' :: Options -> Proxy arity
1028+ consToJSON' :: Options -> ToArgs enc arity a
882− -> Bool -- ^ Are we a record with one field?
883− -> (a -> Encoding) -> ([a] -> Encoding)
884− -> f a -> Tagged isRecord Encoding
1029+ -> f a -> Tagged isRecord enc
886instance ( IsRecord f isRecord
1031instance ( IsRecord f isRecord
887− , ConsToEncoding' arity f isRecord
1032+ , ConsToJSON' enc arity f isRecord
888− ) => ConsToEncoding arity f where
1033+ ) => ConsToJSON enc arity f
889− consToEncoding opts pa te tel =
1034+ where
1035+ consToJSON opts targs =
890− (unTagged :: Tagged isRecord Encoding -> Encoding)
1036+ (unTagged :: Tagged isRecord enc -> enc)
891− . consToEncoding' opts pa (isUnary (undefined :: f a)) te tel
1037+ . consToJSON' opts targs
892−
1038+ {-# INLINE consToJSON #-}
893−instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
1039+
894− consToEncoding' opts pa isUn te tel x =
1040+instance OVERLAPPING_
895− let (enc, mbVal) = recordToEncoding opts pa te tel x
1041+ ( RecordToPairs enc pairs arity (S1 s f)
1042+ , FromPairs enc pairs
1043+ , GToJSON enc arity f
1044+ ) => ConsToJSON' enc arity (S1 s f) True
1046+ consToJSON' opts targs
896− in case (unwrapUnaryRecords opts, isUn, mbVal) of
1047+ | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
897− (True, True, Just val) -> Tagged val
1048+ | otherwise = Tagged . fromPairs . recordToPairs opts targs
898− _ -> Tagged $ E.wrapObject enc
1049+ {-# INLINE consToJSON' #-}
1051+instance ( RecordToPairs enc pairs arity f
1052+ , FromPairs enc pairs
1053+ ) => ConsToJSON' enc arity f True
1055+ consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
1056+ {-# INLINE consToJSON' #-}
900−instance GToEncoding arity f => ConsToEncoding' arity f False where
1058+instance GToJSON enc arity f => ConsToJSON' enc arity f False where
901− consToEncoding' opts pa _ te tel = Tagged . gToEncoding opts pa te tel
1059+ consToJSON' opts targs = Tagged . gToJSON opts targs
902−
1060+ {-# INLINE consToJSON' #-}
903−--------------------------------------------------------------------------------
905−class RecordToPairs arity f where
906− recordToPairs :: Options -> Proxy arity
907− -> (a -> Value) -> ([a] -> Value)
908− -> f a -> DList Pair
910−instance ( RecordToPairs arity a
911− , RecordToPairs arity b
912− ) => RecordToPairs arity (a :*: b) where
913− recordToPairs opts pa tj tjl (a :*: b) = recordToPairs opts pa tj tjl a <>
914− recordToPairs opts pa tj tjl b
916−instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
917− recordToPairs = fieldToPair
919−instance OVERLAPPING_ (Selector s, ToJSON a) =>
920− RecordToPairs arity (S1 s (K1 i (Maybe a))) where
921− recordToPairs opts _ _ _ (M1 k1) | omitNothingFields opts
922− , K1 Nothing <- k1 = DList.empty
923− recordToPairs opts pa tj tjl m1 = fieldToPair opts pa tj tjl m1
925−fieldToPair :: (Selector s, GToJSON arity a)
926− => Options -> Proxy arity
927− -> (p -> Value) -> ([p] -> Value)
928− -> S1 s a p -> DList Pair
929−fieldToPair opts pa tj tjl m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
930− , gToJSON opts pa tj tjl (unM1 m1)
933--------------------------------------------------------------------------------
1062--------------------------------------------------------------------------------
935−class RecordToEncoding arity f where
1064+class RecordToPairs enc pairs arity f where
936 -- 1st element: whole thing
1065 -- 1st element: whole thing
937 -- 2nd element: in case the record has only 1 field, just the value
1066 -- 2nd element: in case the record has only 1 field, just the value
938 -- of the field (without the key); 'Nothing' otherwise
1067 -- of the field (without the key); 'Nothing' otherwise
939− recordToEncoding :: Options -> Proxy arity
1068+ recordToPairs :: Options -> ToArgs enc arity a
940− -> (a -> Encoding) -> ([a] -> Encoding)
1069+ -> f a -> pairs
941− -> f a -> (Encoding, Maybe Encoding)
1070+
942−
1071+instance ( Monoid pairs
943−instance ( RecordToEncoding arity a
1072+ , RecordToPairs enc pairs arity a
944− , RecordToEncoding arity b
1073+ , RecordToPairs enc pairs arity b
945− ) => RecordToEncoding arity (a :*: b) where
1074+ ) => RecordToPairs enc pairs arity (a :*: b)
946− recordToEncoding opts pa te tel (a :*: b) | omitNothingFields opts =
1075+ where
947− (mconcat $ intersperse E.comma $
1076+ recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
948− filter (not . E.nullEncoding)
1077+ pairsOf a `mappend` pairsOf b
949− [ fst (recordToEncoding opts pa te tel a)
1078+ where
950− , fst (recordToEncoding opts pa te tel b) ]
1079+ pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
951− , Nothing)
1080+ pairsOf = recordToPairs opts targs
952− recordToEncoding opts pa te tel (a :*: b) =
1081+ {-# INLINE recordToPairs #-}
953− (fst (recordToEncoding opts pa te tel a) <> E.comma <>
1082+
954− fst (recordToEncoding opts pa te tel b),
1083+instance ( Selector s
955− Nothing)
1084+ , GToJSON enc arity a
956−
1085+ , KeyValuePair enc pairs
957−instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1086+ ) => RecordToPairs enc pairs arity (S1 s a)
958− recordToEncoding = fieldToEncoding
1087+ where
959−
1088+ recordToPairs = fieldToPair
960−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1089+ {-# INLINE recordToPairs #-}
1091+instance INCOHERENT_
1093+ , GToJSON enc arity (K1 i (Maybe a))
1094+ , KeyValuePair enc pairs
961− RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1096+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
962− recordToEncoding opts _ _ _ (M1 k1) | omitNothingFields opts
1098+ recordToPairs opts _ (M1 k1) | omitNothingFields opts
963− , K1 Nothing <- k1 = (mempty, Nothing)
1099+ , K1 Nothing <- k1 = mempty
964− recordToEncoding opts pa te tel m1 = fieldToEncoding opts pa te tel m1
1100+ recordToPairs opts targs m1 = fieldToPair opts targs m1
965−
1101+ {-# INLINE recordToPairs #-}
966−fieldToEncoding :: (Selector s, GToEncoding arity a)
1102+
967− => Options -> Proxy arity
1103+instance INCOHERENT_
968− -> (p -> Encoding) -> ([p] -> Encoding)
1104+ ( Selector s
969− -> S1 s a p -> (Encoding, Maybe Encoding)
1105+ , GToJSON enc arity (K1 i (Maybe a))
970−fieldToEncoding opts pa te tel m1 =
1106+ , KeyValuePair enc pairs
1108+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup.Option a)))
1110+ recordToPairs opts targs = recordToPairs opts targs . unwrap
1112+ unwrap :: S1 s (K1 i (Semigroup.Option a)) p -> S1 s (K1 i (Maybe a)) p
1113+ unwrap (M1 (K1 (Semigroup.Option a))) = M1 (K1 a)
1114+ {-# INLINE recordToPairs #-}
1116+fieldToPair :: (Selector s
1117+ , GToJSON enc arity a
1118+ , KeyValuePair enc pairs)
1119+ => Options -> ToArgs enc arity p
1120+ -> S1 s a p -> pairs
1121+fieldToPair opts targs m1 =
971− let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1122+ let key = fieldLabelModifier opts (selName m1)
972− valueBuilder = gToEncoding opts pa te tel (unM1 m1)
1123+ value = gToJSON opts targs (unM1 m1)
973− in (keyBuilder <> E.colon <> valueBuilder, Just valueBuilder)
1124+ in key `pair` value
1125+{-# INLINE fieldToPair #-}
975--------------------------------------------------------------------------------
1127--------------------------------------------------------------------------------
977class WriteProduct arity f where
1129class WriteProduct arity f where
978 writeProduct :: Options
1130 writeProduct :: Options
979− -> Proxy arity
1131+ -> ToArgs Value arity a
980 -> VM.MVector s Value
1132 -> VM.MVector s Value
981 -> Int -- ^ index
1133 -> Int -- ^ index
982 -> Int -- ^ length
1134 -> Int -- ^ length
986 -> ST s ()
1136 -> ST s ()
988instance ( WriteProduct arity a
1138instance ( WriteProduct arity a
989 , WriteProduct arity b
1139 , WriteProduct arity b
990 ) => WriteProduct arity (a :*: b) where
1140 ) => WriteProduct arity (a :*: b) where
991− writeProduct opts pa mv ix len tj tjl (a :*: b) = do
1141+ writeProduct opts targs mv ix len (a :*: b) = do
992− writeProduct opts pa mv ix lenL tj tjl a
1142+ writeProduct opts targs mv ix lenL a
993− writeProduct opts pa mv ixR lenR tj tjl b
1143+ writeProduct opts targs mv ixR lenR b
995 lenL = len `unsafeShiftR` 1
1145 lenL = len `unsafeShiftR` 1
996 lenR = len - lenL
1146 lenR = len - lenL
997 ixR = ix + lenL
1147 ixR = ix + lenL
1148+ {-# INLINE writeProduct #-}
999−instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
1150+instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1000− writeProduct opts pa mv ix _ tj tjl =
1151+ writeProduct opts targs mv ix _ =
1001− VM.unsafeWrite mv ix . gToJSON opts pa tj tjl
1152+ VM.unsafeWrite mv ix . gToJSON opts targs
1153+ {-# INLINE writeProduct #-}
1003--------------------------------------------------------------------------------
1155--------------------------------------------------------------------------------
1005class EncodeProduct arity f where
1157class EncodeProduct arity f where
1006− encodeProduct :: Options -> Proxy arity
1158+ encodeProduct :: Options -> ToArgs Encoding arity a
1007− -> (a -> Encoding) -> ([a] -> Encoding)
1008− -> f a -> Encoding
1159+ -> f a -> Encoding' E.InArray
1010instance ( EncodeProduct arity a
1161instance ( EncodeProduct arity a
1011 , EncodeProduct arity b
1162 , EncodeProduct arity b
1012 ) => EncodeProduct arity (a :*: b) where
1163 ) => EncodeProduct arity (a :*: b) where
1013− encodeProduct opts pa te tel (a :*: b) | omitNothingFields opts =
1164+ encodeProduct opts targs (a :*: b) | omitNothingFields opts =
1014− mconcat $ intersperse E.comma $
1165+ E.econcat $ intersperse E.comma $
1015 filter (not . E.nullEncoding)
1166 filter (not . E.nullEncoding)
1016− [encodeProduct opts pa te tel a, encodeProduct opts pa te tel b]
1167+ [encodeProduct opts targs a, encodeProduct opts targs b]
1017− encodeProduct opts pa te tel (a :*: b) = encodeProduct opts pa te tel a <>
1168+ encodeProduct opts targs (a :*: b) =
1018− E.comma <>
1169+ encodeProduct opts targs a >*<
1019− encodeProduct opts pa te tel b
1170+ encodeProduct opts targs b
1171+ {-# INLINE encodeProduct #-}
1021−instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1173+instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1022− encodeProduct opts = gToEncoding opts
1174+ encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1175+ {-# INLINE encodeProduct #-}
1024--------------------------------------------------------------------------------
1177--------------------------------------------------------------------------------
1026−class ObjectWithSingleFieldObj arity f where
1027− objectWithSingleFieldObj :: Options -> Proxy arity
1028− -> (a -> Value) -> ([a] -> Value)
1031−instance ( ObjectWithSingleFieldObj arity a
1032− , ObjectWithSingleFieldObj arity b
1033− ) => ObjectWithSingleFieldObj arity (a :+: b) where
1034− objectWithSingleFieldObj opts pa tj tjl (L1 x) =
1035− objectWithSingleFieldObj opts pa tj tjl x
1036− objectWithSingleFieldObj opts pa tj tjl (R1 x) =
1037− objectWithSingleFieldObj opts pa tj tjl x
1039−instance ( GToJSON arity a
1179+instance ( GToJSON enc arity a
1040− , ConsToJSON arity a
1180+ , ConsToJSON enc arity a
1181+ , FromPairs enc pairs
1182+ , KeyValuePair enc pairs
1041 , Constructor c
1183 , Constructor c
1042− ) => ObjectWithSingleFieldObj arity (C1 c a) where
1184+ ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1043− objectWithSingleFieldObj opts pa tj tjl = H.singleton typ . gToJSON opts pa tj tjl
1185+ where
1186+ sumToJSON' opts targs =
1187+ Tagged . fromPairs . (typ `pair`) . gToJSON opts targs
1045− typ = pack $ constructorTagModifier opts $
1189+ typ = constructorTagModifier opts $
1046 conName (undefined :: t c a p)
1190 conName (undefined :: t c a p)
1048--------------------------------------------------------------------------------
1192--------------------------------------------------------------------------------
1050−class ObjectWithSingleFieldEnc arity f where
1194+instance OVERLAPPABLE_
1051− objectWithSingleFieldEnc :: Options -> Proxy arity
1195+ ( ConsToJSON enc arity a
1052− -> (a -> Encoding) -> ([a] -> Encoding)
1196+ ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1053− -> f a -> Encoding
1197+ where
1054−
1198+ sumToJSON' opts targs = Tagged . gToJSON opts targs
1055−instance ( ObjectWithSingleFieldEnc arity a
1199+
1056− , ObjectWithSingleFieldEnc arity b
1200+instance OVERLAPPING_
1057− ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1058− objectWithSingleFieldEnc opts pa te tel (L1 x) =
1059− objectWithSingleFieldEnc opts pa te tel x
1060− objectWithSingleFieldEnc opts pa te tel (R1 x) =
1061− objectWithSingleFieldEnc opts pa te tel x
1063−instance ( GToEncoding arity a
1064− , ConsToEncoding arity a
1065− , Constructor c
1201+ ( Constructor c
1066− ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1202+ , FromString enc
1067− objectWithSingleFieldEnc opts pa te tel v =
1203+ ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1068− E.openCurly <>
1204+ where
1069− toEncoding
1205+ sumToJSON' opts _ _ = Tagged . fromString $
1070− (constructorTagModifier opts
1206+ constructorTagModifier opts $ conName (undefined :: t c U1 p)
1071− (conName (undefined :: t c a p))) <>
1073− gToEncoding opts pa te tel v <>
1076-------------------------------------------------------------------------------
1208-------------------------------------------------------------------------------
1077-- Instances
1209-- Instances
@@ -1081,21 +1213,6 @@ instance ( GToEncoding arity a
1082-------------------------------------------------------------------------------
1214-------------------------------------------------------------------------------
1084−instance ToJSON1 Identity where
1085− liftToJSON t _ (Identity a) = t a
1086− {-# INLINE liftToJSON #-}
1088− liftToEncoding t _ (Identity a) = t a
1089− {-# INLINE liftToEncoding #-}
1091−instance (ToJSON a) => ToJSON (Identity a) where
1093− {-# INLINE toJSON #-}
1095− toEncoding = toEncoding1
1096− {-# INLINE toEncoding #-}
1099instance ToJSON2 Const where
1216instance ToJSON2 Const where
1100 liftToJSON2 t _ _ _ (Const x) = t x
1217 liftToJSON2 t _ _ _ (Const x) = t x
1101 {-# INLINE liftToJSON2 #-}
1218 {-# INLINE liftToJSON2 #-}
@@ -1124,7 +1241,7 @@ instance ToJSON1 Maybe where
1124 {-# INLINE liftToJSON #-}
1241 {-# INLINE liftToJSON #-}
1126 liftToEncoding t _ (Just a) = t a
1243 liftToEncoding t _ (Just a) = t a
1127− liftToEncoding _ _ Nothing = Encoding EB.null_
1244+ liftToEncoding _ _ Nothing = E.null_
1128 {-# INLINE liftToEncoding #-}
1245 {-# INLINE liftToEncoding #-}
1130instance (ToJSON a) => ToJSON (Maybe a) where
1247instance (ToJSON a) => ToJSON (Maybe a) where
@@ -1136,19 +1253,13 @@ instance (ToJSON a) => ToJSON (Maybe a) where
1138instance ToJSON2 Either where
1255instance ToJSON2 Either where
1139− liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton left (toA a)
1256+ liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton "Left" (toA a)
1140− liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1257+ liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton "Right" (toB b)
1141 {-# INLINE liftToJSON2 #-}
1258 {-# INLINE liftToJSON2 #-}
1143− liftToEncoding2 toA _ _toB _ (Left a) =
1260+ liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
1144− Encoding (B.shortByteString "{\"Left\":")
1146− <> Encoding (B.char7 '}')
1148− liftToEncoding2 _toA _ toB _ (Right b) =
1262+ liftToEncoding2 _toA _ toB _ (Right b) = E.pairs $ E.pair "Right" $ toB b
1149− Encoding (B.shortByteString "{\"Right\":")
1151− <> Encoding (B.char7 '}')
1152 {-# INLINE liftToEncoding2 #-}
1263 {-# INLINE liftToEncoding2 #-}
1154instance (ToJSON a) => ToJSON1 (Either a) where
1265instance (ToJSON a) => ToJSON1 (Either a) where
@@ -1165,18 +1276,24 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
1165 toEncoding = toEncoding2
1276 toEncoding = toEncoding2
1166 {-# INLINE toEncoding #-}
1277 {-# INLINE toEncoding #-}
1168−left, right :: Text
1279+instance ToJSON Void where
1169−left = "Left"
1280+ toJSON = absurd
1170−right = "Right"
1281+ {-# INLINE toJSON #-}
1283+ toEncoding = absurd
1284+ {-# INLINE toEncoding #-}
1173instance ToJSON Bool where
1287instance ToJSON Bool where
1174 toJSON = Bool
1288 toJSON = Bool
1175 {-# INLINE toJSON #-}
1289 {-# INLINE toJSON #-}
1177− toEncoding = Encoding . EB.bool
1291+ toEncoding = E.bool
1178 {-# INLINE toEncoding #-}
1292 {-# INLINE toEncoding #-}
1294+instance ToJSONKey Bool where
1295+ toJSONKey = toJSONKeyText $ \x -> if x then "true" else "false"
1181instance ToJSON Ordering where
1298instance ToJSON Ordering where
1182 toJSON = toJSON . orderingToText
1299 toJSON = toJSON . orderingToText
@@ -1203,10 +1320,10 @@ instance ToJSON Char where
1203 toJSONList = String . T.pack
1320 toJSONList = String . T.pack
1204 {-# INLINE toJSONList #-}
1321 {-# INLINE toJSONList #-}
1206− toEncoding = Encoding . EB.string . (:[])
1323+ toEncoding = E.string . (:[])
1207 {-# INLINE toEncoding #-}
1324 {-# INLINE toEncoding #-}
1209− toEncodingList = Encoding . EB.string
1326+ toEncodingList = E.string
1210 {-# INLINE toEncodingList #-}
1327 {-# INLINE toEncodingList #-}
@@ -1214,9 +1331,13 @@ instance ToJSON Double where
1214 toJSON = realFloatToJSON
1331 toJSON = realFloatToJSON
1215 {-# INLINE toJSON #-}
1332 {-# INLINE toJSON #-}
1217− toEncoding = realFloatToEncoding
1334+ toEncoding = E.double
1218 {-# INLINE toEncoding #-}
1335 {-# INLINE toEncoding #-}
1337+instance ToJSONKey Double where
1338+ toJSONKey = toJSONKeyTextEnc E.doubleText
1339+ {-# INLINE toJSONKey #-}
1221instance ToJSON Number where
1342instance ToJSON Number where
1222 toJSON (D d) = toJSON d
1343 toJSON (D d) = toJSON d
@@ -1232,20 +1353,23 @@ instance ToJSON Float where
1232 toJSON = realFloatToJSON
1353 toJSON = realFloatToJSON
1233 {-# INLINE toJSON #-}
1354 {-# INLINE toJSON #-}
1235− toEncoding = realFloatToEncoding
1356+ toEncoding = E.float
1236 {-# INLINE toEncoding #-}
1357 {-# INLINE toEncoding #-}
1359+instance ToJSONKey Float where
1360+ toJSONKey = toJSONKeyTextEnc E.floatText
1361+ {-# INLINE toJSONKey #-}
1239−instance ToJSON (Ratio Integer) where
1364+instance (ToJSON a, Integral a) => ToJSON (Ratio a) where
1240 toJSON r = object [ "numerator" .= numerator r
1365 toJSON r = object [ "numerator" .= numerator r
1241 , "denominator" .= denominator r
1366 , "denominator" .= denominator r
1243 {-# INLINE toJSON #-}
1368 {-# INLINE toJSON #-}
1245− toEncoding r = Encoding $
1370+ toEncoding r = E.pairs $
1246− B.shortByteString "{\"numerator\":" <> builder (numerator r) <>
1371+ "numerator" .= numerator r <>
1247− B.shortByteString ",\"denominator\":" <> builder (denominator r) <>
1372+ "denominator" .= denominator r
1249 {-# INLINE toEncoding #-}
1373 {-# INLINE toEncoding #-}
@@ -1253,24 +1377,37 @@ instance HasResolution a => ToJSON (Fixed a) where
1253 toJSON = Number . realToFrac
1377 toJSON = Number . realToFrac
1254 {-# INLINE toJSON #-}
1378 {-# INLINE toJSON #-}
1256− toEncoding = Encoding . EB.number . realToFrac
1380+ toEncoding = E.scientific . realToFrac
1257 {-# INLINE toEncoding #-}
1381 {-# INLINE toEncoding #-}
1383+instance HasResolution a => ToJSONKey (Fixed a) where
1384+ toJSONKey = toJSONKeyTextEnc (E.scientificText . realToFrac)
1385+ {-# INLINE toJSONKey #-}
1260instance ToJSON Int where
1388instance ToJSON Int where
1261 toJSON = Number . fromIntegral
1389 toJSON = Number . fromIntegral
1262 {-# INLINE toJSON #-}
1390 {-# INLINE toJSON #-}
1264− toEncoding = Encoding . B.intDec
1392+ toEncoding = E.int
1265 {-# INLINE toEncoding #-}
1393 {-# INLINE toEncoding #-}
1395+instance ToJSONKey Int where
1396+ toJSONKey = toJSONKeyTextEnc E.intText
1397+ {-# INLINE toJSONKey #-}
1267instance ToJSON Integer where
1400instance ToJSON Integer where
1268 toJSON = Number . fromInteger
1401 toJSON = Number . fromInteger
1269 {-# INLINE toJSON #-}
1402 {-# INLINE toJSON #-}
1271− toEncoding = Encoding . B.integerDec
1404+ toEncoding = E.integer
1272 {-# INLINE toEncoding #-}
1405 {-# INLINE toEncoding #-}
1407+instance ToJSONKey Integer where
1408+ toJSONKey = toJSONKeyTextEnc E.integerText
1409+ {-# INLINE toJSONKey #-}
1275instance ToJSON Natural where
1412instance ToJSON Natural where
1276 toJSON = toJSON . toInteger
1413 toJSON = toJSON . toInteger
@@ -1279,93 +1416,147 @@ instance ToJSON Natural where
1279 toEncoding = toEncoding . toInteger
1416 toEncoding = toEncoding . toInteger
1280 {-# INLINE toEncoding #-}
1417 {-# INLINE toEncoding #-}
1419+instance ToJSONKey Natural where
1420+ toJSONKey = toJSONKeyTextEnc (E.integerText . toInteger)
1421+ {-# INLINE toJSONKey #-}
1283instance ToJSON Int8 where
1424instance ToJSON Int8 where
1284 toJSON = Number . fromIntegral
1425 toJSON = Number . fromIntegral
1285 {-# INLINE toJSON #-}
1426 {-# INLINE toJSON #-}
1287− toEncoding = Encoding . B.int8Dec
1428+ toEncoding = E.int8
1288 {-# INLINE toEncoding #-}
1429 {-# INLINE toEncoding #-}
1431+instance ToJSONKey Int8 where
1432+ toJSONKey = toJSONKeyTextEnc E.int8Text
1433+ {-# INLINE toJSONKey #-}
1291instance ToJSON Int16 where
1436instance ToJSON Int16 where
1292 toJSON = Number . fromIntegral
1437 toJSON = Number . fromIntegral
1293 {-# INLINE toJSON #-}
1438 {-# INLINE toJSON #-}
1295− toEncoding = Encoding . B.int16Dec
1440+ toEncoding = E.int16
1296 {-# INLINE toEncoding #-}
1441 {-# INLINE toEncoding #-}
1443+instance ToJSONKey Int16 where
1444+ toJSONKey = toJSONKeyTextEnc E.int16Text
1445+ {-# INLINE toJSONKey #-}
1299instance ToJSON Int32 where
1448instance ToJSON Int32 where
1300 toJSON = Number . fromIntegral
1449 toJSON = Number . fromIntegral
1301 {-# INLINE toJSON #-}
1450 {-# INLINE toJSON #-}
1303− toEncoding = Encoding . B.int32Dec
1452+ toEncoding = E.int32
1304 {-# INLINE toEncoding #-}
1453 {-# INLINE toEncoding #-}
1455+instance ToJSONKey Int32 where
1456+ toJSONKey = toJSONKeyTextEnc E.int32Text
1457+ {-# INLINE toJSONKey #-}
1307instance ToJSON Int64 where
1460instance ToJSON Int64 where
1308 toJSON = Number . fromIntegral
1461 toJSON = Number . fromIntegral
1309 {-# INLINE toJSON #-}
1462 {-# INLINE toJSON #-}
1311− toEncoding = Encoding . B.int64Dec
1464+ toEncoding = E.int64
1312 {-# INLINE toEncoding #-}
1465 {-# INLINE toEncoding #-}
1467+instance ToJSONKey Int64 where
1468+ toJSONKey = toJSONKeyTextEnc E.int64Text
1469+ {-# INLINE toJSONKey #-}
1315instance ToJSON Word where
1471instance ToJSON Word where
1316 toJSON = Number . fromIntegral
1472 toJSON = Number . fromIntegral
1317 {-# INLINE toJSON #-}
1473 {-# INLINE toJSON #-}
1319− toEncoding = Encoding . B.wordDec
1475+ toEncoding = E.word
1320 {-# INLINE toEncoding #-}
1476 {-# INLINE toEncoding #-}
1478+instance ToJSONKey Word where
1479+ toJSONKey = toJSONKeyTextEnc E.wordText
1480+ {-# INLINE toJSONKey #-}
1323instance ToJSON Word8 where
1483instance ToJSON Word8 where
1324 toJSON = Number . fromIntegral
1484 toJSON = Number . fromIntegral
1325 {-# INLINE toJSON #-}
1485 {-# INLINE toJSON #-}
1327− toEncoding = Encoding . B.word8Dec
1487+ toEncoding = E.word8
1328 {-# INLINE toEncoding #-}
1488 {-# INLINE toEncoding #-}
1490+instance ToJSONKey Word8 where
1491+ toJSONKey = toJSONKeyTextEnc E.word8Text
1492+ {-# INLINE toJSONKey #-}
1331instance ToJSON Word16 where
1495instance ToJSON Word16 where
1332 toJSON = Number . fromIntegral
1496 toJSON = Number . fromIntegral
1333 {-# INLINE toJSON #-}
1497 {-# INLINE toJSON #-}
1335− toEncoding = Encoding . B.word16Dec
1499+ toEncoding = E.word16
1336 {-# INLINE toEncoding #-}
1500 {-# INLINE toEncoding #-}
1502+instance ToJSONKey Word16 where
1503+ toJSONKey = toJSONKeyTextEnc E.word16Text
1504+ {-# INLINE toJSONKey #-}
1339instance ToJSON Word32 where
1507instance ToJSON Word32 where
1340 toJSON = Number . fromIntegral
1508 toJSON = Number . fromIntegral
1341 {-# INLINE toJSON #-}
1509 {-# INLINE toJSON #-}
1343− toEncoding = Encoding . B.word32Dec
1511+ toEncoding = E.word32
1344 {-# INLINE toEncoding #-}
1512 {-# INLINE toEncoding #-}
1514+instance ToJSONKey Word32 where
1515+ toJSONKey = toJSONKeyTextEnc E.word32Text
1516+ {-# INLINE toJSONKey #-}
1346instance ToJSON Word64 where
1519instance ToJSON Word64 where
1347 toJSON = Number . fromIntegral
1520 toJSON = Number . fromIntegral
1348 {-# INLINE toJSON #-}
1521 {-# INLINE toJSON #-}
1350− toEncoding = Encoding . B.word64Dec
1523+ toEncoding = E.word64
1524+ {-# INLINE toEncoding #-}
1526+instance ToJSONKey Word64 where
1527+ toJSONKey = toJSONKeyTextEnc E.word64Text
1528+ {-# INLINE toJSONKey #-}
1530+instance ToJSON CTime where
1531+ toJSON (CTime i) = toJSON i
1532+ {-# INLINE toJSON #-}
1534+ toEncoding (CTime i) = toEncoding i
1351 {-# INLINE toEncoding #-}
1535 {-# INLINE toEncoding #-}
1353instance ToJSON Text where
1537instance ToJSON Text where
1354 toJSON = String
1538 toJSON = String
1355 {-# INLINE toJSON #-}
1539 {-# INLINE toJSON #-}
1357− toEncoding = Encoding . EB.text
1541+ toEncoding = E.text
1358 {-# INLINE toEncoding #-}
1542 {-# INLINE toEncoding #-}
1544+instance ToJSONKey Text where
1545+ toJSONKey = toJSONKeyText id
1546+ {-# INLINE toJSONKey #-}
1360instance ToJSON LT.Text where
1549instance ToJSON LT.Text where
1361 toJSON = String . LT.toStrict
1550 toJSON = String . LT.toStrict
1362 {-# INLINE toJSON #-}
1551 {-# INLINE toJSON #-}
1364− toEncoding t = Encoding $
1553+ toEncoding = E.lazyText
1366− LT.foldrChunks (\x xs -> EB.unquoted x <> xs) (B.char7 '"') t
1367 {-# INLINE toEncoding #-}
1554 {-# INLINE toEncoding #-}
1556+instance ToJSONKey LT.Text where
1557+ toJSONKey = toJSONKeyText LT.toStrict
1369instance ToJSON Version where
1560instance ToJSON Version where
1370 toJSON = toJSON . showVersion
1561 toJSON = toJSON . showVersion
1371 {-# INLINE toJSON #-}
1562 {-# INLINE toJSON #-}
@@ -1373,6 +1564,8 @@ instance ToJSON Version where
1373 toEncoding = toEncoding . showVersion
1564 toEncoding = toEncoding . showVersion
1374 {-# INLINE toEncoding #-}
1565 {-# INLINE toEncoding #-}
1567+instance ToJSONKey Version where
1568+ toJSONKey = toJSONKeyText (T.pack . showVersion)
1377-------------------------------------------------------------------------------
1570-------------------------------------------------------------------------------
1378-- semigroups NonEmpty
1571-- semigroups NonEmpty
@@ -1400,7 +1593,138 @@ instance ToJSON Scientific where
1400 toJSON = Number
1593 toJSON = Number
1401 {-# INLINE toJSON #-}
1594 {-# INLINE toJSON #-}
1403− toEncoding = Encoding . EB.number
1596+ toEncoding = E.scientific
1597+ {-# INLINE toEncoding #-}
1599+instance ToJSONKey Scientific where
1600+ toJSONKey = toJSONKeyTextEnc E.scientificText
1602+-------------------------------------------------------------------------------
1604+-------------------------------------------------------------------------------
1606+instance ToJSON1 DList.DList where
1607+ liftToJSON t _ = listValue t . toList
1608+ {-# INLINE liftToJSON #-}
1610+ liftToEncoding t _ = listEncoding t . toList
1611+ {-# INLINE liftToEncoding #-}
1613+instance (ToJSON a) => ToJSON (DList.DList a) where
1615+ {-# INLINE toJSON #-}
1617+ toEncoding = toEncoding1
1618+ {-# INLINE toEncoding #-}
1620+-------------------------------------------------------------------------------
1621+-- transformers - Functors
1622+-------------------------------------------------------------------------------
1624+instance ToJSON1 Identity where
1625+ liftToJSON t _ (Identity a) = t a
1626+ {-# INLINE liftToJSON #-}
1628+ liftToJSONList _ tl xs = tl (map runIdentity xs)
1629+ {-# INLINE liftToJSONList #-}
1631+ liftToEncoding t _ (Identity a) = t a
1632+ {-# INLINE liftToEncoding #-}
1634+ liftToEncodingList _ tl xs = tl (map runIdentity xs)
1635+ {-# INLINE liftToEncodingList #-}
1637+instance (ToJSON a) => ToJSON (Identity a) where
1639+ {-# INLINE toJSON #-}
1641+ toJSONList = liftToJSONList toJSON toJSONList
1642+ {-# INLINE toJSONList #-}
1644+ toEncoding = toEncoding1
1645+ {-# INLINE toEncoding #-}
1647+ toEncodingList = liftToEncodingList toEncoding toEncodingList
1648+ {-# INLINE toEncodingList #-}
1650+instance (ToJSONKey a) => ToJSONKey (Identity a) where
1651+ toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1652+ toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
1655+instance (ToJSON1 f, ToJSON1 g) => ToJSON1 (Compose f g) where
1656+ liftToJSON tv tvl (Compose x) = liftToJSON g gl x
1658+ g = liftToJSON tv tvl
1659+ gl = liftToJSONList tv tvl
1660+ {-# INLINE liftToJSON #-}
1662+ liftToJSONList te tel xs = liftToJSONList g gl (map getCompose xs)
1664+ g = liftToJSON te tel
1665+ gl = liftToJSONList te tel
1666+ {-# INLINE liftToJSONList #-}
1668+ liftToEncoding te tel (Compose x) = liftToEncoding g gl x
1670+ g = liftToEncoding te tel
1671+ gl = liftToEncodingList te tel
1672+ {-# INLINE liftToEncoding #-}
1674+ liftToEncodingList te tel xs = liftToEncodingList g gl (map getCompose xs)
1676+ g = liftToEncoding te tel
1677+ gl = liftToEncodingList te tel
1678+ {-# INLINE liftToEncodingList #-}
1680+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Compose f g a) where
1682+ {-# INLINE toJSON #-}
1684+ toJSONList = liftToJSONList toJSON toJSONList
1685+ {-# INLINE toJSONList #-}
1687+ toEncoding = toEncoding1
1688+ {-# INLINE toEncoding #-}
1690+ toEncodingList = liftToEncodingList toEncoding toEncodingList
1691+ {-# INLINE toEncodingList #-}
1694+instance (ToJSON1 f, ToJSON1 g) => ToJSON1 (Product f g) where
1695+ liftToJSON tv tvl (Pair x y) = liftToJSON2 tx txl ty tyl (x, y)
1697+ tx = liftToJSON tv tvl
1698+ txl = liftToJSONList tv tvl
1699+ ty = liftToJSON tv tvl
1700+ tyl = liftToJSONList tv tvl
1702+ liftToEncoding te tel (Pair x y) = liftToEncoding2 tx txl ty tyl (x, y)
1704+ tx = liftToEncoding te tel
1705+ txl = liftToEncodingList te tel
1706+ ty = liftToEncoding te tel
1707+ tyl = liftToEncodingList te tel
1709+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Product f g a) where
1711+ {-# INLINE toJSON #-}
1713+ toEncoding = toEncoding1
1714+ {-# INLINE toEncoding #-}
1716+instance (ToJSON1 f, ToJSON1 g) => ToJSON1 (Sum f g) where
1717+ liftToJSON tv tvl (InL x) = Object $ H.singleton "InL" (liftToJSON tv tvl x)
1718+ liftToJSON tv tvl (InR y) = Object $ H.singleton "InR" (liftToJSON tv tvl y)
1720+ liftToEncoding te tel (InL x) = E.pairs $ E.pair "InL" $ liftToEncoding te tel x
1721+ liftToEncoding te tel (InR y) = E.pairs $ E.pair "InR" $ liftToEncoding te tel y
1723+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Sum f g a) where
1725+ {-# INLINE toJSON #-}
1727+ toEncoding = toEncoding1
1404 {-# INLINE toEncoding #-}
1728 {-# INLINE toEncoding #-}
1406-------------------------------------------------------------------------------
1730-------------------------------------------------------------------------------
@@ -1468,17 +1792,15 @@ instance ToJSON a => ToJSON (IntMap.IntMap a) where
1469instance ToJSONKey k => ToJSON1 (M.Map k) where
1793instance ToJSONKey k => ToJSON1 (M.Map k) where
1470 liftToJSON g _ = case toJSONKey of
1794 liftToJSON g _ = case toJSONKey of
1471− ToJSONKeyText (f,_) -> Object . mapHashKeyVal f g
1795+ ToJSONKeyText f _ -> Object . mapHashKeyVal f g
1472− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . M.toList
1796+ ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1473 {-# INLINE liftToJSON #-}
1797 {-# INLINE liftToJSON #-}
1475− -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
1476 liftToEncoding g _ = case toJSONKey of
1799 liftToEncoding g _ = case toJSONKey of
1477− ToJSONKeyText (_,f) -> dict f g M.foldrWithKey
1800+ ToJSONKeyText _ f -> dict f g M.foldrWithKey
1478− ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . M.toList
1801+ ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1480− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1481− pairEncoding f (a, b) = tuple $ f a >*< g b
1803+ pairEncoding f (a, b) = E.list id [f a, g b]
1482 {-# INLINE liftToEncoding #-}
1804 {-# INLINE liftToEncoding #-}
@@ -1516,6 +1838,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
1516 toEncoding = toEncoding1
1838 toEncoding = toEncoding1
1517 {-# 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
1520-------------------------------------------------------------------------------
1853-------------------------------------------------------------------------------
1521-- vector
1854-- vector
@@ -1525,29 +1858,20 @@ instance ToJSON1 Vector where
1525 liftToJSON t _ = Array . V.map t
1858 liftToJSON t _ = Array . V.map t
1526 {-# INLINE liftToJSON #-}
1859 {-# INLINE liftToJSON #-}
1528− liftToEncoding t _ xs
1861+ liftToEncoding t _ = listEncoding t . V.toList
1529− | V.null xs = emptyArray_
1530− | otherwise = Encoding $
1531− B.char7 '[' <> fromEncoding (t (V.unsafeHead xs)) <>
1532− V.foldr go (B.char7 ']') (V.unsafeTail xs)
1534− go v b = B.char7 ',' <> fromEncoding (t v) <> b
1535 {-# INLINE liftToEncoding #-}
1862 {-# INLINE liftToEncoding #-}
1537instance (ToJSON a) => ToJSON (Vector a) where
1864instance (ToJSON a) => ToJSON (Vector a) where
1538− toJSON = Array . V.map toJSON
1865+ {-# SPECIALIZE instance ToJSON Array #-}
1539 {-# INLINE toJSON #-}
1868 {-# INLINE toJSON #-}
1541− toEncoding = encodeVector
1870+ toEncoding = toEncoding1
1542 {-# INLINE toEncoding #-}
1871 {-# INLINE toEncoding #-}
1544encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1873encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1545−encodeVector xs
1874+encodeVector = listEncoding toEncoding . VG.toList
1546− | VG.null xs = emptyArray_
1547− | otherwise = Encoding $
1548− B.char7 '[' <> builder (VG.unsafeHead xs) <>
1549− VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
1550− where go v b = B.char7 ',' <> builder v <> b
1551{-# INLINE encodeVector #-}
1875{-# INLINE encodeVector #-}
1553vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
1877vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -1598,20 +1922,21 @@ instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
1599instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1923instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1600 liftToJSON g _ = case toJSONKey of
1924 liftToJSON g _ = case toJSONKey of
1601− ToJSONKeyText (f,_) -> Object . mapKeyVal f g
1925+ ToJSONKeyText f _ -> Object . mapKeyVal f g
1602− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . H.toList
1926+ ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . H.toList
1603 {-# INLINE liftToJSON #-}
1927 {-# INLINE liftToJSON #-}
1605 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1929 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1606 liftToEncoding g _ = case toJSONKey of
1930 liftToEncoding g _ = case toJSONKey of
1607− ToJSONKeyText (_,f) -> dict f g H.foldrWithKey
1931+ ToJSONKeyText _ f -> dict f g H.foldrWithKey
1608− ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . H.toList
1932+ ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1610− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1611− pairEncoding f (a, b) = tuple $ f a >*< g b
1934+ pairEncoding f (a, b) = E.list id [f a, g b]
1612 {-# INLINE liftToEncoding #-}
1935 {-# INLINE liftToEncoding #-}
1614instance (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 #-}
1615 toJSON = toJSON1
1940 toJSON = toJSON1
1616 {-# INLINE toJSON #-}
1941 {-# INLINE toJSON #-}
@@ -1626,7 +1951,7 @@ instance ToJSON Value where
1626 toJSON a = a
1951 toJSON a = a
1627 {-# INLINE toJSON #-}
1952 {-# INLINE toJSON #-}
1629− toEncoding = Encoding . EB.encodeToBuilder
1954+ toEncoding = E.value
1630 {-# INLINE toEncoding #-}
1955 {-# INLINE toEncoding #-}
1632instance ToJSON DotNetTime where
1957instance ToJSON DotNetTime where
@@ -1638,69 +1963,204 @@ dotNetTime :: DotNetTime -> String
1638dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1963dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1639 where secs = formatTime defaultTimeLocale "/Date(%s" t
1964 where secs = formatTime defaultTimeLocale "/Date(%s" t
1966+formatMillis :: (FormatTime t) => t -> String
1967+formatMillis = 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
1641-------------------------------------------------------------------------------
1995-------------------------------------------------------------------------------
1643-------------------------------------------------------------------------------
1997-------------------------------------------------------------------------------
1645instance ToJSON Day where
1999instance ToJSON Day where
1646− toJSON = stringEncoding
2000+ toJSON = stringEncoding . E.day
1647− toEncoding z = Encoding (EB.quote $ EB.day z)
2001+ toEncoding = E.day
2003+instance ToJSONKey Day where
2004+ toJSONKey = toJSONKeyTextEnc E.day
1650instance ToJSON TimeOfDay where
2007instance ToJSON TimeOfDay where
1651− toJSON = stringEncoding
2008+ toJSON = stringEncoding . E.timeOfDay
1652− toEncoding z = Encoding (EB.quote $ EB.timeOfDay z)
2009+ toEncoding = E.timeOfDay
2011+instance ToJSONKey TimeOfDay where
2012+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
1655instance ToJSON LocalTime where
2015instance ToJSON LocalTime where
1656− toJSON = stringEncoding
2016+ toJSON = stringEncoding . E.localTime
1657− toEncoding z = Encoding (EB.quote $ EB.localTime z)
2017+ toEncoding = E.localTime
2019+instance ToJSONKey LocalTime where
2020+ toJSONKey = toJSONKeyTextEnc E.localTime
1659instance ToJSON ZonedTime where
2023instance ToJSON ZonedTime where
1660− toJSON = stringEncoding
2024+ toJSON = stringEncoding . E.zonedTime
2025+ toEncoding = E.zonedTime
1662− toEncoding z = Encoding (EB.quote $ EB.zonedTime z)
2027+instance ToJSONKey ZonedTime where
2028+ toJSONKey = toJSONKeyTextEnc E.zonedTime
1664−formatMillis :: (FormatTime t) => t -> String
1665−formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1667instance ToJSON UTCTime where
2031instance ToJSON UTCTime where
1668− toJSON = stringEncoding
2032+ toJSON = stringEncoding . E.utcTime
2033+ toEncoding = E.utcTime
1670− toEncoding t = Encoding (EB.quote $ EB.utcTime t)
2035+instance ToJSONKey UTCTime where
2036+ toJSONKey = toJSONKeyTextEnc E.utcTime
1672-- | Encode something t a JSON string.
2038-- | Encode something t a JSON string.
1674−-- TODO: remove me, this is a hack
1675−stringEncoding :: (ToJSON a) => a -> Value
2039+stringEncoding :: Encoding' Text -> Value
1676stringEncoding = String
2040stringEncoding = String
1677 . T.dropAround (== '"')
2041 . T.dropAround (== '"')
1678 . T.decodeLatin1
2042 . T.decodeLatin1
1679 . L.toStrict
2043 . L.toStrict
1680− . B.toLazyByteString
2044+ . E.encodingToLazyByteString
1683{-# INLINE stringEncoding #-}
2045{-# INLINE stringEncoding #-}
1685instance ToJSON NominalDiffTime where
2048instance ToJSON NominalDiffTime where
1686 toJSON = Number . realToFrac
2049 toJSON = Number . realToFrac
1687 {-# INLINE toJSON #-}
2050 {-# INLINE toJSON #-}
1689− toEncoding = Encoding . EB.number . realToFrac
2052+ toEncoding = E.scientific . realToFrac
1690 {-# 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
1692-------------------------------------------------------------------------------
2107-------------------------------------------------------------------------------
1693-- base Monoid/Semigroup
2108-- base Monoid/Semigroup
1694-------------------------------------------------------------------------------
2109-------------------------------------------------------------------------------
1696−instance ToJSON1 Dual where
2111+instance ToJSON1 Monoid.Dual where
1697− liftToJSON t _ = t . getDual
2112+ liftToJSON t _ = t . Monoid.getDual
2113+ {-# INLINE liftToJSON #-}
2115+ liftToEncoding t _ = t . Monoid.getDual
2116+ {-# INLINE liftToEncoding #-}
2118+instance ToJSON a => ToJSON (Monoid.Dual a) where
2120+ {-# INLINE toJSON #-}
2122+ toEncoding = toEncoding1
2123+ {-# INLINE toEncoding #-}
2126+instance ToJSON1 Monoid.First where
2127+ liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
2128+ {-# INLINE liftToJSON #-}
2130+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
2131+ {-# INLINE liftToEncoding #-}
2133+instance ToJSON a => ToJSON (Monoid.First a) where
2135+ {-# INLINE toJSON #-}
2137+ toEncoding = toEncoding1
2138+ {-# INLINE toEncoding #-}
2141+instance ToJSON1 Monoid.Last where
2142+ liftToJSON t to' = liftToJSON t to' . Monoid.getLast
2143+ {-# INLINE liftToJSON #-}
2145+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2146+ {-# INLINE liftToEncoding #-}
2148+instance ToJSON a => ToJSON (Monoid.Last a) where
2150+ {-# INLINE toJSON #-}
2152+ toEncoding = toEncoding1
2153+ {-# INLINE toEncoding #-}
2156+instance ToJSON1 Semigroup.Min where
2157+ liftToJSON t _ (Semigroup.Min x) = t x
1698 {-# INLINE liftToJSON #-}
2158 {-# INLINE liftToJSON #-}
1700− liftToEncoding t _ = t . getDual
2160+ liftToEncoding t _ (Semigroup.Min x) = t x
1701 {-# INLINE liftToEncoding #-}
2161 {-# INLINE liftToEncoding #-}
1703−instance ToJSON a => ToJSON (Dual a) where
2163+instance ToJSON a => ToJSON (Semigroup.Min a) where
1704 toJSON = toJSON1
2164 toJSON = toJSON1
1705 {-# INLINE toJSON #-}
2165 {-# INLINE toJSON #-}
@@ -1708,14 +2168,28 @@ instance ToJSON a => ToJSON (Dual a) where
1708 {-# INLINE toEncoding #-}
2168 {-# INLINE toEncoding #-}
1711−instance ToJSON1 First where
2171+instance ToJSON1 Semigroup.Max where
1712− liftToJSON t to' = liftToJSON t to' . getFirst
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
1713 {-# INLINE liftToJSON #-}
2187 {-# INLINE liftToJSON #-}
1715− liftToEncoding t to' = liftToEncoding t to' . getFirst
2189+ liftToEncoding t _ (Semigroup.First x) = t x
1716 {-# INLINE liftToEncoding #-}
2190 {-# INLINE liftToEncoding #-}
1718−instance ToJSON a => ToJSON (First a) where
2192+instance ToJSON a => ToJSON (Semigroup.First a) where
1719 toJSON = toJSON1
2193 toJSON = toJSON1
1720 {-# INLINE toJSON #-}
2194 {-# INLINE toJSON #-}
@@ -1723,14 +2197,44 @@ instance ToJSON a => ToJSON (First a) where
1723 {-# INLINE toEncoding #-}
2197 {-# INLINE toEncoding #-}
1726−instance ToJSON1 Last where
2200+instance ToJSON1 Semigroup.Last where
1727− liftToJSON t to' = liftToJSON t to' . getLast
2201+ liftToJSON t _ (Semigroup.Last x) = t x
1728 {-# INLINE liftToJSON #-}
2202 {-# INLINE liftToJSON #-}
1730− liftToEncoding t to' = liftToEncoding t to' . getLast
2204+ liftToEncoding t _ (Semigroup.Last x) = t x
1731 {-# INLINE liftToEncoding #-}
2205 {-# INLINE liftToEncoding #-}
1733−instance ToJSON a => ToJSON (Last a) where
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
1734 toJSON = toJSON1
2238 toJSON = toJSON1
1735 {-# INLINE toJSON #-}
2239 {-# INLINE toJSON #-}
@@ -1741,14 +2245,28 @@ instance ToJSON a => ToJSON (Last a) where
1741-- tagged
2245-- tagged
1742-------------------------------------------------------------------------------
2246-------------------------------------------------------------------------------
2248+instance ToJSON1 Proxy where
2249+ liftToJSON _ _ _ = Null
2250+ {-# INLINE liftToJSON #-}
2252+ liftToEncoding _ _ _ = E.null_
2253+ {-# INLINE liftToEncoding #-}
1744instance ToJSON (Proxy a) where
2255instance ToJSON (Proxy a) where
1745 toJSON _ = Null
2256 toJSON _ = Null
1746 {-# INLINE toJSON #-}
2257 {-# INLINE toJSON #-}
1748− toEncoding _ = Encoding EB.null_
2259+ toEncoding _ = E.null_
1749 {-# 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 #-}
1752instance ToJSON1 (Tagged a) where
2270instance ToJSON1 (Tagged a) where
1753 liftToJSON t _ (Tagged x) = t x
2271 liftToJSON t _ (Tagged x) = t x
1754 {-# INLINE liftToJSON #-}
2272 {-# INLINE liftToJSON #-}
@@ -1763,7 +2281,6 @@ instance ToJSON b => ToJSON (Tagged a b) where
1763 toEncoding = toEncoding1
2281 toEncoding = toEncoding1
1764 {-# INLINE toEncoding #-}
2282 {-# INLINE toEncoding #-}
1767instance ToJSONKey b => ToJSONKey (Tagged a b) where
2284instance ToJSONKey b => ToJSONKey (Tagged a b) where
1768 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
1769 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
2286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -1772,41 +2289,22 @@ instance ToJSONKey b => ToJSONKey (Tagged a b) where
1772-- Instances for converting t map keys
2289-- Instances for converting t map keys
1773-------------------------------------------------------------------------------
2290-------------------------------------------------------------------------------
1775−instance ToJSONKey Text where
1776− toJSONKey = ToJSONKeyText (id,toEncoding)
1779−instance ToJSONKey Bool where
1780− toJSONKey = ToJSONKeyText
1781− ( (\x -> if x then "true" else "false")
1782− , (\x -> Encoding $ if x then "\"true\"" else "\"false\"")
1785−instance ToJSONKey Int where
1786− toJSONKey = ToJSONKeyText
1787− ( LT.toStrict . LTB.toLazyText . LTBI.decimal
1788− , \x -> Encoding $ B.char7 '"' <> fromEncoding (toEncoding x) <> B.char7 '"'
1791instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
2292instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
1792instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
2293instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
1793instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
2294instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
1795instance ToJSONKey Char where
2296instance ToJSONKey Char where
1796− toJSONKey = ToJSONKeyText (T.singleton, toEncoding)
2297+ toJSONKey = ToJSONKeyText T.singleton (E.string . (:[]))
1797− toJSONKeyList = ToJSONKeyText (T.pack, toEncoding . T.pack)
2298+ toJSONKeyList = toJSONKeyText T.pack
1799instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
2300instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
1800 toJSONKey = toJSONKeyList
2301 toJSONKey = toJSONKeyList
1802−instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1803− toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1805-------------------------------------------------------------------------------
2303-------------------------------------------------------------------------------
1806−-- Tuple instances, see tuple-instances-to.hs
2304+-- Tuple instances
1807-------------------------------------------------------------------------------
2305-------------------------------------------------------------------------------
1809−instance ToJSON2 ((,) ) where
2307+instance ToJSON2 (,) where
1810 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
2308 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
1811 mv <- VM.unsafeNew 2
2309 mv <- VM.unsafeNew 2
1812 VM.unsafeWrite mv 0 (toA a)
2310 VM.unsafeWrite mv 0 (toA a)
@@ -1814,9 +2312,7 @@ instance ToJSON2 ((,) ) where
1814 return mv
2312 return mv
1815 {-# INLINE liftToJSON2 #-}
2313 {-# INLINE liftToJSON2 #-}
1817− liftToEncoding2 toA _ toB _ (a, b) = tuple $
2315+ liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
1820 {-# INLINE liftToEncoding2 #-}
2316 {-# INLINE liftToEncoding2 #-}
1822instance (ToJSON a) => ToJSON1 ((,) a) where
2318instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -1840,10 +2336,11 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
1840 return mv
2336 return mv
1841 {-# INLINE liftToJSON2 #-}
2337 {-# INLINE liftToJSON2 #-}
1843− liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2339+ liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
1844− toEncoding a >*<
2340+ [ toEncoding a
1845− toB b >*<
2341+ , toB b
1847 {-# INLINE liftToEncoding2 #-}
2344 {-# INLINE liftToEncoding2 #-}
1849instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
2346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -1868,11 +2365,12 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
1868 return mv
2365 return mv
1869 {-# INLINE liftToJSON2 #-}
2366 {-# INLINE liftToJSON2 #-}
1871− liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2368+ liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
1872− toEncoding a >*<
2369+ [ toEncoding a
1873− toEncoding b >*<
2370+ , toEncoding b
1874− toC c >*<
2371+ , toC c
1876 {-# INLINE liftToEncoding2 #-}
2374 {-# INLINE liftToEncoding2 #-}
1878instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
2376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -1898,12 +2396,13 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
1898 return mv
2396 return mv
1899 {-# INLINE liftToJSON2 #-}
2397 {-# INLINE liftToJSON2 #-}
1901− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2399+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
1902− toEncoding a >*<
2400+ [ toEncoding a
1903− toEncoding b >*<
2401+ , toEncoding b
1904− toEncoding c >*<
2402+ , toEncoding c
1905− toD d >*<
2403+ , toD d
1907 {-# INLINE liftToEncoding2 #-}
2406 {-# INLINE liftToEncoding2 #-}
1909instance (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
@@ -1930,13 +2429,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
1930 return mv
2429 return mv
1931 {-# INLINE liftToJSON2 #-}
2430 {-# INLINE liftToJSON2 #-}
1933− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2432+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
1934− toEncoding a >*<
2433+ [ toEncoding a
1935− toEncoding b >*<
2434+ , toEncoding b
1936− toEncoding c >*<
2435+ , toEncoding c
1937− toEncoding d >*<
2436+ , toEncoding d
1938− toE e >*<
2437+ , toE e
1940 {-# INLINE liftToEncoding2 #-}
2440 {-# INLINE liftToEncoding2 #-}
1942instance (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
@@ -1964,14 +2464,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
1964 return mv
2464 return mv
1965 {-# INLINE liftToJSON2 #-}
2465 {-# INLINE liftToJSON2 #-}
1967− 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
1968− toEncoding a >*<
2468+ [ toEncoding a
1969− toEncoding b >*<
2469+ , toEncoding b
1970− toEncoding c >*<
2470+ , toEncoding c
1971− toEncoding d >*<
2471+ , toEncoding d
1972− toEncoding e >*<
2472+ , toEncoding e
1973− toF f >*<
2473+ , toF f
1975 {-# INLINE liftToEncoding2 #-}
2476 {-# INLINE liftToEncoding2 #-}
1977instance (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
@@ -2000,15 +2501,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
2000 return mv
2501 return mv
2001 {-# INLINE liftToJSON2 #-}
2502 {-# INLINE liftToJSON2 #-}
2003− 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
2004− toEncoding a >*<
2505+ [ toEncoding a
2005− toEncoding b >*<
2506+ , toEncoding b
2006− toEncoding c >*<
2507+ , toEncoding c
2007− toEncoding d >*<
2508+ , toEncoding d
2008− toEncoding e >*<
2509+ , toEncoding e
2009− toEncoding f >*<
2510+ , toEncoding f
2010− toG g >*<
2511+ , toG g
2012 {-# INLINE liftToEncoding2 #-}
2514 {-# INLINE liftToEncoding2 #-}
2014instance (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
@@ -2038,16 +2540,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
2038 return mv
2540 return mv
2039 {-# INLINE liftToJSON2 #-}
2541 {-# INLINE liftToJSON2 #-}
2041− 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
2042− toEncoding a >*<
2544+ [ toEncoding a
2043− toEncoding b >*<
2545+ , toEncoding b
2044− toEncoding c >*<
2546+ , toEncoding c
2045− toEncoding d >*<
2547+ , toEncoding d
2046− toEncoding e >*<
2548+ , toEncoding e
2047− toEncoding f >*<
2549+ , toEncoding f
2048− toEncoding g >*<
2550+ , toEncoding g
2049− toH h >*<
2551+ , toH h
2051 {-# INLINE liftToEncoding2 #-}
2554 {-# INLINE liftToEncoding2 #-}
2053instance (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
@@ -2078,17 +2581,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2078 return mv
2581 return mv
2079 {-# INLINE liftToJSON2 #-}
2582 {-# INLINE liftToJSON2 #-}
2081− 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
2082− toEncoding a >*<
2585+ [ toEncoding a
2083− toEncoding b >*<
2586+ , toEncoding b
2084− toEncoding c >*<
2587+ , toEncoding c
2085− toEncoding d >*<
2588+ , toEncoding d
2086− toEncoding e >*<
2589+ , toEncoding e
2087− toEncoding f >*<
2590+ , toEncoding f
2088− toEncoding g >*<
2591+ , toEncoding g
2089− toEncoding h >*<
2592+ , toEncoding h
2090− toI i >*<
2593+ , toI i
2092 {-# INLINE liftToEncoding2 #-}
2596 {-# INLINE liftToEncoding2 #-}
2094instance (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
@@ -2120,18 +2624,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2120 return mv
2624 return mv
2121 {-# INLINE liftToJSON2 #-}
2625 {-# INLINE liftToJSON2 #-}
2123− 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
2124− toEncoding a >*<
2628+ [ toEncoding a
2125− toEncoding b >*<
2629+ , toEncoding b
2126− toEncoding c >*<
2630+ , toEncoding c
2127− toEncoding d >*<
2631+ , toEncoding d
2128− toEncoding e >*<
2632+ , toEncoding e
2129− toEncoding f >*<
2633+ , toEncoding f
2130− toEncoding g >*<
2634+ , toEncoding g
2131− toEncoding h >*<
2635+ , toEncoding h
2132− toEncoding i >*<
2636+ , toEncoding i
2133− toJ j >*<
2637+ , toJ j
2135 {-# INLINE liftToEncoding2 #-}
2640 {-# INLINE liftToEncoding2 #-}
2137instance (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
@@ -2164,19 +2669,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2164 return mv
2669 return mv
2165 {-# INLINE liftToJSON2 #-}
2670 {-# INLINE liftToJSON2 #-}
2167− 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
2168− toEncoding a >*<
2673+ [ toEncoding a
2169− toEncoding b >*<
2674+ , toEncoding b
2170− toEncoding c >*<
2675+ , toEncoding c
2171− toEncoding d >*<
2676+ , toEncoding d
2172− toEncoding e >*<
2677+ , toEncoding e
2173− toEncoding f >*<
2678+ , toEncoding f
2174− toEncoding g >*<
2679+ , toEncoding g
2175− toEncoding h >*<
2680+ , toEncoding h
2176− toEncoding i >*<
2681+ , toEncoding i
2177− toEncoding j >*<
2682+ , toEncoding j
2178− toK k >*<
2683+ , toK k
2180 {-# INLINE liftToEncoding2 #-}
2686 {-# INLINE liftToEncoding2 #-}
2182instance (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
@@ -2210,20 +2716,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2210 return mv
2716 return mv
2211 {-# INLINE liftToJSON2 #-}
2717 {-# INLINE liftToJSON2 #-}
2213− 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
2214− toEncoding a >*<
2720+ [ toEncoding a
2215− toEncoding b >*<
2721+ , toEncoding b
2216− toEncoding c >*<
2722+ , toEncoding c
2217− toEncoding d >*<
2723+ , toEncoding d
2218− toEncoding e >*<
2724+ , toEncoding e
2219− toEncoding f >*<
2725+ , toEncoding f
2220− toEncoding g >*<
2726+ , toEncoding g
2221− toEncoding h >*<
2727+ , toEncoding h
2222− toEncoding i >*<
2728+ , toEncoding i
2223− toEncoding j >*<
2729+ , toEncoding j
2224− toEncoding k >*<
2730+ , toEncoding k
2225− toL l >*<
2731+ , toL l
2227 {-# INLINE liftToEncoding2 #-}
2734 {-# INLINE liftToEncoding2 #-}
2229instance (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
@@ -2258,21 +2765,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2258 return mv
2765 return mv
2259 {-# INLINE liftToJSON2 #-}
2766 {-# INLINE liftToJSON2 #-}
2261− 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
2262− toEncoding a >*<
2769+ [ toEncoding a
2263− toEncoding b >*<
2770+ , toEncoding b
2264− toEncoding c >*<
2771+ , toEncoding c
2265− toEncoding d >*<
2772+ , toEncoding d
2266− toEncoding e >*<
2773+ , toEncoding e
2267− toEncoding f >*<
2774+ , toEncoding f
2268− toEncoding g >*<
2775+ , toEncoding g
2269− toEncoding h >*<
2776+ , toEncoding h
2270− toEncoding i >*<
2777+ , toEncoding i
2271− toEncoding j >*<
2778+ , toEncoding j
2272− toEncoding k >*<
2779+ , toEncoding k
2273− toEncoding l >*<
2780+ , toEncoding l
2274− toM m >*<
2781+ , toM m
2276 {-# INLINE liftToEncoding2 #-}
2784 {-# INLINE liftToEncoding2 #-}
2278instance (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
@@ -2308,22 +2816,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2308 return mv
2816 return mv
2309 {-# INLINE liftToJSON2 #-}
2817 {-# INLINE liftToJSON2 #-}
2311− 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
2312− toEncoding a >*<
2820+ [ toEncoding a
2313− toEncoding b >*<
2821+ , toEncoding b
2314− toEncoding c >*<
2822+ , toEncoding c
2315− toEncoding d >*<
2823+ , toEncoding d
2316− toEncoding e >*<
2824+ , toEncoding e
2317− toEncoding f >*<
2825+ , toEncoding f
2318− toEncoding g >*<
2826+ , toEncoding g
2319− toEncoding h >*<
2827+ , toEncoding h
2320− toEncoding i >*<
2828+ , toEncoding i
2321− toEncoding j >*<
2829+ , toEncoding j
2322− toEncoding k >*<
2830+ , toEncoding k
2323− toEncoding l >*<
2831+ , toEncoding l
2324− toEncoding m >*<
2832+ , toEncoding m
2325− toN n >*<
2833+ , toN n
2327 {-# INLINE liftToEncoding2 #-}
2836 {-# INLINE liftToEncoding2 #-}
2329instance (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
@@ -2337,3 +2846,27 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2337 {-# INLINE toJSON #-}
2846 {-# INLINE toJSON #-}
2338 toEncoding = toEncoding2
2847 toEncoding = toEncoding2
2339 {-# INLINE toEncoding #-}
2848 {-# INLINE toEncoding #-}
2850+--------------------------------------------------------------------------------
2852+-- | Wrap a list of pairs as an object.
2853+class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2854+ fromPairs :: pairs -> enc
2856+instance (a ~ Value) => FromPairs (Encoding' a) Series where
2857+ fromPairs = E.pairs
2859+instance FromPairs Value (DList Pair) where
2860+ fromPairs = object . toList
2862+-- | Like 'KeyValue' but the value is already converted to JSON
2863+-- ('Value' or 'Encoding'), and the result actually represents lists of pairs
2864+-- so it can be readily concatenated.
2865+class Monoid kv => KeyValuePair v kv where
2866+ pair :: String -> v -> kv
2868+instance (v ~ Value) => KeyValuePair v (DList Pair) where
2869+ pair k v = DList.singleton (pack k .= v)
2871+instance (e ~ Encoding) => KeyValuePair e Series where