@@ -1,29 +1,27 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DataKinds #-}
3−{-# LANGUAGE DefaultSignatures #-}
4−{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
6−{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE MultiParamTypeClasses #-}
8−{-# LANGUAGE NamedFieldPuns #-}
9−{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PatternGuards #-}
11−{-# LANGUAGE RecordWildCards #-}
12−{-# LANGUAGE ScopedTypeVariables #-}
13−{-# LANGUAGE TypeOperators #-}
14−{-# LANGUAGE TypeSynonymInstances #-}
15−{-# LANGUAGE UndecidableInstances #-}
16−
17−#if __GLASGOW_HASKELL__ >= 706
18−{-# LANGUAGE PolyKinds #-}
19−#endif
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE EmptyDataDecls #-}
4+{-# LANGUAGE FlexibleContexts #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE FunctionalDependencies #-}
7+{-# LANGUAGE GADTs #-}
8+{-# LANGUAGE NoImplicitPrelude #-}
9+{-# LANGUAGE OverloadedStrings #-}
10+{-# LANGUAGE PatternGuards #-}
11+{-# LANGUAGE PolyKinds #-}
12+{-# LANGUAGE RecordWildCards #-}
13+{-# LANGUAGE ScopedTypeVariables #-}
14+{-# LANGUAGE TypeOperators #-}
15+{-# LANGUAGE UndecidableInstances #-}
2016
2117#include "overlapping-compat.h"
18+#include "incoherent-compat.h"
2219
2320-- TODO: Drop this when we remove support for Data.Attoparsec.Number
2421{-# OPTIONS_GHC -fno-warn-deprecations #-}
2522
26−module Data.Aeson.Types.ToJSON (
23+module Data.Aeson.Types.ToJSON
24+ (
2725 -- * Core JSON classes
2826 ToJSON(..)
2927 -- * Liftings to unary and binary type constructors
@@ -35,7 +33,7 @@ module Data.Aeson.Types.ToJSON (
3533 , toEncoding2
3634 -- * Generic JSON classes
3735 , GToJSON(..)
38− , GToEncoding(..)
36+ , ToArgs(..)
3937 , genericToJSON
4038 , genericToEncoding
4139 , genericLiftToJSON
@@ -43,89 +41,107 @@ module Data.Aeson.Types.ToJSON (
4341 -- * Classes and types for map keys
4442 , ToJSONKey(..)
4543 , ToJSONKeyFunction(..)
44+ , toJSONKeyText
4645 , contramapToJSONKeyFunction
46+
47+ , GToJSONKey()
48+ , genericToJSONKey
49+
4750 -- * Object key-value pairs
4851 , KeyValue(..)
52+ , KeyValuePair(..)
53+ , FromPairs(..)
4954 -- * Functions needed for documentation
5055 -- * Encoding functions
5156 , listEncoding
5257 , listValue
5358 ) where
5459
55−import Prelude ()
5660import Prelude.Compat
5761
58−import Data.Aeson.Encoding.Internal (Encoding (..), Series, dict, emptyArray_,
59− tuple, (>*<))
60−import Data.Aeson.Functions (mapHashKeyVal, mapKeyVal)
61−import Data.Aeson.Types.Generic
62+import Control.Applicative (Const(..))
63+import Control.Monad.ST (ST)
64+import Data.Aeson.Encoding (Encoding, Encoding', Series, dict, emptyArray_)
65+import Data.Aeson.Encoding.Internal ((>*<))
66+import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
67+import Data.Aeson.Types.Generic (AllNullary, False, IsRecord, One, ProductSize, Tagged2(..), True, Zero, productSize)
6268import Data.Aeson.Types.Internal
63−
64−import qualified Data.Aeson.Encode.Builder as EB
65−import qualified Data.Aeson.Encoding.Internal as E
66−
67−import Control.Applicative (Const (..))
68−import Control.Monad.ST (ST)
69−import Data.Attoparsec.Number (Number (..))
70−import Data.Bits (unsafeShiftR)
71−import Data.DList (DList)
72−import Data.Fixed (Fixed, HasResolution)
73−import Data.Foldable (toList)
74−import Data.Functor.Identity (Identity (..))
75−import Data.Int (Int16, Int32, Int64, Int8)
76−import Data.List (intersperse)
77−import Data.List.NonEmpty (NonEmpty (..))
78−import Data.Monoid (Dual (..), First (..), Last (..), (<>))
79−import Data.Proxy (Proxy (..))
80−import Data.Ratio (Ratio, denominator, numerator)
81−import Data.Scientific (Scientific)
82−import Data.Tagged (Tagged (..))
83−import Data.Text (Text, pack)
84−import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay,
85− UTCTime, ZonedTime)
86−import Data.Time.Format (FormatTime, formatTime)
87−import Data.Time.Locale.Compat (defaultTimeLocale)
88−import Data.Vector (Vector)
89−import Data.Version (Version, showVersion)
90−import Data.Word (Word16, Word32, Word64, Word8)
91−import Foreign.Storable (Storable)
69+import Data.Attoparsec.Number (Number(..))
70+import Data.Bits (unsafeShiftR)
71+import Data.DList (DList)
72+import Data.Fixed (Fixed, HasResolution, Nano)
73+import Data.Foldable (toList)
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(..))
79+import Data.Int (Int16, Int32, Int64, Int8)
80+import Data.List (intersperse)
81+import Data.List.NonEmpty (NonEmpty(..))
82+import Data.Semigroup ((<>))
83+import Data.Proxy (Proxy(..))
84+import Data.Ratio (Ratio, denominator, numerator)
85+import Data.Scientific (Scientific)
86+import Data.Tagged (Tagged(..))
87+import Data.Text (Text, pack)
88+import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
89+import Data.Time.Calendar.Compat (CalendarDiffDays (..), DayOfWeek (..))
90+import Data.Time.LocalTime.Compat (CalendarDiffTime (..))
91+import Data.Time.Clock.System.Compat (SystemTime (..))
92+import Data.Time.Format.Compat (FormatTime, formatTime, defaultTimeLocale)
93+import Data.Vector (Vector)
94+import Data.Version (Version, showVersion)
95+import Data.Void (Void, absurd)
96+import Data.Word (Word16, Word32, Word64, Word8)
97+import Foreign.Storable (Storable)
98+import Foreign.C.Types (CTime (..))
9299import GHC.Generics
93−import Numeric.Natural (Natural)
94−
95−import qualified Data.ByteString.Builder as B
96−import qualified Data.ByteString.Lazy as L
97−import qualified Data.DList as DList
98−import qualified Data.HashMap.Strict as H
99−import qualified Data.HashSet as HashSet
100−import qualified Data.IntMap as IntMap
101−import qualified Data.IntSet as IntSet
102−import qualified Data.List.NonEmpty as NE
103−import qualified Data.Map as M
104−import qualified Data.Scientific as Scientific
105−import qualified Data.Sequence as Seq
106−import qualified Data.Set as Set
107−import qualified Data.Text as T
108−import qualified Data.Text.Encoding as T
109−import qualified Data.Text.Lazy as LT
110−import qualified Data.Text.Lazy.Builder as LTB
111−import qualified Data.Text.Lazy.Builder.Int as LTBI
112−import qualified Data.Tree as Tree
113−import qualified Data.Vector as V
114−import qualified Data.Vector.Generic as VG
115−import qualified Data.Vector.Mutable as VM
116−import qualified Data.Vector.Primitive as VP
117−import qualified Data.Vector.Storable as VS
118−import qualified Data.Vector.Unboxed as VU
119−
120−builder :: ToJSON a => a -> B.Builder
121−builder = fromEncoding . toEncoding
100+import Numeric.Natural (Natural)
101+import qualified Data.Aeson.Encoding as E
102+import qualified Data.Aeson.Encoding.Internal as E (InArray, comma, econcat, retagEncoding)
103+import qualified Data.ByteString.Lazy as L
104+import qualified Data.DList as DList
105+import qualified Data.HashMap.Strict as H
106+import qualified Data.HashSet as HashSet
107+import qualified Data.IntMap as IntMap
108+import qualified Data.IntSet as IntSet
109+import qualified Data.List.NonEmpty as NE
110+import qualified Data.Map as M
111+import qualified Data.Monoid as Monoid
112+import qualified Data.Scientific as Scientific
113+import qualified Data.Semigroup as Semigroup
114+import qualified Data.Sequence as Seq
115+import qualified Data.Set as Set
116+import qualified Data.Text as T
117+import qualified Data.Text.Encoding as T
118+import qualified Data.Text.Lazy as LT
119+import qualified Data.Tree as Tree
120+import qualified Data.UUID.Types as UUID
121+import qualified Data.Vector as V
122+import qualified Data.Vector.Generic as VG
123+import qualified Data.Vector.Mutable as VM
124+import qualified Data.Vector.Primitive as VP
125+import qualified Data.Vector.Storable as VS
126+import qualified Data.Vector.Unboxed as VU
127+
128+import qualified Data.Aeson.Encoding.Builder as EB
129+import qualified Data.ByteString.Builder as B
130+
131+import qualified GHC.Exts as Exts
132+import qualified Data.Primitive.Array as PM
133+import qualified Data.Primitive.SmallArray as PM
134+import qualified Data.Primitive.Types as PM
135+
136+#if MIN_VERSION_primitive(0,6,4)
137+#if !MIN_VERSION_primitive(0,7,0)
138+import qualified Data.Primitive.UnliftedArray as PM
139+#endif
140+import qualified Data.Primitive.PrimArray as PM
141+#endif
122142
123143toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
124−toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
125− mv <- VM.unsafeNew 2
126− VM.unsafeWrite mv 0 (keySerialiser a)
127− VM.unsafeWrite mv 1 (valSerializer b)
128− return mv
144+toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
129145{-# INLINE toJSONPair #-}
130146
131147realFloatToJSON :: RealFloat a => a -> Value
@@ -134,63 +150,59 @@ realFloatToJSON d
134150 | otherwise = Number $ Scientific.fromFloatDigits d
135151{-# INLINE realFloatToJSON #-}
136152
137−realFloatToEncoding :: RealFloat a => a -> Encoding
138−realFloatToEncoding d
139− | isNaN d || isInfinite d = Encoding EB.null_
140− | otherwise = toEncoding (Scientific.fromFloatDigits d)
141−{-# INLINE realFloatToEncoding #-}
142−
143153-------------------------------------------------------------------------------
144154-- Generics
145155-------------------------------------------------------------------------------
146156
147157-- | Class of generic representation types that can be converted to
148158-- JSON.
149−class GToJSON arity f where
159+class GToJSON enc arity f where
150160 -- | This method (applied to 'defaultOptions') is used as the
151− -- default generic implementation of 'toJSON' (if the @arity@ is 'Zero')
152− -- or 'liftToJSON' (if the @arity@ is 'One').
153− gToJSON :: Options -> Proxy arity
154− -> (a -> Value) -> ([a] -> Value) -> f a -> Value
155−
156−-- | Class of generic representation types that can be converted to
157−-- a JSON 'Encoding'.
158−class GToEncoding arity f where
159− -- | This method (applied to 'defaultOptions') can be used as the
160− -- default generic implementation of 'toEncoding' (if the @arity@ is 'Zero')
161− -- or 'liftToEncoding' (if the @arity@ is 'One').
162− gToEncoding :: Options -> Proxy arity
163− -> (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
161+ -- default generic implementation of 'toJSON'
162+ -- (with @enc ~ 'Value'@ and @arity ~ 'Zero'@)
163+ -- and 'liftToJSON' (if the @arity@ is 'One').
164+ --
165+ -- It also provides a generic implementation of 'toEncoding'
166+ -- (with @enc ~ 'Encoding'@ and @arity ~ 'Zero'@)
167+ -- and 'liftToEncoding' (if the @arity@ is 'One').
168+ gToJSON :: Options -> ToArgs enc arity a -> f a -> enc
169+
170+-- | A 'ToArgs' value either stores nothing (for 'ToJSON') or it stores the two
171+-- function arguments that encode occurrences of the type parameter (for
172+-- 'ToJSON1').
173+data ToArgs res arity a where
174+ NoToArgs :: ToArgs res Zero a
175+ To1Args :: (a -> res) -> ([a] -> res) -> ToArgs res One a
164176
165177-- | A configurable generic JSON creator. This function applied to
166178-- 'defaultOptions' is used as the default for 'toJSON' when the type
167179-- is an instance of 'Generic'.
168−genericToJSON :: (Generic a, GToJSON Zero (Rep a))
180+genericToJSON :: (Generic a, GToJSON Value Zero (Rep a))
169181 => Options -> a -> Value
170−genericToJSON opts = gToJSON opts proxyZero undefined undefined . from
182+genericToJSON opts = gToJSON opts NoToArgs . from
171183
172184-- | A configurable generic JSON creator. This function applied to
173185-- 'defaultOptions' is used as the default for 'liftToJSON' when the type
174186-- is an instance of 'Generic1'.
175−genericLiftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
187+genericLiftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
176188 => Options -> (a -> Value) -> ([a] -> Value)
177189 -> f a -> Value
178−genericLiftToJSON opts tj tjl = gToJSON opts proxyOne tj tjl . from1
190+genericLiftToJSON opts tj tjl = gToJSON opts (To1Args tj tjl) . from1
179191
180192-- | A configurable generic JSON encoder. This function applied to
181193-- 'defaultOptions' is used as the default for 'toEncoding' when the type
182194-- is an instance of 'Generic'.
183−genericToEncoding :: (Generic a, GToEncoding Zero (Rep a))
195+genericToEncoding :: (Generic a, GToJSON Encoding Zero (Rep a))
184196 => Options -> a -> Encoding
185−genericToEncoding opts = gToEncoding opts proxyZero undefined undefined . from
197+genericToEncoding opts = gToJSON opts NoToArgs . from
186198
187199-- | A configurable generic JSON encoder. This function applied to
188200-- 'defaultOptions' is used as the default for 'liftToEncoding' when the type
189201-- is an instance of 'Generic1'.
190−genericLiftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
202+genericLiftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
191203 => Options -> (a -> Encoding) -> ([a] -> Encoding)
192204 -> f a -> Encoding
193−genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
205+genericLiftToEncoding opts te tel = gToJSON opts (To1Args te tel) . from1
194206
195207-------------------------------------------------------------------------------
196208-- Class
@@ -198,6 +210,9 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
198210
199211-- | A type that can be converted to JSON.
200212--
213+-- Instances in general /must/ specify 'toJSON' and /should/ (but don't need
214+-- to) specify 'toEncoding'.
215+--
201216-- An example type and instance:
202217--
203218-- @
@@ -206,10 +221,10 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
206221--
207222-- data Coord = Coord { x :: Double, y :: Double }
208223--
209−-- instance ToJSON Coord where
210−-- toJSON (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
224+-- instance 'ToJSON' Coord where
225+-- 'toJSON' (Coord x y) = 'object' [\"x\" '.=' x, \"y\" '.=' y]
211226--
212−-- toEncoding (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
227+-- 'toEncoding' (Coord x y) = 'pairs' (\"x\" '.=' x '<>' \"y\" '.=' y)
213228-- @
214229--
215230-- Instead of manually writing your 'ToJSON' instance, there are two options
@@ -217,17 +232,15 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
217232--
218233-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
219234-- instance at compile time. The generated instance is optimized for your type
220−-- so will probably be more efficient than the following two options:
235+-- so it will probably be more efficient than the following option.
221236--
222237-- * The compiler can provide a default generic implementation for
223238-- 'toJSON'.
224239--
225240-- To use the second, simply add a @deriving 'Generic'@ clause to your
226−-- datatype and declare a 'ToJSON' instance for your datatype without giving
227−-- definitions for 'toJSON' or 'toEncoding'.
228−--
229−-- For example, the previous example can be simplified to a more
230−-- minimal instance:
241+-- datatype and declare a 'ToJSON' instance. If you require nothing other than
242+-- 'defaultOptions', it is sufficient to write (and this is the only
243+-- alternative where the default 'toJSON' implementation is sufficient):
231244--
232245-- @
233246-- {-\# LANGUAGE DeriveGeneric \#-}
@@ -236,35 +249,46 @@ genericLiftToEncoding opts te tel = gToEncoding opts proxyOne te tel . from1
236249--
237250-- data Coord = Coord { x :: Double, y :: Double } deriving 'Generic'
238251--
239−-- instance ToJSON Coord where
240−-- toEncoding = 'genericToEncoding' 'defaultOptions'
252+-- instance 'ToJSON' Coord where
253+-- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
241254-- @
242255--
243−-- Why do we provide an implementation for 'toEncoding' here? The
244−-- 'toEncoding' function is a relatively new addition to this class.
245−-- To allow users of older versions of this library to upgrade without
246−-- having to edit all of their instances or encounter surprising
247−-- incompatibilities, the default implementation of 'toEncoding' uses
248−-- 'toJSON'. This produces correct results, but since it performs an
249−-- intermediate conversion to a 'Value', it will be less efficient
250−-- than directly emitting an 'Encoding'. Our one-liner definition of
251−-- 'toEncoding' above bypasses the intermediate 'Value'.
252−--
253−-- If @DefaultSignatures@ doesn't give exactly the results you want,
254−-- you can customize the generic encoding with only a tiny amount of
255−-- effort, using 'genericToJSON' and 'genericToEncoding' with your
256−-- preferred 'Options':
256+-- If on the other hand you wish to customize the generic decoding, you have
257+-- to implement both methods:
257258--
258259-- @
259−-- instance ToJSON Coord where
260−-- toJSON = 'genericToJSON' 'defaultOptions'
261−-- toEncoding = 'genericToEncoding' 'defaultOptions'
260+-- customOptions = 'defaultOptions'
261+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
262+-- }
263+--
264+-- instance 'ToJSON' Coord where
265+-- 'toJSON' = 'genericToJSON' customOptions
266+-- 'toEncoding' = 'genericToEncoding' customOptions
262267-- @
268+--
269+-- Previous versions of this library only had the 'toJSON' method. Adding
270+-- 'toEncoding' had two reasons:
271+--
272+-- 1. toEncoding is more efficient for the common case that the output of
273+-- 'toJSON' is directly serialized to a @ByteString@.
274+-- Further, expressing either method in terms of the other would be
275+-- non-optimal.
276+--
277+-- 2. The choice of defaults allows a smooth transition for existing users:
278+-- Existing instances that do not define 'toEncoding' still
279+-- compile and have the correct semantics. This is ensured by making
280+-- the default implementation of 'toEncoding' use 'toJSON'. This produces
281+-- correct results, but since it performs an intermediate conversion to a
282+-- 'Value', it will be less efficient than directly emitting an 'Encoding'.
283+-- (this also means that specifying nothing more than
284+-- @instance ToJSON Coord@ would be sufficient as a generically decoding
285+-- instance, but there probably exists no good reason to not specify
286+-- 'toEncoding' in new instances.)
263287class ToJSON a where
264288 -- | Convert a Haskell value to a JSON-friendly intermediate type.
265289 toJSON :: a -> Value
266290
267− default toJSON :: (Generic a, GToJSON Zero (Rep a)) => a -> Value
291+ default toJSON :: (Generic a, GToJSON Value Zero (Rep a)) => a -> Value
268292 toJSON = genericToJSON defaultOptions
269293
270294 -- | Encode a Haskell value as JSON.
@@ -281,12 +305,12 @@ class ToJSON a where
281305 -- extension, and then have GHC generate a method body as follows.
282306 --
283307 -- @
284− -- instance ToJSON Coord where
285− -- toEncoding = 'genericToEncoding' 'defaultOptions'
308+ -- instance 'ToJSON' Coord where
309+ -- 'toEncoding' = 'genericToEncoding' 'defaultOptions'
286310 -- @
287311
288312 toEncoding :: a -> Encoding
289− toEncoding = E.Encoding . EB.encodeToBuilder . toJSON
313+ toEncoding = E.value . toJSON
290314 {-# INLINE toEncoding #-}
291315
292316 toJSONList :: [a] -> Value
@@ -307,33 +331,213 @@ class KeyValue kv where
307331 infixr 8 .=
308332
309333instance KeyValue Series where
310− name .= value = E.Value $ E.text name <> E.colon <> toEncoding value
334+ name .= value = E.pair name (toEncoding value)
311335 {-# INLINE (.=) #-}
312336
313337instance KeyValue Pair where
314338 name .= value = (name, toJSON value)
315339 {-# INLINE (.=) #-}
316340
341+-- | Constructs a singleton 'H.HashMap'. For calling functions that
342+-- demand an 'Object' for constructing objects. To be used in
343+-- conjunction with 'mconcat'. Prefer to use 'object' where possible.
344+instance KeyValue Object where
345+ name .= value = H.singleton name (toJSON value)
346+ {-# INLINE (.=) #-}
347+
317348-------------------------------------------------------------------------------
318349-- Classes and types for map keys
319350-------------------------------------------------------------------------------
320351
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':
356+--
357+-- >>> LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]
358+-- {"foo":"a"}
359+--
360+-- Since 'Int' also has a 'ToJSONKey' instance, we can similarly write:
361+--
362+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(5 :: Int, 'a')]
363+-- {"5":"a"}
364+--
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.
368+--
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.
373+--
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:
376+--
377+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(["foo","bar","baz" :: Text], 'a')]
378+-- [[["foo","bar","baz"],"a"]]
379+--
380+-- The default implementation of 'ToJSONKey' chooses this method of
381+-- encoding a key, using the 'ToJSON' instance of the type.
382+--
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:
389+--
390+-- > data Foo = Foo { fooAge :: Int, fooName :: Text }
391+-- > deriving (Eq,Ord,Generic)
392+-- > instance ToJSON Foo
393+-- > instance ToJSONKey Foo
394+--
395+-- That's it. We can now write:
396+--
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"]]
400+--
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:
404+--
405+-- > newtype RecordId = RecordId { getRecordId :: Text }
406+-- > deriving (Eq,Ord,ToJSONKey)
407+--
408+-- Then we may write:
409+--
410+-- >>> LBC8.putStrLn $ encode $ Map.fromList [(RecordId "abc",'a')]
411+-- {"abc":"a"}
412+--
413+-- Simple sum types are a final case worth considering. Suppose we have:
414+--
415+-- > data Color = Red | Green | Blue
416+-- > deriving (Show,Read,Eq,Ord)
417+--
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:
424+--
425+-- @
426+-- instance 'ToJSONKey' Color where
427+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
428+-- @
429+--
430+-- === __Low-level implementations__
431+--
432+-- The 'Show' instance can be used to help write 'ToJSONKey':
433+--
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
439+--
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:
443+--
444+-- > instance ToJSONKey Color where
445+-- > toJSONKey = toJSONKeyText (Text.pack . show)
446+--
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:
455+--
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
462+--
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.
467+
321468class ToJSONKey a where
469+ -- | Strategy for rendering the key for a map-like container.
322470 toJSONKey :: ToJSONKeyFunction a
323471 default toJSONKey :: ToJSON a => ToJSONKeyFunction a
324− toJSONKey = ToJSONKeyValue (toJSON, toEncoding)
472+ toJSONKey = ToJSONKeyValue toJSON toEncoding
473+
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.
325478 toJSONKeyList :: ToJSONKeyFunction [a]
326479 default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
327− toJSONKeyList = ToJSONKeyValue (toJSON, toEncoding)
480+ toJSONKeyList = ToJSONKeyValue toJSON toEncoding
328481
329482data ToJSONKeyFunction a
330− = ToJSONKeyText (a -> Text, a -> Encoding)
331− | ToJSONKeyValue (a -> Value, a -> Encoding)
483+ = ToJSONKeyText !(a -> Text) !(a -> Encoding' Text)
484+ -- ^ key is encoded to string, produces object
485+ | ToJSONKeyValue !(a -> Value) !(a -> Encoding)
486+ -- ^ key is encoded to value, produces array
332487
488+-- | Helper for creating textual keys.
489+--
490+-- @
491+-- instance 'ToJSONKey' MyKey where
492+-- 'toJSONKey' = 'toJSONKeyText' myKeyToText
493+-- where
494+-- myKeyToText = Text.pack . show -- or showt from text-show
495+-- @
496+toJSONKeyText :: (a -> Text) -> ToJSONKeyFunction a
497+toJSONKeyText f = ToJSONKeyText f (E.text . f)
498+
499+-- | TODO: should this be exported?
500+toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
501+toJSONKeyTextEnc e = ToJSONKeyText tot e
502+ where
503+ -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
504+ tot = T.dropAround (== '"')
505+ . T.decodeLatin1
506+ . L.toStrict
507+ . E.encodingToLazyByteString
508+ . e
509+
510+instance Contravariant ToJSONKeyFunction where
511+ contramap = contramapToJSONKeyFunction
512+
513+-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
333514contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
334515contramapToJSONKeyFunction h x = case x of
335− ToJSONKeyText (f,g) -> ToJSONKeyText (f . h, g . h)
336− ToJSONKeyValue (f,g) -> ToJSONKeyValue (f . h, g . h)
516+ ToJSONKeyText f g -> ToJSONKeyText (f . h) (g . h)
517+ ToJSONKeyValue f g -> ToJSONKeyValue (f . h) (g . h)
518+
519+-- 'toJSONKey' for 'Generic' types.
520+-- Deriving is supported for enumeration types, i.e. the sums of nullary
521+-- constructors. The names of constructors will be used as keys for JSON
522+-- objects.
523+--
524+-- See also 'genericFromJSONKey'.
525+--
526+-- === __Example__
527+--
528+-- @
529+-- data Color = Red | Green | Blue
530+-- deriving 'Generic'
531+--
532+-- instance 'ToJSONKey' Color where
533+-- 'toJSONKey' = 'genericToJSONKey' 'defaultJSONKeyOptions'
534+-- @
535+genericToJSONKey :: (Generic a, GToJSONKey (Rep a))
536+ => JSONKeyOptions -> ToJSONKeyFunction a
537+genericToJSONKey opts = toJSONKeyText (pack . keyModifier opts . getConName . from)
538+
539+class GetConName f => GToJSONKey f
540+instance GetConName f => GToJSONKey f
337541
338542-------------------------------------------------------------------------------
339543-- Lifings of FromJSON and ToJSON to unary and binary type constructors
@@ -347,7 +551,7 @@ contramapToJSONKeyFunction h x = case x of
347551--
348552-- * "Data.Aeson.TH" provides Template Haskell functions which will derive an
349553-- instance at compile time. The generated instance is optimized for your type
350−-- so will probably be more efficient than the following two options:
554+-- so it will probably be more efficient than the following option.
351555--
352556-- * The compiler can provide a default generic implementation for
353557-- 'toJSON1'.
@@ -365,23 +569,29 @@ contramapToJSONKeyFunction h x = case x of
365569--
366570-- data Pair = Pair { pairFst :: a, pairSnd :: b } deriving 'Generic1'
367571--
368−-- instance ToJSON a => ToJSON1 (Pair a)
572+-- instance 'ToJSON' a => 'ToJSON1' (Pair a)
369573-- @
370574--
371−-- If @DefaultSignatures@ doesn't give exactly the results you want,
575+-- If the default implementation doesn't give exactly the results you want,
372576-- you can customize the generic encoding with only a tiny amount of
373577-- effort, using 'genericLiftToJSON' and 'genericLiftToEncoding' with
374578-- your preferred 'Options':
375579--
376580-- @
377−-- instance ToJSON a => ToJSON1 (Pair a) where
378−-- liftToJSON = 'genericLiftToJSON' 'defaultOptions'
379−-- liftToEncoding = 'genericLiftToEncoding' 'defaultOptions'
581+-- customOptions = 'defaultOptions'
582+-- { 'fieldLabelModifier' = 'map' 'Data.Char.toUpper'
583+-- }
584+--
585+-- instance 'ToJSON' a => 'ToJSON1' (Pair a) where
586+-- 'liftToJSON' = 'genericLiftToJSON' customOptions
587+-- 'liftToEncoding' = 'genericLiftToEncoding' customOptions
380588-- @
589+--
590+-- See also 'ToJSON'.
381591class ToJSON1 f where
382592 liftToJSON :: (a -> Value) -> ([a] -> Value) -> f a -> Value
383593
384− default liftToJSON :: (Generic1 f, GToJSON One (Rep1 f))
594+ default liftToJSON :: (Generic1 f, GToJSON Value One (Rep1 f))
385595 => (a -> Value) -> ([a] -> Value) -> f a -> Value
386596 liftToJSON = genericLiftToJSON defaultOptions
387597
@@ -390,7 +600,7 @@ class ToJSON1 f where
390600
391601 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
392602
393− default liftToEncoding :: (Generic1 f, GToEncoding One (Rep1 f))
603+ default liftToEncoding :: (Generic1 f, GToJSON Encoding One (Rep1 f))
394604 => (a -> Encoding) -> ([a] -> Encoding)
395605 -> f a -> Encoding
396606 liftToEncoding = genericLiftToEncoding defaultOptions
@@ -438,9 +648,25 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
438648-- Encoding functions
439649-------------------------------------------------------------------------------
440650
651+-- | Helper function to use with 'liftToEncoding'.
652+-- Useful when writing own 'ToJSON1' instances.
653+--
654+-- @
655+-- newtype F a = F [a]
656+--
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
661+--
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
664+-- @
441665listEncoding :: (a -> Encoding) -> [a] -> Encoding
442666listEncoding = E.list
667+{-# INLINE listEncoding #-}
443668
669+-- | Helper function to use with 'liftToJSON', see 'listEncoding'.
444670listValue :: (a -> Value) -> [a] -> Value
445671listValue f = Array . V.fromList . map f
446672{-# INLINE listValue #-}
@@ -459,6 +685,11 @@ instance ToJSON1 [] where
459685 {-# INLINE liftToEncoding #-}
460686
461687instance (ToJSON a) => ToJSON [a] where
688+ {-# SPECIALIZE instance ToJSON String #-}
689+ {-# SPECIALIZE instance ToJSON [String] #-}
690+ {-# SPECIALIZE instance ToJSON [Array] #-}
691+ {-# SPECIALIZE instance ToJSON [Object] #-}
692+
462693 toJSON = toJSON1
463694 {-# INLINE toJSON #-}
464695
@@ -469,283 +700,260 @@ instance (ToJSON a) => ToJSON [a] where
469700-- Generic toJSON / toEncoding
470701-------------------------------------------------------------------------------
471702
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 #-}
707+
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 #-}
713+
714+instance ( ConsToJSON enc arity a
715+ , AllNullary (C1 c a) allNullary
716+ , SumToJSON enc arity (C1 c a) allNullary
717+ ) => GToJSON enc arity (D1 d (C1 c a)) where
718+ -- The option 'tagSingleConstructors' determines whether to wrap
719+ -- a single-constructor type.
720+ gToJSON opts targs
721+ | tagSingleConstructors opts = (unTagged :: Tagged allNullary enc -> enc)
722+ . sumToJSON opts targs
723+ . unM1
724+ | otherwise = consToJSON opts targs . unM1 . unM1
725+ {-# INLINE gToJSON #-}
726+
727+instance (ConsToJSON enc arity a) => GToJSON enc arity (C1 c a) where
728+ -- Constructors need to be encoded differently depending on whether they're
729+ -- a record or not. This distinction is made by 'consToJSON':
730+ gToJSON opts targs = consToJSON opts targs . unM1
731+ {-# INLINE gToJSON #-}
732+
733+instance ( AllNullary (a :+: b) allNullary
734+ , SumToJSON enc arity (a :+: b) allNullary
735+ ) => GToJSON enc arity (a :+: b)
736+ where
737+ -- If all constructors of a sum datatype are nullary and the
738+ -- 'allNullaryToStringTag' option is set they are encoded to
739+ -- strings. This distinction is made by 'sumToJSON':
740+ gToJSON opts targs = (unTagged :: Tagged allNullary enc -> enc)
741+ . sumToJSON opts targs
742+ {-# INLINE gToJSON #-}
743+
472744--------------------------------------------------------------------------------
473745-- Generic toJSON
474746
475−instance OVERLAPPABLE_ (GToJSON arity a) => GToJSON arity (M1 i c a) where
476− -- Meta-information, which is not handled elsewhere, is ignored:
477− gToJSON opts pa tj tjl = gToJSON opts pa tj tjl . unM1
747+-- Note: Refactoring 'ToJSON a' to 'ToJSON enc a' (and 'ToJSON1' similarly) is
748+-- possible but makes error messages a bit harder to understand for missing
749+-- instances.
478750
479−instance (ToJSON a) => GToJSON arity (K1 i a) where
480− -- Constant values are encoded using their ToJSON instance:
481− gToJSON _opts _ _ _ = toJSON . unK1
751+instance GToJSON Value arity V1 where
752+ -- Empty values do not exist, which makes the job of formatting them
753+ -- rather easy:
754+ gToJSON _ _ x = x `seq` error "case: V1"
755+ {-# INLINE gToJSON #-}
482756
483−instance GToJSON One Par1 where
484− -- Direct occurrences of the last type parameter are encoded with the
485− -- function passed in as an argument:
486− gToJSON _opts _ tj _ = tj . unPar1
757+instance ToJSON a => GToJSON Value arity (K1 i a) where
758+ -- Constant values are encoded using their ToJSON instance:
759+ gToJSON _opts _ = toJSON . unK1
760+ {-# INLINE gToJSON #-}
487761
488−instance (ToJSON1 f) => GToJSON One (Rec1 f) where
762+instance ToJSON1 f => GToJSON Value One (Rec1 f) where
489763 -- Recursive occurrences of the last type parameter are encoded using their
490764 -- ToJSON1 instance:
491− gToJSON _opts _ tj tjl = liftToJSON tj tjl . unRec1
765+ gToJSON _opts (To1Args tj tjl) = liftToJSON tj tjl . unRec1
766+ {-# INLINE gToJSON #-}
492767
493−instance GToJSON arity U1 where
768+instance GToJSON Value arity U1 where
494769 -- Empty constructors are encoded to an empty array:
495− gToJSON _opts _ _ _ _ = emptyArray
496−
497−instance (ConsToJSON arity a) => GToJSON arity (C1 c a) where
498− -- Constructors need to be encoded differently depending on whether they're
499− -- a record or not. This distinction is made by 'consToJSON':
500− gToJSON opts pa tj tjl = consToJSON opts pa tj tjl . unM1
770+ gToJSON _opts _ _ = emptyArray
771+ {-# INLINE gToJSON #-}
501772
502773instance ( WriteProduct arity a, WriteProduct arity b
503774 , ProductSize a, ProductSize b
504− ) => GToJSON arity (a :*: b) where
775+ ) => GToJSON Value arity (a :*: b)
776+ where
505777 -- Products are encoded to an array. Here we allocate a mutable vector of
506778 -- the same size as the product and write the product's elements to it using
507779 -- 'writeProduct':
508− gToJSON opts pa tj tjl p =
780+ gToJSON opts targs p =
509781 Array $ V.create $ do
510782 mv <- VM.unsafeNew lenProduct
511− writeProduct opts pa mv 0 lenProduct tj tjl p
783+ writeProduct opts targs mv 0 lenProduct p
512784 return mv
513785 where
514786 lenProduct = (unTagged2 :: Tagged2 (a :*: b) Int -> Int)
515787 productSize
788+ {-# INLINE gToJSON #-}
516789
517−instance ( AllNullary (a :+: b) allNullary
518− , SumToJSON arity (a :+: b) allNullary
519− ) => GToJSON arity (a :+: b) where
520− -- If all constructors of a sum datatype are nullary and the
521− -- 'allNullaryToStringTag' option is set they are encoded to
522− -- strings. This distinction is made by 'sumToJSON':
523− gToJSON opts pa tj tjl = (unTagged :: Tagged allNullary Value -> Value)
524− . sumToJSON opts pa tj tjl
525−
526−instance (ToJSON1 f, GToJSON One g) => GToJSON One (f :.: g) where
790+instance ( ToJSON1 f
791+ , GToJSON Value One g
792+ ) => GToJSON Value One (f :.: g)
793+ where
527794 -- If an occurrence of the last type parameter is nested inside two
528795 -- composed types, it is encoded by using the outermost type's ToJSON1
529796 -- instance to generically encode the innermost type:
530− gToJSON opts pa tj tjl =
531− let gtj = gToJSON opts pa tj tjl in
797+ gToJSON opts targs =
798+ let gtj = gToJSON opts targs in
532799 liftToJSON gtj (listValue gtj) . unComp1
800+ {-# INLINE gToJSON #-}
533801
534802--------------------------------------------------------------------------------
535803-- Generic toEncoding
536804
537−instance OVERLAPPABLE_ (GToEncoding arity a) => GToEncoding arity (M1 i c a) where
538− -- Meta-information, which is not handled elsewhere, is ignored:
539− gToEncoding opts pa te tel = gToEncoding opts pa te tel . unM1
540−
541−instance (ToJSON a) => GToEncoding arity (K1 i a) where
805+instance ToJSON a => GToJSON Encoding arity (K1 i a) where
542806 -- Constant values are encoded using their ToJSON instance:
543− gToEncoding _opts _ _ _ = toEncoding . unK1
807+ gToJSON _opts _ = toEncoding . unK1
808+ {-# INLINE gToJSON #-}
544809
545−instance GToEncoding One Par1 where
546− -- Direct occurrences of the last type parameter are encoded with the
547− -- function passed in as an argument:
548− gToEncoding _opts _ te _ = te . unPar1
549−
550−instance (ToJSON1 f) => GToEncoding One (Rec1 f) where
810+instance ToJSON1 f => GToJSON Encoding One (Rec1 f) where
551811 -- Recursive occurrences of the last type parameter are encoded using their
552812 -- ToEncoding1 instance:
553− gToEncoding _opts _ te tel = liftToEncoding te tel . unRec1
813+ gToJSON _opts (To1Args te tel) = liftToEncoding te tel . unRec1
814+ {-# INLINE gToJSON #-}
554815
555−instance GToEncoding arity U1 where
816+instance GToJSON Encoding arity U1 where
556817 -- Empty constructors are encoded to an empty array:
557− gToEncoding _opts _ _ _ _ = E.emptyArray_
558−
559−instance (ConsToEncoding arity a) => GToEncoding arity (C1 c a) where
560− -- Constructors need to be encoded differently depending on whether they're
561− -- a record or not. This distinction is made by 'consToEncoding':
562− gToEncoding opts pa te tel = consToEncoding opts pa te tel . unM1
818+ gToJSON _opts _ _ = E.emptyArray_
819+ {-# INLINE gToJSON #-}
563820
564821instance ( EncodeProduct arity a
565822 , EncodeProduct arity b
566− ) => GToEncoding arity (a :*: b) where
823+ ) => GToJSON Encoding arity (a :*: b)
824+ where
567825 -- Products are encoded to an array. Here we allocate a mutable vector of
568826 -- the same size as the product and write the product's elements to it using
569827 -- 'encodeProduct':
570− gToEncoding opts pa te tel p = E.tuple $ encodeProduct opts pa te tel p
571−
572−instance ( AllNullary (a :+: b) allNullary
573− , SumToEncoding arity (a :+: b) allNullary
574− ) => GToEncoding arity (a :+: b) where
575− -- If all constructors of a sum datatype are nullary and the
576− -- 'allNullaryToStringTag' option is set they are encoded to
577− -- strings. This distinction is made by 'sumToEncoding':
578− gToEncoding opts pa te tel
579− = (unTagged :: Tagged allNullary Encoding -> Encoding)
580− . sumToEncoding opts pa te tel
828+ gToJSON opts targs p = E.list E.retagEncoding [encodeProduct opts targs p]
829+ {-# INLINE gToJSON #-}
581830
582−instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
831+instance ( ToJSON1 f
832+ , GToJSON Encoding One g
833+ ) => GToJSON Encoding One (f :.: g)
834+ where
583835 -- If an occurrence of the last type parameter is nested inside two
584836 -- composed types, it is encoded by using the outermost type's ToJSON1
585837 -- instance to generically encode the innermost type:
586− gToEncoding opts pa te tel =
587− let gte = gToEncoding opts pa te tel in
838+ gToJSON opts targs =
839+ let gte = gToJSON opts targs in
588840 liftToEncoding gte (listEncoding gte) . unComp1
841+ {-# INLINE gToJSON #-}
589842
590843--------------------------------------------------------------------------------
591844
592−class SumToJSON arity f allNullary where
593− sumToJSON :: Options -> Proxy arity
594− -> (a -> Value) -> ([a] -> Value)
595− -> f a -> Tagged allNullary Value
596−
597−instance ( GetConName f
598− , TaggedObjectPairs arity f
599− , ObjectWithSingleFieldObj arity f
600− , TwoElemArrayObj arity f
601− ) => SumToJSON arity f True where
602− sumToJSON opts pa tj tjl
603− | allNullaryToStringTag opts = Tagged . String . pack
845+class SumToJSON enc arity f allNullary where
846+ sumToJSON :: Options -> ToArgs enc arity a
847+ -> f a -> Tagged allNullary enc
848+
849+instance ( GetConName f
850+ , FromString enc
851+ , TaggedObject enc arity f
852+ , SumToJSON' ObjectWithSingleField enc arity f
853+ , SumToJSON' TwoElemArray enc arity f
854+ , SumToJSON' UntaggedValue enc arity f
855+ ) => SumToJSON enc arity f True
856+ where
857+ sumToJSON opts targs
858+ | allNullaryToStringTag opts = Tagged . fromString
604859 . constructorTagModifier opts . getConName
605− | otherwise = Tagged . nonAllNullarySumToJSON opts pa tj tjl
606−
607−instance ( TwoElemArrayObj arity f
608− , TaggedObjectPairs arity f
609− , ObjectWithSingleFieldObj arity f
610− ) => SumToJSON arity f False where
611− sumToJSON opts pa tj tjl = Tagged . nonAllNullarySumToJSON opts pa tj tjl
612−
613−nonAllNullarySumToJSON :: ( TwoElemArrayObj arity f
614− , TaggedObjectPairs arity f
615− , ObjectWithSingleFieldObj arity f
616− ) => Options -> Proxy arity
617− -> (a -> Value) -> ([a] -> Value)
618− -> f a -> Value
619−nonAllNullarySumToJSON opts pa tj tjl =
860+ | otherwise = Tagged . nonAllNullarySumToJSON opts targs
861+
862+instance ( TaggedObject enc arity f
863+ , SumToJSON' ObjectWithSingleField enc arity f
864+ , SumToJSON' TwoElemArray enc arity f
865+ , SumToJSON' UntaggedValue enc arity f
866+ ) => SumToJSON enc arity f False
867+ where
868+ sumToJSON opts targs = Tagged . nonAllNullarySumToJSON opts targs
869+
870+nonAllNullarySumToJSON :: ( TaggedObject enc arity f
871+ , SumToJSON' ObjectWithSingleField enc arity f
872+ , SumToJSON' TwoElemArray enc arity f
873+ , SumToJSON' UntaggedValue enc arity f
874+ ) => Options -> ToArgs enc arity a
875+ -> f a -> enc
876+nonAllNullarySumToJSON opts targs =
620877 case sumEncoding opts of
621− TaggedObject{..} ->
622− object . taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl
623− ObjectWithSingleField -> Object . objectWithSingleFieldObj opts pa tj tjl
624− TwoElemArray -> Array . twoElemArrayObj opts pa tj tjl
625−
626−--------------------------------------------------------------------------------
627878
628−class SumToEncoding arity f allNullary where
629− sumToEncoding :: Options -> Proxy arity
630− -> (a -> Encoding) -> ([a] -> Encoding)
631− -> f a -> Tagged allNullary Encoding
632−
633−instance ( GetConName f
634− , TaggedObjectEnc arity f
635− , ObjectWithSingleFieldEnc arity f
636− , TwoElemArrayEnc arity f
637− ) => SumToEncoding arity f True where
638− sumToEncoding opts pa te tel
639− | allNullaryToStringTag opts = Tagged . toEncoding .
640− constructorTagModifier opts . getConName
641− | otherwise = Tagged . nonAllNullarySumToEncoding opts pa te tel
642−
643−instance ( TwoElemArrayEnc arity f
644− , TaggedObjectEnc arity f
645− , ObjectWithSingleFieldEnc arity f
646− ) => SumToEncoding arity f False where
647− sumToEncoding opts pa te tel = Tagged . nonAllNullarySumToEncoding opts pa te tel
648−
649−nonAllNullarySumToEncoding :: ( TwoElemArrayEnc arity f
650− , TaggedObjectEnc arity f
651− , ObjectWithSingleFieldEnc arity f
652− ) => Options -> Proxy arity
653− -> (a -> Encoding) -> ([a] -> Encoding)
654− -> f a -> Encoding
655−nonAllNullarySumToEncoding opts pa te tel =
656− case sumEncoding opts of
657879 TaggedObject{..} ->
658− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel
659− ObjectWithSingleField -> objectWithSingleFieldEnc opts pa te tel
660− TwoElemArray -> twoElemArrayEnc opts pa te tel
880+ taggedObject opts targs tagFieldName contentsFieldName
661881
662−--------------------------------------------------------------------------------
882+ ObjectWithSingleField ->
883+ (unTagged :: Tagged ObjectWithSingleField enc -> enc)
884+ . sumToJSON' opts targs
663885
664−class TaggedObjectPairs arity f where
665− taggedObjectPairs :: Options -> Proxy arity
666− -> String -> String
667− -> (a -> Value) -> ([a] -> Value)
668− -> f a -> [Pair]
669−
670−instance ( TaggedObjectPairs arity a
671− , TaggedObjectPairs arity b
672− ) => TaggedObjectPairs arity (a :+: b) where
673− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (L1 x) =
674− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
675− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl (R1 x) =
676− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl x
677−
678−instance ( IsRecord a isRecord
679− , TaggedObjectPairs' arity a isRecord
680− , Constructor c
681− ) => TaggedObjectPairs arity (C1 c a) where
682− taggedObjectPairs opts pa tagFieldName contentsFieldName tj tjl =
683− (pack tagFieldName .= constructorTagModifier opts
684− (conName (undefined :: t c a p)) :) .
685− (unTagged :: Tagged isRecord [Pair] -> [Pair]) .
686− taggedObjectPairs' opts pa contentsFieldName tj tjl . unM1
886+ TwoElemArray ->
887+ (unTagged :: Tagged TwoElemArray enc -> enc)
888+ . sumToJSON' opts targs
687889
688−class TaggedObjectPairs' arity f isRecord where
689− taggedObjectPairs' :: Options -> Proxy arity
690− -> String -> (a -> Value) -> ([a] -> Value)
691− -> f a -> Tagged isRecord [Pair]
890+ UntaggedValue ->
891+ (unTagged :: Tagged UntaggedValue enc -> enc)
892+ . sumToJSON' opts targs
692893
693−instance OVERLAPPING_ TaggedObjectPairs' arity U1 False where
694− taggedObjectPairs' _ _ _ _ _ _ = Tagged []
894+--------------------------------------------------------------------------------
895+
896+class FromString enc where
897+ fromString :: String -> enc
695898
696−instance (RecordToPairs arity f) => TaggedObjectPairs' arity f True where
697− taggedObjectPairs' opts pa _ tj tjl =
698− Tagged . toList . recordToPairs opts pa tj tjl
899+instance FromString Encoding where
900+ fromString = toEncoding
699901
700−instance (GToJSON arity f) => TaggedObjectPairs' arity f False where
701− taggedObjectPairs' opts pa contentsFieldName tj tjl =
702− Tagged . (:[]) . (pack contentsFieldName .=) . gToJSON opts pa tj tjl
902+instance FromString Value where
903+ fromString = String . pack
703904
704905--------------------------------------------------------------------------------
705906
706−class TaggedObjectEnc arity f where
707− taggedObjectEnc :: Options -> Proxy arity
708− -> String -> String
709− -> (a -> Encoding) -> ([a] -> Encoding)
710− -> f a -> Encoding
711−
712−instance ( TaggedObjectEnc arity a
713− , TaggedObjectEnc arity b
714− ) => TaggedObjectEnc arity (a :+: b) where
715− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (L1 x) =
716− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
717− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel (R1 x) =
718− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel x
719−
720−instance ( IsRecord a isRecord
721− , TaggedObjectEnc' arity a isRecord
907+class TaggedObject enc arity f where
908+ taggedObject :: Options -> ToArgs enc arity a
909+ -> String -> String
910+ -> f a -> enc
911+
912+instance ( TaggedObject enc arity a
913+ , TaggedObject enc arity b
914+ ) => TaggedObject enc arity (a :+: b)
915+ where
916+ taggedObject opts targs tagFieldName contentsFieldName (L1 x) =
917+ taggedObject opts targs tagFieldName contentsFieldName x
918+ taggedObject opts targs tagFieldName contentsFieldName (R1 x) =
919+ taggedObject opts targs tagFieldName contentsFieldName x
920+
921+instance ( IsRecord a isRecord
922+ , TaggedObject' enc pairs arity a isRecord
923+ , FromPairs enc pairs
924+ , FromString enc
925+ , KeyValuePair enc pairs
722926 , Constructor c
723− ) => TaggedObjectEnc arity (C1 c a) where
724− taggedObjectEnc opts pa tagFieldName contentsFieldName te tel v =
725− E.openCurly <>
726− (toEncoding tagFieldName <>
727− E.colon <>
728− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) <>
729− ((unTagged :: Tagged isRecord Encoding -> Encoding) .
730− taggedObjectEnc' opts pa contentsFieldName te tel . unM1 $ v) <>
731− E.closeCurly
732−
733−class TaggedObjectEnc' arity f isRecord where
734− taggedObjectEnc' :: Options -> Proxy arity
735− -> String -> (a -> Encoding) -> ([a] -> Encoding)
736− -> f a -> Tagged isRecord Encoding
737−
738−instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
739− taggedObjectEnc' _ _ _ _ _ _ = Tagged mempty
740−
741−instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
742− taggedObjectEnc' opts pa _ te tel = Tagged . (E.comma <>) . fst
743− . recordToEncoding opts pa te tel
744−
745−instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
746− taggedObjectEnc' opts pa contentsFieldName te tel =
747− Tagged . (\z -> E.comma <> toEncoding contentsFieldName <> E.colon <> z) .
748− gToEncoding opts pa te tel
927+ ) => TaggedObject enc arity (C1 c a)
928+ where
929+ taggedObject opts targs tagFieldName contentsFieldName =
930+ fromPairs . mappend tag . contents
931+ where
932+ tag = tagFieldName `pair`
933+ (fromString (constructorTagModifier opts (conName (undefined :: t c a p)))
934+ :: enc)
935+ contents =
936+ (unTagged :: Tagged isRecord pairs -> pairs) .
937+ taggedObject' opts targs contentsFieldName . unM1
938+
939+class TaggedObject' enc pairs arity f isRecord where
940+ taggedObject' :: Options -> ToArgs enc arity a
941+ -> String -> f a -> Tagged isRecord pairs
942+
943+instance ( GToJSON enc arity f
944+ , KeyValuePair enc pairs
945+ ) => TaggedObject' enc pairs arity f False
946+ where
947+ taggedObject' opts targs contentsFieldName =
948+ Tagged . (contentsFieldName `pair`) . gToJSON opts targs
949+
950+instance OVERLAPPING_ Monoid pairs => TaggedObject' enc pairs arity U1 False where
951+ taggedObject' _ _ _ _ = Tagged mempty
952+
953+instance ( RecordToPairs enc pairs arity f
954+ ) => TaggedObject' enc pairs arity f True
955+ where
956+ taggedObject' opts targs _ = Tagged . recordToPairs opts targs
749957
750958--------------------------------------------------------------------------------
751959
@@ -760,283 +968,242 @@ instance (GetConName a, GetConName b) => GetConName (a :+: b) where
760968instance (Constructor c) => GetConName (C1 c a) where
761969 getConName = conName
762970
971+-- For genericToJSONKey
972+instance GetConName a => GetConName (D1 d a) where
973+ getConName (M1 x) = getConName x
974+
975+--------------------------------------------------------------------------------
976+
977+-- Reflection of SumEncoding variants
978+
979+data ObjectWithSingleField
980+data TwoElemArray
981+data UntaggedValue
982+
763983--------------------------------------------------------------------------------
764984
765−class TwoElemArrayObj arity f where
766− twoElemArrayObj :: Options -> Proxy arity
767− -> (a -> Value) -> ([a] -> Value)
768− -> f a -> V.Vector Value
985+class SumToJSON' s enc arity f where
986+ sumToJSON' :: Options -> ToArgs enc arity a
987+ -> f a -> Tagged s enc
988+
989+instance ( SumToJSON' s enc arity a
990+ , SumToJSON' s enc arity b
991+ ) => SumToJSON' s enc arity (a :+: b)
992+ where
993+ sumToJSON' opts targs (L1 x) = sumToJSON' opts targs x
994+ sumToJSON' opts targs (R1 x) = sumToJSON' opts targs x
769995
770−instance ( TwoElemArrayObj arity a
771− , TwoElemArrayObj arity b
772− ) => TwoElemArrayObj arity (a :+: b) where
773− twoElemArrayObj opts pa tj tjl (L1 x) = twoElemArrayObj opts pa tj tjl x
774− twoElemArrayObj opts pa tj tjl (R1 x) = twoElemArrayObj opts pa tj tjl x
996+--------------------------------------------------------------------------------
775997
776−instance ( GToJSON arity a
777− , ConsToJSON arity a
998+instance ( GToJSON Value arity a
999+ , ConsToJSON Value arity a
7781000 , Constructor c
779− ) => TwoElemArrayObj arity (C1 c a) where
780− twoElemArrayObj opts pa tj tjl x = V.create $ do
1001+ ) => SumToJSON' TwoElemArray Value arity (C1 c a) where
1002+ sumToJSON' opts targs x = Tagged $ Array $ V.create $ do
7811003 mv <- VM.unsafeNew 2
7821004 VM.unsafeWrite mv 0 $ String $ pack $ constructorTagModifier opts
7831005 $ conName (undefined :: t c a p)
784− VM.unsafeWrite mv 1 $ gToJSON opts pa tj tjl x
1006+ VM.unsafeWrite mv 1 $ gToJSON opts targs x
7851007 return mv
7861008
7871009--------------------------------------------------------------------------------
7881010
789−class TwoElemArrayEnc arity f where
790− twoElemArrayEnc :: Options -> Proxy arity
791− -> (a -> Encoding) -> ([a] -> Encoding)
792− -> f a -> Encoding
793−
794−instance ( TwoElemArrayEnc arity a
795− , TwoElemArrayEnc arity b
796− ) => TwoElemArrayEnc arity (a :+: b) where
797− twoElemArrayEnc opts pa te tel (L1 x) = twoElemArrayEnc opts pa te tel x
798− twoElemArrayEnc opts pa te tel (R1 x) = twoElemArrayEnc opts pa te tel x
799−
800−instance ( GToEncoding arity a
801− , ConsToEncoding arity a
1011+instance ( GToJSON Encoding arity a
1012+ , ConsToJSON Encoding arity a
8021013 , Constructor c
803− ) => TwoElemArrayEnc arity (C1 c a) where
804− twoElemArrayEnc opts pa te tel x = E.tuple $
805− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
806− gToEncoding opts pa te tel x
1014+ ) => SumToJSON' TwoElemArray Encoding arity (C1 c a)
1015+ where
1016+ sumToJSON' opts targs x = Tagged $ E.list id
1017+ [ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))
1018+ , gToJSON opts targs x
1019+ ]
8071020
8081021--------------------------------------------------------------------------------
8091022
810−class ConsToJSON arity f where
811− consToJSON :: Options -> Proxy arity
812− -> (a -> Value) -> ([a] -> Value)
813− -> f a -> Value
814−
815−class ConsToJSON' arity f isRecord where
816− consToJSON' :: Options -> Proxy arity
817− -> Bool -- ^ Are we a record with one field?
818− -> (a -> Value) -> ([a] -> Value)
819− -> f a -> Tagged isRecord Value
820−
821−instance ( IsRecord f isRecord
822− , ConsToJSON' arity f isRecord
823− ) => ConsToJSON arity f where
824− consToJSON opts pa tj tjl =
825− (unTagged :: Tagged isRecord Value -> Value)
826− . consToJSON' opts pa (isUnary (undefined :: f a)) tj tjl
827−
828−instance (RecordToPairs arity f) => ConsToJSON' arity f True where
829− consToJSON' opts pa isUn tj tjl f = let
830− vals = toList $ recordToPairs opts pa tj tjl f
831− in case (unwrapUnaryRecords opts,isUn,vals) of
832− (True,True,[(_,val)]) -> Tagged val
833− _ -> Tagged $ object vals
834−
835−instance GToJSON arity f => ConsToJSON' arity f False where
836− consToJSON' opts pa _ tj tjl = Tagged . gToJSON opts pa tj tjl
1023+class ConsToJSON enc arity f where
1024+ consToJSON :: Options -> ToArgs enc arity a
1025+ -> f a -> enc
8371026
838−--------------------------------------------------------------------------------
839−
840−class ConsToEncoding arity f where
841− consToEncoding :: Options -> Proxy arity
842− -> (a -> Encoding) -> ([a] -> Encoding)
843− -> f a -> Encoding
844−
845−class ConsToEncoding' arity f isRecord where
846− consToEncoding' :: Options -> Proxy arity
847− -> Bool -- ^ Are we a record with one field?
848− -> (a -> Encoding) -> ([a] -> Encoding)
849− -> f a -> Tagged isRecord Encoding
1027+class ConsToJSON' enc arity f isRecord where
1028+ consToJSON' :: Options -> ToArgs enc arity a
1029+ -> f a -> Tagged isRecord enc
8501030
8511031instance ( IsRecord f isRecord
852− , ConsToEncoding' arity f isRecord
853− ) => ConsToEncoding arity f where
854− consToEncoding opts pa te tel =
855− (unTagged :: Tagged isRecord Encoding -> Encoding)
856− . consToEncoding' opts pa (isUnary (undefined :: f a)) te tel
857−
858−instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
859− consToEncoding' opts pa isUn te tel x =
860− let (enc, mbVal) = recordToEncoding opts pa te tel x
861− in case (unwrapUnaryRecords opts, isUn, mbVal) of
862− (True, True, Just val) -> Tagged val
863− _ -> Tagged $ E.wrapObject enc
864−
865−instance GToEncoding arity f => ConsToEncoding' arity f False where
866− consToEncoding' opts pa _ te tel = Tagged . gToEncoding opts pa te tel
867−
868−--------------------------------------------------------------------------------
869−
870−class RecordToPairs arity f where
871− recordToPairs :: Options -> Proxy arity
872− -> (a -> Value) -> ([a] -> Value)
873− -> f a -> DList Pair
874−
875−instance ( RecordToPairs arity a
876− , RecordToPairs arity b
877− ) => RecordToPairs arity (a :*: b) where
878− recordToPairs opts pa tj tjl (a :*: b) = recordToPairs opts pa tj tjl a <>
879− recordToPairs opts pa tj tjl b
880−
881−instance (Selector s, GToJSON arity a) => RecordToPairs arity (S1 s a) where
882− recordToPairs = fieldToPair
883−
884−instance OVERLAPPING_ (Selector s, ToJSON a) =>
885− RecordToPairs arity (S1 s (K1 i (Maybe a))) where
886− recordToPairs opts _ _ _ (M1 k1) | omitNothingFields opts
887− , K1 Nothing <- k1 = DList.empty
888− recordToPairs opts pa tj tjl m1 = fieldToPair opts pa tj tjl m1
889−
890−fieldToPair :: (Selector s, GToJSON arity a)
891− => Options -> Proxy arity
892− -> (p -> Value) -> ([p] -> Value)
893− -> S1 s a p -> DList Pair
894−fieldToPair opts pa tj tjl m1 = pure ( pack $ fieldLabelModifier opts $ selName m1
895− , gToJSON opts pa tj tjl (unM1 m1)
896− )
1032+ , ConsToJSON' enc arity f isRecord
1033+ ) => ConsToJSON enc arity f
1034+ where
1035+ consToJSON opts targs =
1036+ (unTagged :: Tagged isRecord enc -> enc)
1037+ . consToJSON' opts targs
1038+ {-# INLINE consToJSON #-}
1039+
1040+instance OVERLAPPING_
1041+ ( RecordToPairs enc pairs arity (S1 s f)
1042+ , FromPairs enc pairs
1043+ , GToJSON enc arity f
1044+ ) => ConsToJSON' enc arity (S1 s f) True
1045+ where
1046+ consToJSON' opts targs
1047+ | unwrapUnaryRecords opts = Tagged . gToJSON opts targs
1048+ | otherwise = Tagged . fromPairs . recordToPairs opts targs
1049+ {-# INLINE consToJSON' #-}
1050+
1051+instance ( RecordToPairs enc pairs arity f
1052+ , FromPairs enc pairs
1053+ ) => ConsToJSON' enc arity f True
1054+ where
1055+ consToJSON' opts targs = Tagged . fromPairs . recordToPairs opts targs
1056+ {-# INLINE consToJSON' #-}
1057+
1058+instance GToJSON enc arity f => ConsToJSON' enc arity f False where
1059+ consToJSON' opts targs = Tagged . gToJSON opts targs
1060+ {-# INLINE consToJSON' #-}
8971061
8981062--------------------------------------------------------------------------------
8991063
900−class RecordToEncoding arity f where
1064+class RecordToPairs enc pairs arity f where
9011065 -- 1st element: whole thing
9021066 -- 2nd element: in case the record has only 1 field, just the value
9031067 -- of the field (without the key); 'Nothing' otherwise
904− recordToEncoding :: Options -> Proxy arity
905− -> (a -> Encoding) -> ([a] -> Encoding)
906− -> f a -> (Encoding, Maybe Encoding)
907−
908−instance ( RecordToEncoding arity a
909− , RecordToEncoding arity b
910− ) => RecordToEncoding arity (a :*: b) where
911− recordToEncoding opts pa te tel (a :*: b) | omitNothingFields opts =
912− (mconcat $ intersperse E.comma $
913− filter (not . E.nullEncoding)
914− [ fst (recordToEncoding opts pa te tel a)
915− , fst (recordToEncoding opts pa te tel b) ]
916− , Nothing)
917− recordToEncoding opts pa te tel (a :*: b) =
918− (fst (recordToEncoding opts pa te tel a) <> E.comma <>
919− fst (recordToEncoding opts pa te tel b),
920− Nothing)
921−
922−instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
923− recordToEncoding = fieldToEncoding
924−
925−instance OVERLAPPING_ (Selector s, ToJSON a) =>
926− RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
927− recordToEncoding opts _ _ _ (M1 k1) | omitNothingFields opts
928− , K1 Nothing <- k1 = (mempty, Nothing)
929− recordToEncoding opts pa te tel m1 = fieldToEncoding opts pa te tel m1
930−
931−fieldToEncoding :: (Selector s, GToEncoding arity a)
932− => Options -> Proxy arity
933− -> (p -> Encoding) -> ([p] -> Encoding)
934− -> S1 s a p -> (Encoding, Maybe Encoding)
935−fieldToEncoding opts pa te tel m1 =
936− let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
937− valueBuilder = gToEncoding opts pa te tel (unM1 m1)
938− in (keyBuilder <> E.colon <> valueBuilder, Just valueBuilder)
1068+ recordToPairs :: Options -> ToArgs enc arity a
1069+ -> f a -> pairs
1070+
1071+instance ( Monoid pairs
1072+ , RecordToPairs enc pairs arity a
1073+ , RecordToPairs enc pairs arity b
1074+ ) => RecordToPairs enc pairs arity (a :*: b)
1075+ where
1076+ recordToPairs opts (targs :: ToArgs enc arity p) (a :*: b) =
1077+ pairsOf a `mappend` pairsOf b
1078+ where
1079+ pairsOf :: (RecordToPairs enc pairs arity f) => f p -> pairs
1080+ pairsOf = recordToPairs opts targs
1081+ {-# INLINE recordToPairs #-}
1082+
1083+instance ( Selector s
1084+ , GToJSON enc arity a
1085+ , KeyValuePair enc pairs
1086+ ) => RecordToPairs enc pairs arity (S1 s a)
1087+ where
1088+ recordToPairs = fieldToPair
1089+ {-# INLINE recordToPairs #-}
1090+
1091+instance INCOHERENT_
1092+ ( Selector s
1093+ , GToJSON enc arity (K1 i (Maybe a))
1094+ , KeyValuePair enc pairs
1095+ , Monoid pairs
1096+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Maybe a)))
1097+ where
1098+ recordToPairs opts _ (M1 k1) | omitNothingFields opts
1099+ , K1 Nothing <- k1 = mempty
1100+ recordToPairs opts targs m1 = fieldToPair opts targs m1
1101+ {-# INLINE recordToPairs #-}
1102+
1103+instance INCOHERENT_
1104+ ( Selector s
1105+ , GToJSON enc arity (K1 i (Maybe a))
1106+ , KeyValuePair enc pairs
1107+ , Monoid pairs
1108+ ) => RecordToPairs enc pairs arity (S1 s (K1 i (Semigroup.Option a)))
1109+ where
1110+ recordToPairs opts targs = recordToPairs opts targs . unwrap
1111+ where
1112+ unwrap :: S1 s (K1 i (Semigroup.Option a)) p -> S1 s (K1 i (Maybe a)) p
1113+ unwrap (M1 (K1 (Semigroup.Option a))) = M1 (K1 a)
1114+ {-# INLINE recordToPairs #-}
1115+
1116+fieldToPair :: (Selector s
1117+ , GToJSON enc arity a
1118+ , KeyValuePair enc pairs)
1119+ => Options -> ToArgs enc arity p
1120+ -> S1 s a p -> pairs
1121+fieldToPair opts targs m1 =
1122+ let key = fieldLabelModifier opts (selName m1)
1123+ value = gToJSON opts targs (unM1 m1)
1124+ in key `pair` value
1125+{-# INLINE fieldToPair #-}
9391126
9401127--------------------------------------------------------------------------------
9411128
9421129class WriteProduct arity f where
9431130 writeProduct :: Options
944− -> Proxy arity
1131+ -> ToArgs Value arity a
9451132 -> VM.MVector s Value
9461133 -> Int -- ^ index
9471134 -> Int -- ^ length
948− -> (a -> Value)
949− -> ([a] -> Value)
9501135 -> f a
9511136 -> ST s ()
9521137
9531138instance ( WriteProduct arity a
9541139 , WriteProduct arity b
9551140 ) => WriteProduct arity (a :*: b) where
956− writeProduct opts pa mv ix len tj tjl (a :*: b) = do
957− writeProduct opts pa mv ix lenL tj tjl a
958− writeProduct opts pa mv ixR lenR tj tjl b
1141+ writeProduct opts targs mv ix len (a :*: b) = do
1142+ writeProduct opts targs mv ix lenL a
1143+ writeProduct opts targs mv ixR lenR b
9591144 where
9601145 lenL = len `unsafeShiftR` 1
9611146 lenR = len - lenL
9621147 ixR = ix + lenL
1148+ {-# INLINE writeProduct #-}
9631149
964−instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
965− writeProduct opts pa mv ix _ tj tjl =
966− VM.unsafeWrite mv ix . gToJSON opts pa tj tjl
1150+instance OVERLAPPABLE_ (GToJSON Value arity a) => WriteProduct arity a where
1151+ writeProduct opts targs mv ix _ =
1152+ VM.unsafeWrite mv ix . gToJSON opts targs
1153+ {-# INLINE writeProduct #-}
9671154
9681155--------------------------------------------------------------------------------
9691156
9701157class EncodeProduct arity f where
971− encodeProduct :: Options -> Proxy arity
972− -> (a -> Encoding) -> ([a] -> Encoding)
973− -> f a -> Encoding
1158+ encodeProduct :: Options -> ToArgs Encoding arity a
1159+ -> f a -> Encoding' E.InArray
9741160
9751161instance ( EncodeProduct arity a
9761162 , EncodeProduct arity b
9771163 ) => EncodeProduct arity (a :*: b) where
978− encodeProduct opts pa te tel (a :*: b) | omitNothingFields opts =
979− mconcat $ intersperse E.comma $
1164+ encodeProduct opts targs (a :*: b) | omitNothingFields opts =
1165+ E.econcat $ intersperse E.comma $
9801166 filter (not . E.nullEncoding)
981− [encodeProduct opts pa te tel a, encodeProduct opts pa te tel b]
982− encodeProduct opts pa te tel (a :*: b) = encodeProduct opts pa te tel a <>
983− E.comma <>
984− encodeProduct opts pa te tel b
1167+ [encodeProduct opts targs a, encodeProduct opts targs b]
1168+ encodeProduct opts targs (a :*: b) =
1169+ encodeProduct opts targs a >*<
1170+ encodeProduct opts targs b
1171+ {-# INLINE encodeProduct #-}
9851172
986−instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
987− encodeProduct opts = gToEncoding opts
1173+instance OVERLAPPABLE_ (GToJSON Encoding arity a) => EncodeProduct arity a where
1174+ encodeProduct opts targs a = E.retagEncoding $ gToJSON opts targs a
1175+ {-# INLINE encodeProduct #-}
9881176
9891177--------------------------------------------------------------------------------
9901178
991−class ObjectWithSingleFieldObj arity f where
992− objectWithSingleFieldObj :: Options -> Proxy arity
993− -> (a -> Value) -> ([a] -> Value)
994− -> f a -> Object
995−
996−instance ( ObjectWithSingleFieldObj arity a
997− , ObjectWithSingleFieldObj arity b
998− ) => ObjectWithSingleFieldObj arity (a :+: b) where
999− objectWithSingleFieldObj opts pa tj tjl (L1 x) =
1000− objectWithSingleFieldObj opts pa tj tjl x
1001− objectWithSingleFieldObj opts pa tj tjl (R1 x) =
1002− objectWithSingleFieldObj opts pa tj tjl x
1003−
1004−instance ( GToJSON arity a
1005− , ConsToJSON arity a
1179+instance ( GToJSON enc arity a
1180+ , ConsToJSON enc arity a
1181+ , FromPairs enc pairs
1182+ , KeyValuePair enc pairs
10061183 , Constructor c
1007− ) => ObjectWithSingleFieldObj arity (C1 c a) where
1008− objectWithSingleFieldObj opts pa tj tjl = H.singleton typ . gToJSON opts pa tj tjl
1184+ ) => SumToJSON' ObjectWithSingleField enc arity (C1 c a)
1185+ where
1186+ sumToJSON' opts targs =
1187+ Tagged . fromPairs . (typ `pair`) . gToJSON opts targs
10091188 where
1010− typ = pack $ constructorTagModifier opts $
1189+ typ = constructorTagModifier opts $
10111190 conName (undefined :: t c a p)
10121191
10131192--------------------------------------------------------------------------------
10141193
1015−class ObjectWithSingleFieldEnc arity f where
1016− objectWithSingleFieldEnc :: Options -> Proxy arity
1017− -> (a -> Encoding) -> ([a] -> Encoding)
1018− -> f a -> Encoding
1019−
1020−instance ( ObjectWithSingleFieldEnc arity a
1021− , ObjectWithSingleFieldEnc arity b
1022− ) => ObjectWithSingleFieldEnc arity (a :+: b) where
1023− objectWithSingleFieldEnc opts pa te tel (L1 x) =
1024− objectWithSingleFieldEnc opts pa te tel x
1025− objectWithSingleFieldEnc opts pa te tel (R1 x) =
1026− objectWithSingleFieldEnc opts pa te tel x
1027−
1028−instance ( GToEncoding arity a
1029− , ConsToEncoding arity a
1030− , Constructor c
1031− ) => ObjectWithSingleFieldEnc arity (C1 c a) where
1032− objectWithSingleFieldEnc opts pa te tel v =
1033− E.openCurly <>
1034− toEncoding
1035− (constructorTagModifier opts
1036− (conName (undefined :: t c a p))) <>
1037− E.colon <>
1038− gToEncoding opts pa te tel v <>
1039− E.closeCurly
1194+instance OVERLAPPABLE_
1195+ ( ConsToJSON enc arity a
1196+ ) => SumToJSON' UntaggedValue enc arity (C1 c a)
1197+ where
1198+ sumToJSON' opts targs = Tagged . gToJSON opts targs
1199+
1200+instance OVERLAPPING_
1201+ ( Constructor c
1202+ , FromString enc
1203+ ) => SumToJSON' UntaggedValue enc arity (C1 c U1)
1204+ where
1205+ sumToJSON' opts _ _ = Tagged . fromString $
1206+ constructorTagModifier opts $ conName (undefined :: t c U1 p)
10401207
10411208-------------------------------------------------------------------------------
10421209-- Instances
@@ -1046,21 +1213,6 @@ instance ( GToEncoding arity a
10461213-- base
10471214-------------------------------------------------------------------------------
10481215
1049−instance ToJSON1 Identity where
1050− liftToJSON t _ (Identity a) = t a
1051− {-# INLINE liftToJSON #-}
1052−
1053− liftToEncoding t _ (Identity a) = t a
1054− {-# INLINE liftToEncoding #-}
1055−
1056−instance (ToJSON a) => ToJSON (Identity a) where
1057− toJSON = toJSON1
1058− {-# INLINE toJSON #-}
1059−
1060− toEncoding = toEncoding1
1061− {-# INLINE toEncoding #-}
1062−
1063−
10641216instance ToJSON2 Const where
10651217 liftToJSON2 t _ _ _ (Const x) = t x
10661218 {-# INLINE liftToJSON2 #-}
@@ -1089,7 +1241,7 @@ instance ToJSON1 Maybe where
10891241 {-# INLINE liftToJSON #-}
10901242
10911243 liftToEncoding t _ (Just a) = t a
1092− liftToEncoding _ _ Nothing = Encoding EB.null_
1244+ liftToEncoding _ _ Nothing = E.null_
10931245 {-# INLINE liftToEncoding #-}
10941246
10951247instance (ToJSON a) => ToJSON (Maybe a) where
@@ -1101,19 +1253,13 @@ instance (ToJSON a) => ToJSON (Maybe a) where
11011253
11021254
11031255instance ToJSON2 Either where
1104− liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton left (toA a)
1105− liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1256+ liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton "Left" (toA a)
1257+ liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton "Right" (toB b)
11061258 {-# INLINE liftToJSON2 #-}
11071259
1108− liftToEncoding2 toA _ _toB _ (Left a) =
1109− Encoding (B.shortByteString "{\"Left\":")
1110− <> toA a
1111− <> Encoding (B.char7 '}')
1260+ liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
11121261
1113− liftToEncoding2 _toA _ toB _ (Right b) =
1114− Encoding (B.shortByteString "{\"Right\":")
1115− <> toB b
1116− <> Encoding (B.char7 '}')
1262+ liftToEncoding2 _toA _ toB _ (Right b) = E.pairs $ E.pair "Right" $ toB b
11171263 {-# INLINE liftToEncoding2 #-}
11181264
11191265instance (ToJSON a) => ToJSON1 (Either a) where
@@ -1130,18 +1276,24 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
11301276 toEncoding = toEncoding2
11311277 {-# INLINE toEncoding #-}
11321278
1133−left, right :: Text
1134−left = "Left"
1135−right = "Right"
1279+instance ToJSON Void where
1280+ toJSON = absurd
1281+ {-# INLINE toJSON #-}
1282+
1283+ toEncoding = absurd
1284+ {-# INLINE toEncoding #-}
11361285
11371286
11381287instance ToJSON Bool where
11391288 toJSON = Bool
11401289 {-# INLINE toJSON #-}
11411290
1142− toEncoding = Encoding . EB.bool
1291+ toEncoding = E.bool
11431292 {-# INLINE toEncoding #-}
11441293
1294+instance ToJSONKey Bool where
1295+ toJSONKey = toJSONKeyText $ \x -> if x then "true" else "false"
1296+
11451297
11461298instance ToJSON Ordering where
11471299 toJSON = toJSON . orderingToText
@@ -1168,10 +1320,10 @@ instance ToJSON Char where
11681320 toJSONList = String . T.pack
11691321 {-# INLINE toJSONList #-}
11701322
1171− toEncoding = Encoding . EB.string . (:[])
1323+ toEncoding = E.string . (:[])
11721324 {-# INLINE toEncoding #-}
11731325
1174− toEncodingList = Encoding . EB.string
1326+ toEncodingList = E.string
11751327 {-# INLINE toEncodingList #-}
11761328
11771329
@@ -1179,9 +1331,13 @@ instance ToJSON Double where
11791331 toJSON = realFloatToJSON
11801332 {-# INLINE toJSON #-}
11811333
1182− toEncoding = realFloatToEncoding
1334+ toEncoding = E.double
11831335 {-# INLINE toEncoding #-}
11841336
1337+instance ToJSONKey Double where
1338+ toJSONKey = toJSONKeyTextEnc E.doubleText
1339+ {-# INLINE toJSONKey #-}
1340+
11851341
11861342instance ToJSON Number where
11871343 toJSON (D d) = toJSON d
@@ -1197,20 +1353,23 @@ instance ToJSON Float where
11971353 toJSON = realFloatToJSON
11981354 {-# INLINE toJSON #-}
11991355
1200− toEncoding = realFloatToEncoding
1356+ toEncoding = E.float
12011357 {-# INLINE toEncoding #-}
12021358
1359+instance ToJSONKey Float where
1360+ toJSONKey = toJSONKeyTextEnc E.floatText
1361+ {-# INLINE toJSONKey #-}
1362+
12031363
1204−instance ToJSON (Ratio Integer) where
1364+instance (ToJSON a, Integral a) => ToJSON (Ratio a) where
12051365 toJSON r = object [ "numerator" .= numerator r
12061366 , "denominator" .= denominator r
12071367 ]
12081368 {-# INLINE toJSON #-}
12091369
1210− toEncoding r = Encoding $
1211− B.shortByteString "{\"numerator\":" <> builder (numerator r) <>
1212− B.shortByteString ",\"denominator\":" <> builder (denominator r) <>
1213− B.char7 '}'
1370+ toEncoding r = E.pairs $
1371+ "numerator" .= numerator r <>
1372+ "denominator" .= denominator r
12141373 {-# INLINE toEncoding #-}
12151374
12161375
@@ -1218,24 +1377,37 @@ instance HasResolution a => ToJSON (Fixed a) where
12181377 toJSON = Number . realToFrac
12191378 {-# INLINE toJSON #-}
12201379
1221− toEncoding = Encoding . EB.number . realToFrac
1380+ toEncoding = E.scientific . realToFrac
12221381 {-# INLINE toEncoding #-}
12231382
1383+instance HasResolution a => ToJSONKey (Fixed a) where
1384+ toJSONKey = toJSONKeyTextEnc (E.scientificText . realToFrac)
1385+ {-# INLINE toJSONKey #-}
1386+
12241387
12251388instance ToJSON Int where
12261389 toJSON = Number . fromIntegral
12271390 {-# INLINE toJSON #-}
12281391
1229− toEncoding = Encoding . B.intDec
1392+ toEncoding = E.int
12301393 {-# INLINE toEncoding #-}
12311394
1395+instance ToJSONKey Int where
1396+ toJSONKey = toJSONKeyTextEnc E.intText
1397+ {-# INLINE toJSONKey #-}
1398+
1399+
12321400instance ToJSON Integer where
12331401 toJSON = Number . fromInteger
12341402 {-# INLINE toJSON #-}
12351403
1236− toEncoding = Encoding . B.integerDec
1404+ toEncoding = E.integer
12371405 {-# INLINE toEncoding #-}
12381406
1407+instance ToJSONKey Integer where
1408+ toJSONKey = toJSONKeyTextEnc E.integerText
1409+ {-# INLINE toJSONKey #-}
1410+
12391411
12401412instance ToJSON Natural where
12411413 toJSON = toJSON . toInteger
@@ -1244,93 +1416,147 @@ instance ToJSON Natural where
12441416 toEncoding = toEncoding . toInteger
12451417 {-# INLINE toEncoding #-}
12461418
1419+instance ToJSONKey Natural where
1420+ toJSONKey = toJSONKeyTextEnc (E.integerText . toInteger)
1421+ {-# INLINE toJSONKey #-}
1422+
12471423
12481424instance ToJSON Int8 where
12491425 toJSON = Number . fromIntegral
12501426 {-# INLINE toJSON #-}
12511427
1252− toEncoding = Encoding . B.int8Dec
1428+ toEncoding = E.int8
12531429 {-# INLINE toEncoding #-}
12541430
1431+instance ToJSONKey Int8 where
1432+ toJSONKey = toJSONKeyTextEnc E.int8Text
1433+ {-# INLINE toJSONKey #-}
1434+
12551435
12561436instance ToJSON Int16 where
12571437 toJSON = Number . fromIntegral
12581438 {-# INLINE toJSON #-}
12591439
1260− toEncoding = Encoding . B.int16Dec
1440+ toEncoding = E.int16
12611441 {-# INLINE toEncoding #-}
12621442
1443+instance ToJSONKey Int16 where
1444+ toJSONKey = toJSONKeyTextEnc E.int16Text
1445+ {-# INLINE toJSONKey #-}
1446+
12631447
12641448instance ToJSON Int32 where
12651449 toJSON = Number . fromIntegral
12661450 {-# INLINE toJSON #-}
12671451
1268− toEncoding = Encoding . B.int32Dec
1452+ toEncoding = E.int32
12691453 {-# INLINE toEncoding #-}
12701454
1455+instance ToJSONKey Int32 where
1456+ toJSONKey = toJSONKeyTextEnc E.int32Text
1457+ {-# INLINE toJSONKey #-}
1458+
12711459
12721460instance ToJSON Int64 where
12731461 toJSON = Number . fromIntegral
12741462 {-# INLINE toJSON #-}
12751463
1276− toEncoding = Encoding . B.int64Dec
1464+ toEncoding = E.int64
12771465 {-# INLINE toEncoding #-}
12781466
1467+instance ToJSONKey Int64 where
1468+ toJSONKey = toJSONKeyTextEnc E.int64Text
1469+ {-# INLINE toJSONKey #-}
12791470
12801471instance ToJSON Word where
12811472 toJSON = Number . fromIntegral
12821473 {-# INLINE toJSON #-}
12831474
1284− toEncoding = Encoding . B.wordDec
1475+ toEncoding = E.word
12851476 {-# INLINE toEncoding #-}
12861477
1478+instance ToJSONKey Word where
1479+ toJSONKey = toJSONKeyTextEnc E.wordText
1480+ {-# INLINE toJSONKey #-}
1481+
12871482
12881483instance ToJSON Word8 where
12891484 toJSON = Number . fromIntegral
12901485 {-# INLINE toJSON #-}
12911486
1292− toEncoding = Encoding . B.word8Dec
1487+ toEncoding = E.word8
12931488 {-# INLINE toEncoding #-}
12941489
1490+instance ToJSONKey Word8 where
1491+ toJSONKey = toJSONKeyTextEnc E.word8Text
1492+ {-# INLINE toJSONKey #-}
1493+
12951494
12961495instance ToJSON Word16 where
12971496 toJSON = Number . fromIntegral
12981497 {-# INLINE toJSON #-}
12991498
1300− toEncoding = Encoding . B.word16Dec
1499+ toEncoding = E.word16
13011500 {-# INLINE toEncoding #-}
13021501
1502+instance ToJSONKey Word16 where
1503+ toJSONKey = toJSONKeyTextEnc E.word16Text
1504+ {-# INLINE toJSONKey #-}
1505+
13031506
13041507instance ToJSON Word32 where
13051508 toJSON = Number . fromIntegral
13061509 {-# INLINE toJSON #-}
13071510
1308− toEncoding = Encoding . B.word32Dec
1511+ toEncoding = E.word32
13091512 {-# INLINE toEncoding #-}
13101513
1514+instance ToJSONKey Word32 where
1515+ toJSONKey = toJSONKeyTextEnc E.word32Text
1516+ {-# INLINE toJSONKey #-}
1517+
1518+
13111519instance ToJSON Word64 where
13121520 toJSON = Number . fromIntegral
13131521 {-# INLINE toJSON #-}
13141522
1315− toEncoding = Encoding . B.word64Dec
1523+ toEncoding = E.word64
1524+ {-# INLINE toEncoding #-}
1525+
1526+instance ToJSONKey Word64 where
1527+ toJSONKey = toJSONKeyTextEnc E.word64Text
1528+ {-# INLINE toJSONKey #-}
1529+
1530+instance ToJSON CTime where
1531+ toJSON (CTime i) = toJSON i
1532+ {-# INLINE toJSON #-}
1533+
1534+ toEncoding (CTime i) = toEncoding i
13161535 {-# INLINE toEncoding #-}
13171536
13181537instance ToJSON Text where
13191538 toJSON = String
13201539 {-# INLINE toJSON #-}
13211540
1322− toEncoding = Encoding . EB.text
1541+ toEncoding = E.text
13231542 {-# INLINE toEncoding #-}
13241543
1544+instance ToJSONKey Text where
1545+ toJSONKey = toJSONKeyText id
1546+ {-# INLINE toJSONKey #-}
1547+
1548+
13251549instance ToJSON LT.Text where
13261550 toJSON = String . LT.toStrict
13271551 {-# INLINE toJSON #-}
13281552
1329− toEncoding t = Encoding $
1330− B.char7 '"' <>
1331− LT.foldrChunks (\x xs -> EB.unquoted x <> xs) (B.char7 '"') t
1553+ toEncoding = E.lazyText
13321554 {-# INLINE toEncoding #-}
13331555
1556+instance ToJSONKey LT.Text where
1557+ toJSONKey = toJSONKeyText LT.toStrict
1558+
1559+
13341560instance ToJSON Version where
13351561 toJSON = toJSON . showVersion
13361562 {-# INLINE toJSON #-}
@@ -1338,6 +1564,8 @@ instance ToJSON Version where
13381564 toEncoding = toEncoding . showVersion
13391565 {-# INLINE toEncoding #-}
13401566
1567+instance ToJSONKey Version where
1568+ toJSONKey = toJSONKeyText (T.pack . showVersion)
13411569
13421570-------------------------------------------------------------------------------
13431571-- semigroups NonEmpty
@@ -1365,7 +1593,138 @@ instance ToJSON Scientific where
13651593 toJSON = Number
13661594 {-# INLINE toJSON #-}
13671595
1368− toEncoding = Encoding . EB.number
1596+ toEncoding = E.scientific
1597+ {-# INLINE toEncoding #-}
1598+
1599+instance ToJSONKey Scientific where
1600+ toJSONKey = toJSONKeyTextEnc E.scientificText
1601+
1602+-------------------------------------------------------------------------------
1603+-- DList
1604+-------------------------------------------------------------------------------
1605+
1606+instance ToJSON1 DList.DList where
1607+ liftToJSON t _ = listValue t . toList
1608+ {-# INLINE liftToJSON #-}
1609+
1610+ liftToEncoding t _ = listEncoding t . toList
1611+ {-# INLINE liftToEncoding #-}
1612+
1613+instance (ToJSON a) => ToJSON (DList.DList a) where
1614+ toJSON = toJSON1
1615+ {-# INLINE toJSON #-}
1616+
1617+ toEncoding = toEncoding1
1618+ {-# INLINE toEncoding #-}
1619+
1620+-------------------------------------------------------------------------------
1621+-- transformers - Functors
1622+-------------------------------------------------------------------------------
1623+
1624+instance ToJSON1 Identity where
1625+ liftToJSON t _ (Identity a) = t a
1626+ {-# INLINE liftToJSON #-}
1627+
1628+ liftToJSONList _ tl xs = tl (map runIdentity xs)
1629+ {-# INLINE liftToJSONList #-}
1630+
1631+ liftToEncoding t _ (Identity a) = t a
1632+ {-# INLINE liftToEncoding #-}
1633+
1634+ liftToEncodingList _ tl xs = tl (map runIdentity xs)
1635+ {-# INLINE liftToEncodingList #-}
1636+
1637+instance (ToJSON a) => ToJSON (Identity a) where
1638+ toJSON = toJSON1
1639+ {-# INLINE toJSON #-}
1640+
1641+ toJSONList = liftToJSONList toJSON toJSONList
1642+ {-# INLINE toJSONList #-}
1643+
1644+ toEncoding = toEncoding1
1645+ {-# INLINE toEncoding #-}
1646+
1647+ toEncodingList = liftToEncodingList toEncoding toEncodingList
1648+ {-# INLINE toEncodingList #-}
1649+
1650+instance (ToJSONKey a) => ToJSONKey (Identity a) where
1651+ toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1652+ toJSONKeyList = contramapToJSONKeyFunction (map runIdentity) toJSONKeyList
1653+
1654+
1655+instance (ToJSON1 f, ToJSON1 g) => ToJSON1 (Compose f g) where
1656+ liftToJSON tv tvl (Compose x) = liftToJSON g gl x
1657+ where
1658+ g = liftToJSON tv tvl
1659+ gl = liftToJSONList tv tvl
1660+ {-# INLINE liftToJSON #-}
1661+
1662+ liftToJSONList te tel xs = liftToJSONList g gl (map getCompose xs)
1663+ where
1664+ g = liftToJSON te tel
1665+ gl = liftToJSONList te tel
1666+ {-# INLINE liftToJSONList #-}
1667+
1668+ liftToEncoding te tel (Compose x) = liftToEncoding g gl x
1669+ where
1670+ g = liftToEncoding te tel
1671+ gl = liftToEncodingList te tel
1672+ {-# INLINE liftToEncoding #-}
1673+
1674+ liftToEncodingList te tel xs = liftToEncodingList g gl (map getCompose xs)
1675+ where
1676+ g = liftToEncoding te tel
1677+ gl = liftToEncodingList te tel
1678+ {-# INLINE liftToEncodingList #-}
1679+
1680+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Compose f g a) where
1681+ toJSON = toJSON1
1682+ {-# INLINE toJSON #-}
1683+
1684+ toJSONList = liftToJSONList toJSON toJSONList
1685+ {-# INLINE toJSONList #-}
1686+
1687+ toEncoding = toEncoding1
1688+ {-# INLINE toEncoding #-}
1689+
1690+ toEncodingList = liftToEncodingList toEncoding toEncodingList
1691+ {-# INLINE toEncodingList #-}
1692+
1693+
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)
1696+ where
1697+ tx = liftToJSON tv tvl
1698+ txl = liftToJSONList tv tvl
1699+ ty = liftToJSON tv tvl
1700+ tyl = liftToJSONList tv tvl
1701+
1702+ liftToEncoding te tel (Pair x y) = liftToEncoding2 tx txl ty tyl (x, y)
1703+ where
1704+ tx = liftToEncoding te tel
1705+ txl = liftToEncodingList te tel
1706+ ty = liftToEncoding te tel
1707+ tyl = liftToEncodingList te tel
1708+
1709+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Product f g a) where
1710+ toJSON = toJSON1
1711+ {-# INLINE toJSON #-}
1712+
1713+ toEncoding = toEncoding1
1714+ {-# INLINE toEncoding #-}
1715+
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)
1719+
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
1722+
1723+instance (ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Sum f g a) where
1724+ toJSON = toJSON1
1725+ {-# INLINE toJSON #-}
1726+
1727+ toEncoding = toEncoding1
13691728 {-# INLINE toEncoding #-}
13701729
13711730-------------------------------------------------------------------------------
@@ -1433,17 +1792,15 @@ instance ToJSON a => ToJSON (IntMap.IntMap a) where
14331792
14341793instance ToJSONKey k => ToJSON1 (M.Map k) where
14351794 liftToJSON g _ = case toJSONKey of
1436− ToJSONKeyText (f,_) -> Object . mapHashKeyVal f g
1437− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . M.toList
1795+ ToJSONKeyText f _ -> Object . mapHashKeyVal f g
1796+ ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . M.toList
14381797 {-# INLINE liftToJSON #-}
14391798
1440− -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
14411799 liftToEncoding g _ = case toJSONKey of
1442− ToJSONKeyText (_,f) -> dict f g M.foldrWithKey
1443− ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . M.toList
1800+ ToJSONKeyText _ f -> dict f g M.foldrWithKey
1801+ ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . M.toList
14441802 where
1445− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1446− pairEncoding f (a, b) = tuple $ f a >*< g b
1803+ pairEncoding f (a, b) = E.list id [f a, g b]
14471804 {-# INLINE liftToEncoding #-}
14481805
14491806
@@ -1481,6 +1838,17 @@ instance (ToJSON v) => ToJSON (Tree.Tree v) where
14811838 toEncoding = toEncoding1
14821839 {-# INLINE toEncoding #-}
14831840
1841+-------------------------------------------------------------------------------
1842+-- uuid
1843+-------------------------------------------------------------------------------
1844+
1845+instance ToJSON UUID.UUID where
1846+ toJSON = toJSON . UUID.toText
1847+ toEncoding = E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
1848+
1849+instance ToJSONKey UUID.UUID where
1850+ toJSONKey = ToJSONKeyText UUID.toText $
1851+ E.unsafeToEncoding . EB.quote . B.byteString . UUID.toASCIIBytes
14841852
14851853-------------------------------------------------------------------------------
14861854-- vector
@@ -1490,29 +1858,20 @@ instance ToJSON1 Vector where
14901858 liftToJSON t _ = Array . V.map t
14911859 {-# INLINE liftToJSON #-}
14921860
1493− liftToEncoding t _ xs
1494− | V.null xs = emptyArray_
1495− | otherwise = Encoding $
1496− B.char7 '[' <> fromEncoding (t (V.unsafeHead xs)) <>
1497− V.foldr go (B.char7 ']') (V.unsafeTail xs)
1498− where
1499− go v b = B.char7 ',' <> fromEncoding (t v) <> b
1861+ liftToEncoding t _ = listEncoding t . V.toList
15001862 {-# INLINE liftToEncoding #-}
15011863
15021864instance (ToJSON a) => ToJSON (Vector a) where
1503− toJSON = Array . V.map toJSON
1865+ {-# SPECIALIZE instance ToJSON Array #-}
1866+
1867+ toJSON = toJSON1
15041868 {-# INLINE toJSON #-}
15051869
1506− toEncoding = encodeVector
1870+ toEncoding = toEncoding1
15071871 {-# INLINE toEncoding #-}
15081872
15091873encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1510−encodeVector xs
1511− | VG.null xs = emptyArray_
1512− | otherwise = Encoding $
1513− B.char7 '[' <> builder (VG.unsafeHead xs) <>
1514− VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
1515− where go v b = B.char7 ',' <> builder v <> b
1874+encodeVector = listEncoding toEncoding . VG.toList
15161875{-# INLINE encodeVector #-}
15171876
15181877vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -1563,20 +1922,21 @@ instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
15631922
15641923instance ToJSONKey k => ToJSON1 (H.HashMap k) where
15651924 liftToJSON g _ = case toJSONKey of
1566− ToJSONKeyText (f,_) -> Object . mapKeyVal f g
1567− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . H.toList
1925+ ToJSONKeyText f _ -> Object . mapKeyVal f g
1926+ ToJSONKeyValue f _ -> Array . V.fromList . map (toJSONPair f g) . H.toList
15681927 {-# INLINE liftToJSON #-}
15691928
15701929 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
15711930 liftToEncoding g _ = case toJSONKey of
1572− ToJSONKeyText (_,f) -> dict f g H.foldrWithKey
1573− ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . H.toList
1931+ ToJSONKeyText _ f -> dict f g H.foldrWithKey
1932+ ToJSONKeyValue _ f -> listEncoding (pairEncoding f) . H.toList
15741933 where
1575− -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1576− pairEncoding f (a, b) = tuple $ f a >*< g b
1934+ pairEncoding f (a, b) = E.list id [f a, g b]
15771935 {-# INLINE liftToEncoding #-}
15781936
15791937instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1938+ {-# SPECIALIZE instance ToJSON Object #-}
1939+
15801940 toJSON = toJSON1
15811941 {-# INLINE toJSON #-}
15821942
@@ -1591,7 +1951,7 @@ instance ToJSON Value where
15911951 toJSON a = a
15921952 {-# INLINE toJSON #-}
15931953
1594− toEncoding = Encoding . EB.encodeToBuilder
1954+ toEncoding = E.value
15951955 {-# INLINE toEncoding #-}
15961956
15971957instance ToJSON DotNetTime where
@@ -1603,69 +1963,159 @@ dotNetTime :: DotNetTime -> String
16031963dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
16041964 where secs = formatTime defaultTimeLocale "/Date(%s" t
16051965
1966+formatMillis :: (FormatTime t) => t -> String
1967+formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1968+
1969+-------------------------------------------------------------------------------
1970+-- primitive
1971+-------------------------------------------------------------------------------
1972+
1973+instance ToJSON a => ToJSON (PM.Array a) where
1974+ -- note: we could do better than this if vector exposed the data
1975+ -- constructor in Data.Vector.
1976+ toJSON = toJSON . Exts.toList
1977+ toEncoding = toEncoding . Exts.toList
1978+
1979+instance ToJSON a => ToJSON (PM.SmallArray a) where
1980+ toJSON = toJSON . Exts.toList
1981+ toEncoding = toEncoding . Exts.toList
1982+
1983+#if (MIN_VERSION_primitive(0,6,4))
1984+instance (PM.Prim a,ToJSON a) => ToJSON (PM.PrimArray a) where
1985+ toJSON = toJSON . Exts.toList
1986+ toEncoding = toEncoding . Exts.toList
1987+
1988+#if !MIN_VERSION_primitive(0,7,0)
1989+instance (PM.PrimUnlifted a,ToJSON a) => ToJSON (PM.UnliftedArray a) where
1990+ toJSON = toJSON . Exts.toList
1991+ toEncoding = toEncoding . Exts.toList
1992+#endif
1993+#endif
1994+
16061995-------------------------------------------------------------------------------
16071996-- time
16081997-------------------------------------------------------------------------------
16091998
16101999instance ToJSON Day where
1611− toJSON = stringEncoding
1612− toEncoding z = Encoding (EB.quote $ EB.day z)
2000+ toJSON = stringEncoding . E.day
2001+ toEncoding = E.day
2002+
2003+instance ToJSONKey Day where
2004+ toJSONKey = toJSONKeyTextEnc E.day
16132005
16142006
16152007instance ToJSON TimeOfDay where
1616− toJSON = stringEncoding
1617− toEncoding z = Encoding (EB.quote $ EB.timeOfDay z)
2008+ toJSON = stringEncoding . E.timeOfDay
2009+ toEncoding = E.timeOfDay
2010+
2011+instance ToJSONKey TimeOfDay where
2012+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
16182013
16192014
16202015instance ToJSON LocalTime where
1621− toJSON = stringEncoding
1622− toEncoding z = Encoding (EB.quote $ EB.localTime z)
2016+ toJSON = stringEncoding . E.localTime
2017+ toEncoding = E.localTime
2018+
2019+instance ToJSONKey LocalTime where
2020+ toJSONKey = toJSONKeyTextEnc E.localTime
2021+
16232022
16242023instance ToJSON ZonedTime where
1625− toJSON = stringEncoding
2024+ toJSON = stringEncoding . E.zonedTime
2025+ toEncoding = E.zonedTime
16262026
1627− toEncoding z = Encoding (EB.quote $ EB.zonedTime z)
2027+instance ToJSONKey ZonedTime where
2028+ toJSONKey = toJSONKeyTextEnc E.zonedTime
16282029
1629−formatMillis :: (FormatTime t) => t -> String
1630−formatMillis = take 3 . formatTime defaultTimeLocale "%q"
16312030
16322031instance ToJSON UTCTime where
1633− toJSON = stringEncoding
2032+ toJSON = stringEncoding . E.utcTime
2033+ toEncoding = E.utcTime
16342034
1635− toEncoding t = Encoding (EB.quote $ EB.utcTime t)
2035+instance ToJSONKey UTCTime where
2036+ toJSONKey = toJSONKeyTextEnc E.utcTime
16362037
16372038-- | Encode something t a JSON string.
1638−--
1639−-- TODO: remove me, this is a hack
1640−stringEncoding :: (ToJSON a) => a -> Value
2039+stringEncoding :: Encoding' Text -> Value
16412040stringEncoding = String
16422041 . T.dropAround (== '"')
16432042 . T.decodeLatin1
16442043 . L.toStrict
1645− . B.toLazyByteString
1646− . fromEncoding
1647− . toEncoding
2044+ . E.encodingToLazyByteString
16482045{-# INLINE stringEncoding #-}
16492046
2047+
16502048instance ToJSON NominalDiffTime where
16512049 toJSON = Number . realToFrac
16522050 {-# INLINE toJSON #-}
16532051
1654− toEncoding = Encoding . EB.number . realToFrac
2052+ toEncoding = E.scientific . realToFrac
16552053 {-# INLINE toEncoding #-}
16562054
2055+
2056+instance ToJSON DiffTime where
2057+ toJSON = Number . realToFrac
2058+ {-# INLINE toJSON #-}
2059+
2060+ toEncoding = E.scientific . realToFrac
2061+ {-# INLINE toEncoding #-}
2062+
2063+-- | Encoded as number
2064+instance ToJSON SystemTime where
2065+ toJSON (MkSystemTime secs nsecs) =
2066+ toJSON (fromIntegral secs + fromIntegral nsecs / 1000000000 :: Nano)
2067+ toEncoding (MkSystemTime secs nsecs) =
2068+ toEncoding (fromIntegral secs + fromIntegral nsecs / 1000000000 :: Nano)
2069+
2070+instance ToJSON CalendarDiffTime where
2071+ toJSON (CalendarDiffTime m nt) = object
2072+ [ "months" .= m
2073+ , "time" .= nt
2074+ ]
2075+ toEncoding (CalendarDiffTime m nt) = E.pairs
2076+ ("months" .= m <> "time" .= nt)
2077+
2078+instance ToJSON CalendarDiffDays where
2079+ toJSON (CalendarDiffDays m d) = object
2080+ [ "months" .= m
2081+ , "days" .= d
2082+ ]
2083+ toEncoding (CalendarDiffDays m d) = E.pairs
2084+ ("months" .= m <> "days" .= d)
2085+
2086+instance ToJSON DayOfWeek where
2087+ toJSON Monday = "monday"
2088+ toJSON Tuesday = "tuesday"
2089+ toJSON Wednesday = "wednesday"
2090+ toJSON Thursday = "thursday"
2091+ toJSON Friday = "friday"
2092+ toJSON Saturday = "saturday"
2093+ toJSON Sunday = "sunday"
2094+
2095+toEncodingDayOfWeek :: DayOfWeek -> E.Encoding' Text
2096+toEncodingDayOfWeek Monday = E.unsafeToEncoding "\"monday\""
2097+toEncodingDayOfWeek Tuesday = E.unsafeToEncoding "\"tuesday\""
2098+toEncodingDayOfWeek Wednesday = E.unsafeToEncoding "\"wednesday\""
2099+toEncodingDayOfWeek Thursday = E.unsafeToEncoding "\"thursday\""
2100+toEncodingDayOfWeek Friday = E.unsafeToEncoding "\"friday\""
2101+toEncodingDayOfWeek Saturday = E.unsafeToEncoding "\"saturday\""
2102+toEncodingDayOfWeek Sunday = E.unsafeToEncoding "\"sunday\""
2103+
2104+instance ToJSONKey DayOfWeek where
2105+ toJSONKey = toJSONKeyTextEnc toEncodingDayOfWeek
2106+
16572107-------------------------------------------------------------------------------
16582108-- base Monoid/Semigroup
16592109-------------------------------------------------------------------------------
16602110
1661−instance ToJSON1 Dual where
1662− liftToJSON t _ = t . getDual
2111+instance ToJSON1 Monoid.Dual where
2112+ liftToJSON t _ = t . Monoid.getDual
16632113 {-# INLINE liftToJSON #-}
16642114
1665− liftToEncoding t _ = t . getDual
2115+ liftToEncoding t _ = t . Monoid.getDual
16662116 {-# INLINE liftToEncoding #-}
16672117
1668−instance ToJSON a => ToJSON (Dual a) where
2118+instance ToJSON a => ToJSON (Monoid.Dual a) where
16692119 toJSON = toJSON1
16702120 {-# INLINE toJSON #-}
16712121
@@ -1673,14 +2123,73 @@ instance ToJSON a => ToJSON (Dual a) where
16732123 {-# INLINE toEncoding #-}
16742124
16752125
1676−instance ToJSON1 First where
1677− liftToJSON t to' = liftToJSON t to' . getFirst
2126+instance ToJSON1 Monoid.First where
2127+ liftToJSON t to' = liftToJSON t to' . Monoid.getFirst
2128+ {-# INLINE liftToJSON #-}
2129+
2130+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getFirst
2131+ {-# INLINE liftToEncoding #-}
2132+
2133+instance ToJSON a => ToJSON (Monoid.First a) where
2134+ toJSON = toJSON1
2135+ {-# INLINE toJSON #-}
2136+
2137+ toEncoding = toEncoding1
2138+ {-# INLINE toEncoding #-}
2139+
2140+
2141+instance ToJSON1 Monoid.Last where
2142+ liftToJSON t to' = liftToJSON t to' . Monoid.getLast
2143+ {-# INLINE liftToJSON #-}
2144+
2145+ liftToEncoding t to' = liftToEncoding t to' . Monoid.getLast
2146+ {-# INLINE liftToEncoding #-}
2147+
2148+instance ToJSON a => ToJSON (Monoid.Last a) where
2149+ toJSON = toJSON1
2150+ {-# INLINE toJSON #-}
2151+
2152+ toEncoding = toEncoding1
2153+ {-# INLINE toEncoding #-}
2154+
2155+
2156+instance ToJSON1 Semigroup.Min where
2157+ liftToJSON t _ (Semigroup.Min x) = t x
2158+ {-# INLINE liftToJSON #-}
2159+
2160+ liftToEncoding t _ (Semigroup.Min x) = t x
2161+ {-# INLINE liftToEncoding #-}
2162+
2163+instance ToJSON a => ToJSON (Semigroup.Min a) where
2164+ toJSON = toJSON1
2165+ {-# INLINE toJSON #-}
2166+
2167+ toEncoding = toEncoding1
2168+ {-# INLINE toEncoding #-}
2169+
2170+
2171+instance ToJSON1 Semigroup.Max where
2172+ liftToJSON t _ (Semigroup.Max x) = t x
2173+ {-# INLINE liftToJSON #-}
2174+
2175+ liftToEncoding t _ (Semigroup.Max x) = t x
2176+ {-# INLINE liftToEncoding #-}
2177+
2178+instance ToJSON a => ToJSON (Semigroup.Max a) where
2179+ toJSON = toJSON1
2180+ {-# INLINE toJSON #-}
2181+
2182+ toEncoding = toEncoding1
2183+ {-# INLINE toEncoding #-}
2184+
2185+instance ToJSON1 Semigroup.First where
2186+ liftToJSON t _ (Semigroup.First x) = t x
16782187 {-# INLINE liftToJSON #-}
16792188
1680− liftToEncoding t to' = liftToEncoding t to' . getFirst
2189+ liftToEncoding t _ (Semigroup.First x) = t x
16812190 {-# INLINE liftToEncoding #-}
16822191
1683−instance ToJSON a => ToJSON (First a) where
2192+instance ToJSON a => ToJSON (Semigroup.First a) where
16842193 toJSON = toJSON1
16852194 {-# INLINE toJSON #-}
16862195
@@ -1688,14 +2197,44 @@ instance ToJSON a => ToJSON (First a) where
16882197 {-# INLINE toEncoding #-}
16892198
16902199
1691−instance ToJSON1 Last where
1692− liftToJSON t to' = liftToJSON t to' . getLast
2200+instance ToJSON1 Semigroup.Last where
2201+ liftToJSON t _ (Semigroup.Last x) = t x
16932202 {-# INLINE liftToJSON #-}
16942203
1695− liftToEncoding t to' = liftToEncoding t to' . getLast
2204+ liftToEncoding t _ (Semigroup.Last x) = t x
16962205 {-# INLINE liftToEncoding #-}
16972206
1698−instance ToJSON a => ToJSON (Last a) where
2207+instance ToJSON a => ToJSON (Semigroup.Last a) where
2208+ toJSON = toJSON1
2209+ {-# INLINE toJSON #-}
2210+
2211+ toEncoding = toEncoding1
2212+ {-# INLINE toEncoding #-}
2213+
2214+
2215+instance ToJSON1 Semigroup.WrappedMonoid where
2216+ liftToJSON t _ (Semigroup.WrapMonoid x) = t x
2217+ {-# INLINE liftToJSON #-}
2218+
2219+ liftToEncoding t _ (Semigroup.WrapMonoid x) = t x
2220+ {-# INLINE liftToEncoding #-}
2221+
2222+instance ToJSON a => ToJSON (Semigroup.WrappedMonoid a) where
2223+ toJSON = toJSON1
2224+ {-# INLINE toJSON #-}
2225+
2226+ toEncoding = toEncoding1
2227+ {-# INLINE toEncoding #-}
2228+
2229+
2230+instance ToJSON1 Semigroup.Option where
2231+ liftToJSON t to' = liftToJSON t to' . Semigroup.getOption
2232+ {-# INLINE liftToJSON #-}
2233+
2234+ liftToEncoding t to' = liftToEncoding t to' . Semigroup.getOption
2235+ {-# INLINE liftToEncoding #-}
2236+
2237+instance ToJSON a => ToJSON (Semigroup.Option a) where
16992238 toJSON = toJSON1
17002239 {-# INLINE toJSON #-}
17012240
@@ -1706,14 +2245,28 @@ instance ToJSON a => ToJSON (Last a) where
17062245-- tagged
17072246-------------------------------------------------------------------------------
17082247
2248+instance ToJSON1 Proxy where
2249+ liftToJSON _ _ _ = Null
2250+ {-# INLINE liftToJSON #-}
2251+
2252+ liftToEncoding _ _ _ = E.null_
2253+ {-# INLINE liftToEncoding #-}
2254+
17092255instance ToJSON (Proxy a) where
17102256 toJSON _ = Null
17112257 {-# INLINE toJSON #-}
17122258
1713− toEncoding _ = Encoding EB.null_
2259+ toEncoding _ = E.null_
17142260 {-# INLINE toEncoding #-}
17152261
17162262
2263+instance ToJSON2 Tagged where
2264+ liftToJSON2 _ _ t _ (Tagged x) = t x
2265+ {-# INLINE liftToJSON2 #-}
2266+
2267+ liftToEncoding2 _ _ t _ (Tagged x) = t x
2268+ {-# INLINE liftToEncoding2 #-}
2269+
17172270instance ToJSON1 (Tagged a) where
17182271 liftToJSON t _ (Tagged x) = t x
17192272 {-# INLINE liftToJSON #-}
@@ -1728,7 +2281,6 @@ instance ToJSON b => ToJSON (Tagged a b) where
17282281 toEncoding = toEncoding1
17292282 {-# INLINE toEncoding #-}
17302283
1731−
17322284instance ToJSONKey b => ToJSONKey (Tagged a b) where
17332285 toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
17342286 toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
@@ -1737,41 +2289,22 @@ instance ToJSONKey b => ToJSONKey (Tagged a b) where
17372289-- Instances for converting t map keys
17382290-------------------------------------------------------------------------------
17392291
1740−instance ToJSONKey Text where
1741− toJSONKey = ToJSONKeyText (id,toEncoding)
1742−
1743−
1744−instance ToJSONKey Bool where
1745− toJSONKey = ToJSONKeyText
1746− ( (\x -> if x then "true" else "false")
1747− , (\x -> Encoding $ if x then "\"true\"" else "\"false\"")
1748− )
1749−
1750−instance ToJSONKey Int where
1751− toJSONKey = ToJSONKeyText
1752− ( LT.toStrict . LTB.toLazyText . LTBI.decimal
1753− , \x -> Encoding $ B.char7 '"' <> fromEncoding (toEncoding x) <> B.char7 '"'
1754− )
1755−
17562292instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
17572293instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
17582294instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
17592295
17602296instance ToJSONKey Char where
1761− toJSONKey = ToJSONKeyText (T.singleton, toEncoding)
1762− toJSONKeyList = ToJSONKeyText (T.pack, toEncoding . T.pack)
2297+ toJSONKey = ToJSONKeyText T.singleton (E.string . (:[]))
2298+ toJSONKeyList = toJSONKeyText T.pack
17632299
17642300instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
17652301 toJSONKey = toJSONKeyList
17662302
1767−instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1768− toJSONKey = contramapToJSONKeyFunction runIdentity toJSONKey
1769−
17702303-------------------------------------------------------------------------------
1771−-- Tuple instances, see tuple-instances-to.hs
2304+-- Tuple instances
17722305-------------------------------------------------------------------------------
17732306
1774−instance ToJSON2 ((,) ) where
2307+instance ToJSON2 (,) where
17752308 liftToJSON2 toA _ toB _ (a, b) = Array $ V.create $ do
17762309 mv <- VM.unsafeNew 2
17772310 VM.unsafeWrite mv 0 (toA a)
@@ -1779,9 +2312,7 @@ instance ToJSON2 ((,) ) where
17792312 return mv
17802313 {-# INLINE liftToJSON2 #-}
17812314
1782− liftToEncoding2 toA _ toB _ (a, b) = tuple $
1783− toA a >*<
1784− toB b
2315+ liftToEncoding2 toA _ toB _ (a, b) = E.list id [toA a, toB b]
17852316 {-# INLINE liftToEncoding2 #-}
17862317
17872318instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -1805,10 +2336,11 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
18052336 return mv
18062337 {-# INLINE liftToJSON2 #-}
18072338
1808− liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
1809− toEncoding a >*<
1810− toB b >*<
1811− toC c
2339+ liftToEncoding2 toB _ toC _ (a, b, c) = E.list id
2340+ [ toEncoding a
2341+ , toB b
2342+ , toC c
2343+ ]
18122344 {-# INLINE liftToEncoding2 #-}
18132345
18142346instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -1833,11 +2365,12 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
18332365 return mv
18342366 {-# INLINE liftToJSON2 #-}
18352367
1836− liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
1837− toEncoding a >*<
1838− toEncoding b >*<
1839− toC c >*<
1840− toD d
2368+ liftToEncoding2 toC _ toD _ (a, b, c, d) = E.list id
2369+ [ toEncoding a
2370+ , toEncoding b
2371+ , toC c
2372+ , toD d
2373+ ]
18412374 {-# INLINE liftToEncoding2 #-}
18422375
18432376instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -1863,12 +2396,13 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
18632396 return mv
18642397 {-# INLINE liftToJSON2 #-}
18652398
1866− liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
1867− toEncoding a >*<
1868− toEncoding b >*<
1869− toEncoding c >*<
1870− toD d >*<
1871− toE e
2399+ liftToEncoding2 toD _ toE _ (a, b, c, d, e) = E.list id
2400+ [ toEncoding a
2401+ , toEncoding b
2402+ , toEncoding c
2403+ , toD d
2404+ , toE e
2405+ ]
18722406 {-# INLINE liftToEncoding2 #-}
18732407
18742408instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
@@ -1895,13 +2429,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
18952429 return mv
18962430 {-# INLINE liftToJSON2 #-}
18972431
1898− liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
1899− toEncoding a >*<
1900− toEncoding b >*<
1901− toEncoding c >*<
1902− toEncoding d >*<
1903− toE e >*<
1904− toF f
2432+ liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = E.list id
2433+ [ toEncoding a
2434+ , toEncoding b
2435+ , toEncoding c
2436+ , toEncoding d
2437+ , toE e
2438+ , toF f
2439+ ]
19052440 {-# INLINE liftToEncoding2 #-}
19062441
19072442instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
@@ -1929,14 +2464,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
19292464 return mv
19302465 {-# INLINE liftToJSON2 #-}
19312466
1932− liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
1933− toEncoding a >*<
1934− toEncoding b >*<
1935− toEncoding c >*<
1936− toEncoding d >*<
1937− toEncoding e >*<
1938− toF f >*<
1939− toG g
2467+ liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = E.list id
2468+ [ toEncoding a
2469+ , toEncoding b
2470+ , toEncoding c
2471+ , toEncoding d
2472+ , toEncoding e
2473+ , toF f
2474+ , toG g
2475+ ]
19402476 {-# INLINE liftToEncoding2 #-}
19412477
19422478instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
@@ -1965,15 +2501,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
19652501 return mv
19662502 {-# INLINE liftToJSON2 #-}
19672503
1968− liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
1969− toEncoding a >*<
1970− toEncoding b >*<
1971− toEncoding c >*<
1972− toEncoding d >*<
1973− toEncoding e >*<
1974− toEncoding f >*<
1975− toG g >*<
1976− toH h
2504+ liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = E.list id
2505+ [ toEncoding a
2506+ , toEncoding b
2507+ , toEncoding c
2508+ , toEncoding d
2509+ , toEncoding e
2510+ , toEncoding f
2511+ , toG g
2512+ , toH h
2513+ ]
19772514 {-# INLINE liftToEncoding2 #-}
19782515
19792516instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
@@ -2003,16 +2540,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
20032540 return mv
20042541 {-# INLINE liftToJSON2 #-}
20052542
2006− liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
2007− toEncoding a >*<
2008− toEncoding b >*<
2009− toEncoding c >*<
2010− toEncoding d >*<
2011− toEncoding e >*<
2012− toEncoding f >*<
2013− toEncoding g >*<
2014− toH h >*<
2015− toI i
2543+ liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = E.list id
2544+ [ toEncoding a
2545+ , toEncoding b
2546+ , toEncoding c
2547+ , toEncoding d
2548+ , toEncoding e
2549+ , toEncoding f
2550+ , toEncoding g
2551+ , toH h
2552+ , toI i
2553+ ]
20162554 {-# INLINE liftToEncoding2 #-}
20172555
20182556instance (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
@@ -2043,17 +2581,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
20432581 return mv
20442582 {-# INLINE liftToJSON2 #-}
20452583
2046− liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
2047− toEncoding a >*<
2048− toEncoding b >*<
2049− toEncoding c >*<
2050− toEncoding d >*<
2051− toEncoding e >*<
2052− toEncoding f >*<
2053− toEncoding g >*<
2054− toEncoding h >*<
2055− toI i >*<
2056− toJ j
2584+ liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = E.list id
2585+ [ toEncoding a
2586+ , toEncoding b
2587+ , toEncoding c
2588+ , toEncoding d
2589+ , toEncoding e
2590+ , toEncoding f
2591+ , toEncoding g
2592+ , toEncoding h
2593+ , toI i
2594+ , toJ j
2595+ ]
20572596 {-# INLINE liftToEncoding2 #-}
20582597
20592598instance (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
@@ -2085,18 +2624,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
20852624 return mv
20862625 {-# INLINE liftToJSON2 #-}
20872626
2088− liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
2089− toEncoding a >*<
2090− toEncoding b >*<
2091− toEncoding c >*<
2092− toEncoding d >*<
2093− toEncoding e >*<
2094− toEncoding f >*<
2095− toEncoding g >*<
2096− toEncoding h >*<
2097− toEncoding i >*<
2098− toJ j >*<
2099− toK k
2627+ liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = E.list id
2628+ [ toEncoding a
2629+ , toEncoding b
2630+ , toEncoding c
2631+ , toEncoding d
2632+ , toEncoding e
2633+ , toEncoding f
2634+ , toEncoding g
2635+ , toEncoding h
2636+ , toEncoding i
2637+ , toJ j
2638+ , toK k
2639+ ]
21002640 {-# INLINE liftToEncoding2 #-}
21012641
21022642instance (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
@@ -2129,19 +2669,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
21292669 return mv
21302670 {-# INLINE liftToJSON2 #-}
21312671
2132− liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
2133− toEncoding a >*<
2134− toEncoding b >*<
2135− toEncoding c >*<
2136− toEncoding d >*<
2137− toEncoding e >*<
2138− toEncoding f >*<
2139− toEncoding g >*<
2140− toEncoding h >*<
2141− toEncoding i >*<
2142− toEncoding j >*<
2143− toK k >*<
2144− toL l
2672+ liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = E.list id
2673+ [ toEncoding a
2674+ , toEncoding b
2675+ , toEncoding c
2676+ , toEncoding d
2677+ , toEncoding e
2678+ , toEncoding f
2679+ , toEncoding g
2680+ , toEncoding h
2681+ , toEncoding i
2682+ , toEncoding j
2683+ , toK k
2684+ , toL l
2685+ ]
21452686 {-# INLINE liftToEncoding2 #-}
21462687
21472688instance (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
@@ -2175,20 +2716,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
21752716 return mv
21762717 {-# INLINE liftToJSON2 #-}
21772718
2178− liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
2179− toEncoding a >*<
2180− toEncoding b >*<
2181− toEncoding c >*<
2182− toEncoding d >*<
2183− toEncoding e >*<
2184− toEncoding f >*<
2185− toEncoding g >*<
2186− toEncoding h >*<
2187− toEncoding i >*<
2188− toEncoding j >*<
2189− toEncoding k >*<
2190− toL l >*<
2191− toM m
2719+ liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = E.list id
2720+ [ toEncoding a
2721+ , toEncoding b
2722+ , toEncoding c
2723+ , toEncoding d
2724+ , toEncoding e
2725+ , toEncoding f
2726+ , toEncoding g
2727+ , toEncoding h
2728+ , toEncoding i
2729+ , toEncoding j
2730+ , toEncoding k
2731+ , toL l
2732+ , toM m
2733+ ]
21922734 {-# INLINE liftToEncoding2 #-}
21932735
21942736instance (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
@@ -2223,21 +2765,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
22232765 return mv
22242766 {-# INLINE liftToJSON2 #-}
22252767
2226− liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
2227− toEncoding a >*<
2228− toEncoding b >*<
2229− toEncoding c >*<
2230− toEncoding d >*<
2231− toEncoding e >*<
2232− toEncoding f >*<
2233− toEncoding g >*<
2234− toEncoding h >*<
2235− toEncoding i >*<
2236− toEncoding j >*<
2237− toEncoding k >*<
2238− toEncoding l >*<
2239− toM m >*<
2240− toN n
2768+ liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = E.list id
2769+ [ toEncoding a
2770+ , toEncoding b
2771+ , toEncoding c
2772+ , toEncoding d
2773+ , toEncoding e
2774+ , toEncoding f
2775+ , toEncoding g
2776+ , toEncoding h
2777+ , toEncoding i
2778+ , toEncoding j
2779+ , toEncoding k
2780+ , toEncoding l
2781+ , toM m
2782+ , toN n
2783+ ]
22412784 {-# INLINE liftToEncoding2 #-}
22422785
22432786instance (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
@@ -2273,22 +2816,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
22732816 return mv
22742817 {-# INLINE liftToJSON2 #-}
22752818
2276− liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
2277− toEncoding a >*<
2278− toEncoding b >*<
2279− toEncoding c >*<
2280− toEncoding d >*<
2281− toEncoding e >*<
2282− toEncoding f >*<
2283− toEncoding g >*<
2284− toEncoding h >*<
2285− toEncoding i >*<
2286− toEncoding j >*<
2287− toEncoding k >*<
2288− toEncoding l >*<
2289− toEncoding m >*<
2290− toN n >*<
2291− toO o
2819+ liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = E.list id
2820+ [ toEncoding a
2821+ , toEncoding b
2822+ , toEncoding c
2823+ , toEncoding d
2824+ , toEncoding e
2825+ , toEncoding f
2826+ , toEncoding g
2827+ , toEncoding h
2828+ , toEncoding i
2829+ , toEncoding j
2830+ , toEncoding k
2831+ , toEncoding l
2832+ , toEncoding m
2833+ , toN n
2834+ , toO o
2835+ ]
22922836 {-# INLINE liftToEncoding2 #-}
22932837
22942838instance (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
@@ -2302,3 +2846,27 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
23022846 {-# INLINE toJSON #-}
23032847 toEncoding = toEncoding2
23042848 {-# INLINE toEncoding #-}
2849+
2850+--------------------------------------------------------------------------------
2851+
2852+-- | Wrap a list of pairs as an object.
2853+class Monoid pairs => FromPairs enc pairs | enc -> pairs where
2854+ fromPairs :: pairs -> enc
2855+
2856+instance (a ~ Value) => FromPairs (Encoding' a) Series where
2857+ fromPairs = E.pairs
2858+
2859+instance FromPairs Value (DList Pair) where
2860+ fromPairs = object . toList
2861+
2862+-- | Like 'KeyValue' but the value is already converted to JSON
2863+-- ('Value' or 'Encoding'), and the result actually represents lists of pairs
2864+-- so it can be readily concatenated.
2865+class Monoid kv => KeyValuePair v kv where
2866+ pair :: String -> v -> kv
2867+
2868+instance (v ~ Value) => KeyValuePair v (DList Pair) where
2869+ pair k v = DList.singleton (pack k .= v)
2870+
2871+instance (e ~ Encoding) => KeyValuePair e Series where
2872+ pair = E.pairStr