@@ -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
@@ -45,75 +43,102 @@ module Data.Aeson.Types.ToJSON (
45 , ToJSONKeyFunction(..)
43 , ToJSONKeyFunction(..)
46 , toJSONKeyText
44 , toJSONKeyText
47 , contramapToJSONKeyFunction
45 , contramapToJSONKeyFunction
48 -- * Object key-value pairs
50 -- * Object key-value pairs
49 , KeyValue(..)
51 , KeyValue(..)
50 -- * Functions needed for documentation
54 -- * Functions needed for documentation
51 -- * Encoding functions
55 -- * Encoding functions
52 , listEncoding
56 , listEncoding
53 , listValue
57 , listValue
57import Prelude.Compat
60import Prelude.Compat
62+import Control.Applicative (Const(..))
63+import Control.Monad.ST (ST)
59−import Data.Aeson.Encoding.Internal (Encoding, Encoding', Series, dict,
64+import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
60− emptyArray_, tuple, (>*<), (><))
65+import Data.Aeson.Encoding.Internal ((>*<))
61import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
66import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
62−import Data.Aeson.Types.Generic
67+import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
63import Data.Aeson.Types.Internal
68import Data.Aeson.Types.Internal
65−-- We need internal here for generic deriving
66−import qualified Data.Aeson.Encoding.Internal as E
68−import Control.Applicative (Const (..))
69−import Control.Monad.ST (ST)
70−import Data.Attoparsec.Number (Number (..))
69+import Data.Attoparsec.Number (Number(..))
71−import Data.Bits (unsafeShiftR)
70+import Data.Bits (unsafeShiftR)
72−import Data.DList (DList)
71+import Data.DList (DList)
73−import Data.Fixed (Fixed, HasResolution)
72+import Data.Fixed (Fixed, HasResolution, Nano)
74−import Data.Foldable (toList)
73+import Data.Foldable (toList)
75−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(..))
76−import Data.Int (Int16, Int32, Int64, Int8)
79+import Data.Int (Int16, Int32, Int64, Int8)
77−import Data.List (intersperse)
80+import Data.List (intersperse)
78−import Data.List.NonEmpty (NonEmpty (..))
81+import Data.List.NonEmpty (NonEmpty(..))
79−import Data.Monoid (Dual (..), First (..), Last (..), (<>))
82+import Data.Semigroup ((<>))
80−import Data.Proxy (Proxy (..))
83+import Data.Proxy (Proxy(..))
81−import Data.Ratio (Ratio, denominator, numerator)
84+import Data.Ratio (Ratio, denominator, numerator)
82−import Data.Scientific (Scientific)
85+import Data.Scientific (Scientific)
83−import Data.Tagged (Tagged (..))
86+import Data.Tagged (Tagged(..))
84−import Data.Text (Text, pack)
87+import Data.Text (Text, pack)
85−import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
88+import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
86− 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 (..))
87−import Data.Time.Format (FormatTime, formatTime)
92+import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
88−import Data.Time.Locale.Compat (defaultTimeLocale)
89−import Data.Vector (Vector)
93+import Data.Vector (Vector)
90−import Data.Version (Version, showVersion)
94+import Data.Version (Version, showVersion)
95+import Data.Void (Void, absurd)
91−import Data.Word (Word16, Word32, Word64, Word8)
96+import Data.Word (Word16, Word32, Word64, Word8)
92−import Foreign.Storable (Storable)
97+import Foreign.Storable (Storable)
98+import Foreign.C.Types (CTime (..))
93import GHC.Generics
99import GHC.Generics
94−import Numeric.Natural (Natural)
100+import Numeric.Natural (Natural)
95−
101+import qualified Data.Aeson.Encoding as E
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.Tree as Tree
119+import qualified Data.Tree as Tree
111−import qualified Data.Vector as V
120+import qualified Data.UUID.Types as UUID
121+import qualified Data.Vector as V
112−import qualified Data.Vector.Generic as VG
122+import qualified Data.Vector.Generic as VG
113−import qualified Data.Vector.Mutable as VM
123+import qualified Data.Vector.Mutable as VM
114−import qualified Data.Vector.Primitive as VP
124+import qualified Data.Vector.Primitive as VP
115−import qualified Data.Vector.Storable as VS
125+import qualified Data.Vector.Storable as VS
116−import qualified Data.Vector.Unboxed as VU
126+import qualified Data.Vector.Unboxed as VU
128+import qualified Data.Aeson.Encoding.Builder as EB
129+import qualified Data.ByteString.Builder as B
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
118toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
119toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
144toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
@@ -131,51 +156,53 @@ realFloatToJSON d
132-- | Class of generic representation types that can be converted to
157-- | Class of generic representation types that can be converted to
134−class GToJSON arity f where
159+class GToJSON enc arity f where
135 -- | This method (applied to 'defaultOptions') is used as the
160 -- | This method (applied to 'defaultOptions') is used as the
136− -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
161+ -- default generic implementation of 'toJSON'
162+ -- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
137− -- 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').
138− gToJSON :: Options -> Proxy arity
168+ gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
139− -> (a -> Value) -> ([a] -> Value) -> f a -> Value
169+
140−
170+-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
141−-- | Class of generic representation types that can be converted to
171+-- function arguments that encode occurrences of the type parameter (for
142−-- a JSON 'Encoding'.
172+-- 'ToJSON1').
143−class GToEncoding arity f where
173+data ToArgs res arity a where
144− -- | This method (applied to 'defaultOptions') can be used as the
174+ NoToArgs :: ToArgs res Zero a
145− -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
175+ To1Args :: (a -> res) -> ([a] -> res) -> ToArgs res One a
146− -- or 'liftToEncoding' (if the @arity@ is 'One').
147− gToEncoding :: Options -> Proxy arity
148− -> (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
150-- | A configurable generic JSON creator. This function applied to
177-- | A configurable generic JSON creator. This function applied to
151-- 'defaultOptions' is used as the default for 'toJSON' when the type
178-- 'defaultOptions' is used as the default for 'toJSON' when the type
152-- is an instance of 'Generic'.
179-- is an instance of 'Generic'.
153−genericToJSON :: (Generic a, GToJSON Zero (Rep a))
180+genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
154 => Options -> a -> Value
181 => Options -> a -> Value
155−genericToJSON opts = gToJSON opts proxyZero undefined undefined . from
182+genericToJSON opts = gToJSON opts NoToArgs . from
157-- | A configurable generic JSON creator. This function applied to
184-- | A configurable generic JSON creator. This function applied to
158-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
159-- is an instance of 'Generic1'.
186-- is an instance of 'Generic1'.
160−genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
187+genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
161 => Options -> (a -> Value) -> ([a] -> Value)
188 => Options -> (a -> Value) -> ([a] -> Value)
162 -> f a -> Value
189 -> f a -> Value
163−genericLiftToJSON opts tj tjl = gToJSON opts proxyOne tj tjl . from1
190+genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
165-- | A configurable generic JSON encoder. This function applied to
192-- | A configurable generic JSON encoder. This function applied to
166-- 'defaultOptions' is used as the default for 'toEncoding' when the type
193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
167-- is an instance of 'Generic'.
194-- is an instance of 'Generic'.
168−genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
195+genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
169 => Options -> a -> Encoding
196 => Options -> a -> Encoding
170−genericToEncoding opts = gToEncoding opts proxyZero undefined undefined . from
197+genericToEncoding opts = gToJSON opts NoToArgs . from
172-- | A configurable generic JSON encoder. This function applied to
199-- | A configurable generic JSON encoder. This function applied to
173-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
174-- is an instance of 'Generic1'.
201-- is an instance of 'Generic1'.
175−genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
202+genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
176 => Options -> (a -> Encoding) -> ([a] -> Encoding)
203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
177 -> f a -> Encoding
204 -> f a -> Encoding
178−genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
205+genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
180-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
@@ -183,6 +210,9 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
184-- | 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'.
186-- An example type and instance:
216-- An example type and instance:
@@ -191,10 +221,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
192-- data Coord = Coord { x :: Double, y :: Double }
222-- data Coord = Coord { x :: Double, y :: Double }
194−-- instance ToJSON Coord where
224+-- instance 'ToJSON' Coord where
195−-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
225+-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
197−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
227+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
200-- Instead of manually writing your 'ToJSON' instance, there are two options
230-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -202,17 +232,15 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
203-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
204-- 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
205−-- so will probably be more efficient than the following two options:
235+-- so it will probably be more efficient than the following option.
207-- * The compiler can provide a default generic implementation for
237-- * The compiler can provide a default generic implementation for
208-- 'toJSON'.
238-- 'toJSON'.
210-- 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
211−-- datatype and declare a 'ToJSON' instance for your datatype without giving
241+-- datatype and declare a 'ToJSON' instance. If you require nothing other than
212−-- definitions for 'toJSON' or 'toEncoding'.
242+-- 'defaultOptions', it is sufficient to write (and this is the only
213−--
243+-- alternative where the default 'toJSON' implementation is sufficient):
214−-- For example, the previous example can be simplified to a more
218-- {-\# LANGUAGE DeriveGeneric \#-}
246-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -221,35 +249,46 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
222-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
224−-- instance ToJSON Coord where
252+-- instance 'ToJSON' Coord where
225−-- toEncoding = 'genericToEncoding' 'defaultOptions'
253+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
228−-- 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
229−-- 'toEncoding' function is a relatively new addition to this class.
257+-- to implement both methods:
230−-- To allow users of older versions of this library to upgrade without
231−-- having to edit all of their instances or encounter surprising
232−-- incompatibilities, the default implementation of 'toEncoding' uses
233−-- 'toJSON'. This produces correct results, but since it performs an
234−-- intermediate conversion to a 'Value', it will be less efficient
235−-- than directly emitting an 'Encoding'. Our one-liner definition of
236−-- 'toEncoding' above bypasses the intermediate 'Value'.
238−-- If @DefaultSignatures@ doesn't give exactly the results you want,
239−-- you can customize the generic encoding with only a tiny amount of
240−-- effort, using 'genericToJSON' and 'genericToEncoding' with your
241−-- preferred 'Options':
260+-- customOptions = 'defaultOptions'
261+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
244−-- instance ToJSON Coord where
264+-- instance 'ToJSON' Coord where
245−-- toJSON = 'genericToJSON' 'defaultOptions'
265+-- 'toJSON' = 'genericToJSON' customOptions
246−-- 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.)
248class ToJSON a where
287class ToJSON a where
249 -- | Convert a Haskell value to a JSON-friendly intermediate type.
288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
250 toJSON :: a -> Value
289 toJSON :: a -> Value
252− default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
291+ default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
253 toJSON = genericToJSON defaultOptions
292 toJSON = genericToJSON defaultOptions
255 -- | Encode a Haskell value as JSON.
294 -- | Encode a Haskell value as JSON.
@@ -266,8 +305,8 @@ class ToJSON a where
266 -- extension, and then have GHC generate a method body as follows.
305 -- extension, and then have GHC generate a method body as follows.
269− -- instance ToJSON Coord where
308+ -- instance 'ToJSON' Coord where
270− -- toEncoding = 'genericToEncoding' 'defaultOptions'
309+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
273 toEncoding :: a -> Encoding
312 toEncoding :: a -> Encoding
@@ -299,13 +338,20 @@ instance KeyValue Pair where
299 name .= value = (name, toJSON value)
338 name .= value = (name, toJSON value)
300 {-# 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)
302-------------------------------------------------------------------------------
348-------------------------------------------------------------------------------
303-- Classes and types for map keys
349-- Classes and types for map keys
304-------------------------------------------------------------------------------
350-------------------------------------------------------------------------------
306-- | 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
307-- (like 'Map' or 'HashMap'). For example, since 'Text' has a 'ToJSONKey'
353-- (like 'Map' or 'HashMap'). For example, since 'Text' has a 'ToJSONKey'
308−-- 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
309-- type 'Map' 'Text' 'Char':
355-- type 'Map' 'Text' 'Char':
311-- >>> LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]
357-- >>> LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]
@@ -351,16 +397,16 @@ instance KeyValue Pair where
351-- >>> let m = Map.fromList [(Foo 4 "bar",'a'),(Foo 6 "arg",'b')]
397-- >>> let m = Map.fromList [(Foo 4 "bar",'a'),(Foo 6 "arg",'b')]
352-- >>> LBC8.putStrLn $ encode m
398-- >>> LBC8.putStrLn $ encode m
353-- [[{"fooName":"bar","fooAge":4},"a"],[{"fooName":"arg","fooAge":6},"b"]]
399-- [[{"fooName":"bar","fooAge":4},"a"],[{"fooName":"arg","fooAge":6},"b"]]
355-- The next case to consider is if we have a type that is a
401-- The next case to consider is if we have a type that is a
356-- newtype wrapper around 'Text'. The recommended approach is to use
402-- newtype wrapper around 'Text'. The recommended approach is to use
357-- generalized newtype deriving:
403-- generalized newtype deriving:
359−-- > newtype RecordId = RecordId { getRecordId :: Text}
405+-- > newtype RecordId = RecordId { getRecordId :: Text }
360-- > deriving (Eq,Ord,ToJSONKey)
406-- > deriving (Eq,Ord,ToJSONKey)
362-- Then we may write:
408-- Then we may write:
364-- >>> LBC8.putStrLn $ encode $ Map.fromList [(RecordId "abc",'a')]
410-- >>> LBC8.putStrLn $ encode $ Map.fromList [(RecordId "abc",'a')]
365-- {"abc":"a"}
411-- {"abc":"a"}
@@ -371,14 +417,24 @@ instance KeyValue Pair where
372-- It is possible to get the 'ToJSONKey' instance for free as we did
418-- It is possible to get the 'ToJSONKey' instance for free as we did
373-- with 'Foo'. However, in this case, we have a natural way to go to
419-- with 'Foo'. However, in this case, we have a natural way to go to
374−-- and from 'Text' that does not require any escape sequences. So, in
420+-- and from 'Text' that does not require any escape sequences. So
375−-- this example, 'ToJSONKeyText' will be used instead of 'ToJSONKeyValue'.
421+-- 'ToJSONKeyText' can be used instead of 'ToJSONKeyValue' to encode maps
422+-- as objects instead of arrays of pairs. This instance may be
423+-- implemented using generics as follows:
426+-- instance 'ToJSONKey' Color where
427+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
430+-- === __Low-level implementations__
376-- The 'Show' instance can be used to help write 'ToJSONKey':
432-- The 'Show' instance can be used to help write 'ToJSONKey':
378-- > instance ToJSONKey Color where
434-- > instance ToJSONKey Color where
379-- > toJSONKey = ToJSONKeyText f g
435-- > toJSONKey = ToJSONKeyText f g
380-- > where f = Text.pack . show
436-- > where f = Text.pack . show
381−-- > g = text . Text.pack . show
437+-- > g = text . Text.pack . show
382-- > -- text function is from Data.Aeson.Encoding
438-- > -- text function is from Data.Aeson.Encoding
384-- The situation of needing to turning function @a -> Text@ into
440-- The situation of needing to turning function @a -> Text@ into
@@ -387,7 +443,7 @@ instance KeyValue Pair where
388-- > instance ToJSONKey Color where
444-- > instance ToJSONKey Color where
389-- > toJSONKey = toJSONKeyText (Text.pack . show)
445-- > toJSONKey = toJSONKeyText (Text.pack . show)
391-- The performance of the above instance can be improved by
447-- The performance of the above instance can be improved by
392-- not using 'String' as an intermediate step when converting to
448-- not using 'String' as an intermediate step when converting to
393-- 'Text'. One option for improving performance would be to use
449-- 'Text'. One option for improving performance would be to use
@@ -396,7 +452,7 @@ instance KeyValue Pair where
396-- builder) is generated by encoding the 'Text' to a 'ByteString',
452-- builder) is generated by encoding the 'Text' to a 'ByteString',
397-- an intermediate step that could be avoided. The fastest possible
453-- an intermediate step that could be avoided. The fastest possible
398-- implementation would be:
454-- implementation would be:
400-- > -- Assuming that OverloadedStrings is enabled
456-- > -- Assuming that OverloadedStrings is enabled
401-- > instance ToJSONKey Color where
457-- > instance ToJSONKey Color where
402-- > toJSONKey = ToJSONKeyText f g
458-- > toJSONKey = ToJSONKeyText f g
@@ -410,15 +466,15 @@ instance KeyValue Pair where
410-- maximize performance.
466-- maximize performance.
412class ToJSONKey a where
468class ToJSONKey a where
413− -- | 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.
415 toJSONKey :: ToJSONKeyFunction a
470 toJSONKey :: ToJSONKeyFunction a
416 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
471 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
417 toJSONKey = ToJSONKeyValue toJSON toEncoding
472 toJSONKey = ToJSONKeyValue toJSON toEncoding
419 -- | This is similar in spirit to the 'showsList' method of 'Show'.
474 -- | This is similar in spirit to the 'showsList' method of 'Show'.
420 -- It makes it possible to give 'String' keys special treatment
475 -- It makes it possible to give 'String' keys special treatment
421− -- without using @OverlappingInstances@.
476+ -- without using @OverlappingInstances@. End users should always
477+ -- be able to use the default implementation of this method.
422 toJSONKeyList :: ToJSONKeyFunction [a]
478 toJSONKeyList :: ToJSONKeyFunction [a]
423 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
479 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
424 toJSONKeyList = ToJSONKeyValue toJSON toEncoding
480 toJSONKeyList = ToJSONKeyValue toJSON toEncoding
@@ -445,7 +501,14 @@ toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
445toJSONKeyTextEnc e = ToJSONKeyText tot e
501toJSONKeyTextEnc e = ToJSONKeyText tot e
447 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
503 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
504+ tot = T.dropAround (== '"')
448− tot = T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . E.encodingToLazyByteString . e
507+ . E.encodingToLazyByteString
510+instance Contravariant ToJSONKeyFunction where
511+ contramap = contramapToJSONKeyFunction
450-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
513-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
451contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
@@ -453,6 +516,29 @@ contramapToJSONKeyFunction h x = case x of
453 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
516 ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
454 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
456-------------------------------------------------------------------------------
542-------------------------------------------------------------------------------
457-- Lifings of FromJSON and ToJSON to unary and binary type constructors
543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
458-------------------------------------------------------------------------------
544-------------------------------------------------------------------------------
@@ -465,7 +551,7 @@ contramapToJSONKeyFunction h x = case x of
466-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
467-- 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
468−-- so will probably be more efficient than the following two options:
554+-- so it will probably be more efficient than the following option.
470-- * The compiler can provide a default generic implementation for
556-- * The compiler can provide a default generic implementation for
471-- 'toJSON1'.
557-- 'toJSON1'.
@@ -483,23 +569,29 @@ contramapToJSONKeyFunction h x = case x of
484-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
486−-- instance ToJSON a => ToJSON1 (Pair a)
572+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
489−-- If @DefaultSignatures@ doesn't give exactly the results you want,
575+-- If the default implementation doesn't give exactly the results you want,
490-- 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
491-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
492-- your preferred 'Options':
578-- your preferred 'Options':
581+-- customOptions = 'defaultOptions'
582+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
495−-- instance ToJSON a => ToJSON1 (Pair a) where
585+-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
496−-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
586+-- 'liftToJSON' = 'genericLiftToJSON' customOptions
497−-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
587+-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
590+-- See also 'ToJSON'.
499class ToJSON1 f where
591class ToJSON1 f where
500 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
502− default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
594+ default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
503 => (a -> Value) -> ([a] -> Value) -> f a -> Value
595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
504 liftToJSON = genericLiftToJSON defaultOptions
596 liftToJSON = genericLiftToJSON defaultOptions
@@ -508,7 +600,7 @@ class ToJSON1 f where
509 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
511− default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
603+ default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
512 => (a -> Encoding) -> ([a] -> Encoding)
604 => (a -> Encoding) -> ([a] -> Encoding)
513 -> f a -> Encoding
605 -> f a -> Encoding
514 liftToEncoding = genericLiftToEncoding defaultOptions
606 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -556,9 +648,25 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
556-- Encoding functions
648-- Encoding functions
557-------------------------------------------------------------------------------
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
559listEncoding :: (a -> Encoding) -> [a] -> Encoding
665listEncoding :: (a -> Encoding) -> [a] -> Encoding
560listEncoding = E.list
666listEncoding = E.list
667+{-# INLINE listEncoding #-}
669+-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
562listValue :: (a -> Value) -> [a] -> Value
670listValue :: (a -> Value) -> [a] -> Value
563listValue f = Array . V.fromList . map f
671listValue f = Array . V.fromList . map f
564{-# INLINE listValue #-}
672{-# INLINE listValue #-}
@@ -577,6 +685,11 @@ instance ToJSON1 [] where
577 {-# INLINE liftToEncoding #-}
685 {-# INLINE liftToEncoding #-}
579instance (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] #-}
580 toJSON = toJSON1
693 toJSON = toJSON1
581 {-# INLINE toJSON #-}
694 {-# INLINE toJSON #-}
@@ -587,282 +700,260 @@ instance (ToJSON a) => ToJSON [a] where
587-- Generic toJSON / toEncoding
700-- Generic toJSON / toEncoding
588-------------------------------------------------------------------------------
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 #-}
590--------------------------------------------------------------------------------
744--------------------------------------------------------------------------------
591-- Generic toJSON
745-- Generic toJSON
593−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
594− -- Meta-information, which is not handled elsewhere, is ignored:
748+-- possible but makes error messages a bit harder to understand for missing
595− gToJSON opts pa tj tjl = gToJSON opts pa tj tjl . unM1
749+-- instances.
597−instance (ToJSON a) => GToJSON arity (K1 i a) where
751+instance GToJSON Value arity V1 where
598− -- Constant values are encoded using their ToJSON instance:
752+ -- Empty values do not exist, which makes the job of formatting them
599− gToJSON _opts _ _ _ = toJSON . unK1
753+ -- rather easy:
754+ gToJSON _ _ x = x `seq` error "case: V1"
755+ {-# INLINE gToJSON #-}
601−instance GToJSON One Par1 where
757+instance ToJSON a => GToJSON Value arity (K1 i a) where
602− -- Direct occurrences of the last type parameter are encoded with the
758+ -- Constant values are encoded using their ToJSON instance:
603− -- function passed in as an argument:
604− gToJSON _opts _ tj _ = tj . unPar1
759+ gToJSON _opts _ = toJSON . unK1
760+ {-# INLINE gToJSON #-}
606−instance (ToJSON1 f) => GToJSON One (Rec1 f) where
762+instance ToJSON1 f => GToJSON Value One (Rec1 f) where
607 -- Recursive occurrences of the last type parameter are encoded using their
763 -- Recursive occurrences of the last type parameter are encoded using their
608 -- ToJSON1 instance:
764 -- ToJSON1 instance:
609− gToJSON _opts _ tj tjl = liftToJSON tj tjl . unRec1
765+ gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766+ {-# INLINE gToJSON #-}
611−instance GToJSON arity U1 where
768+instance GToJSON Value arity U1 where
612 -- Empty constructors are encoded to an empty array:
769 -- Empty constructors are encoded to an empty array:
613− gToJSON _opts _ _ _ _ = emptyArray
770+ gToJSON _opts _ _ = emptyArray
614−
771+ {-# INLINE gToJSON #-}
615−instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
616− -- Constructors need to be encoded differently depending on whether they're
617− -- a record or not. This distinction is made by 'consToJSON':
618− gToJSON opts pa tj tjl = consToJSON opts pa tj tjl . unM1
620instance ( WriteProduct arity a, WriteProduct arity b
773instance ( WriteProduct arity a, WriteProduct arity b
621 , ProductSize a, ProductSize b
774 , ProductSize a, ProductSize b
622− ) => GToJSON arity (a :*: b) where
775+ ) => GToJSON Value arity (a :*: b)
623 -- 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
624 -- 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
625 -- 'writeProduct':
779 -- 'writeProduct':
626− gToJSON opts pa tj tjl p =
780+ gToJSON opts targs p =
627 Array $ V.create $ do
781 Array $ V.create $ do
628 mv <- VM.unsafeNew lenProduct
782 mv <- VM.unsafeNew lenProduct
629− writeProduct opts pa mv 0 lenProduct tj tjl p
783+ writeProduct opts targs mv 0 lenProduct p
630 return mv
784 return mv
632 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
633 productSize
787 productSize
788+ {-# INLINE gToJSON #-}
635−instance ( AllNullary (a :+: b) allNullary
790+instance ( ToJSON1 f
636− , SumToJSON arity (a :+: b) allNullary
791+ , GToJSON Value One g
637− ) => GToJSON arity (a :+: b) where
792+ ) => GToJSON Value One (f :.: g)
638− -- If all constructors of a sum datatype are nullary and the
793+ where
639− -- 'allNullaryToStringTag' option is set they are encoded to
640− -- strings. This distinction is made by 'sumToJSON':
641− gToJSON opts pa tj tjl = (unTagged :: Tagged allNullary Value -> Value)
642− . sumToJSON opts pa tj tjl
644−instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
645 -- 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
646 -- 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
647 -- instance to generically encode the innermost type:
796 -- instance to generically encode the innermost type:
648− gToJSON opts pa tj tjl =
797+ gToJSON opts targs =
649− let gtj = gToJSON opts pa tj tjl in
798+ let gtj = gToJSON opts targs in
650 liftToJSON gtj (listValue gtj) . unComp1
799 liftToJSON gtj (listValue gtj) . unComp1
800+ {-# INLINE gToJSON #-}
652--------------------------------------------------------------------------------
802--------------------------------------------------------------------------------
653-- Generic toEncoding
803-- Generic toEncoding
655−instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
656− -- Meta-information, which is not handled elsewhere, is ignored:
657− gToEncoding opts pa te tel = gToEncoding opts pa te tel . unM1
659−instance (ToJSON a) => GToEncoding arity (K1 i a) where
805+instance ToJSON a => GToJSON Encoding arity (K1 i a) where
660 -- Constant values are encoded using their ToJSON instance:
806 -- Constant values are encoded using their ToJSON instance:
661− gToEncoding _opts _ _ _ = toEncoding . unK1
807+ gToJSON _opts _ = toEncoding . unK1
662−
808+ {-# INLINE gToJSON #-}
663−instance GToEncoding One Par1 where
664− -- Direct occurrences of the last type parameter are encoded with the
665− -- function passed in as an argument:
666− gToEncoding _opts _ te _ = te . unPar1
668−instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
810+instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
669 -- Recursive occurrences of the last type parameter are encoded using their
811 -- Recursive occurrences of the last type parameter are encoded using their
670 -- ToEncoding1 instance:
812 -- ToEncoding1 instance:
671− gToEncoding _opts _ te tel = liftToEncoding te tel . unRec1
813+ gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814+ {-# INLINE gToJSON #-}
673−instance GToEncoding arity U1 where
816+instance GToJSON Encoding arity U1 where
674 -- Empty constructors are encoded to an empty array:
817 -- Empty constructors are encoded to an empty array:
675− gToEncoding _opts _ _ _ _ = E.emptyArray_
818+ gToJSON _opts _ _ = E.emptyArray_
676−
819+ {-# INLINE gToJSON #-}
677−instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
678− -- Constructors need to be encoded differently depending on whether they're
679− -- a record or not. This distinction is made by 'consToEncoding':
680− gToEncoding opts pa te tel = consToEncoding opts pa te tel . unM1
682instance ( EncodeProduct arity a
821instance ( EncodeProduct arity a
683 , EncodeProduct arity b
822 , EncodeProduct arity b
684− ) => GToEncoding arity (a :*: b) where
823+ ) => GToJSON Encoding arity (a :*: b)
685 -- 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
686 -- 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
687 -- 'encodeProduct':
827 -- 'encodeProduct':
688− 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 #-}
690−instance ( AllNullary (a :+: b) allNullary
831+instance ( ToJSON1 f
691− , SumToEncoding arity (a :+: b) allNullary
832+ , GToJSON Encoding One g
692− ) => GToEncoding arity (a :+: b) where
833+ ) => GToJSON Encoding One (f :.: g)
693− -- If all constructors of a sum datatype are nullary and the
834+ where
694− -- 'allNullaryToStringTag' option is set they are encoded to
695− -- strings. This distinction is made by 'sumToEncoding':
696− gToEncoding opts pa te tel
697− = (unTagged :: Tagged allNullary (Encoding) -> Encoding)
698− . sumToEncoding opts pa te tel
700−instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
701 -- 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
702 -- 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
703 -- instance to generically encode the innermost type:
837 -- instance to generically encode the innermost type:
704− gToEncoding opts pa te tel =
838+ gToJSON opts targs =
705− let gte = gToEncoding opts pa te tel in
839+ let gte = gToJSON opts targs in
706 liftToEncoding gte (listEncoding gte) . unComp1
840 liftToEncoding gte (listEncoding gte) . unComp1
841+ {-# INLINE gToJSON #-}
708--------------------------------------------------------------------------------
843--------------------------------------------------------------------------------
710−class SumToJSON arity f allNullary where
845+class SumToJSON enc arity f allNullary where
711− sumToJSON :: Options -> Proxy arity
846+ sumToJSON :: Options -> ToArgs enc arity a
712− -> (a -> Value) -> ([a] -> Value)
713− -> f a -> Tagged allNullary Value
847+ -> f a -> Tagged allNullary enc
715−instance ( GetConName f
849+instance ( GetConName f
716− , TaggedObjectPairs arity f
850+ , FromString enc
717− , ObjectWithSingleFieldObj arity f
851+ , TaggedObject enc arity f
718− , TwoElemArrayObj arity f
852+ , SumToJSON' ObjectWithSingleField enc arity f
853+ , SumToJSON' TwoElemArray enc arity f
854+ , SumToJSON' UntaggedValue enc arity f
719− ) => SumToJSON arity f True where
855+ ) => SumToJSON enc arity f True
720− sumToJSON opts pa tj tjl
857+ sumToJSON opts targs
721− | allNullaryToStringTag opts = Tagged . String . pack
858+ | allNullaryToStringTag opts = Tagged . fromString
722 . constructorTagModifier opts . getConName
859 . constructorTagModifier opts . getConName
723− | otherwise = Tagged . nonAllNullarySumToJSON opts pa tj tjl
860+ | otherwise = Tagged . nonAllNullarySumToJSON opts targs
725−instance ( TwoElemArrayObj arity f
862+instance ( TaggedObject enc arity f
726− , TaggedObjectPairs arity f
863+ , SumToJSON' ObjectWithSingleField enc arity f
727− , ObjectWithSingleFieldObj arity f
864+ , SumToJSON' TwoElemArray enc arity f
865+ , SumToJSON' UntaggedValue enc arity f
728− ) => SumToJSON arity f False where
866+ ) => SumToJSON enc arity f False
729− sumToJSON opts pa tj tjl = Tagged . nonAllNullarySumToJSON opts pa tj tjl
868+ sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
731−nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
870+nonAllNullarySumToJSON :: ( TaggedObject enc arity f
732− , TaggedObjectPairs arity f
871+ , SumToJSON' ObjectWithSingleField enc arity f
733− , ObjectWithSingleFieldObj arity f
872+ , SumToJSON' TwoElemArray enc arity f
873+ , SumToJSON' UntaggedValue enc arity f
734− ) => Options -> Proxy arity
874+ ) => Options -> ToArgs enc arity a
735− -> (a -> Value) -> ([a] -> Value)
875+ -> f a -> enc
737−nonAllNullarySumToJSON opts pa tj tjl =
876+nonAllNullarySumToJSON opts targs =
738 case sumEncoding opts of
877 case sumEncoding opts of
740− object . taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl
741− ObjectWithSingleField -> Object . objectWithSingleFieldObj opts pa tj tjl
742− TwoElemArray -> Array . twoElemArrayObj opts pa tj tjl
744−--------------------------------------------------------------------------------
746−class SumToEncoding arity f allNullary where
747− sumToEncoding :: Options -> Proxy arity
748− -> (a -> Encoding) -> ([a] -> Encoding)
749− -> f a -> Tagged allNullary (Encoding)
751−instance ( GetConName f
752− , TaggedObjectEnc arity f
753− , ObjectWithSingleFieldEnc arity f
754− , TwoElemArrayEnc arity f
755− ) => SumToEncoding arity f True where
756− sumToEncoding opts pa te tel
757− | allNullaryToStringTag opts = Tagged . toEncoding .
758− constructorTagModifier opts . getConName
759− | otherwise = Tagged . nonAllNullarySumToEncoding opts pa te tel
761−instance ( TwoElemArrayEnc arity f
762− , TaggedObjectEnc arity f
763− , ObjectWithSingleFieldEnc arity f
764− ) => SumToEncoding arity f False where
765− sumToEncoding opts pa te tel = Tagged . nonAllNullarySumToEncoding opts pa te tel
767−nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
768− , TaggedObjectEnc arity f
769− , ObjectWithSingleFieldEnc arity f
770− ) => Options -> Proxy arity
771− -> (a -> Encoding) -> ([a] -> Encoding)
773−nonAllNullarySumToEncoding opts pa te tel =
774− case sumEncoding opts of
775 TaggedObject{..} ->
879 TaggedObject{..} ->
776− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel
880+ taggedObject opts targs tagFieldName contentsFieldName
777− ObjectWithSingleField -> objectWithSingleFieldEnc opts pa te tel
778− TwoElemArray -> twoElemArrayEnc opts pa te tel
780−--------------------------------------------------------------------------------
882+ ObjectWithSingleField ->
883+ (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884+ . sumToJSON' opts targs
782−class TaggedObjectPairs arity f where
886+ TwoElemArray ->
783− taggedObjectPairs :: Options -> Proxy arity
785− -> (a -> Value) -> ([a] -> Value)
788−instance ( TaggedObjectPairs arity a
789− , TaggedObjectPairs arity b
790− ) => TaggedObjectPairs arity (a :+: b) where
791− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (L1 x) =
792− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
793− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (R1 x) =
794− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
796−instance ( IsRecord a isRecord
797− , TaggedObjectPairs' arity a isRecord
799− ) => TaggedObjectPairs arity (C1 c a) where
800− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl =
801− (pack tagFieldName .= constructorTagModifier opts
802− (conName (undefined :: t c a p)) :) .
803− (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
887+ (unTagged :: Tagged TwoElemArray enc -> enc)
804− taggedObjectPairs' opts pa contentsFieldName tj tjl . unM1
888+ . sumToJSON' opts targs
806−class TaggedObjectPairs' arity f isRecord where
890+ UntaggedValue ->
807− taggedObjectPairs' :: Options -> Proxy arity
891+ (unTagged :: Tagged UntaggedValue enc -> enc)
808− -> String -> (a -> Value) -> ([a] -> Value)
892+ . sumToJSON' opts targs
809− -> f a -> Tagged isRecord [Pair]
811−instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
894+--------------------------------------------------------------------------------
812− taggedObjectPairs' _ _ _ _ _ _ = Tagged []
814−instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
896+class FromString enc where
815− taggedObjectPairs' opts pa _ tj tjl =
897+ fromString :: String -> enc
816− Tagged . toList . recordToPairs opts pa tj tjl
818−instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
899+instance FromString Encoding where
819− taggedObjectPairs' opts pa contentsFieldName tj tjl =
900+ fromString = toEncoding
820− Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts pa tj tjl
901+
902+instance FromString Value where
903+ fromString = String . pack
822--------------------------------------------------------------------------------
905--------------------------------------------------------------------------------
824−class TaggedObjectEnc arity f where
907+class TaggedObject enc arity f where
825− taggedObjectEnc :: Options -> Proxy arity
908+ taggedObject :: Options -> ToArgs enc arity a
826− -> String -> String
909+ -> String -> String
827− -> (a -> Encoding) -> ([a] -> Encoding)
910+ -> f a -> enc
828− -> f a -> Encoding
911+
830−instance ( TaggedObjectEnc arity a
912+instance ( TaggedObject enc arity a
831− , TaggedObjectEnc arity b
913+ , TaggedObject enc arity b
832− ) => TaggedObjectEnc arity (a :+: b) where
914+ ) => TaggedObject enc arity (a :+: b)
833− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (L1 x) =
916+ taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
834− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
917+ taggedObject opts targs tagFieldName contentsFieldName x
835− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (R1 x) =
918+ taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
836− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
919+ taggedObject opts targs tagFieldName contentsFieldName x
838−instance ( IsRecord a isRecord
921+instance ( IsRecord a isRecord
839− , TaggedObjectEnc' arity a isRecord
922+ , TaggedObject' enc pairs arity a isRecord
923+ , FromPairs enc pairs
925+ , KeyValuePair enc pairs
840 , Constructor c
926 , Constructor c
841− ) => TaggedObjectEnc arity (C1 c a) where
927+ ) => TaggedObject enc arity (C1 c a)
842− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel v =
929+ taggedObject opts targs tagFieldName contentsFieldName =
843− E.wrapObject $
930+ fromPairs . mappend tag . contents
844− (E.string tagFieldName ><
932+ tag = tagFieldName `pair`
846− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) ><
933+ (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
847− ((unTagged :: Tagged isRecord (Encoding) -> Encoding) .
936+ (unTagged :: Tagged isRecord pairs -> pairs) .
848− taggedObjectEnc' opts pa contentsFieldName te tel . unM1 $ v)
937+ taggedObject' opts targs contentsFieldName . unM1
850−class TaggedObjectEnc' arity f isRecord where
939+class TaggedObject' enc pairs arity f isRecord where
851− taggedObjectEnc' :: Options -> Proxy arity
940+ taggedObject' :: Options -> ToArgs enc arity a
852− -> String -> (a -> Encoding) -> ([a] -> Encoding)
941+ -> String -> f a -> Tagged isRecord pairs
853− -> f a -> Tagged isRecord (Encoding)
942+
854−
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
855−instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
950+instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
856− taggedObjectEnc' _ _ _ _ _ _ = Tagged E.empty
951+ taggedObject' _ _ _ _ = Tagged mempty
858−instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
953+instance ( RecordToPairs enc pairs arity f
859− taggedObjectEnc' opts pa _ te tel = Tagged . (E.comma ><) . fst
954+ ) => TaggedObject' enc pairs arity f True
860− . recordToEncoding opts pa te tel
955+ where
861−
956+ taggedObject' opts targs _ = Tagged . recordToPairs opts targs
862−instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
863− taggedObjectEnc' opts pa contentsFieldName te tel =
864− Tagged . (\z -> E.comma >< toEncoding contentsFieldName >< E.colon >< z) .
865− gToEncoding opts pa te tel
867--------------------------------------------------------------------------------
958--------------------------------------------------------------------------------
@@ -877,283 +968,242 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
877instance (Constructor c) => GetConName (C1 c a) where
968instance (Constructor c) => GetConName (C1 c a) where
878 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
880--------------------------------------------------------------------------------
983--------------------------------------------------------------------------------
882−class TwoElemArrayObj arity f where
985+class SumToJSON' s enc arity f where
883− twoElemArrayObj :: Options -> Proxy arity
986+ sumToJSON' :: Options -> ToArgs enc arity a
884− -> (a -> Value) -> ([a] -> Value)
987+ -> f a -> Tagged s enc
885− -> f a -> V.Vector Value
887−instance ( TwoElemArrayObj arity a
989+instance ( SumToJSON' s enc arity a
888− , TwoElemArrayObj arity b
990+ , SumToJSON' s enc arity b
889− ) => TwoElemArrayObj arity (a :+: b) where
991+ ) => SumToJSON' s enc arity (a :+: b)
890− twoElemArrayObj opts pa tj tjl (L1 x) = twoElemArrayObj opts pa tj tjl x
992+ where
891− 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+--------------------------------------------------------------------------------
893−instance ( GToJSON arity a
998+instance ( GToJSON Value arity a
894− , ConsToJSON arity a
999+ , ConsToJSON Value arity a
895 , Constructor c
1000 , Constructor c
896− ) => TwoElemArrayObj arity (C1 c a) where
1001+ ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
897− twoElemArrayObj opts pa tj tjl x = V.create $ do
1002+ sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
898 mv <- VM.unsafeNew 2
1003 mv <- VM.unsafeNew 2
899 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
1004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
900 $ conName (undefined :: t c a p)
1005 $ conName (undefined :: t c a p)
901− VM.unsafeWrite mv 1 $ gToJSON opts pa tj tjl x
1006+ VM.unsafeWrite mv 1 $ gToJSON opts targs x
902 return mv
1007 return mv
904--------------------------------------------------------------------------------
1009--------------------------------------------------------------------------------
906−class TwoElemArrayEnc arity f where
1011+instance ( GToJSON Encoding arity a
907− twoElemArrayEnc :: Options -> Proxy arity
1012+ , ConsToJSON Encoding arity a
908− -> (a -> Encoding) -> ([a] -> Encoding)
911−instance ( TwoElemArrayEnc arity a
912− , TwoElemArrayEnc arity b
913− ) => TwoElemArrayEnc arity (a :+: b) where
914− twoElemArrayEnc opts pa te tel (L1 x) = twoElemArrayEnc opts pa te tel x
915− twoElemArrayEnc opts pa te tel (R1 x) = twoElemArrayEnc opts pa te tel x
917−instance ( GToEncoding arity a
918− , ConsToEncoding arity a
919 , Constructor c
1013 , Constructor c
920− ) => TwoElemArrayEnc arity (C1 c a) where
1014+ ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
921− twoElemArrayEnc opts pa te tel x = E.tuple $
1015+ where
1016+ sumToJSON' opts targs x = Tagged $ E.list id
922− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
1017+ [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
923− gToEncoding opts pa te tel x
1018+ , gToJSON opts targs x
925--------------------------------------------------------------------------------
1021--------------------------------------------------------------------------------
927−class ConsToJSON arity f where
1023+class ConsToJSON enc arity f where
928− consToJSON :: Options -> Proxy arity
1024+ consToJSON :: Options -> ToArgs enc arity a
929− -> (a -> Value) -> ([a] -> Value)
1025+ -> f a -> enc
932−class ConsToJSON' arity f isRecord where
933− consToJSON' :: Options -> Proxy arity
934− -> Bool -- ^ Are we a record with one field?
935− -> (a -> Value) -> ([a] -> Value)
936− -> f a -> Tagged isRecord Value
938−instance ( IsRecord f isRecord
939− , ConsToJSON' arity f isRecord
940− ) => ConsToJSON arity f where
941− consToJSON opts pa tj tjl =
942− (unTagged :: Tagged isRecord Value -> Value)
943− . consToJSON' opts pa (isUnary (undefined :: f a)) tj tjl
945−instance (RecordToPairs arity f) => ConsToJSON' arity f True where
946− consToJSON' opts pa isUn tj tjl f = let
947− vals = toList $ recordToPairs opts pa tj tjl f
948− in case (unwrapUnaryRecords opts,isUn,vals) of
949− (True,True,[(_,val)]) -> Tagged val
950− _ -> Tagged $ object vals
952−instance GToJSON arity f => ConsToJSON' arity f False where
953− consToJSON' opts pa _ tj tjl = Tagged . gToJSON opts pa tj tjl
955−--------------------------------------------------------------------------------
957−class ConsToEncoding arity f where
958− consToEncoding :: Options -> Proxy arity
959− -> (a -> Encoding) -> ([a] -> Encoding)
962−class ConsToEncoding' arity f isRecord where
1027+class ConsToJSON' enc arity f isRecord where
963− consToEncoding' :: Options -> Proxy arity
1028+ consToJSON' :: Options -> ToArgs enc arity a
964− -> Bool -- ^ Are we a record with one field?
965− -> (a -> Encoding) -> ([a] -> Encoding)
966− -> f a -> Tagged isRecord (Encoding)
1029+ -> f a -> Tagged isRecord enc
968instance ( IsRecord f isRecord
1031instance ( IsRecord f isRecord
969− , ConsToEncoding' arity f isRecord
1032+ , ConsToJSON' enc arity f isRecord
970− ) => ConsToEncoding arity f where
1033+ ) => ConsToJSON enc arity f
971− consToEncoding opts pa te tel =
1034+ where
1035+ consToJSON opts targs =
972− (unTagged :: Tagged isRecord (Encoding) -> (Encoding))
1036+ (unTagged :: Tagged isRecord enc -> enc)
973− . consToEncoding' opts pa (isUnary (undefined :: f a)) te tel
1037+ . consToJSON' opts targs
974−
1038+ {-# INLINE consToJSON #-}
975−instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
1039+
976− consToEncoding' opts pa isUn te tel x =
1040+instance OVERLAPPING_
977− 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
978− in case (unwrapUnaryRecords opts, isUn, mbVal) of
1047+ | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
979− (True, True, Just val) -> Tagged val
1048+ | otherwise = Tagged . fromPairs . recordToPairs opts targs
980− _ -> 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' #-}
982−instance GToEncoding arity f => ConsToEncoding' arity f False where
1058+instance GToJSON enc arity f => ConsToJSON' enc arity f False where
983− consToEncoding' opts pa _ te tel = Tagged . gToEncoding opts pa te tel
1059+ consToJSON' opts targs = Tagged . gToJSON opts targs
984−
1060+ {-# INLINE consToJSON' #-}
985−--------------------------------------------------------------------------------
987−class RecordToPairs arity f where
988− recordToPairs :: Options -> Proxy arity
989− -> (a -> Value) -> ([a] -> Value)
990− -> f a -> DList Pair
992−instance ( RecordToPairs arity a
993− , RecordToPairs arity b
994− ) => RecordToPairs arity (a :*: b) where
995− recordToPairs opts pa tj tjl (a :*: b) = recordToPairs opts pa tj tjl a <>
996− recordToPairs opts pa tj tjl b
998−instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
999− recordToPairs = fieldToPair
1001−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1002− RecordToPairs arity (S1 s (K1 i (Maybe a))) where
1003− recordToPairs opts _ _ _ (M1 k1) | omitNothingFields opts
1004− , K1 Nothing <- k1 = DList.empty
1005− recordToPairs opts pa tj tjl m1 = fieldToPair opts pa tj tjl m1
1007−fieldToPair :: (Selector s, GToJSON arity a)
1008− => Options -> Proxy arity
1009− -> (p -> Value) -> ([p] -> Value)
1010− -> S1 s a p -> DList Pair
1011−fieldToPair opts pa tj tjl m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
1012− , gToJSON opts pa tj tjl (unM1 m1)
1015--------------------------------------------------------------------------------
1062--------------------------------------------------------------------------------
1017−class RecordToEncoding arity f where
1064+class RecordToPairs enc pairs arity f where
1018 -- 1st element: whole thing
1065 -- 1st element: whole thing
1019 -- 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
1020 -- of the field (without the key); 'Nothing' otherwise
1067 -- of the field (without the key); 'Nothing' otherwise
1021− recordToEncoding :: Options -> Proxy arity
1068+ recordToPairs :: Options -> ToArgs enc arity a
1022− -> (a -> Encoding) -> ([a] -> Encoding)
1069+ -> f a -> pairs
1023− -> f a -> (Encoding, Maybe (Encoding))
1070+
1024−
1071+instance ( Monoid pairs
1025−instance ( RecordToEncoding arity a
1072+ , RecordToPairs enc pairs arity a
1026− , RecordToEncoding arity b
1073+ , RecordToPairs enc pairs arity b
1027− ) => RecordToEncoding arity (a :*: b) where
1074+ ) => RecordToPairs enc pairs arity (a :*: b)
1028− recordToEncoding opts pa te tel (a :*: b) | omitNothingFields opts =
1075+ where
1029− (E.econcat $ intersperse E.comma $
1076+ recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
1030− filter (not . E.nullEncoding)
1077+ pairsOf a `mappend` pairsOf b
1031− [ fst (recordToEncoding opts pa te tel a)
1078+ where
1032− , fst (recordToEncoding opts pa te tel b) ]
1079+ pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
1033− , Nothing)
1080+ pairsOf = recordToPairs opts targs
1034− recordToEncoding opts pa te tel (a :*: b) =
1081+ {-# INLINE recordToPairs #-}
1035− (fst (recordToEncoding opts pa te tel a) >< E.comma ><
1082+
1036− fst (recordToEncoding opts pa te tel b),
1083+instance ( Selector s
1037− Nothing)
1084+ , GToJSON enc arity a
1038−
1085+ , KeyValuePair enc pairs
1039−
1086+ ) => RecordToPairs enc pairs arity (S1 s a)
1040−instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1087+ where
1041− recordToEncoding = fieldToEncoding
1088+ recordToPairs = fieldToPair
1042−
1089+ {-# INLINE recordToPairs #-}
1043−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1090+
1091+instance INCOHERENT_
1093+ , GToJSON enc arity (K1 i (Maybe a))
1094+ , KeyValuePair enc pairs
1044− RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1096+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
1045− recordToEncoding opts _ _ _ (M1 k1) | omitNothingFields opts
1098+ recordToPairs opts _ (M1 k1) | omitNothingFields opts
1046− , K1 Nothing <- k1 = (E.empty, Nothing)
1099+ , K1 Nothing <- k1 = mempty
1047− recordToEncoding opts pa te tel m1 = fieldToEncoding opts pa te tel m1
1100+ recordToPairs opts targs m1 = fieldToPair opts targs m1
1048−
1101+ {-# INLINE recordToPairs #-}
1049−fieldToEncoding :: (Selector s, GToEncoding arity a)
1102+
1050− => Options -> Proxy arity
1103+instance INCOHERENT_
1051− -> (p -> Encoding) -> ([p] -> Encoding)
1104+ ( Selector s
1052− -> S1 s a p -> (Encoding, Maybe (Encoding))
1105+ , GToJSON enc arity (K1 i (Maybe a))
1053−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 =
1054− let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1122+ let key = fieldLabelModifier opts (selName m1)
1055− valueBuilder = gToEncoding opts pa te tel (unM1 m1)
1123+ value = gToJSON opts targs (unM1 m1)
1056− in (keyBuilder >< E.colon >< valueBuilder, Just valueBuilder)
1124+ in key `pair` value
1125+{-# INLINE fieldToPair #-}
1058--------------------------------------------------------------------------------
1127--------------------------------------------------------------------------------
1060class WriteProduct arity f where
1129class WriteProduct arity f where
1061 writeProduct :: Options
1130 writeProduct :: Options
1062− -> Proxy arity
1131+ -> ToArgs Value arity a
1063 -> VM.MVector s Value
1132 -> VM.MVector s Value
1064 -> Int -- ^ index
1133 -> Int -- ^ index
1065 -> Int -- ^ length
1134 -> Int -- ^ length
1069 -> ST s ()
1136 -> ST s ()
1071instance ( WriteProduct arity a
1138instance ( WriteProduct arity a
1072 , WriteProduct arity b
1139 , WriteProduct arity b
1073 ) => WriteProduct arity (a :*: b) where
1140 ) => WriteProduct arity (a :*: b) where
1074− writeProduct opts pa mv ix len tj tjl (a :*: b) = do
1141+ writeProduct opts targs mv ix len (a :*: b) = do
1075− writeProduct opts pa mv ix lenL tj tjl a
1142+ writeProduct opts targs mv ix lenL a
1076− writeProduct opts pa mv ixR lenR tj tjl b
1143+ writeProduct opts targs mv ixR lenR b
1078 lenL = len `unsafeShiftR` 1
1145 lenL = len `unsafeShiftR` 1
1079 lenR = len - lenL
1146 lenR = len - lenL
1080 ixR = ix + lenL
1147 ixR = ix + lenL
1148+ {-# INLINE writeProduct #-}
1082−instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
1150+instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1083− writeProduct opts pa mv ix _ tj tjl =
1151+ writeProduct opts targs mv ix _ =
1084− VM.unsafeWrite mv ix . gToJSON opts pa tj tjl
1152+ VM.unsafeWrite mv ix . gToJSON opts targs
1153+ {-# INLINE writeProduct #-}
1086--------------------------------------------------------------------------------
1155--------------------------------------------------------------------------------
1088class EncodeProduct arity f where
1157class EncodeProduct arity f where
1089− encodeProduct :: Options -> Proxy arity
1158+ encodeProduct :: Options -> ToArgs Encoding arity a
1090− -> (a -> Encoding) -> ([a] -> Encoding)
1091 -> f a -> Encoding' E.InArray
1159 -> f a -> Encoding' E.InArray
1093instance ( EncodeProduct arity a
1161instance ( EncodeProduct arity a
1094 , EncodeProduct arity b
1162 , EncodeProduct arity b
1095 ) => EncodeProduct arity (a :*: b) where
1163 ) => EncodeProduct arity (a :*: b) where
1096− encodeProduct opts pa te tel (a :*: b) | omitNothingFields opts =
1164+ encodeProduct opts targs (a :*: b) | omitNothingFields opts =
1097 E.econcat $ intersperse E.comma $
1165 E.econcat $ intersperse E.comma $
1098 filter (not . E.nullEncoding)
1166 filter (not . E.nullEncoding)
1099− [encodeProduct opts pa te tel a, encodeProduct opts pa te tel b]
1167+ [encodeProduct opts targs a, encodeProduct opts targs b]
1100− encodeProduct opts pa te tel (a :*: b) = encodeProduct opts pa te tel a >*<
1168+ encodeProduct opts targs (a :*: b) =
1169+ encodeProduct opts targs a >*<
1101− encodeProduct opts pa te tel b
1170+ encodeProduct opts targs b
1171+ {-# INLINE encodeProduct #-}
1103−instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1173+instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1104− encodeProduct opts pa te tel a = E.retagEncoding $ gToEncoding opts pa te tel a
1174+ encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1175+ {-# INLINE encodeProduct #-}
1106--------------------------------------------------------------------------------
1177--------------------------------------------------------------------------------
1108−class ObjectWithSingleFieldObj arity f where
1109− objectWithSingleFieldObj :: Options -> Proxy arity
1110− -> (a -> Value) -> ([a] -> Value)
1113−instance ( ObjectWithSingleFieldObj arity a
1114− , ObjectWithSingleFieldObj arity b
1115− ) => ObjectWithSingleFieldObj arity (a :+: b) where
1116− objectWithSingleFieldObj opts pa tj tjl (L1 x) =
1117− objectWithSingleFieldObj opts pa tj tjl x
1118− objectWithSingleFieldObj opts pa tj tjl (R1 x) =
1119− objectWithSingleFieldObj opts pa tj tjl x
1121−instance ( GToJSON arity a
1179+instance ( GToJSON enc arity a
1122− , ConsToJSON arity a
1180+ , ConsToJSON enc arity a
1181+ , FromPairs enc pairs
1182+ , KeyValuePair enc pairs
1123 , Constructor c
1183 , Constructor c
1124− ) => ObjectWithSingleFieldObj arity (C1 c a) where
1184+ ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1125− 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
1127− typ = pack $ constructorTagModifier opts $
1189+ typ = constructorTagModifier opts $
1128 conName (undefined :: t c a p)
1190 conName (undefined :: t c a p)
1130--------------------------------------------------------------------------------
1192--------------------------------------------------------------------------------
1132−class ObjectWithSingleFieldEnc arity f where
1194+instance OVERLAPPABLE_
1133− objectWithSingleFieldEnc :: Options -> Proxy arity
1195+ ( ConsToJSON enc arity a
1134− -> (a -> Encoding) -> ([a] -> Encoding)
1196+ ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1135− -> f a -> Encoding
1197+ where
1136−
1198+ sumToJSON' opts targs = Tagged . gToJSON opts targs
1137−instance ( ObjectWithSingleFieldEnc arity a
1199+
1138− , ObjectWithSingleFieldEnc arity b
1200+instance OVERLAPPING_
1139− ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1140− objectWithSingleFieldEnc opts pa te tel (L1 x) =
1141− objectWithSingleFieldEnc opts pa te tel x
1142− objectWithSingleFieldEnc opts pa te tel (R1 x) =
1143− objectWithSingleFieldEnc opts pa te tel x
1145−instance ( GToEncoding arity a
1146− , ConsToEncoding arity a
1147− , Constructor c
1201+ ( Constructor c
1148− ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1202+ , FromString enc
1149− objectWithSingleFieldEnc opts pa te tel v =
1203+ ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1150− E.openCurly ><
1204+ where
1151− toEncoding
1205+ sumToJSON' opts _ _ = Tagged . fromString $
1152− (constructorTagModifier opts
1206+ constructorTagModifier opts $ conName (undefined :: t c U1 p)
1153− (conName (undefined :: t c a p))) ><
1155− gToEncoding opts pa te tel v ><
1158-------------------------------------------------------------------------------
1208-------------------------------------------------------------------------------
1159-- Instances
1209-- Instances
@@ -1163,21 +1213,6 @@ instance ( GToEncoding arity a
1164-------------------------------------------------------------------------------
1214-------------------------------------------------------------------------------
1166−instance ToJSON1 Identity where
1167− liftToJSON t _ (Identity a) = t a
1168− {-# INLINE liftToJSON #-}
1170− liftToEncoding t _ (Identity a) = t a
1171− {-# INLINE liftToEncoding #-}
1173−instance (ToJSON a) => ToJSON (Identity a) where
1175− {-# INLINE toJSON #-}
1177− toEncoding = toEncoding1
1178− {-# INLINE toEncoding #-}
1181instance ToJSON2 Const where
1216instance ToJSON2 Const where
1182 liftToJSON2 t _ _ _ (Const x) = t x
1217 liftToJSON2 t _ _ _ (Const x) = t x
1183 {-# INLINE liftToJSON2 #-}
1218 {-# INLINE liftToJSON2 #-}
@@ -1218,8 +1253,8 @@ instance (ToJSON a) => ToJSON (Maybe a) where
1220instance ToJSON2 Either where
1255instance ToJSON2 Either where
1221− liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton left (toA a)
1256+ liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton "Left" (toA a)
1222− liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1257+ liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton "Right" (toB b)
1223 {-# INLINE liftToJSON2 #-}
1258 {-# INLINE liftToJSON2 #-}
1225 liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
1260 liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
@@ -1241,9 +1276,12 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
1241 toEncoding = toEncoding2
1276 toEncoding = toEncoding2
1242 {-# INLINE toEncoding #-}
1277 {-# INLINE toEncoding #-}
1244−left, right :: Text
1279+instance ToJSON Void where
1245−left = "Left"
1280+ toJSON = absurd
1246−right = "Right"
1281+ {-# INLINE toJSON #-}
1283+ toEncoding = absurd
1284+ {-# INLINE toEncoding #-}
1249instance ToJSON Bool where
1287instance ToJSON Bool where
@@ -1430,7 +1468,6 @@ instance ToJSONKey Int64 where
1430 toJSONKey = toJSONKeyTextEnc E.int64Text
1468 toJSONKey = toJSONKeyTextEnc E.int64Text
1431 {-# INLINE toJSONKey #-}
1469 {-# INLINE toJSONKey #-}
1434instance ToJSON Word where
1471instance ToJSON Word where
1435 toJSON = Number . fromIntegral
1472 toJSON = Number . fromIntegral
1436 {-# INLINE toJSON #-}
1473 {-# INLINE toJSON #-}
@@ -1490,6 +1527,12 @@ instance ToJSONKey Word64 where
1490 toJSONKey = toJSONKeyTextEnc E.word64Text
1527 toJSONKey = toJSONKeyTextEnc E.word64Text
1491 {-# INLINE toJSONKey #-}
1528 {-# INLINE toJSONKey #-}
1530+instance ToJSON CTime where
1531+ toJSON (CTime i) = toJSON i
1532+ {-# INLINE toJSON #-}
1534+ toEncoding (CTime i) = toEncoding i
1535+ {-# INLINE toEncoding #-}
1494instance ToJSON Text where
1537instance ToJSON Text where
1495 toJSON = String
1538 toJSON = String
@@ -1502,6 +1545,7 @@ instance ToJSONKey Text where
1502 toJSONKey = toJSONKeyText id
1545 toJSONKey = toJSONKeyText id
1503 {-# INLINE toJSONKey #-}
1546 {-# INLINE toJSONKey #-}
1505instance ToJSON LT.Text where
1549instance ToJSON LT.Text where
1506 toJSON = String . LT.toStrict
1550 toJSON = String . LT.toStrict
1507 {-# INLINE toJSON #-}
1551 {-# INLINE toJSON #-}
@@ -1509,6 +1553,10 @@ instance ToJSON LT.Text where
1509 toEncoding = E.lazyText
1553 toEncoding = E.lazyText
1510 {-# INLINE toEncoding #-}
1554 {-# INLINE toEncoding #-}
1556+instance ToJSONKey LT.Text where
1557+ toJSONKey = toJSONKeyText LT.toStrict
1512instance ToJSON Version where
1560instance ToJSON Version where
1513 toJSON = toJSON . showVersion
1561 toJSON = toJSON . showVersion
1514 {-# INLINE toJSON #-}
1562 {-# INLINE toJSON #-}
@@ -1516,6 +1564,9 @@ instance ToJSON Version where
1516 toEncoding = toEncoding . showVersion
1564 toEncoding = toEncoding . showVersion
1517 {-# INLINE toEncoding #-}
1565 {-# INLINE toEncoding #-}
1567+instance ToJSONKey Version where
1568+ toJSONKey = toJSONKeyText (T.pack . showVersion)
1519-------------------------------------------------------------------------------
1570-------------------------------------------------------------------------------
1520-- semigroups NonEmpty
1571-- semigroups NonEmpty
1521-------------------------------------------------------------------------------
1572-------------------------------------------------------------------------------
@@ -1566,6 +1617,116 @@ instance (ToJSON a) => ToJSON (DList.DList a) where
1566 toEncoding = toEncoding1
1617 toEncoding = toEncoding1
1567 {-# INLINE toEncoding #-}
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
1728+ {-# INLINE toEncoding #-}
1569-------------------------------------------------------------------------------
1730-------------------------------------------------------------------------------
1570-- containers
1731-- containers
1571-------------------------------------------------------------------------------
1732-------------------------------------------------------------------------------
@@ -1635,13 +1796,11 @@ instance ToJSONKey k => ToJSON1 (M.Map k) where
1635 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1796 ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1636 {-# INLINE liftToJSON #-}
1797 {-# INLINE liftToJSON #-}
1638− -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
1639 liftToEncoding g _ = case toJSONKey of
1799 liftToEncoding g _ = case toJSONKey of
1640 ToJSONKeyText _ f -> dict f g M.foldrWithKey
1800 ToJSONKeyText _ f -> dict f g M.foldrWithKey
1641 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1801 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1643− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1644− pairEncoding f (a, b) = tuple $ f a >*< g b
1803+ pairEncoding f (a, b) = E.list id [f a, g b]
1645 {-# INLINE liftToEncoding #-}
1804 {-# INLINE liftToEncoding #-}
@@ -1679,6 +1838,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
1679 toEncoding = toEncoding1
1838 toEncoding = toEncoding1
1680 {-# 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
1683-------------------------------------------------------------------------------
1853-------------------------------------------------------------------------------
1684-- vector
1854-- vector
@@ -1692,10 +1862,12 @@ instance ToJSON1 Vector where
1692 {-# INLINE liftToEncoding #-}
1862 {-# INLINE liftToEncoding #-}
1694instance (ToJSON a) => ToJSON (Vector a) where
1864instance (ToJSON a) => ToJSON (Vector a) where
1695− toJSON = Array . V.map toJSON
1865+ {-# SPECIALIZE instance ToJSON Array #-}
1696 {-# INLINE toJSON #-}
1868 {-# INLINE toJSON #-}
1698− toEncoding = encodeVector
1870+ toEncoding = toEncoding1
1699 {-# INLINE toEncoding #-}
1871 {-# INLINE toEncoding #-}
1701encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1873encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
@@ -1759,11 +1931,12 @@ instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1759 ToJSONKeyText _ f -> dict f g H.foldrWithKey
1931 ToJSONKeyText _ f -> dict f g H.foldrWithKey
1760 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1932 ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1762− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1763− pairEncoding f (a, b) = tuple $ f a >*< g b
1934+ pairEncoding f (a, b) = E.list id [f a, g b]
1764 {-# INLINE liftToEncoding #-}
1935 {-# INLINE liftToEncoding #-}
1766instance (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 #-}
1767 toJSON = toJSON1
1940 toJSON = toJSON1
1768 {-# INLINE toJSON #-}
1941 {-# INLINE toJSON #-}
@@ -1793,6 +1966,32 @@ dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1793formatMillis :: (FormatTime t) => t -> String
1966formatMillis :: (FormatTime t) => t -> String
1794formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1967formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1969+-------------------------------------------------------------------------------
1971+-------------------------------------------------------------------------------
1973+instance ToJSON a => ToJSON (PM.Array a) where
1974+ -- note: we could do better than this if vector exposed the data
1975+ -- constructor in Data.Vector.
1976+ toJSON = toJSON . Exts.toList
1977+ toEncoding = toEncoding . Exts.toList
1979+instance ToJSON a => ToJSON (PM.SmallArray a) where
1980+ toJSON = toJSON . Exts.toList
1981+ toEncoding = toEncoding . Exts.toList
1983+#if (MIN_VERSION_primitive(0,6,4))
1984+instance (PM.Prim a,ToJSON a) => ToJSON (PM.PrimArray a) where
1985+ toJSON = toJSON . Exts.toList
1986+ toEncoding = toEncoding . Exts.toList
1988+#if !MIN_VERSION_primitive(0,7,0)
1989+instance (PM.PrimUnlifted a,ToJSON a) => ToJSON (PM.UnliftedArray a) where
1990+ toJSON = toJSON . Exts.toList
1991+ toEncoding = toEncoding . Exts.toList
1796-------------------------------------------------------------------------------
1995-------------------------------------------------------------------------------
1798-------------------------------------------------------------------------------
1997-------------------------------------------------------------------------------
@@ -1845,6 +2044,7 @@ stringEncoding = String
1845 . E.encodingToLazyByteString
2044 . E.encodingToLazyByteString
1846{-# INLINE stringEncoding #-}
2045{-# INLINE stringEncoding #-}
1848instance ToJSON NominalDiffTime where
2048instance ToJSON NominalDiffTime where
1849 toJSON = Number . realToFrac
2049 toJSON = Number . realToFrac
1850 {-# INLINE toJSON #-}
2050 {-# INLINE toJSON #-}
@@ -1852,18 +2052,70 @@ instance ToJSON NominalDiffTime where
1852 toEncoding = E.scientific . realToFrac
2052 toEncoding = E.scientific . realToFrac
1853 {-# 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
1855-------------------------------------------------------------------------------
2107-------------------------------------------------------------------------------
1856-- base Monoid/Semigroup
2108-- base Monoid/Semigroup
1857-------------------------------------------------------------------------------
2109-------------------------------------------------------------------------------
1859−instance ToJSON1 Dual where
2111+instance ToJSON1 Monoid.Dual where
1860− liftToJSON t _ = t . getDual
2112+ liftToJSON t _ = t . Monoid.getDual
1861 {-# INLINE liftToJSON #-}
2113 {-# INLINE liftToJSON #-}
1863− liftToEncoding t _ = t . getDual
2115+ liftToEncoding t _ = t . Monoid.getDual
1864 {-# INLINE liftToEncoding #-}
2116 {-# INLINE liftToEncoding #-}
1866−instance ToJSON a => ToJSON (Dual a) where
2118+instance ToJSON a => ToJSON (Monoid.Dual a) where
1867 toJSON = toJSON1
2119 toJSON = toJSON1
1868 {-# INLINE toJSON #-}
2120 {-# INLINE toJSON #-}
@@ -1871,14 +2123,14 @@ instance ToJSON a => ToJSON (Dual a) where
1871 {-# INLINE toEncoding #-}
2123 {-# INLINE toEncoding #-}
1874−instance ToJSON1 First where
2126+instance ToJSON1 Monoid.First where
1875− liftToJSON t to' = liftToJSON t to' . getFirst
2127+ liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
1876 {-# INLINE liftToJSON #-}
2128 {-# INLINE liftToJSON #-}
1878− liftToEncoding t to' = liftToEncoding t to' . getFirst
2130+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
1879 {-# INLINE liftToEncoding #-}
2131 {-# INLINE liftToEncoding #-}
1881−instance ToJSON a => ToJSON (First a) where
2133+instance ToJSON a => ToJSON (Monoid.First a) where
1882 toJSON = toJSON1
2134 toJSON = toJSON1
1883 {-# INLINE toJSON #-}
2135 {-# INLINE toJSON #-}
@@ -1886,14 +2138,103 @@ instance ToJSON a => ToJSON (First a) where
1886 {-# INLINE toEncoding #-}
2138 {-# INLINE toEncoding #-}
1889−instance ToJSON1 Last where
2141+instance ToJSON1 Monoid.Last where
1890− liftToJSON t to' = liftToJSON t to' . getLast
2142+ liftToJSON t to' = liftToJSON t to' . Monoid.getLast
1891 {-# INLINE liftToJSON #-}
2143 {-# INLINE liftToJSON #-}
1893− liftToEncoding t to' = liftToEncoding t to' . getLast
2145+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
1894 {-# INLINE liftToEncoding #-}
2146 {-# INLINE liftToEncoding #-}
1896−instance ToJSON a => ToJSON (Last a) where
2148+instance ToJSON a => ToJSON (Monoid.Last a) where
2150+ {-# INLINE toJSON #-}
2152+ toEncoding = toEncoding1
2153+ {-# INLINE toEncoding #-}
2156+instance ToJSON1 Semigroup.Min where
2157+ liftToJSON t _ (Semigroup.Min x) = t x
2158+ {-# INLINE liftToJSON #-}
2160+ liftToEncoding t _ (Semigroup.Min x) = t x
2161+ {-# INLINE liftToEncoding #-}
2163+instance ToJSON a => ToJSON (Semigroup.Min a) where
2165+ {-# INLINE toJSON #-}
2167+ toEncoding = toEncoding1
2168+ {-# INLINE toEncoding #-}
2171+instance ToJSON1 Semigroup.Max where
2172+ liftToJSON t _ (Semigroup.Max x) = t x
2173+ {-# INLINE liftToJSON #-}
2175+ liftToEncoding t _ (Semigroup.Max x) = t x
2176+ {-# INLINE liftToEncoding #-}
2178+instance ToJSON a => ToJSON (Semigroup.Max a) where
2180+ {-# INLINE toJSON #-}
2182+ toEncoding = toEncoding1
2183+ {-# INLINE toEncoding #-}
2185+instance ToJSON1 Semigroup.First where
2186+ liftToJSON t _ (Semigroup.First x) = t x
2187+ {-# INLINE liftToJSON #-}
2189+ liftToEncoding t _ (Semigroup.First x) = t x
2190+ {-# INLINE liftToEncoding #-}
2192+instance ToJSON a => ToJSON (Semigroup.First a) where
2194+ {-# INLINE toJSON #-}
2196+ toEncoding = toEncoding1
2197+ {-# INLINE toEncoding #-}
2200+instance ToJSON1 Semigroup.Last where
2201+ liftToJSON t _ (Semigroup.Last x) = t x
2202+ {-# INLINE liftToJSON #-}
2204+ liftToEncoding t _ (Semigroup.Last x) = t x
2205+ {-# INLINE liftToEncoding #-}
2207+instance ToJSON a => ToJSON (Semigroup.Last a) where
2209+ {-# INLINE toJSON #-}
2211+ toEncoding = toEncoding1
2212+ {-# INLINE toEncoding #-}
2215+instance ToJSON1 Semigroup.WrappedMonoid where
2216+ liftToJSON t _ (Semigroup.WrapMonoid x) = t x
2217+ {-# INLINE liftToJSON #-}
2219+ liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
2220+ {-# INLINE liftToEncoding #-}
2222+instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
2224+ {-# INLINE toJSON #-}
2226+ toEncoding = toEncoding1
2227+ {-# INLINE toEncoding #-}
2230+instance ToJSON1 Semigroup.Option where
2231+ liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
2232+ {-# INLINE liftToJSON #-}
2234+ liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
2235+ {-# INLINE liftToEncoding #-}
2237+instance ToJSON a => ToJSON (Semigroup.Option a) where
1897 toJSON = toJSON1
2238 toJSON = toJSON1
1898 {-# INLINE toJSON #-}
2239 {-# INLINE toJSON #-}
@@ -1904,6 +2245,13 @@ instance ToJSON a => ToJSON (Last a) where
1904-- tagged
2245-- tagged
1905-------------------------------------------------------------------------------
2246-------------------------------------------------------------------------------
2248+instance ToJSON1 Proxy where
2249+ liftToJSON _ _ _ = Null
2250+ {-# INLINE liftToJSON #-}
2252+ liftToEncoding _ _ _ = E.null_
2253+ {-# INLINE liftToEncoding #-}
1907instance ToJSON (Proxy a) where
2255instance ToJSON (Proxy a) where
1908 toJSON _ = Null
2256 toJSON _ = Null
1909 {-# INLINE toJSON #-}
2257 {-# INLINE toJSON #-}
@@ -1912,6 +2260,13 @@ instance ToJSON (Proxy a) where
1912 {-# 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 #-}
1915instance ToJSON1 (Tagged a) where
2270instance ToJSON1 (Tagged a) where
1916 liftToJSON t _ (Tagged x) = t x
2271 liftToJSON t _ (Tagged x) = t x
1917 {-# INLINE liftToJSON #-}
2272 {-# INLINE liftToJSON #-}
@@ -1926,7 +2281,6 @@ instance ToJSON b => ToJSON (Tagged a b) where
1926 toEncoding = toEncoding1
2281 toEncoding = toEncoding1
1927 {-# INLINE toEncoding #-}
2282 {-# INLINE toEncoding #-}
1930instance ToJSONKey b => ToJSONKey (Tagged a b) where
2284instance ToJSONKey b => ToJSONKey (Tagged a b) where
1931 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
1932 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
2286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -1946,14 +2300,11 @@ instance ToJSONKey Char where
1946instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
2300instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
1947 toJSONKey = toJSONKeyList
2301 toJSONKey = toJSONKeyList
1949−instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1950− toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1952-------------------------------------------------------------------------------
2303-------------------------------------------------------------------------------
1953−-- Tuple instances, see tuple-instances-to.hs
2304+-- Tuple instances
1954-------------------------------------------------------------------------------
2305-------------------------------------------------------------------------------
1956−instance ToJSON2 ((,) ) where
2307+instance ToJSON2 (,) where
1957 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
2308 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
1958 mv <- VM.unsafeNew 2
2309 mv <- VM.unsafeNew 2
1959 VM.unsafeWrite mv 0 (toA a)
2310 VM.unsafeWrite mv 0 (toA a)
@@ -1961,9 +2312,7 @@ instance ToJSON2 ((,) ) where
1961 return mv
2312 return mv
1962 {-# INLINE liftToJSON2 #-}
2313 {-# INLINE liftToJSON2 #-}
1964− liftToEncoding2 toA _ toB _ (a, b) = tuple $
2315+ liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
1967 {-# INLINE liftToEncoding2 #-}
2316 {-# INLINE liftToEncoding2 #-}
1969instance (ToJSON a) => ToJSON1 ((,) a) where
2318instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -1987,10 +2336,11 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
1987 return mv
2336 return mv
1988 {-# INLINE liftToJSON2 #-}
2337 {-# INLINE liftToJSON2 #-}
1990− liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2339+ liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
1991− toEncoding a >*<
2340+ [ toEncoding a
1992− toB b >*<
2341+ , toB b
1994 {-# INLINE liftToEncoding2 #-}
2344 {-# INLINE liftToEncoding2 #-}
1996instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
2346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -2015,11 +2365,12 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
2015 return mv
2365 return mv
2016 {-# INLINE liftToJSON2 #-}
2366 {-# INLINE liftToJSON2 #-}
2018− liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2368+ liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
2019− toEncoding a >*<
2369+ [ toEncoding a
2020− toEncoding b >*<
2370+ , toEncoding b
2021− toC c >*<
2371+ , toC c
2023 {-# INLINE liftToEncoding2 #-}
2374 {-# INLINE liftToEncoding2 #-}
2025instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
2376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -2045,12 +2396,13 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
2045 return mv
2396 return mv
2046 {-# INLINE liftToJSON2 #-}
2397 {-# INLINE liftToJSON2 #-}
2048− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2399+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
2049− toEncoding a >*<
2400+ [ toEncoding a
2050− toEncoding b >*<
2401+ , toEncoding b
2051− toEncoding c >*<
2402+ , toEncoding c
2052− toD d >*<
2403+ , toD d
2054 {-# INLINE liftToEncoding2 #-}
2406 {-# INLINE liftToEncoding2 #-}
2056instance (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
@@ -2077,13 +2429,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
2077 return mv
2429 return mv
2078 {-# INLINE liftToJSON2 #-}
2430 {-# INLINE liftToJSON2 #-}
2080− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2432+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
2081− toEncoding a >*<
2433+ [ toEncoding a
2082− toEncoding b >*<
2434+ , toEncoding b
2083− toEncoding c >*<
2435+ , toEncoding c
2084− toEncoding d >*<
2436+ , toEncoding d
2085− toE e >*<
2437+ , toE e
2087 {-# INLINE liftToEncoding2 #-}
2440 {-# INLINE liftToEncoding2 #-}
2089instance (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
@@ -2111,14 +2464,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
2111 return mv
2464 return mv
2112 {-# INLINE liftToJSON2 #-}
2465 {-# INLINE liftToJSON2 #-}
2114− 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
2115− toEncoding a >*<
2468+ [ toEncoding a
2116− toEncoding b >*<
2469+ , toEncoding b
2117− toEncoding c >*<
2470+ , toEncoding c
2118− toEncoding d >*<
2471+ , toEncoding d
2119− toEncoding e >*<
2472+ , toEncoding e
2120− toF f >*<
2473+ , toF f
2122 {-# INLINE liftToEncoding2 #-}
2476 {-# INLINE liftToEncoding2 #-}
2124instance (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
@@ -2147,15 +2501,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
2147 return mv
2501 return mv
2148 {-# INLINE liftToJSON2 #-}
2502 {-# INLINE liftToJSON2 #-}
2150− 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
2151− toEncoding a >*<
2505+ [ toEncoding a
2152− toEncoding b >*<
2506+ , toEncoding b
2153− toEncoding c >*<
2507+ , toEncoding c
2154− toEncoding d >*<
2508+ , toEncoding d
2155− toEncoding e >*<
2509+ , toEncoding e
2156− toEncoding f >*<
2510+ , toEncoding f
2157− toG g >*<
2511+ , toG g
2159 {-# INLINE liftToEncoding2 #-}
2514 {-# INLINE liftToEncoding2 #-}
2161instance (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
@@ -2185,16 +2540,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
2185 return mv
2540 return mv
2186 {-# INLINE liftToJSON2 #-}
2541 {-# INLINE liftToJSON2 #-}
2188− 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
2189− toEncoding a >*<
2544+ [ toEncoding a
2190− toEncoding b >*<
2545+ , toEncoding b
2191− toEncoding c >*<
2546+ , toEncoding c
2192− toEncoding d >*<
2547+ , toEncoding d
2193− toEncoding e >*<
2548+ , toEncoding e
2194− toEncoding f >*<
2549+ , toEncoding f
2195− toEncoding g >*<
2550+ , toEncoding g
2196− toH h >*<
2551+ , toH h
2198 {-# INLINE liftToEncoding2 #-}
2554 {-# INLINE liftToEncoding2 #-}
2200instance (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
@@ -2225,17 +2581,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2225 return mv
2581 return mv
2226 {-# INLINE liftToJSON2 #-}
2582 {-# INLINE liftToJSON2 #-}
2228− 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
2229− toEncoding a >*<
2585+ [ toEncoding a
2230− toEncoding b >*<
2586+ , toEncoding b
2231− toEncoding c >*<
2587+ , toEncoding c
2232− toEncoding d >*<
2588+ , toEncoding d
2233− toEncoding e >*<
2589+ , toEncoding e
2234− toEncoding f >*<
2590+ , toEncoding f
2235− toEncoding g >*<
2591+ , toEncoding g
2236− toEncoding h >*<
2592+ , toEncoding h
2237− toI i >*<
2593+ , toI i
2239 {-# INLINE liftToEncoding2 #-}
2596 {-# INLINE liftToEncoding2 #-}
2241instance (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
@@ -2267,18 +2624,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2267 return mv
2624 return mv
2268 {-# INLINE liftToJSON2 #-}
2625 {-# INLINE liftToJSON2 #-}
2270− 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
2271− toEncoding a >*<
2628+ [ toEncoding a
2272− toEncoding b >*<
2629+ , toEncoding b
2273− toEncoding c >*<
2630+ , toEncoding c
2274− toEncoding d >*<
2631+ , toEncoding d
2275− toEncoding e >*<
2632+ , toEncoding e
2276− toEncoding f >*<
2633+ , toEncoding f
2277− toEncoding g >*<
2634+ , toEncoding g
2278− toEncoding h >*<
2635+ , toEncoding h
2279− toEncoding i >*<
2636+ , toEncoding i
2280− toJ j >*<
2637+ , toJ j
2282 {-# INLINE liftToEncoding2 #-}
2640 {-# INLINE liftToEncoding2 #-}
2284instance (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
@@ -2311,19 +2669,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2311 return mv
2669 return mv
2312 {-# INLINE liftToJSON2 #-}
2670 {-# INLINE liftToJSON2 #-}
2314− 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
2315− toEncoding a >*<
2673+ [ toEncoding a
2316− toEncoding b >*<
2674+ , toEncoding b
2317− toEncoding c >*<
2675+ , toEncoding c
2318− toEncoding d >*<
2676+ , toEncoding d
2319− toEncoding e >*<
2677+ , toEncoding e
2320− toEncoding f >*<
2678+ , toEncoding f
2321− toEncoding g >*<
2679+ , toEncoding g
2322− toEncoding h >*<
2680+ , toEncoding h
2323− toEncoding i >*<
2681+ , toEncoding i
2324− toEncoding j >*<
2682+ , toEncoding j
2325− toK k >*<
2683+ , toK k
2327 {-# INLINE liftToEncoding2 #-}
2686 {-# 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) => 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
@@ -2357,20 +2716,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2357 return mv
2716 return mv
2358 {-# INLINE liftToJSON2 #-}
2717 {-# INLINE liftToJSON2 #-}
2360− 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
2361− toEncoding a >*<
2720+ [ toEncoding a
2362− toEncoding b >*<
2721+ , toEncoding b
2363− toEncoding c >*<
2722+ , toEncoding c
2364− toEncoding d >*<
2723+ , toEncoding d
2365− toEncoding e >*<
2724+ , toEncoding e
2366− toEncoding f >*<
2725+ , toEncoding f
2367− toEncoding g >*<
2726+ , toEncoding g
2368− toEncoding h >*<
2727+ , toEncoding h
2369− toEncoding i >*<
2728+ , toEncoding i
2370− toEncoding j >*<
2729+ , toEncoding j
2371− toEncoding k >*<
2730+ , toEncoding k
2372− toL l >*<
2731+ , toL l
2374 {-# INLINE liftToEncoding2 #-}
2734 {-# INLINE liftToEncoding2 #-}
2376instance (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
@@ -2405,21 +2765,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2405 return mv
2765 return mv
2406 {-# INLINE liftToJSON2 #-}
2766 {-# INLINE liftToJSON2 #-}
2408− 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
2409− toEncoding a >*<
2769+ [ toEncoding a
2410− toEncoding b >*<
2770+ , toEncoding b
2411− toEncoding c >*<
2771+ , toEncoding c
2412− toEncoding d >*<
2772+ , toEncoding d
2413− toEncoding e >*<
2773+ , toEncoding e
2414− toEncoding f >*<
2774+ , toEncoding f
2415− toEncoding g >*<
2775+ , toEncoding g
2416− toEncoding h >*<
2776+ , toEncoding h
2417− toEncoding i >*<
2777+ , toEncoding i
2418− toEncoding j >*<
2778+ , toEncoding j
2419− toEncoding k >*<
2779+ , toEncoding k
2420− toEncoding l >*<
2780+ , toEncoding l
2421− toM m >*<
2781+ , toM m
2423 {-# INLINE liftToEncoding2 #-}
2784 {-# INLINE liftToEncoding2 #-}
2425instance (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
@@ -2455,22 +2816,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2455 return mv
2816 return mv
2456 {-# INLINE liftToJSON2 #-}
2817 {-# INLINE liftToJSON2 #-}
2458− 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
2459− toEncoding a >*<
2820+ [ toEncoding a
2460− toEncoding b >*<
2821+ , toEncoding b
2461− toEncoding c >*<
2822+ , toEncoding c
2462− toEncoding d >*<
2823+ , toEncoding d
2463− toEncoding e >*<
2824+ , toEncoding e
2464− toEncoding f >*<
2825+ , toEncoding f
2465− toEncoding g >*<
2826+ , toEncoding g
2466− toEncoding h >*<
2827+ , toEncoding h
2467− toEncoding i >*<
2828+ , toEncoding i
2468− toEncoding j >*<
2829+ , toEncoding j
2469− toEncoding k >*<
2830+ , toEncoding k
2470− toEncoding l >*<
2831+ , toEncoding l
2471− toEncoding m >*<
2832+ , toEncoding m
2472− toN n >*<
2833+ , toN n
2474 {-# INLINE liftToEncoding2 #-}
2836 {-# INLINE liftToEncoding2 #-}
2476instance (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
@@ -2484,3 +2846,27 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2484 {-# INLINE toJSON #-}
2846 {-# INLINE toJSON #-}
2485 toEncoding = toEncoding2
2847 toEncoding = toEncoding2
2486 {-# 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