@@ -1,29 +1,27 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DataKinds #-}
3−{-# LANGUAGE DefaultSignatures #-}
2+{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE EmptyDataDecls #-}
4−{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE FunctionalDependencies #-}
6−{-# LANGUAGE GADTs #-}
7+{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE NoImplicitPrelude #-}
8−{-# LANGUAGE NamedFieldPuns #-}
9−{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PatternGuards #-}
10+{-# LANGUAGE PatternGuards #-}
11+{-# LANGUAGE PolyKinds #-}
11−{-# LANGUAGE RecordWildCards #-}
12+{-# LANGUAGE RecordWildCards #-}
12−{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE ScopedTypeVariables #-}
13−{-# LANGUAGE TypeOperators #-}
14+{-# LANGUAGE TypeOperators #-}
14−{-# LANGUAGE TypeSynonymInstances #-}
15−{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE UndecidableInstances #-}
17−#if __GLASGOW_HASKELL__ >= 706
18−{-# LANGUAGE PolyKinds #-}
21#include "overlapping-compat.h"
17#include "overlapping-compat.h"
18+#include "incoherent-compat.h"
23-- TODO: Drop this when we remove support for Data.Attoparsec.Number
20-- TODO: Drop this when we remove support for Data.Attoparsec.Number
24{-# OPTIONS_GHC -fno-warn-deprecations #-}
21{-# OPTIONS_GHC -fno-warn-deprecations #-}
26−module Data.Aeson.Types.ToJSON (
23+module Data.Aeson.Types.ToJSON
27 -- * Core JSON classes
25 -- * Core JSON classes
28 ToJSON(..)
26 ToJSON(..)
29 -- * Liftings to unary and binary type constructors
27 -- * Liftings to unary and binary type constructors
@@ -35,7 +33,7 @@ module Data.Aeson.Types.ToJSON (
35 , toEncoding2
33 , toEncoding2
36 -- * Generic JSON classes
34 -- * Generic JSON classes
37 , GToJSON(..)
35 , GToJSON(..)
38− , GToEncoding(..)
36+ , ToArgs(..)
39 , genericToJSON
37 , genericToJSON
40 , genericToEncoding
38 , genericToEncoding
41 , genericLiftToJSON
39 , genericLiftToJSON
@@ -43,87 +41,107 @@ module Data.Aeson.Types.ToJSON (
43 -- * Classes and types for map keys
41 -- * Classes and types for map keys
44 , ToJSONKey(..)
42 , ToJSONKey(..)
45 , ToJSONKeyFunction(..)
43 , ToJSONKeyFunction(..)
46 , contramapToJSONKeyFunction
45 , contramapToJSONKeyFunction
47 -- * Object key-value pairs
50 -- * Object key-value pairs
48 , KeyValue(..)
51 , KeyValue(..)
49 -- * Functions needed for documentation
54 -- * Functions needed for documentation
50 -- * Encoding functions
55 -- * Encoding functions
51 , listEncoding
56 , listEncoding
52 , listValue
57 , listValue
56import Prelude.Compat
60import Prelude.Compat
62+import Control.Applicative (Const(..))
63+import Control.Monad.ST (ST)
58−import Data.Aeson.Encoding.Internal (Encoding (..), Series, dict, emptyArray_,
64+import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
59− tuple, (>*<))
65+import Data.Aeson.Encoding.Internal ((>*<))
60−import Data.Aeson.Functions (mapHashKeyVal, mapKeyVal)
66+import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
61−import Data.Aeson.Types.Generic
67+import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
62import Data.Aeson.Types.Internal
68import Data.Aeson.Types.Internal
64−import qualified Data.Aeson.Encode.Builder as EB
65−import qualified Data.Aeson.Encoding.Internal as E
67−import Control.Applicative (Const (..))
68−import Control.Monad.ST (ST)
69−import Data.Attoparsec.Number (Number (..))
69+import Data.Attoparsec.Number (Number(..))
70−import Data.Bits (unsafeShiftR)
70+import Data.Bits (unsafeShiftR)
71−import Data.DList (DList)
71+import Data.DList (DList)
72−import Data.Fixed (Fixed, HasResolution)
72+import Data.Fixed (Fixed, HasResolution, Nano)
73−import Data.Foldable (toList)
73+import Data.Foldable (toList)
74−import Data.Functor.Identity (Identity (..))
74+import Data.Functor.Compose (Compose(..))
75+import Data.Functor.Contravariant (Contravariant (..))
76+import Data.Functor.Identity (Identity(..))
77+import Data.Functor.Product (Product(..))
78+import Data.Functor.Sum (Sum(..))
75−import Data.Int (Int16, Int32, Int64, Int8)
79+import Data.Int (Int16, Int32, Int64, Int8)
76−import Data.List (intersperse)
80+import Data.List (intersperse)
77−import Data.List.NonEmpty (NonEmpty (..))
81+import Data.List.NonEmpty (NonEmpty(..))
78−import Data.Monoid (Dual (..), First (..), Last (..), (<>))
82+import Data.Semigroup ((<>))
79−import Data.Proxy (Proxy (..))
83+import Data.Proxy (Proxy(..))
80−import Data.Ratio (Ratio, denominator, numerator)
84+import Data.Ratio (Ratio, denominator, numerator)
81−import Data.Scientific (Scientific)
85+import Data.Scientific (Scientific)
82−import Data.Tagged (Tagged (..))
86+import Data.Tagged (Tagged(..))
83−import Data.Text (Text, pack)
87+import Data.Text (Text, pack)
84−import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
88+import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
85− UTCTime, ZonedTime)
89+import Data.Time.Calendar.Compat (CalendarDiffDays (..), DayOfWeek (..))
90+import Data.Time.LocalTime.Compat (CalendarDiffTime (..))
91+import Data.Time.Clock.System.Compat (SystemTime (..))
86−import Data.Time.Format (FormatTime, formatTime)
92+import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
87−import Data.Time.Locale.Compat (defaultTimeLocale)
88−import Data.Vector (Vector)
93+import Data.Vector (Vector)
89−import Data.Version (Version, showVersion)
94+import Data.Version (Version, showVersion)
95+import Data.Void (Void, absurd)
90−import Data.Word (Word16, Word32, Word64, Word8)
96+import Data.Word (Word16, Word32, Word64, Word8)
91−import Foreign.Storable (Storable)
97+import Foreign.Storable (Storable)
98+import Foreign.C.Types (CTime (..))
92import GHC.Generics
99import GHC.Generics
93−import Numeric.Natural (Natural)
100+import Numeric.Natural (Natural)
94−
101+import qualified Data.Aeson.Encoding as E
95−import qualified Data.ByteString.Builder as B
102+import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
96−import qualified Data.ByteString.Lazy as L
103+import qualified Data.ByteString.Lazy as L
97−import qualified Data.DList as DList
104+import qualified Data.DList as DList
98−import qualified Data.HashMap.Strict as H
105+import qualified Data.HashMap.Strict as H
99−import qualified Data.HashSet as HashSet
106+import qualified Data.HashSet as HashSet
100−import qualified Data.IntMap as IntMap
107+import qualified Data.IntMap as IntMap
101−import qualified Data.IntSet as IntSet
108+import qualified Data.IntSet as IntSet
102−import qualified Data.List.NonEmpty as NE
109+import qualified Data.List.NonEmpty as NE
103−import qualified Data.Map as M
110+import qualified Data.Map as M
111+import qualified Data.Monoid as Monoid
104−import qualified Data.Scientific as Scientific
112+import qualified Data.Scientific as Scientific
105−import qualified Data.Sequence as Seq
113+import qualified Data.Semigroup as Semigroup
114+import qualified Data.Sequence as Seq
106−import qualified Data.Set as Set
115+import qualified Data.Set as Set
107−import qualified Data.Text as T
116+import qualified Data.Text as T
108−import qualified Data.Text.Encoding as T
117+import qualified Data.Text.Encoding as T
109−import qualified Data.Text.Lazy as LT
118+import qualified Data.Text.Lazy as LT
110−import qualified Data.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
118−builder :: ToJSON a => a -> B.Builder
128+import qualified Data.Aeson.Encoding.Builder as EB
119−builder = fromEncoding . toEncoding
129+import qualified Data.ByteString.Builder as B
131+import qualified GHC.Exts as Exts
132+import qualified Data.Primitive.Array as PM
133+import qualified Data.Primitive.SmallArray as PM
134+import qualified Data.Primitive.Types as PM
136+#if MIN_VERSION_primitive(0,6,4)
137+#if !MIN_VERSION_primitive(0,7,0)
138+import qualified Data.Primitive.UnliftedArray as PM
140+import qualified Data.Primitive.PrimArray as PM
121toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
122−toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
144+toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
123− mv <- VM.unsafeNew 2
124− VM.unsafeWrite mv 0 (keySerialiser a)
125− VM.unsafeWrite mv 1 (valSerializer b)
127{-# INLINE toJSONPair #-}
145{-# INLINE toJSONPair #-}
129realFloatToJSON :: RealFloat a => a -> Value
147realFloatToJSON :: RealFloat a => a -> Value
@@ -138,51 +156,53 @@ realFloatToJSON d
139-- | Class of generic representation types that can be converted to
157-- | Class of generic representation types that can be converted to
141−class GToJSON arity f where
159+class GToJSON enc arity f where
142 -- | This method (applied to 'defaultOptions') is used as the
160 -- | This method (applied to 'defaultOptions') is used as the
143− -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
161+ -- default generic implementation of 'toJSON'
162+ -- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
144− -- 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').
145− gToJSON :: Options -> Proxy arity
168+ gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
146− -> (a -> Value) -> ([a] -> Value) -> f a -> Value
169+
147−
170+-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
148−-- | Class of generic representation types that can be converted to
171+-- function arguments that encode occurrences of the type parameter (for
149−-- a JSON 'Encoding'.
172+-- 'ToJSON1').
150−class GToEncoding arity f where
173+data ToArgs res arity a where
151− -- | This method (applied to 'defaultOptions') can be used as the
174+ NoToArgs :: ToArgs res Zero a
152− -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
175+ To1Args :: (a -> res) -> ([a] -> res) -> ToArgs res One a
153− -- or 'liftToEncoding' (if the @arity@ is 'One').
154− gToEncoding :: Options -> Proxy arity
155− -> (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
157-- | A configurable generic JSON creator. This function applied to
177-- | A configurable generic JSON creator. This function applied to
158-- 'defaultOptions' is used as the default for 'toJSON' when the type
178-- 'defaultOptions' is used as the default for 'toJSON' when the type
159-- is an instance of 'Generic'.
179-- is an instance of 'Generic'.
160−genericToJSON :: (Generic a, GToJSON Zero (Rep a))
180+genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
161 => Options -> a -> Value
181 => Options -> a -> Value
162−genericToJSON opts = gToJSON opts proxyZero undefined undefined . from
182+genericToJSON opts = gToJSON opts NoToArgs . from
164-- | A configurable generic JSON creator. This function applied to
184-- | A configurable generic JSON creator. This function applied to
165-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
166-- is an instance of 'Generic1'.
186-- is an instance of 'Generic1'.
167−genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
187+genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
168 => Options -> (a -> Value) -> ([a] -> Value)
188 => Options -> (a -> Value) -> ([a] -> Value)
169 -> f a -> Value
189 -> f a -> Value
170−genericLiftToJSON opts tj tjl = gToJSON opts proxyOne tj tjl . from1
190+genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
172-- | A configurable generic JSON encoder. This function applied to
192-- | A configurable generic JSON encoder. This function applied to
173-- 'defaultOptions' is used as the default for 'toEncoding' when the type
193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
174-- is an instance of 'Generic'.
194-- is an instance of 'Generic'.
175−genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
195+genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
176 => Options -> a -> Encoding
196 => Options -> a -> Encoding
177−genericToEncoding opts = gToEncoding opts proxyZero undefined undefined . from
197+genericToEncoding opts = gToJSON opts NoToArgs . from
179-- | A configurable generic JSON encoder. This function applied to
199-- | A configurable generic JSON encoder. This function applied to
180-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
181-- is an instance of 'Generic1'.
201-- is an instance of 'Generic1'.
182−genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
202+genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
183 => Options -> (a -> Encoding) -> ([a] -> Encoding)
203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
184 -> f a -> Encoding
204 -> f a -> Encoding
185−genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
205+genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
187-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
@@ -190,6 +210,9 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
191-- | 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'.
193-- An example type and instance:
216-- An example type and instance:
@@ -198,10 +221,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
199-- data Coord = Coord { x :: Double, y :: Double }
222-- data Coord = Coord { x :: Double, y :: Double }
201−-- instance ToJSON Coord where
224+-- instance 'ToJSON' Coord where
202−-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
225+-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
204−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
227+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
207-- Instead of manually writing your 'ToJSON' instance, there are two options
230-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -209,17 +232,15 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
210-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
211-- 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
212−-- so will probably be more efficient than the following two options:
235+-- so it will probably be more efficient than the following option.
214-- * The compiler can provide a default generic implementation for
237-- * The compiler can provide a default generic implementation for
215-- 'toJSON'.
238-- 'toJSON'.
217-- 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
218−-- datatype and declare a 'ToJSON' instance for your datatype without giving
241+-- datatype and declare a 'ToJSON' instance. If you require nothing other than
219−-- definitions for 'toJSON' or 'toEncoding'.
242+-- 'defaultOptions', it is sufficient to write (and this is the only
220−--
243+-- alternative where the default 'toJSON' implementation is sufficient):
221−-- For example, the previous example can be simplified to a more
225-- {-\# LANGUAGE DeriveGeneric \#-}
246-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -228,35 +249,46 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
229-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
231−-- instance ToJSON Coord where
252+-- instance 'ToJSON' Coord where
232−-- toEncoding = 'genericToEncoding' 'defaultOptions'
253+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
235−-- 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
236−-- 'toEncoding' function is a relatively new addition to this class.
257+-- to implement both methods:
237−-- To allow users of older versions of this library to upgrade without
238−-- having to edit all of their instances or encounter surprising
239−-- incompatibilities, the default implementation of 'toEncoding' uses
240−-- 'toJSON'. This produces correct results, but since it performs an
241−-- intermediate conversion to a 'Value', it will be less efficient
242−-- than directly emitting an 'Encoding'. Our one-liner definition of
243−-- 'toEncoding' above bypasses the intermediate 'Value'.
245−-- If @DefaultSignatures@ doesn't give exactly the results you want,
246−-- you can customize the generic encoding with only a tiny amount of
247−-- effort, using 'genericToJSON' and 'genericToEncoding' with your
248−-- preferred 'Options':
260+-- customOptions = 'defaultOptions'
261+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
251−-- instance ToJSON Coord where
264+-- instance 'ToJSON' Coord where
252−-- toJSON = 'genericToJSON' 'defaultOptions'
265+-- 'toJSON' = 'genericToJSON' customOptions
253−-- 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.)
255class ToJSON a where
287class ToJSON a where
256 -- | Convert a Haskell value to a JSON-friendly intermediate type.
288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
257 toJSON :: a -> Value
289 toJSON :: a -> Value
259− default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
291+ default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
260 toJSON = genericToJSON defaultOptions
292 toJSON = genericToJSON defaultOptions
262 -- | Encode a Haskell value as JSON.
294 -- | Encode a Haskell value as JSON.
@@ -273,12 +305,12 @@ class ToJSON a where
273 -- extension, and then have GHC generate a method body as follows.
305 -- extension, and then have GHC generate a method body as follows.
276− -- instance ToJSON Coord where
308+ -- instance 'ToJSON' Coord where
277− -- toEncoding = 'genericToEncoding' 'defaultOptions'
309+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
280 toEncoding :: a -> Encoding
312 toEncoding :: a -> Encoding
281− toEncoding = E.Encoding . EB.encodeToBuilder . toJSON
313+ toEncoding = E.value . toJSON
282 {-# INLINE toEncoding #-}
314 {-# INLINE toEncoding #-}
284 toJSONList :: [a] -> Value
316 toJSONList :: [a] -> Value
@@ -299,33 +331,213 @@ class KeyValue kv where
299 infixr 8 .=
331 infixr 8 .=
301instance KeyValue Series where
333instance KeyValue Series where
302− name .= value = E.Value $ E.text name <> E.colon <> toEncoding value
334+ name .= value = E.pair name (toEncoding value)
303 {-# INLINE (.=) #-}
335 {-# INLINE (.=) #-}
305instance KeyValue Pair where
337instance KeyValue Pair where
306 name .= value = (name, toJSON value)
338 name .= value = (name, toJSON value)
307 {-# 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)
309-------------------------------------------------------------------------------
348-------------------------------------------------------------------------------
310-- Classes and types for map keys
349-- Classes and types for map keys
311-------------------------------------------------------------------------------
350-------------------------------------------------------------------------------
352+-- | Typeclass for types that can be used as the key of a map-like container
353+-- (like 'Map' or 'HashMap'). For example, since 'Text' has a 'ToJSONKey'
354+-- instance and 'Char' has a 'ToJSON' instance, we can encode a value of
355+-- type 'Map' 'Text' 'Char':
357+-- >>> LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]
360+-- Since 'Int' also has a 'ToJSONKey' instance, we can similarly write:
362+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(5 :: Int, 'a')]
365+-- JSON documents only accept strings as object keys. For any type
366+-- from @base@ that has a natural textual representation, it can be
367+-- expected that its 'ToJSONKey' instance will choose that representation.
369+-- For data types that lack a natural textual representation, an alternative
370+-- is provided. The map-like container is represented as a JSON array
371+-- instead of a JSON object. Each value in the array is an array with
372+-- exactly two values. The first is the key and the second is the value.
374+-- For example, values of type '[Text]' cannot be encoded to a
375+-- string, so a 'Map' with keys of type '[Text]' is encoded as follows:
377+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(["foo","bar","baz" :: Text], 'a')]
378+-- [[["foo","bar","baz"],"a"]]
380+-- The default implementation of 'ToJSONKey' chooses this method of
381+-- encoding a key, using the 'ToJSON' instance of the type.
383+-- To use your own data type as the key in a map, all that is needed
384+-- is to write a 'ToJSONKey' (and possibly a 'FromJSONKey') instance
385+-- for it. If the type cannot be trivially converted to and from 'Text',
386+-- it is recommended that 'ToJSONKeyValue' is used. Since the default
387+-- implementations of the typeclass methods can build this from a
388+-- 'ToJSON' instance, there is nothing that needs to be written:
390+-- > data Foo = Foo { fooAge :: Int, fooName :: Text }
391+-- > deriving (Eq,Ord,Generic)
392+-- > instance ToJSON Foo
393+-- > instance ToJSONKey Foo
395+-- That's it. We can now write:
397+-- >>> let m = Map.fromList [(Foo 4 "bar",'a'),(Foo 6 "arg",'b')]
398+-- >>> LBC8.putStrLn $ encode m
399+-- [[{"fooName":"bar","fooAge":4},"a"],[{"fooName":"arg","fooAge":6},"b"]]
401+-- The next case to consider is if we have a type that is a
402+-- newtype wrapper around 'Text'. The recommended approach is to use
403+-- generalized newtype deriving:
405+-- > newtype RecordId = RecordId { getRecordId :: Text }
406+-- > deriving (Eq,Ord,ToJSONKey)
408+-- Then we may write:
410+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(RecordId "abc",'a')]
413+-- Simple sum types are a final case worth considering. Suppose we have:
415+-- > data Color = Red | Green | Blue
416+-- > deriving (Show,Read,Eq,Ord)
418+-- It is possible to get the 'ToJSONKey' instance for free as we did
419+-- with 'Foo'. However, in this case, we have a natural way to go to
420+-- and from 'Text' that does not require any escape sequences. So
421+-- 'ToJSONKeyText' can be used instead of 'ToJSONKeyValue' to encode maps
422+-- as objects instead of arrays of pairs. This instance may be
423+-- implemented using generics as follows:
426+-- instance 'ToJSONKey' Color where
427+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
430+-- === __Low-level implementations__
432+-- The 'Show' instance can be used to help write 'ToJSONKey':
434+-- > instance ToJSONKey Color where
435+-- > toJSONKey = ToJSONKeyText f g
436+-- > where f = Text.pack . show
437+-- > g = text . Text.pack . show
438+-- > -- text function is from Data.Aeson.Encoding
440+-- The situation of needing to turning function @a -> Text@ into
441+-- a 'ToJSONKeyFunction' is common enough that a special combinator
442+-- is provided for it. The above instance can be rewritten as:
444+-- > instance ToJSONKey Color where
445+-- > toJSONKey = toJSONKeyText (Text.pack . show)
447+-- The performance of the above instance can be improved by
448+-- not using 'String' as an intermediate step when converting to
449+-- 'Text'. One option for improving performance would be to use
450+-- template haskell machinery from the @text-show@ package. However,
451+-- even with the approach, the 'Encoding' (a wrapper around a bytestring
452+-- builder) is generated by encoding the 'Text' to a 'ByteString',
453+-- an intermediate step that could be avoided. The fastest possible
454+-- implementation would be:
456+-- > -- Assuming that OverloadedStrings is enabled
457+-- > instance ToJSONKey Color where
458+-- > toJSONKey = ToJSONKeyText f g
459+-- > where f x = case x of {Red -> "Red";Green ->"Green";Blue -> "Blue"}
460+-- > g x = case x of {Red -> text "Red";Green -> text "Green";Blue -> text "Blue"}
461+-- > -- text function is from Data.Aeson.Encoding
463+-- This works because GHC can lift the encoded values out of the case
464+-- statements, which means that they are only evaluated once. This
465+-- approach should only be used when there is a serious need to
466+-- maximize performance.
313class ToJSONKey a where
468class ToJSONKey a where
469+ -- | Strategy for rendering the key for a map-like container.
314 toJSONKey :: ToJSONKeyFunction a
470 toJSONKey :: ToJSONKeyFunction a
315 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
471 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
316− toJSONKey = ToJSONKeyValue (toJSON, toEncoding)
472+ toJSONKey = ToJSONKeyValue toJSON toEncoding
474+ -- | This is similar in spirit to the 'showsList' method of 'Show'.
475+ -- It makes it possible to give 'String' keys special treatment
476+ -- without using @OverlappingInstances@. End users should always
477+ -- be able to use the default implementation of this method.
317 toJSONKeyList :: ToJSONKeyFunction [a]
478 toJSONKeyList :: ToJSONKeyFunction [a]
318 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
479 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
319− toJSONKeyList = ToJSONKeyValue (toJSON, toEncoding)
480+ toJSONKeyList = ToJSONKeyValue toJSON toEncoding
321data ToJSONKeyFunction a
482data ToJSONKeyFunction a
322− = ToJSONKeyText (a -> Text, a -> Encoding)
483+ = ToJSONKeyText !(a -> Text) !(a -> Encoding' Text)
484+ -- ^ key is encoded to string, produces object
323− | ToJSONKeyValue (a -> Value, a -> Encoding)
485+ | ToJSONKeyValue !(a -> Value) !(a -> Encoding)
486+ -- ^ key is encoded to value, produces array
488+-- | Helper for creating textual keys.
491+-- instance 'ToJSONKey' MyKey where
492+-- 'toJSONKey' = 'toJSONKeyText' myKeyToText
494+-- myKeyToText = Text.pack . show -- or showt from text-show
496+toJSONKeyText :: (a -> Text) -> ToJSONKeyFunction a
497+toJSONKeyText f = ToJSONKeyText f (E.text . f)
499+-- | TODO: should this be exported?
500+toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
501+toJSONKeyTextEnc e = ToJSONKeyText tot e
503+ -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
504+ tot = T.dropAround (== '"')
507+ . E.encodingToLazyByteString
510+instance Contravariant ToJSONKeyFunction where
511+ contramap = contramapToJSONKeyFunction
513+-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
325contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
326contramapToJSONKeyFunction h x = case x of
515contramapToJSONKeyFunction h x = case x of
327− ToJSONKeyText (f,g) -> ToJSONKeyText (f . h, g . h)
516+ ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
328− 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
330-------------------------------------------------------------------------------
542-------------------------------------------------------------------------------
331-- Lifings of FromJSON and ToJSON to unary and binary type constructors
543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
@@ -339,7 +551,7 @@ contramapToJSONKeyFunction h x = case x of
340-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
341-- 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
342−-- so will probably be more efficient than the following two options:
554+-- so it will probably be more efficient than the following option.
344-- * The compiler can provide a default generic implementation for
556-- * The compiler can provide a default generic implementation for
345-- 'toJSON1'.
557-- 'toJSON1'.
@@ -357,23 +569,29 @@ contramapToJSONKeyFunction h x = case x of
358-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
360−-- instance ToJSON a => ToJSON1 (Pair a)
572+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
363−-- If @DefaultSignatures@ doesn't give exactly the results you want,
575+-- If the default implementation doesn't give exactly the results you want,
364-- 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
365-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
366-- your preferred 'Options':
578-- your preferred 'Options':
581+-- customOptions = 'defaultOptions'
582+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
369−-- instance ToJSON a => ToJSON1 (Pair a) where
585+-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
370−-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
586+-- 'liftToJSON' = 'genericLiftToJSON' customOptions
371−-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
587+-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
590+-- See also 'ToJSON'.
373class ToJSON1 f where
591class ToJSON1 f where
374 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
376− default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
594+ default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
377 => (a -> Value) -> ([a] -> Value) -> f a -> Value
595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
378 liftToJSON = genericLiftToJSON defaultOptions
596 liftToJSON = genericLiftToJSON defaultOptions
@@ -382,7 +600,7 @@ class ToJSON1 f where
383 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
385− default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
603+ default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
386 => (a -> Encoding) -> ([a] -> Encoding)
604 => (a -> Encoding) -> ([a] -> Encoding)
387 -> f a -> Encoding
605 -> f a -> Encoding
388 liftToEncoding = genericLiftToEncoding defaultOptions
606 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -430,9 +648,25 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
430-- Encoding functions
648-- Encoding functions
431-------------------------------------------------------------------------------
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
433listEncoding :: (a -> Encoding) -> [a] -> Encoding
665listEncoding :: (a -> Encoding) -> [a] -> Encoding
434listEncoding = E.list
666listEncoding = E.list
667+{-# INLINE listEncoding #-}
669+-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
436listValue :: (a -> Value) -> [a] -> Value
670listValue :: (a -> Value) -> [a] -> Value
437listValue f = Array . V.fromList . map f
671listValue f = Array . V.fromList . map f
438{-# INLINE listValue #-}
672{-# INLINE listValue #-}
@@ -451,6 +685,11 @@ instance ToJSON1 [] where
451 {-# INLINE liftToEncoding #-}
685 {-# INLINE liftToEncoding #-}
453instance (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] #-}
454 toJSON = toJSON1
693 toJSON = toJSON1
455 {-# INLINE toJSON #-}
694 {-# INLINE toJSON #-}
@@ -461,283 +700,260 @@ instance (ToJSON a) => ToJSON [a] where
461-- Generic toJSON / toEncoding
700-- Generic toJSON / toEncoding
462-------------------------------------------------------------------------------
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 #-}
464--------------------------------------------------------------------------------
744--------------------------------------------------------------------------------
465-- Generic toJSON
745-- Generic toJSON
467−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
468− -- Meta-information, which is not handled elsewhere, is ignored:
748+-- possible but makes error messages a bit harder to understand for missing
469− gToJSON opts pa tj tjl = gToJSON opts pa tj tjl . unM1
749+-- instances.
471−instance (ToJSON a) => GToJSON arity (K1 i a) where
751+instance GToJSON Value arity V1 where
472− -- Constant values are encoded using their ToJSON instance:
752+ -- Empty values do not exist, which makes the job of formatting them
473− gToJSON _opts _ _ _ = toJSON . unK1
753+ -- rather easy:
754+ gToJSON _ _ x = x `seq` error "case: V1"
755+ {-# INLINE gToJSON #-}
475−instance GToJSON One Par1 where
757+instance ToJSON a => GToJSON Value arity (K1 i a) where
476− -- Direct occurrences of the last type parameter are encoded with the
758+ -- Constant values are encoded using their ToJSON instance:
477− -- function passed in as an argument:
478− gToJSON _opts _ tj _ = tj . unPar1
759+ gToJSON _opts _ = toJSON . unK1
760+ {-# INLINE gToJSON #-}
480−instance (ToJSON1 f) => GToJSON One (Rec1 f) where
762+instance ToJSON1 f => GToJSON Value One (Rec1 f) where
481 -- Recursive occurrences of the last type parameter are encoded using their
763 -- Recursive occurrences of the last type parameter are encoded using their
482 -- ToJSON1 instance:
764 -- ToJSON1 instance:
483− gToJSON _opts _ tj tjl = liftToJSON tj tjl . unRec1
765+ gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766+ {-# INLINE gToJSON #-}
485−instance GToJSON arity U1 where
768+instance GToJSON Value arity U1 where
486 -- Empty constructors are encoded to an empty array:
769 -- Empty constructors are encoded to an empty array:
487− gToJSON _opts _ _ _ _ = emptyArray
770+ gToJSON _opts _ _ = emptyArray
488−
771+ {-# INLINE gToJSON #-}
489−instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
490− -- Constructors need to be encoded differently depending on whether they're
491− -- a record or not. This distinction is made by 'consToJSON':
492− gToJSON opts pa tj tjl = consToJSON opts pa tj tjl . unM1
494instance ( WriteProduct arity a, WriteProduct arity b
773instance ( WriteProduct arity a, WriteProduct arity b
495 , ProductSize a, ProductSize b
774 , ProductSize a, ProductSize b
496− ) => GToJSON arity (a :*: b) where
775+ ) => GToJSON Value arity (a :*: b)
497 -- 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
498 -- 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
499 -- 'writeProduct':
779 -- 'writeProduct':
500− gToJSON opts pa tj tjl p =
780+ gToJSON opts targs p =
501 Array $ V.create $ do
781 Array $ V.create $ do
502 mv <- VM.unsafeNew lenProduct
782 mv <- VM.unsafeNew lenProduct
503− writeProduct opts pa mv 0 lenProduct tj tjl p
783+ writeProduct opts targs mv 0 lenProduct p
504 return mv
784 return mv
506 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
507 productSize
787 productSize
788+ {-# INLINE gToJSON #-}
509−instance ( AllNullary (a :+: b) allNullary
790+instance ( ToJSON1 f
510− , SumToJSON arity (a :+: b) allNullary
791+ , GToJSON Value One g
511− ) => GToJSON arity (a :+: b) where
792+ ) => GToJSON Value One (f :.: g)
512− -- If all constructors of a sum datatype are nullary and the
793+ where
513− -- 'allNullaryToStringTag' option is set they are encoded to
514− -- strings. This distinction is made by 'sumToJSON':
515− gToJSON opts pa tj tjl = (unTagged :: Tagged allNullary Value -> Value)
516− . sumToJSON opts pa tj tjl
518−instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
519 -- 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
520 -- 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
521 -- instance to generically encode the innermost type:
796 -- instance to generically encode the innermost type:
522− gToJSON opts pa tj tjl =
797+ gToJSON opts targs =
523− let gtj = gToJSON opts pa tj tjl in
798+ let gtj = gToJSON opts targs in
524 liftToJSON gtj (listValue gtj) . unComp1
799 liftToJSON gtj (listValue gtj) . unComp1
800+ {-# INLINE gToJSON #-}
526--------------------------------------------------------------------------------
802--------------------------------------------------------------------------------
527-- Generic toEncoding
803-- Generic toEncoding
529−instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
530− -- Meta-information, which is not handled elsewhere, is ignored:
531− gToEncoding opts pa te tel = gToEncoding opts pa te tel . unM1
533−instance (ToJSON a) => GToEncoding arity (K1 i a) where
805+instance ToJSON a => GToJSON Encoding arity (K1 i a) where
534 -- Constant values are encoded using their ToJSON instance:
806 -- Constant values are encoded using their ToJSON instance:
535− gToEncoding _opts _ _ _ = toEncoding . unK1
807+ gToJSON _opts _ = toEncoding . unK1
808+ {-# INLINE gToJSON #-}
537−instance GToEncoding One Par1 where
538− -- Direct occurrences of the last type parameter are encoded with the
539− -- function passed in as an argument:
540− gToEncoding _opts _ te _ = te . unPar1
542−instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
810+instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
543 -- Recursive occurrences of the last type parameter are encoded using their
811 -- Recursive occurrences of the last type parameter are encoded using their
544 -- ToEncoding1 instance:
812 -- ToEncoding1 instance:
545− gToEncoding _opts _ te tel = liftToEncoding te tel . unRec1
813+ gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814+ {-# INLINE gToJSON #-}
547−instance GToEncoding arity U1 where
816+instance GToJSON Encoding arity U1 where
548 -- Empty constructors are encoded to an empty array:
817 -- Empty constructors are encoded to an empty array:
549− gToEncoding _opts _ _ _ _ = E.emptyArray_
818+ gToJSON _opts _ _ = E.emptyArray_
550−
819+ {-# INLINE gToJSON #-}
551−instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
552− -- Constructors need to be encoded differently depending on whether they're
553− -- a record or not. This distinction is made by 'consToEncoding':
554− gToEncoding opts pa te tel = consToEncoding opts pa te tel . unM1
556instance ( EncodeProduct arity a
821instance ( EncodeProduct arity a
557 , EncodeProduct arity b
822 , EncodeProduct arity b
558− ) => GToEncoding arity (a :*: b) where
823+ ) => GToJSON Encoding arity (a :*: b)
559 -- 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
560 -- 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
561 -- 'encodeProduct':
827 -- 'encodeProduct':
562− 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 #-}
564−instance ( AllNullary (a :+: b) allNullary
831+instance ( ToJSON1 f
565− , SumToEncoding arity (a :+: b) allNullary
832+ , GToJSON Encoding One g
566− ) => GToEncoding arity (a :+: b) where
833+ ) => GToJSON Encoding One (f :.: g)
567− -- If all constructors of a sum datatype are nullary and the
834+ where
568− -- 'allNullaryToStringTag' option is set they are encoded to
569− -- strings. This distinction is made by 'sumToEncoding':
570− gToEncoding opts pa te tel
571− = (unTagged :: Tagged allNullary Encoding -> Encoding)
572− . sumToEncoding opts pa te tel
574−instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
575 -- 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
576 -- 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
577 -- instance to generically encode the innermost type:
837 -- instance to generically encode the innermost type:
578− gToEncoding opts pa te tel =
838+ gToJSON opts targs =
579− let gte = gToEncoding opts pa te tel in
839+ let gte = gToJSON opts targs in
580 liftToEncoding gte (listEncoding gte) . unComp1
840 liftToEncoding gte (listEncoding gte) . unComp1
841+ {-# INLINE gToJSON #-}
582--------------------------------------------------------------------------------
843--------------------------------------------------------------------------------
584−class SumToJSON arity f allNullary where
845+class SumToJSON enc arity f allNullary where
585− sumToJSON :: Options -> Proxy arity
846+ sumToJSON :: Options -> ToArgs enc arity a
586− -> (a -> Value) -> ([a] -> Value)
587− -> f a -> Tagged allNullary Value
847+ -> f a -> Tagged allNullary enc
589−instance ( GetConName f
849+instance ( GetConName f
590− , TaggedObjectPairs arity f
850+ , FromString enc
591− , ObjectWithSingleFieldObj arity f
851+ , TaggedObject enc arity f
592− , TwoElemArrayObj arity f
852+ , SumToJSON' ObjectWithSingleField enc arity f
853+ , SumToJSON' TwoElemArray enc arity f
854+ , SumToJSON' UntaggedValue enc arity f
593− ) => SumToJSON arity f True where
855+ ) => SumToJSON enc arity f True
594− sumToJSON opts pa tj tjl
857+ sumToJSON opts targs
595− | allNullaryToStringTag opts = Tagged . String . pack
858+ | allNullaryToStringTag opts = Tagged . fromString
596 . constructorTagModifier opts . getConName
859 . constructorTagModifier opts . getConName
597− | otherwise = Tagged . nonAllNullarySumToJSON opts pa tj tjl
860+ | otherwise = Tagged . nonAllNullarySumToJSON opts targs
599−instance ( TwoElemArrayObj arity f
862+instance ( TaggedObject enc arity f
600− , TaggedObjectPairs arity f
863+ , SumToJSON' ObjectWithSingleField enc arity f
601− , ObjectWithSingleFieldObj arity f
864+ , SumToJSON' TwoElemArray enc arity f
865+ , SumToJSON' UntaggedValue enc arity f
602− ) => SumToJSON arity f False where
866+ ) => SumToJSON enc arity f False
603− sumToJSON opts pa tj tjl = Tagged . nonAllNullarySumToJSON opts pa tj tjl
868+ sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
605−nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
870+nonAllNullarySumToJSON :: ( TaggedObject enc arity f
606− , TaggedObjectPairs arity f
871+ , SumToJSON' ObjectWithSingleField enc arity f
607− , ObjectWithSingleFieldObj arity f
872+ , SumToJSON' TwoElemArray enc arity f
873+ , SumToJSON' UntaggedValue enc arity f
608− ) => Options -> Proxy arity
874+ ) => Options -> ToArgs enc arity a
609− -> (a -> Value) -> ([a] -> Value)
875+ -> f a -> enc
611−nonAllNullarySumToJSON opts pa tj tjl =
876+nonAllNullarySumToJSON opts targs =
612 case sumEncoding opts of
877 case sumEncoding opts of
614− object . taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl
615− ObjectWithSingleField -> Object . objectWithSingleFieldObj opts pa tj tjl
616− TwoElemArray -> Array . twoElemArrayObj opts pa tj tjl
618−--------------------------------------------------------------------------------
620−class SumToEncoding arity f allNullary where
621− sumToEncoding :: Options -> Proxy arity
622− -> (a -> Encoding) -> ([a] -> Encoding)
623− -> f a -> Tagged allNullary Encoding
625−instance ( GetConName f
626− , TaggedObjectEnc arity f
627− , ObjectWithSingleFieldEnc arity f
628− , TwoElemArrayEnc arity f
629− ) => SumToEncoding arity f True where
630− sumToEncoding opts pa te tel
631− | allNullaryToStringTag opts = Tagged . toEncoding .
632− constructorTagModifier opts . getConName
633− | otherwise = Tagged . nonAllNullarySumToEncoding opts pa te tel
635−instance ( TwoElemArrayEnc arity f
636− , TaggedObjectEnc arity f
637− , ObjectWithSingleFieldEnc arity f
638− ) => SumToEncoding arity f False where
639− sumToEncoding opts pa te tel = Tagged . nonAllNullarySumToEncoding opts pa te tel
641−nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
642− , TaggedObjectEnc arity f
643− , ObjectWithSingleFieldEnc arity f
644− ) => Options -> Proxy arity
645− -> (a -> Encoding) -> ([a] -> Encoding)
647−nonAllNullarySumToEncoding opts pa te tel =
648− case sumEncoding opts of
649 TaggedObject{..} ->
879 TaggedObject{..} ->
650− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel
880+ taggedObject opts targs tagFieldName contentsFieldName
651− ObjectWithSingleField -> objectWithSingleFieldEnc opts pa te tel
652− TwoElemArray -> twoElemArrayEnc opts pa te tel
654−--------------------------------------------------------------------------------
882+ ObjectWithSingleField ->
883+ (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884+ . sumToJSON' opts targs
656−class TaggedObjectPairs arity f where
886+ TwoElemArray ->
657− taggedObjectPairs :: Options -> Proxy arity
659− -> (a -> Value) -> ([a] -> Value)
662−instance ( TaggedObjectPairs arity a
663− , TaggedObjectPairs arity b
664− ) => TaggedObjectPairs arity (a :+: b) where
665− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (L1 x) =
666− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
667− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (R1 x) =
668− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
670−instance ( IsRecord a isRecord
671− , TaggedObjectPairs' arity a isRecord
673− ) => TaggedObjectPairs arity (C1 c a) where
674− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl =
675− (pack tagFieldName .= constructorTagModifier opts
676− (conName (undefined :: t c a p)) :) .
677− (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
887+ (unTagged :: Tagged TwoElemArray enc -> enc)
678− taggedObjectPairs' opts pa contentsFieldName tj tjl . unM1
888+ . sumToJSON' opts targs
680−class TaggedObjectPairs' arity f isRecord where
890+ UntaggedValue ->
681− taggedObjectPairs' :: Options -> Proxy arity
891+ (unTagged :: Tagged UntaggedValue enc -> enc)
682− -> String -> (a -> Value) -> ([a] -> Value)
892+ . sumToJSON' opts targs
683− -> f a -> Tagged isRecord [Pair]
685−instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
894+--------------------------------------------------------------------------------
686− taggedObjectPairs' _ _ _ _ _ _ = Tagged []
895+
896+class FromString enc where
897+ fromString :: String -> enc
688−instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
899+instance FromString Encoding where
689− taggedObjectPairs' opts pa _ tj tjl =
900+ fromString = toEncoding
690− Tagged . toList . recordToPairs opts pa tj tjl
692−instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
902+instance FromString Value where
693− taggedObjectPairs' opts pa contentsFieldName tj tjl =
903+ fromString = String . pack
694− Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts pa tj tjl
696--------------------------------------------------------------------------------
905--------------------------------------------------------------------------------
698−class TaggedObjectEnc arity f where
907+class TaggedObject enc arity f where
699− taggedObjectEnc :: Options -> Proxy arity
908+ taggedObject :: Options -> ToArgs enc arity a
700− -> String -> String
909+ -> String -> String
701− -> (a -> Encoding) -> ([a] -> Encoding)
910+ -> f a -> enc
702− -> f a -> Encoding
911+
704−instance ( TaggedObjectEnc arity a
912+instance ( TaggedObject enc arity a
705− , TaggedObjectEnc arity b
913+ , TaggedObject enc arity b
706− ) => TaggedObjectEnc arity (a :+: b) where
914+ ) => TaggedObject enc arity (a :+: b)
707− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (L1 x) =
916+ taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
708− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
917+ taggedObject opts targs tagFieldName contentsFieldName x
709− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (R1 x) =
918+ taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
710− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
919+ taggedObject opts targs tagFieldName contentsFieldName x
712−instance ( IsRecord a isRecord
921+instance ( IsRecord a isRecord
713− , TaggedObjectEnc' arity a isRecord
922+ , TaggedObject' enc pairs arity a isRecord
923+ , FromPairs enc pairs
925+ , KeyValuePair enc pairs
714 , Constructor c
926 , Constructor c
715− ) => TaggedObjectEnc arity (C1 c a) where
927+ ) => TaggedObject enc arity (C1 c a)
716− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel v =
929+ taggedObject opts targs tagFieldName contentsFieldName =
717− E.openCurly <>
930+ fromPairs . mappend tag . contents
718− (toEncoding tagFieldName <>
932+ tag = tagFieldName `pair`
720− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) <>
933+ (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
721− ((unTagged :: Tagged isRecord Encoding -> Encoding) .
936+ (unTagged :: Tagged isRecord pairs -> pairs) .
722− taggedObjectEnc' opts pa contentsFieldName te tel . unM1 $ v) <>
937+ taggedObject' opts targs contentsFieldName . unM1
725−class TaggedObjectEnc' arity f isRecord where
939+class TaggedObject' enc pairs arity f isRecord where
726− taggedObjectEnc' :: Options -> Proxy arity
940+ taggedObject' :: Options -> ToArgs enc arity a
727− -> String -> (a -> Encoding) -> ([a] -> Encoding)
941+ -> String -> f a -> Tagged isRecord pairs
728− -> f a -> Tagged isRecord Encoding
942+
729−
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
730−instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
950+instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
731− taggedObjectEnc' _ _ _ _ _ _ = Tagged mempty
951+ taggedObject' _ _ _ _ = Tagged mempty
733−instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
953+instance ( RecordToPairs enc pairs arity f
734− taggedObjectEnc' opts pa _ te tel = Tagged . (E.comma <>) . fst
954+ ) => TaggedObject' enc pairs arity f True
735− . recordToEncoding opts pa te tel
955+ where
736−
956+ taggedObject' opts targs _ = Tagged . recordToPairs opts targs
737−instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
738− taggedObjectEnc' opts pa contentsFieldName te tel =
739− Tagged . (\z -> E.comma <> toEncoding contentsFieldName <> E.colon <> z) .
740− gToEncoding opts pa te tel
742--------------------------------------------------------------------------------
958--------------------------------------------------------------------------------
@@ -752,283 +968,242 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
752instance (Constructor c) => GetConName (C1 c a) where
968instance (Constructor c) => GetConName (C1 c a) where
753 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
755--------------------------------------------------------------------------------
983--------------------------------------------------------------------------------
757−class TwoElemArrayObj arity f where
985+class SumToJSON' s enc arity f where
758− twoElemArrayObj :: Options -> Proxy arity
986+ sumToJSON' :: Options -> ToArgs enc arity a
759− -> (a -> Value) -> ([a] -> Value)
987+ -> f a -> Tagged s enc
760− -> f a -> V.Vector Value
762−instance ( TwoElemArrayObj arity a
989+instance ( SumToJSON' s enc arity a
763− , TwoElemArrayObj arity b
990+ , SumToJSON' s enc arity b
764− ) => TwoElemArrayObj arity (a :+: b) where
991+ ) => SumToJSON' s enc arity (a :+: b)
765− twoElemArrayObj opts pa tj tjl (L1 x) = twoElemArrayObj opts pa tj tjl x
992+ where
766− 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+--------------------------------------------------------------------------------
768−instance ( GToJSON arity a
998+instance ( GToJSON Value arity a
769− , ConsToJSON arity a
999+ , ConsToJSON Value arity a
770 , Constructor c
1000 , Constructor c
771− ) => TwoElemArrayObj arity (C1 c a) where
1001+ ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
772− twoElemArrayObj opts pa tj tjl x = V.create $ do
1002+ sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
773 mv <- VM.unsafeNew 2
1003 mv <- VM.unsafeNew 2
774 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
1004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
775 $ conName (undefined :: t c a p)
1005 $ conName (undefined :: t c a p)
776− VM.unsafeWrite mv 1 $ gToJSON opts pa tj tjl x
1006+ VM.unsafeWrite mv 1 $ gToJSON opts targs x
777 return mv
1007 return mv
779--------------------------------------------------------------------------------
1009--------------------------------------------------------------------------------
781−class TwoElemArrayEnc arity f where
1011+instance ( GToJSON Encoding arity a
782− twoElemArrayEnc :: Options -> Proxy arity
1012+ , ConsToJSON Encoding arity a
783− -> (a -> Encoding) -> ([a] -> Encoding)
786−instance ( TwoElemArrayEnc arity a
787− , TwoElemArrayEnc arity b
788− ) => TwoElemArrayEnc arity (a :+: b) where
789− twoElemArrayEnc opts pa te tel (L1 x) = twoElemArrayEnc opts pa te tel x
790− twoElemArrayEnc opts pa te tel (R1 x) = twoElemArrayEnc opts pa te tel x
792−instance ( GToEncoding arity a
793− , ConsToEncoding arity a
794 , Constructor c
1013 , Constructor c
795− ) => TwoElemArrayEnc arity (C1 c a) where
1014+ ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
796− twoElemArrayEnc opts pa te tel x = E.tuple $
1015+ where
1016+ sumToJSON' opts targs x = Tagged $ E.list id
797− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
1017+ [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
798− gToEncoding opts pa te tel x
1018+ , gToJSON opts targs x
800--------------------------------------------------------------------------------
1021--------------------------------------------------------------------------------
802−class ConsToJSON arity f where
1023+class ConsToJSON enc arity f where
803− consToJSON :: Options -> Proxy arity
804− -> (a -> Value) -> ([a] -> Value)
807−class ConsToJSON' arity f isRecord where
808− consToJSON' :: Options -> Proxy arity
1024+ consToJSON :: Options -> ToArgs enc arity a
809− -> Bool -- ^ Are we a record with one field?
1025+ -> f a -> enc
810− -> (a -> Value) -> ([a] -> Value)
811− -> f a -> Tagged isRecord Value
813−instance ( IsRecord f isRecord
814− , ConsToJSON' arity f isRecord
815− ) => ConsToJSON arity f where
816− consToJSON opts pa tj tjl =
817− (unTagged :: Tagged isRecord Value -> Value)
818− . consToJSON' opts pa (isUnary (undefined :: f a)) tj tjl
820−instance (RecordToPairs arity f) => ConsToJSON' arity f True where
821− consToJSON' opts pa isUn tj tjl f = let
822− vals = toList $ recordToPairs opts pa tj tjl f
823− in case (unwrapUnaryRecords opts,isUn,vals) of
824− (True,True,[(_,val)]) -> Tagged val
825− _ -> Tagged $ object vals
827−instance GToJSON arity f => ConsToJSON' arity f False where
828− consToJSON' opts pa _ tj tjl = Tagged . gToJSON opts pa tj tjl
830−--------------------------------------------------------------------------------
832−class ConsToEncoding arity f where
833− consToEncoding :: Options -> Proxy arity
834− -> (a -> Encoding) -> ([a] -> Encoding)
837−class ConsToEncoding' arity f isRecord where
1027+class ConsToJSON' enc arity f isRecord where
838− consToEncoding' :: Options -> Proxy arity
1028+ consToJSON' :: Options -> ToArgs enc arity a
839− -> Bool -- ^ Are we a record with one field?
840− -> (a -> Encoding) -> ([a] -> Encoding)
841− -> f a -> Tagged isRecord Encoding
1029+ -> f a -> Tagged isRecord enc
843instance ( IsRecord f isRecord
1031instance ( IsRecord f isRecord
844− , ConsToEncoding' arity f isRecord
1032+ , ConsToJSON' enc arity f isRecord
845− ) => ConsToEncoding arity f where
1033+ ) => ConsToJSON enc arity f
846− consToEncoding opts pa te tel =
1034+ where
1035+ consToJSON opts targs =
847− (unTagged :: Tagged isRecord Encoding -> Encoding)
1036+ (unTagged :: Tagged isRecord enc -> enc)
848− . consToEncoding' opts pa (isUnary (undefined :: f a)) te tel
1037+ . consToJSON' opts targs
849−
1038+ {-# INLINE consToJSON #-}
850−instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
1039+
851− consToEncoding' opts pa isUn te tel x =
1040+instance OVERLAPPING_
852− 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
853− in case (unwrapUnaryRecords opts, isUn, mbVal) of
1047+ | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
854− (True, True, Just val) -> Tagged val
1048+ | otherwise = Tagged . fromPairs . recordToPairs opts targs
855− _ -> 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' #-}
857−instance GToEncoding arity f => ConsToEncoding' arity f False where
1058+instance GToJSON enc arity f => ConsToJSON' enc arity f False where
858− consToEncoding' opts pa _ te tel = Tagged . gToEncoding opts pa te tel
1059+ consToJSON' opts targs = Tagged . gToJSON opts targs
859−
1060+ {-# INLINE consToJSON' #-}
860−--------------------------------------------------------------------------------
862−class RecordToPairs arity f where
863− recordToPairs :: Options -> Proxy arity
864− -> (a -> Value) -> ([a] -> Value)
865− -> f a -> DList Pair
867−instance ( RecordToPairs arity a
868− , RecordToPairs arity b
869− ) => RecordToPairs arity (a :*: b) where
870− recordToPairs opts pa tj tjl (a :*: b) = recordToPairs opts pa tj tjl a <>
871− recordToPairs opts pa tj tjl b
873−instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
874− recordToPairs = fieldToPair
876−instance OVERLAPPING_ (Selector s, ToJSON a) =>
877− RecordToPairs arity (S1 s (K1 i (Maybe a))) where
878− recordToPairs opts _ _ _ (M1 k1) | omitNothingFields opts
879− , K1 Nothing <- k1 = DList.empty
880− recordToPairs opts pa tj tjl m1 = fieldToPair opts pa tj tjl m1
882−fieldToPair :: (Selector s, GToJSON arity a)
883− => Options -> Proxy arity
884− -> (p -> Value) -> ([p] -> Value)
885− -> S1 s a p -> DList Pair
886−fieldToPair opts pa tj tjl m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
887− , gToJSON opts pa tj tjl (unM1 m1)
890--------------------------------------------------------------------------------
1062--------------------------------------------------------------------------------
892−class RecordToEncoding arity f where
1064+class RecordToPairs enc pairs arity f where
893 -- 1st element: whole thing
1065 -- 1st element: whole thing
894 -- 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
895 -- of the field (without the key); 'Nothing' otherwise
1067 -- of the field (without the key); 'Nothing' otherwise
896− recordToEncoding :: Options -> Proxy arity
1068+ recordToPairs :: Options -> ToArgs enc arity a
897− -> (a -> Encoding) -> ([a] -> Encoding)
1069+ -> f a -> pairs
898− -> f a -> (Encoding, Maybe Encoding)
1070+
899−
1071+instance ( Monoid pairs
900−instance ( RecordToEncoding arity a
1072+ , RecordToPairs enc pairs arity a
901− , RecordToEncoding arity b
1073+ , RecordToPairs enc pairs arity b
902− ) => RecordToEncoding arity (a :*: b) where
1074+ ) => RecordToPairs enc pairs arity (a :*: b)
903− recordToEncoding opts pa te tel (a :*: b) | omitNothingFields opts =
1075+ where
904− (mconcat $ intersperse E.comma $
1076+ recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
905− filter (not . E.nullEncoding)
1077+ pairsOf a `mappend` pairsOf b
906− [ fst (recordToEncoding opts pa te tel a)
1078+ where
907− , fst (recordToEncoding opts pa te tel b) ]
1079+ pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
908− , Nothing)
1080+ pairsOf = recordToPairs opts targs
909− recordToEncoding opts pa te tel (a :*: b) =
1081+ {-# INLINE recordToPairs #-}
910− (fst (recordToEncoding opts pa te tel a) <> E.comma <>
1082+
911− fst (recordToEncoding opts pa te tel b),
1083+instance ( Selector s
912− Nothing)
1084+ , GToJSON enc arity a
913−
1085+ , KeyValuePair enc pairs
914−instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
1086+ ) => RecordToPairs enc pairs arity (S1 s a)
915− recordToEncoding = fieldToEncoding
1087+ where
916−
1088+ recordToPairs = fieldToPair
917−instance OVERLAPPING_ (Selector s, ToJSON a) =>
1089+ {-# INLINE recordToPairs #-}
1091+instance INCOHERENT_
1093+ , GToJSON enc arity (K1 i (Maybe a))
1094+ , KeyValuePair enc pairs
918− RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
1096+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
919− recordToEncoding opts _ _ _ (M1 k1) | omitNothingFields opts
1098+ recordToPairs opts _ (M1 k1) | omitNothingFields opts
920− , K1 Nothing <- k1 = (mempty, Nothing)
1099+ , K1 Nothing <- k1 = mempty
921− recordToEncoding opts pa te tel m1 = fieldToEncoding opts pa te tel m1
1100+ recordToPairs opts targs m1 = fieldToPair opts targs m1
922−
1101+ {-# INLINE recordToPairs #-}
923−fieldToEncoding :: (Selector s, GToEncoding arity a)
1102+
924− => Options -> Proxy arity
1103+instance INCOHERENT_
925− -> (p -> Encoding) -> ([p] -> Encoding)
1104+ ( Selector s
926− -> S1 s a p -> (Encoding, Maybe Encoding)
1105+ , GToJSON enc arity (K1 i (Maybe a))
927−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 =
928− let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
1122+ let key = fieldLabelModifier opts (selName m1)
929− valueBuilder = gToEncoding opts pa te tel (unM1 m1)
1123+ value = gToJSON opts targs (unM1 m1)
930− in (keyBuilder <> E.colon <> valueBuilder, Just valueBuilder)
1124+ in key `pair` value
1125+{-# INLINE fieldToPair #-}
932--------------------------------------------------------------------------------
1127--------------------------------------------------------------------------------
934class WriteProduct arity f where
1129class WriteProduct arity f where
935 writeProduct :: Options
1130 writeProduct :: Options
936− -> Proxy arity
1131+ -> ToArgs Value arity a
937 -> VM.MVector s Value
1132 -> VM.MVector s Value
938 -> Int -- ^ index
1133 -> Int -- ^ index
939 -> Int -- ^ length
1134 -> Int -- ^ length
943 -> ST s ()
1136 -> ST s ()
945instance ( WriteProduct arity a
1138instance ( WriteProduct arity a
946 , WriteProduct arity b
1139 , WriteProduct arity b
947 ) => WriteProduct arity (a :*: b) where
1140 ) => WriteProduct arity (a :*: b) where
948− writeProduct opts pa mv ix len tj tjl (a :*: b) = do
1141+ writeProduct opts targs mv ix len (a :*: b) = do
949− writeProduct opts pa mv ix lenL tj tjl a
1142+ writeProduct opts targs mv ix lenL a
950− writeProduct opts pa mv ixR lenR tj tjl b
1143+ writeProduct opts targs mv ixR lenR b
952 lenL = len `unsafeShiftR` 1
1145 lenL = len `unsafeShiftR` 1
953 lenR = len - lenL
1146 lenR = len - lenL
954 ixR = ix + lenL
1147 ixR = ix + lenL
1148+ {-# INLINE writeProduct #-}
956−instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
1150+instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
957− writeProduct opts pa mv ix _ tj tjl =
1151+ writeProduct opts targs mv ix _ =
958− VM.unsafeWrite mv ix . gToJSON opts pa tj tjl
1152+ VM.unsafeWrite mv ix . gToJSON opts targs
1153+ {-# INLINE writeProduct #-}
960--------------------------------------------------------------------------------
1155--------------------------------------------------------------------------------
962class EncodeProduct arity f where
1157class EncodeProduct arity f where
963− encodeProduct :: Options -> Proxy arity
1158+ encodeProduct :: Options -> ToArgs Encoding arity a
964− -> (a -> Encoding) -> ([a] -> Encoding)
965− -> f a -> Encoding
1159+ -> f a -> Encoding' E.InArray
967instance ( EncodeProduct arity a
1161instance ( EncodeProduct arity a
968 , EncodeProduct arity b
1162 , EncodeProduct arity b
969 ) => EncodeProduct arity (a :*: b) where
1163 ) => EncodeProduct arity (a :*: b) where
970− encodeProduct opts pa te tel (a :*: b) | omitNothingFields opts =
1164+ encodeProduct opts targs (a :*: b) | omitNothingFields opts =
971− mconcat $ intersperse E.comma $
1165+ E.econcat $ intersperse E.comma $
972 filter (not . E.nullEncoding)
1166 filter (not . E.nullEncoding)
973− [encodeProduct opts pa te tel a, encodeProduct opts pa te tel b]
1167+ [encodeProduct opts targs a, encodeProduct opts targs b]
974− encodeProduct opts pa te tel (a :*: b) = encodeProduct opts pa te tel a <>
1168+ encodeProduct opts targs (a :*: b) =
975− E.comma <>
1169+ encodeProduct opts targs a >*<
976− encodeProduct opts pa te tel b
1170+ encodeProduct opts targs b
1171+ {-# INLINE encodeProduct #-}
978−instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1173+instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
979− encodeProduct opts = gToEncoding opts
1174+ encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1175+ {-# INLINE encodeProduct #-}
981--------------------------------------------------------------------------------
1177--------------------------------------------------------------------------------
983−class ObjectWithSingleFieldObj arity f where
984− objectWithSingleFieldObj :: Options -> Proxy arity
985− -> (a -> Value) -> ([a] -> Value)
988−instance ( ObjectWithSingleFieldObj arity a
989− , ObjectWithSingleFieldObj arity b
990− ) => ObjectWithSingleFieldObj arity (a :+: b) where
991− objectWithSingleFieldObj opts pa tj tjl (L1 x) =
992− objectWithSingleFieldObj opts pa tj tjl x
993− objectWithSingleFieldObj opts pa tj tjl (R1 x) =
994− objectWithSingleFieldObj opts pa tj tjl x
996−instance ( GToJSON arity a
1179+instance ( GToJSON enc arity a
997− , ConsToJSON arity a
1180+ , ConsToJSON enc arity a
1181+ , FromPairs enc pairs
1182+ , KeyValuePair enc pairs
998 , Constructor c
1183 , Constructor c
999− ) => ObjectWithSingleFieldObj arity (C1 c a) where
1184+ ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1000− 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
1002− typ = pack $ constructorTagModifier opts $
1189+ typ = constructorTagModifier opts $
1003 conName (undefined :: t c a p)
1190 conName (undefined :: t c a p)
1005--------------------------------------------------------------------------------
1192--------------------------------------------------------------------------------
1007−class ObjectWithSingleFieldEnc arity f where
1194+instance OVERLAPPABLE_
1008− objectWithSingleFieldEnc :: Options -> Proxy arity
1195+ ( ConsToJSON enc arity a
1009− -> (a -> Encoding) -> ([a] -> Encoding)
1196+ ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1010− -> f a -> Encoding
1197+ where
1011−
1198+ sumToJSON' opts targs = Tagged . gToJSON opts targs
1012−instance ( ObjectWithSingleFieldEnc arity a
1199+
1013− , ObjectWithSingleFieldEnc arity b
1200+instance OVERLAPPING_
1014− ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1015− objectWithSingleFieldEnc opts pa te tel (L1 x) =
1016− objectWithSingleFieldEnc opts pa te tel x
1017− objectWithSingleFieldEnc opts pa te tel (R1 x) =
1018− objectWithSingleFieldEnc opts pa te tel x
1020−instance ( GToEncoding arity a
1021− , ConsToEncoding arity a
1022− , Constructor c
1201+ ( Constructor c
1023− ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1202+ , FromString enc
1024− objectWithSingleFieldEnc opts pa te tel v =
1203+ ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1025− E.openCurly <>
1204+ where
1026− toEncoding
1205+ sumToJSON' opts _ _ = Tagged . fromString $
1027− (constructorTagModifier opts
1206+ constructorTagModifier opts $ conName (undefined :: t c U1 p)
1028− (conName (undefined :: t c a p))) <>
1030− gToEncoding opts pa te tel v <>
1033-------------------------------------------------------------------------------
1208-------------------------------------------------------------------------------
1034-- Instances
1209-- Instances
@@ -1038,21 +1213,6 @@ instance ( GToEncoding arity a
1039-------------------------------------------------------------------------------
1214-------------------------------------------------------------------------------
1041−instance ToJSON1 Identity where
1042− liftToJSON t _ (Identity a) = t a
1043− {-# INLINE liftToJSON #-}
1045− liftToEncoding t _ (Identity a) = t a
1046− {-# INLINE liftToEncoding #-}
1048−instance (ToJSON a) => ToJSON (Identity a) where
1050− {-# INLINE toJSON #-}
1052− toEncoding = toEncoding1
1053− {-# INLINE toEncoding #-}
1056instance ToJSON2 Const where
1216instance ToJSON2 Const where
1057 liftToJSON2 t _ _ _ (Const x) = t x
1217 liftToJSON2 t _ _ _ (Const x) = t x
1058 {-# INLINE liftToJSON2 #-}
1218 {-# INLINE liftToJSON2 #-}
@@ -1081,7 +1241,7 @@ instance ToJSON1 Maybe where
1081 {-# INLINE liftToJSON #-}
1241 {-# INLINE liftToJSON #-}
1083 liftToEncoding t _ (Just a) = t a
1243 liftToEncoding t _ (Just a) = t a
1084− liftToEncoding _ _ Nothing = Encoding EB.null_
1244+ liftToEncoding _ _ Nothing = E.null_
1085 {-# INLINE liftToEncoding #-}
1245 {-# INLINE liftToEncoding #-}
1087instance (ToJSON a) => ToJSON (Maybe a) where
1247instance (ToJSON a) => ToJSON (Maybe a) where
@@ -1093,19 +1253,13 @@ instance (ToJSON a) => ToJSON (Maybe a) where
1095instance ToJSON2 Either where
1255instance ToJSON2 Either where
1096− liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton left (toA a)
1256+ liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton "Left" (toA a)
1097− liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1257+ liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton "Right" (toB b)
1098 {-# INLINE liftToJSON2 #-}
1258 {-# INLINE liftToJSON2 #-}
1100− liftToEncoding2 toA _ _toB _ (Left a) =
1260+ liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
1101− Encoding (B.shortByteString "{\"Left\":")
1103− <> Encoding (B.char7 '}')
1105− liftToEncoding2 _toA _ toB _ (Right b) =
1262+ liftToEncoding2 _toA _ toB _ (Right b) = E.pairs $ E.pair "Right" $ toB b
1106− Encoding (B.shortByteString "{\"Right\":")
1108− <> Encoding (B.char7 '}')
1109 {-# INLINE liftToEncoding2 #-}
1263 {-# INLINE liftToEncoding2 #-}
1111instance (ToJSON a) => ToJSON1 (Either a) where
1265instance (ToJSON a) => ToJSON1 (Either a) where
@@ -1122,9 +1276,12 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
1122 toEncoding = toEncoding2
1276 toEncoding = toEncoding2
1123 {-# INLINE toEncoding #-}
1277 {-# INLINE toEncoding #-}
1125−left, right :: Text
1279+instance ToJSON Void where
1126−left = "Left"
1280+ toJSON = absurd
1127−right = "Right"
1281+ {-# INLINE toJSON #-}
1283+ toEncoding = absurd
1284+ {-# INLINE toEncoding #-}
1130instance ToJSON Bool where
1287instance ToJSON Bool where
@@ -1135,10 +1292,7 @@ instance ToJSON Bool where
1135 {-# INLINE toEncoding #-}
1292 {-# INLINE toEncoding #-}
1137instance ToJSONKey Bool where
1294instance ToJSONKey Bool where
1138− toJSONKey = ToJSONKeyText
1139− ( (\x -> if x then "true" else "false")
1140− , (\x -> toEncoding $ if x then ("true" :: Text) else "false")
1295+ toJSONKey = toJSONKeyText $ \x -> if x then "true" else "false"
1144instance ToJSON Ordering where
1298instance ToJSON Ordering where
@@ -1166,10 +1320,10 @@ instance ToJSON Char where
1166 toJSONList = String . T.pack
1320 toJSONList = String . T.pack
1167 {-# INLINE toJSONList #-}
1321 {-# INLINE toJSONList #-}
1169− toEncoding = Encoding . EB.string . (:[])
1323+ toEncoding = E.string . (:[])
1170 {-# INLINE toEncoding #-}
1324 {-# INLINE toEncoding #-}
1172− toEncodingList = Encoding . EB.string
1326+ toEncodingList = E.string
1173 {-# INLINE toEncodingList #-}
1327 {-# INLINE toEncodingList #-}
@@ -1314,7 +1468,6 @@ instance ToJSONKey Int64 where
1314 toJSONKey = toJSONKeyTextEnc E.int64Text
1468 toJSONKey = toJSONKeyTextEnc E.int64Text
1315 {-# INLINE toJSONKey #-}
1469 {-# INLINE toJSONKey #-}
1318instance ToJSON Word where
1471instance ToJSON Word where
1319 toJSON = Number . fromIntegral
1472 toJSON = Number . fromIntegral
1320 {-# INLINE toJSON #-}
1473 {-# INLINE toJSON #-}
@@ -1374,6 +1527,12 @@ instance ToJSONKey Word64 where
1374 toJSONKey = toJSONKeyTextEnc E.word64Text
1527 toJSONKey = toJSONKeyTextEnc E.word64Text
1375 {-# 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 #-}
1378instance ToJSON Text where
1537instance ToJSON Text where
1379 toJSON = String
1538 toJSON = String
@@ -1383,18 +1542,21 @@ instance ToJSON Text where
1383 {-# INLINE toEncoding #-}
1542 {-# INLINE toEncoding #-}
1385instance ToJSONKey Text where
1544instance ToJSONKey Text where
1386− toJSONKey = ToJSONKeyText (id, toEncoding)
1545+ toJSONKey = toJSONKeyText id
1387 {-# INLINE toJSONKey #-}
1546 {-# INLINE toJSONKey #-}
1389instance ToJSON LT.Text where
1549instance ToJSON LT.Text where
1390 toJSON = String . LT.toStrict
1550 toJSON = String . LT.toStrict
1391 {-# INLINE toJSON #-}
1551 {-# INLINE toJSON #-}
1393− toEncoding t = Encoding $
1553+ toEncoding = E.lazyText
1395− LT.foldrChunks (\x xs -> EB.unquoted x <> xs) (B.char7 '"') t
1396 {-# INLINE toEncoding #-}
1554 {-# INLINE toEncoding #-}
1556+instance ToJSONKey LT.Text where
1557+ toJSONKey = toJSONKeyText LT.toStrict
1398instance ToJSON Version where
1560instance ToJSON Version where
1399 toJSON = toJSON . showVersion
1561 toJSON = toJSON . showVersion
1400 {-# INLINE toJSON #-}
1562 {-# INLINE toJSON #-}
@@ -1402,6 +1564,8 @@ instance ToJSON Version where
1402 toEncoding = toEncoding . showVersion
1564 toEncoding = toEncoding . showVersion
1403 {-# INLINE toEncoding #-}
1565 {-# INLINE toEncoding #-}
1567+instance ToJSONKey Version where
1568+ toJSONKey = toJSONKeyText (T.pack . showVersion)
1406-------------------------------------------------------------------------------
1570-------------------------------------------------------------------------------
1407-- semigroups NonEmpty
1571-- semigroups NonEmpty
@@ -1453,6 +1617,116 @@ instance (ToJSON a) => ToJSON (DList.DList a) where
1453 toEncoding = toEncoding1
1617 toEncoding = toEncoding1
1454 {-# 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 #-}
1456-------------------------------------------------------------------------------
1730-------------------------------------------------------------------------------
1457-- containers
1731-- containers
1458-------------------------------------------------------------------------------
1732-------------------------------------------------------------------------------
@@ -1518,17 +1792,15 @@ instance ToJSON a => ToJSON (IntMap.IntMap a) where
1519instance ToJSONKey k => ToJSON1 (M.Map k) where
1793instance ToJSONKey k => ToJSON1 (M.Map k) where
1520 liftToJSON g _ = case toJSONKey of
1794 liftToJSON g _ = case toJSONKey of
1521− ToJSONKeyText (f,_) -> Object . mapHashKeyVal f g
1795+ ToJSONKeyText f _ -> Object . mapHashKeyVal f g
1522− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . M.toList
1796+ ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
1523 {-# INLINE liftToJSON #-}
1797 {-# INLINE liftToJSON #-}
1525− -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
1526 liftToEncoding g _ = case toJSONKey of
1799 liftToEncoding g _ = case toJSONKey of
1527− ToJSONKeyText (_,f) -> dict f g M.foldrWithKey
1800+ ToJSONKeyText _ f -> dict f g M.foldrWithKey
1528− ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . M.toList
1801+ ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
1530− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1531− pairEncoding f (a, b) = tuple $ f a >*< g b
1803+ pairEncoding f (a, b) = E.list id [f a, g b]
1532 {-# INLINE liftToEncoding #-}
1804 {-# INLINE liftToEncoding #-}
@@ -1566,6 +1838,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
1566 toEncoding = toEncoding1
1838 toEncoding = toEncoding1
1567 {-# 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
1570-------------------------------------------------------------------------------
1853-------------------------------------------------------------------------------
1571-- vector
1854-- vector
@@ -1575,29 +1858,20 @@ instance ToJSON1 Vector where
1575 liftToJSON t _ = Array . V.map t
1858 liftToJSON t _ = Array . V.map t
1576 {-# INLINE liftToJSON #-}
1859 {-# INLINE liftToJSON #-}
1578− liftToEncoding t _ xs
1861+ liftToEncoding t _ = listEncoding t . V.toList
1579− | V.null xs = emptyArray_
1580− | otherwise = Encoding $
1581− B.char7 '[' <> fromEncoding (t (V.unsafeHead xs)) <>
1582− V.foldr go (B.char7 ']') (V.unsafeTail xs)
1584− go v b = B.char7 ',' <> fromEncoding (t v) <> b
1585 {-# INLINE liftToEncoding #-}
1862 {-# INLINE liftToEncoding #-}
1587instance (ToJSON a) => ToJSON (Vector a) where
1864instance (ToJSON a) => ToJSON (Vector a) where
1588− toJSON = Array . V.map toJSON
1865+ {-# SPECIALIZE instance ToJSON Array #-}
1589 {-# INLINE toJSON #-}
1868 {-# INLINE toJSON #-}
1591− toEncoding = encodeVector
1870+ toEncoding = toEncoding1
1592 {-# INLINE toEncoding #-}
1871 {-# INLINE toEncoding #-}
1594encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1873encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1595−encodeVector xs
1874+encodeVector = listEncoding toEncoding . VG.toList
1596− | VG.null xs = emptyArray_
1597− | otherwise = Encoding $
1598− B.char7 '[' <> builder (VG.unsafeHead xs) <>
1599− VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
1600− where go v b = B.char7 ',' <> builder v <> b
1601{-# INLINE encodeVector #-}
1875{-# INLINE encodeVector #-}
1603vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
1877vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -1648,20 +1922,21 @@ instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
1649instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1923instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1650 liftToJSON g _ = case toJSONKey of
1924 liftToJSON g _ = case toJSONKey of
1651− ToJSONKeyText (f,_) -> Object . mapKeyVal f g
1925+ ToJSONKeyText f _ -> Object . mapKeyVal f g
1652− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . H.toList
1926+ ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . H.toList
1653 {-# INLINE liftToJSON #-}
1927 {-# INLINE liftToJSON #-}
1655 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1929 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1656 liftToEncoding g _ = case toJSONKey of
1930 liftToEncoding g _ = case toJSONKey of
1657− ToJSONKeyText (_,f) -> dict f g H.foldrWithKey
1931+ ToJSONKeyText _ f -> dict f g H.foldrWithKey
1658− ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . H.toList
1932+ ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
1660− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1661− pairEncoding f (a, b) = tuple $ f a >*< g b
1934+ pairEncoding f (a, b) = E.list id [f a, g b]
1662 {-# INLINE liftToEncoding #-}
1935 {-# INLINE liftToEncoding #-}
1664instance (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 #-}
1665 toJSON = toJSON1
1940 toJSON = toJSON1
1666 {-# INLINE toJSON #-}
1941 {-# INLINE toJSON #-}
@@ -1676,7 +1951,7 @@ instance ToJSON Value where
1676 toJSON a = a
1951 toJSON a = a
1677 {-# INLINE toJSON #-}
1952 {-# INLINE toJSON #-}
1679− toEncoding = Encoding . EB.encodeToBuilder
1954+ toEncoding = E.value
1680 {-# INLINE toEncoding #-}
1955 {-# INLINE toEncoding #-}
1682instance ToJSON DotNetTime where
1957instance ToJSON DotNetTime where
@@ -1688,69 +1963,248 @@ dotNetTime :: DotNetTime -> String
1688dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1963dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1689 where secs = formatTime defaultTimeLocale "/Date(%s" t
1964 where secs = formatTime defaultTimeLocale "/Date(%s" t
1966+formatMillis :: (FormatTime t) => t -> String
1967+formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1969+-------------------------------------------------------------------------------
1971+-------------------------------------------------------------------------------
1973+instance ToJSON a => ToJSON (PM.Array a) where
1974+ -- note: we could do better than this if vector exposed the data
1975+ -- constructor in Data.Vector.
1976+ toJSON = toJSON . Exts.toList
1977+ toEncoding = toEncoding . Exts.toList
1979+instance ToJSON a => ToJSON (PM.SmallArray a) where
1980+ toJSON = toJSON . Exts.toList
1981+ toEncoding = toEncoding . Exts.toList
1983+#if (MIN_VERSION_primitive(0,6,4))
1984+instance (PM.Prim a,ToJSON a) => ToJSON (PM.PrimArray a) where
1985+ toJSON = toJSON . Exts.toList
1986+ toEncoding = toEncoding . Exts.toList
1988+#if !MIN_VERSION_primitive(0,7,0)
1989+instance (PM.PrimUnlifted a,ToJSON a) => ToJSON (PM.UnliftedArray a) where
1990+ toJSON = toJSON . Exts.toList
1991+ toEncoding = toEncoding . Exts.toList
1691-------------------------------------------------------------------------------
1995-------------------------------------------------------------------------------
1693-------------------------------------------------------------------------------
1997-------------------------------------------------------------------------------
1695instance ToJSON Day where
1999instance ToJSON Day where
1696− toJSON = stringEncoding
2000+ toJSON = stringEncoding . E.day
1697− toEncoding z = Encoding (EB.quote $ EB.day z)
2001+ toEncoding = E.day
2003+instance ToJSONKey Day where
2004+ toJSONKey = toJSONKeyTextEnc E.day
1700instance ToJSON TimeOfDay where
2007instance ToJSON TimeOfDay where
1701− toJSON = stringEncoding
2008+ toJSON = stringEncoding . E.timeOfDay
1702− toEncoding z = Encoding (EB.quote $ EB.timeOfDay z)
2009+ toEncoding = E.timeOfDay
2011+instance ToJSONKey TimeOfDay where
2012+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
1705instance ToJSON LocalTime where
2015instance ToJSON LocalTime where
1706− toJSON = stringEncoding
2016+ toJSON = stringEncoding . E.localTime
1707− toEncoding z = Encoding (EB.quote $ EB.localTime z)
2017+ toEncoding = E.localTime
2019+instance ToJSONKey LocalTime where
2020+ toJSONKey = toJSONKeyTextEnc E.localTime
1709instance ToJSON ZonedTime where
2023instance ToJSON ZonedTime where
1710− toJSON = stringEncoding
2024+ toJSON = stringEncoding . E.zonedTime
2025+ toEncoding = E.zonedTime
1712− toEncoding z = Encoding (EB.quote $ EB.zonedTime z)
2027+instance ToJSONKey ZonedTime where
2028+ toJSONKey = toJSONKeyTextEnc E.zonedTime
1714−formatMillis :: (FormatTime t) => t -> String
1715−formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1717instance ToJSON UTCTime where
2031instance ToJSON UTCTime where
1718− toJSON = stringEncoding
2032+ toJSON = stringEncoding . E.utcTime
2033+ toEncoding = E.utcTime
1720− toEncoding t = Encoding (EB.quote $ EB.utcTime t)
2035+instance ToJSONKey UTCTime where
2036+ toJSONKey = toJSONKeyTextEnc E.utcTime
1722-- | Encode something t a JSON string.
2038-- | Encode something t a JSON string.
1724−-- TODO: remove me, this is a hack
1725−stringEncoding :: (ToJSON a) => a -> Value
2039+stringEncoding :: Encoding' Text -> Value
1726stringEncoding = String
2040stringEncoding = String
1727 . T.dropAround (== '"')
2041 . T.dropAround (== '"')
1728 . T.decodeLatin1
2042 . T.decodeLatin1
1729 . L.toStrict
2043 . L.toStrict
1730− . B.toLazyByteString
2044+ . E.encodingToLazyByteString
1733{-# INLINE stringEncoding #-}
2045{-# INLINE stringEncoding #-}
1735instance ToJSON NominalDiffTime where
2048instance ToJSON NominalDiffTime where
1736 toJSON = Number . realToFrac
2049 toJSON = Number . realToFrac
1737 {-# INLINE toJSON #-}
2050 {-# INLINE toJSON #-}
1739− toEncoding = Encoding . EB.scientific . realToFrac
2052+ toEncoding = E.scientific . realToFrac
1740 {-# 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
1742-------------------------------------------------------------------------------
2107-------------------------------------------------------------------------------
1743-- base Monoid/Semigroup
2108-- base Monoid/Semigroup
1744-------------------------------------------------------------------------------
2109-------------------------------------------------------------------------------
1746−instance ToJSON1 Dual where
2111+instance ToJSON1 Monoid.Dual where
1747− liftToJSON t _ = t . getDual
2112+ liftToJSON t _ = t . Monoid.getDual
2113+ {-# INLINE liftToJSON #-}
2115+ liftToEncoding t _ = t . Monoid.getDual
2116+ {-# INLINE liftToEncoding #-}
2118+instance ToJSON a => ToJSON (Monoid.Dual a) where
2120+ {-# INLINE toJSON #-}
2122+ toEncoding = toEncoding1
2123+ {-# INLINE toEncoding #-}
2126+instance ToJSON1 Monoid.First where
2127+ liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
2128+ {-# INLINE liftToJSON #-}
2130+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
2131+ {-# INLINE liftToEncoding #-}
2133+instance ToJSON a => ToJSON (Monoid.First a) where
2135+ {-# INLINE toJSON #-}
2137+ toEncoding = toEncoding1
2138+ {-# INLINE toEncoding #-}
2141+instance ToJSON1 Monoid.Last where
2142+ liftToJSON t to' = liftToJSON t to' . Monoid.getLast
2143+ {-# INLINE liftToJSON #-}
2145+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2146+ {-# INLINE liftToEncoding #-}
2148+instance ToJSON a => ToJSON (Monoid.Last a) where
2150+ {-# INLINE toJSON #-}
2152+ toEncoding = toEncoding1
2153+ {-# INLINE toEncoding #-}
2156+instance ToJSON1 Semigroup.Min where
2157+ liftToJSON t _ (Semigroup.Min x) = t x
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
1748 {-# INLINE liftToJSON #-}
2202 {-# INLINE liftToJSON #-}
1750− liftToEncoding t _ = t . getDual
2204+ liftToEncoding t _ (Semigroup.Last x) = t x
1751 {-# INLINE liftToEncoding #-}
2205 {-# INLINE liftToEncoding #-}
1753−instance ToJSON a => ToJSON (Dual a) where
2207+instance ToJSON a => ToJSON (Semigroup.Last a) where
1754 toJSON = toJSON1
2208 toJSON = toJSON1
1755 {-# INLINE toJSON #-}
2209 {-# INLINE toJSON #-}
@@ -1758,14 +2212,14 @@ instance ToJSON a => ToJSON (Dual a) where
1758 {-# INLINE toEncoding #-}
2212 {-# INLINE toEncoding #-}
1761−instance ToJSON1 First where
2215+instance ToJSON1 Semigroup.WrappedMonoid where
1762− liftToJSON t to' = liftToJSON t to' . getFirst
2216+ liftToJSON t _ (Semigroup.WrapMonoid x) = t x
1763 {-# INLINE liftToJSON #-}
2217 {-# INLINE liftToJSON #-}
1765− liftToEncoding t to' = liftToEncoding t to' . getFirst
2219+ liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
1766 {-# INLINE liftToEncoding #-}
2220 {-# INLINE liftToEncoding #-}
1768−instance ToJSON a => ToJSON (First a) where
2222+instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
1769 toJSON = toJSON1
2223 toJSON = toJSON1
1770 {-# INLINE toJSON #-}
2224 {-# INLINE toJSON #-}
@@ -1773,14 +2227,14 @@ instance ToJSON a => ToJSON (First a) where
1773 {-# INLINE toEncoding #-}
2227 {-# INLINE toEncoding #-}
1776−instance ToJSON1 Last where
2230+instance ToJSON1 Semigroup.Option where
1777− liftToJSON t to' = liftToJSON t to' . getLast
2231+ liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
1778 {-# INLINE liftToJSON #-}
2232 {-# INLINE liftToJSON #-}
1780− liftToEncoding t to' = liftToEncoding t to' . getLast
2234+ liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
1781 {-# INLINE liftToEncoding #-}
2235 {-# INLINE liftToEncoding #-}
1783−instance ToJSON a => ToJSON (Last a) where
2237+instance ToJSON a => ToJSON (Semigroup.Option a) where
1784 toJSON = toJSON1
2238 toJSON = toJSON1
1785 {-# INLINE toJSON #-}
2239 {-# INLINE toJSON #-}
@@ -1791,14 +2245,28 @@ instance ToJSON a => ToJSON (Last a) where
1791-- tagged
2245-- tagged
1792-------------------------------------------------------------------------------
2246-------------------------------------------------------------------------------
2248+instance ToJSON1 Proxy where
2249+ liftToJSON _ _ _ = Null
2250+ {-# INLINE liftToJSON #-}
2252+ liftToEncoding _ _ _ = E.null_
2253+ {-# INLINE liftToEncoding #-}
1794instance ToJSON (Proxy a) where
2255instance ToJSON (Proxy a) where
1795 toJSON _ = Null
2256 toJSON _ = Null
1796 {-# INLINE toJSON #-}
2257 {-# INLINE toJSON #-}
1798− toEncoding _ = Encoding EB.null_
2259+ toEncoding _ = E.null_
1799 {-# 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 #-}
1802instance ToJSON1 (Tagged a) where
2270instance ToJSON1 (Tagged a) where
1803 liftToJSON t _ (Tagged x) = t x
2271 liftToJSON t _ (Tagged x) = t x
1804 {-# INLINE liftToJSON #-}
2272 {-# INLINE liftToJSON #-}
@@ -1813,7 +2281,6 @@ instance ToJSON b => ToJSON (Tagged a b) where
1813 toEncoding = toEncoding1
2281 toEncoding = toEncoding1
1814 {-# INLINE toEncoding #-}
2282 {-# INLINE toEncoding #-}
1817instance ToJSONKey b => ToJSONKey (Tagged a b) where
2284instance ToJSONKey b => ToJSONKey (Tagged a b) where
1818 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
2285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
1819 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
2286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -1822,34 +2289,22 @@ instance ToJSONKey b => ToJSONKey (Tagged a b) where
1822-- Instances for converting t map keys
2289-- Instances for converting t map keys
1823-------------------------------------------------------------------------------
2290-------------------------------------------------------------------------------
1825−-- | TODO: Move ToJSONKEyFunction to .Types(.Internal),
1826−-- export and document this function from there
1827−toJSONKeyTextEnc :: (a -> Encoding) -> ToJSONKeyFunction a
1828−toJSONKeyTextEnc e = ToJSONKeyText
1829− -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
1830− ( T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . B.toLazyByteString . fromEncoding . e
1834instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
2292instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
1835instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
2293instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
1836instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
2294instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
1838instance ToJSONKey Char where
2296instance ToJSONKey Char where
1839− toJSONKey = ToJSONKeyText (T.singleton, toEncoding)
2297+ toJSONKey = ToJSONKeyText T.singleton (E.string . (:[]))
1840− toJSONKeyList = ToJSONKeyText (T.pack, toEncoding . T.pack)
2298+ toJSONKeyList = toJSONKeyText T.pack
1842instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
2300instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
1843 toJSONKey = toJSONKeyList
2301 toJSONKey = toJSONKeyList
1845−instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1846− toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1848-------------------------------------------------------------------------------
2303-------------------------------------------------------------------------------
1849−-- Tuple instances, see tuple-instances-to.hs
2304+-- Tuple instances
1850-------------------------------------------------------------------------------
2305-------------------------------------------------------------------------------
1852−instance ToJSON2 ((,) ) where
2307+instance ToJSON2 (,) where
1853 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
2308 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
1854 mv <- VM.unsafeNew 2
2309 mv <- VM.unsafeNew 2
1855 VM.unsafeWrite mv 0 (toA a)
2310 VM.unsafeWrite mv 0 (toA a)
@@ -1857,9 +2312,7 @@ instance ToJSON2 ((,) ) where
1857 return mv
2312 return mv
1858 {-# INLINE liftToJSON2 #-}
2313 {-# INLINE liftToJSON2 #-}
1860− liftToEncoding2 toA _ toB _ (a, b) = tuple $
2315+ liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
1863 {-# INLINE liftToEncoding2 #-}
2316 {-# INLINE liftToEncoding2 #-}
1865instance (ToJSON a) => ToJSON1 ((,) a) where
2318instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -1883,10 +2336,11 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
1883 return mv
2336 return mv
1884 {-# INLINE liftToJSON2 #-}
2337 {-# INLINE liftToJSON2 #-}
1886− liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
2339+ liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
1887− toEncoding a >*<
2340+ [ toEncoding a
1888− toB b >*<
2341+ , toB b
1890 {-# INLINE liftToEncoding2 #-}
2344 {-# INLINE liftToEncoding2 #-}
1892instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
2346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -1911,11 +2365,12 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
1911 return mv
2365 return mv
1912 {-# INLINE liftToJSON2 #-}
2366 {-# INLINE liftToJSON2 #-}
1914− liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
2368+ liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
1915− toEncoding a >*<
2369+ [ toEncoding a
1916− toEncoding b >*<
2370+ , toEncoding b
1917− toC c >*<
2371+ , toC c
1919 {-# INLINE liftToEncoding2 #-}
2374 {-# INLINE liftToEncoding2 #-}
1921instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
2376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -1941,12 +2396,13 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
1941 return mv
2396 return mv
1942 {-# INLINE liftToJSON2 #-}
2397 {-# INLINE liftToJSON2 #-}
1944− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
2399+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
1945− toEncoding a >*<
2400+ [ toEncoding a
1946− toEncoding b >*<
2401+ , toEncoding b
1947− toEncoding c >*<
2402+ , toEncoding c
1948− toD d >*<
2403+ , toD d
1950 {-# INLINE liftToEncoding2 #-}
2406 {-# INLINE liftToEncoding2 #-}
1952instance (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
@@ -1973,13 +2429,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
1973 return mv
2429 return mv
1974 {-# INLINE liftToJSON2 #-}
2430 {-# INLINE liftToJSON2 #-}
1976− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
2432+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
1977− toEncoding a >*<
2433+ [ toEncoding a
1978− toEncoding b >*<
2434+ , toEncoding b
1979− toEncoding c >*<
2435+ , toEncoding c
1980− toEncoding d >*<
2436+ , toEncoding d
1981− toE e >*<
2437+ , toE e
1983 {-# INLINE liftToEncoding2 #-}
2440 {-# INLINE liftToEncoding2 #-}
1985instance (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
@@ -2007,14 +2464,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
2007 return mv
2464 return mv
2008 {-# INLINE liftToJSON2 #-}
2465 {-# INLINE liftToJSON2 #-}
2010− 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
2011− toEncoding a >*<
2468+ [ toEncoding a
2012− toEncoding b >*<
2469+ , toEncoding b
2013− toEncoding c >*<
2470+ , toEncoding c
2014− toEncoding d >*<
2471+ , toEncoding d
2015− toEncoding e >*<
2472+ , toEncoding e
2016− toF f >*<
2473+ , toF f
2018 {-# INLINE liftToEncoding2 #-}
2476 {-# INLINE liftToEncoding2 #-}
2020instance (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
@@ -2043,15 +2501,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
2043 return mv
2501 return mv
2044 {-# INLINE liftToJSON2 #-}
2502 {-# INLINE liftToJSON2 #-}
2046− 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
2047− toEncoding a >*<
2505+ [ toEncoding a
2048− toEncoding b >*<
2506+ , toEncoding b
2049− toEncoding c >*<
2507+ , toEncoding c
2050− toEncoding d >*<
2508+ , toEncoding d
2051− toEncoding e >*<
2509+ , toEncoding e
2052− toEncoding f >*<
2510+ , toEncoding f
2053− toG g >*<
2511+ , toG g
2055 {-# INLINE liftToEncoding2 #-}
2514 {-# INLINE liftToEncoding2 #-}
2057instance (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
@@ -2081,16 +2540,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
2081 return mv
2540 return mv
2082 {-# INLINE liftToJSON2 #-}
2541 {-# INLINE liftToJSON2 #-}
2084− 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
2085− toEncoding a >*<
2544+ [ toEncoding a
2086− toEncoding b >*<
2545+ , toEncoding b
2087− toEncoding c >*<
2546+ , toEncoding c
2088− toEncoding d >*<
2547+ , toEncoding d
2089− toEncoding e >*<
2548+ , toEncoding e
2090− toEncoding f >*<
2549+ , toEncoding f
2091− toEncoding g >*<
2550+ , toEncoding g
2092− toH h >*<
2551+ , toH h
2094 {-# INLINE liftToEncoding2 #-}
2554 {-# INLINE liftToEncoding2 #-}
2096instance (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
@@ -2121,17 +2581,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2121 return mv
2581 return mv
2122 {-# INLINE liftToJSON2 #-}
2582 {-# INLINE liftToJSON2 #-}
2124− 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
2125− toEncoding a >*<
2585+ [ toEncoding a
2126− toEncoding b >*<
2586+ , toEncoding b
2127− toEncoding c >*<
2587+ , toEncoding c
2128− toEncoding d >*<
2588+ , toEncoding d
2129− toEncoding e >*<
2589+ , toEncoding e
2130− toEncoding f >*<
2590+ , toEncoding f
2131− toEncoding g >*<
2591+ , toEncoding g
2132− toEncoding h >*<
2592+ , toEncoding h
2133− toI i >*<
2593+ , toI i
2135 {-# INLINE liftToEncoding2 #-}
2596 {-# INLINE liftToEncoding2 #-}
2137instance (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
@@ -2163,18 +2624,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2163 return mv
2624 return mv
2164 {-# INLINE liftToJSON2 #-}
2625 {-# INLINE liftToJSON2 #-}
2166− 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
2167− toEncoding a >*<
2628+ [ toEncoding a
2168− toEncoding b >*<
2629+ , toEncoding b
2169− toEncoding c >*<
2630+ , toEncoding c
2170− toEncoding d >*<
2631+ , toEncoding d
2171− toEncoding e >*<
2632+ , toEncoding e
2172− toEncoding f >*<
2633+ , toEncoding f
2173− toEncoding g >*<
2634+ , toEncoding g
2174− toEncoding h >*<
2635+ , toEncoding h
2175− toEncoding i >*<
2636+ , toEncoding i
2176− toJ j >*<
2637+ , toJ j
2178 {-# INLINE liftToEncoding2 #-}
2640 {-# INLINE liftToEncoding2 #-}
2180instance (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
@@ -2207,19 +2669,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2207 return mv
2669 return mv
2208 {-# INLINE liftToJSON2 #-}
2670 {-# INLINE liftToJSON2 #-}
2210− 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
2211− toEncoding a >*<
2673+ [ toEncoding a
2212− toEncoding b >*<
2674+ , toEncoding b
2213− toEncoding c >*<
2675+ , toEncoding c
2214− toEncoding d >*<
2676+ , toEncoding d
2215− toEncoding e >*<
2677+ , toEncoding e
2216− toEncoding f >*<
2678+ , toEncoding f
2217− toEncoding g >*<
2679+ , toEncoding g
2218− toEncoding h >*<
2680+ , toEncoding h
2219− toEncoding i >*<
2681+ , toEncoding i
2220− toEncoding j >*<
2682+ , toEncoding j
2221− toK k >*<
2683+ , toK k
2223 {-# INLINE liftToEncoding2 #-}
2686 {-# INLINE liftToEncoding2 #-}
2225instance (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
@@ -2253,20 +2716,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2253 return mv
2716 return mv
2254 {-# INLINE liftToJSON2 #-}
2717 {-# INLINE liftToJSON2 #-}
2256− 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
2257− toEncoding a >*<
2720+ [ toEncoding a
2258− toEncoding b >*<
2721+ , toEncoding b
2259− toEncoding c >*<
2722+ , toEncoding c
2260− toEncoding d >*<
2723+ , toEncoding d
2261− toEncoding e >*<
2724+ , toEncoding e
2262− toEncoding f >*<
2725+ , toEncoding f
2263− toEncoding g >*<
2726+ , toEncoding g
2264− toEncoding h >*<
2727+ , toEncoding h
2265− toEncoding i >*<
2728+ , toEncoding i
2266− toEncoding j >*<
2729+ , toEncoding j
2267− toEncoding k >*<
2730+ , toEncoding k
2268− toL l >*<
2731+ , toL l
2270 {-# INLINE liftToEncoding2 #-}
2734 {-# INLINE liftToEncoding2 #-}
2272instance (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
@@ -2301,21 +2765,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2301 return mv
2765 return mv
2302 {-# INLINE liftToJSON2 #-}
2766 {-# INLINE liftToJSON2 #-}
2304− 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
2305− toEncoding a >*<
2769+ [ toEncoding a
2306− toEncoding b >*<
2770+ , toEncoding b
2307− toEncoding c >*<
2771+ , toEncoding c
2308− toEncoding d >*<
2772+ , toEncoding d
2309− toEncoding e >*<
2773+ , toEncoding e
2310− toEncoding f >*<
2774+ , toEncoding f
2311− toEncoding g >*<
2775+ , toEncoding g
2312− toEncoding h >*<
2776+ , toEncoding h
2313− toEncoding i >*<
2777+ , toEncoding i
2314− toEncoding j >*<
2778+ , toEncoding j
2315− toEncoding k >*<
2779+ , toEncoding k
2316− toEncoding l >*<
2780+ , toEncoding l
2317− toM m >*<
2781+ , toM m
2319 {-# INLINE liftToEncoding2 #-}
2784 {-# INLINE liftToEncoding2 #-}
2321instance (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
@@ -2351,22 +2816,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2351 return mv
2816 return mv
2352 {-# INLINE liftToJSON2 #-}
2817 {-# INLINE liftToJSON2 #-}
2354− 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
2355− toEncoding a >*<
2820+ [ toEncoding a
2356− toEncoding b >*<
2821+ , toEncoding b
2357− toEncoding c >*<
2822+ , toEncoding c
2358− toEncoding d >*<
2823+ , toEncoding d
2359− toEncoding e >*<
2824+ , toEncoding e
2360− toEncoding f >*<
2825+ , toEncoding f
2361− toEncoding g >*<
2826+ , toEncoding g
2362− toEncoding h >*<
2827+ , toEncoding h
2363− toEncoding i >*<
2828+ , toEncoding i
2364− toEncoding j >*<
2829+ , toEncoding j
2365− toEncoding k >*<
2830+ , toEncoding k
2366− toEncoding l >*<
2831+ , toEncoding l
2367− toEncoding m >*<
2832+ , toEncoding m
2368− toN n >*<
2833+ , toN n
2370 {-# INLINE liftToEncoding2 #-}
2836 {-# INLINE liftToEncoding2 #-}
2372instance (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
@@ -2380,3 +2846,27 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2380 {-# INLINE toJSON #-}
2846 {-# INLINE toJSON #-}
2381 toEncoding = toEncoding2
2847 toEncoding = toEncoding2
2382 {-# 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