on master help sign in
WIP: Abstract some usages of raw Builder
9f487ceOleg Grenrus
Data/Aeson/Encode/Builder.hs+8−14
@@ -36,7 +36,7 @@ module Data.Aeson.Encode.Builder
36 ) where
36 ) where
37
37
38import Data.Aeson.Internal.Time
38import Data.Aeson.Internal.Time
39−import Data.Aeson.Types.Internal (Encoding(..), Value(..))
39+import Data.Aeson.Types.Internal (Value (..))
40import Data.ByteString.Builder as B
40import Data.ByteString.Builder as B
41import Data.ByteString.Builder.Prim as BP
41import Data.ByteString.Builder.Prim as BP
42import Data.ByteString.Builder.Scientific (scientificBuilder)
42import Data.ByteString.Builder.Scientific (scientificBuilder)
@@ -76,7 +76,7 @@ bool = BP.primBounded (BP.condB id (ascii4 ('t',('r',('u','e'))))
76-- | Encode a JSON array.
76-- | Encode a JSON array.
77array :: V.Vector Value -> Builder
77array :: V.Vector Value -> Builder
78array v
78array v
79− | V.null v = emptyArray__
79+ | V.null v = emptyArray_
80 | otherwise = B.char8 '[' <>
80 | otherwise = B.char8 '[' <>
81 encodeToBuilder (V.unsafeHead v) <>
81 encodeToBuilder (V.unsafeHead v) <>
82 V.foldr withComma (B.char8 ']') (V.unsafeTail v)
82 V.foldr withComma (B.char8 ']') (V.unsafeTail v)
@@ -87,7 +87,7 @@ array v
87object :: HMS.HashMap T.Text Value -> Builder
87object :: HMS.HashMap T.Text Value -> Builder
88object m = case HMS.toList m of
88object m = case HMS.toList m of
89 (x:xs) -> B.char8 '{' <> one x <> foldr withComma (B.char8 '}') xs
89 (x:xs) -> B.char8 '{' <> one x <> foldr withComma (B.char8 '}') xs
90− _ -> emptyObject__
90+ _ -> emptyObject_
91 where
91 where
92 withComma a z = B.char8 ',' <> one a <> z
92 withComma a z = B.char8 ',' <> one a <> z
93 one (k,v) = text k <> B.char8 ':' <> encodeToBuilder v
93 one (k,v) = text k <> B.char8 ':' <> encodeToBuilder v
@@ -98,7 +98,7 @@ text t = B.char8 '"' <> unquoted t <> B.char8 '"'
98
98
99-- | Encode a JSON string, without enclosing quotes.
99-- | Encode a JSON string, without enclosing quotes.
100unquoted :: T.Text -> Builder
100unquoted :: T.Text -> Builder
101−unquoted t = TE.encodeUtf8BuilderEscaped escapeAscii t
101+unquoted = TE.encodeUtf8BuilderEscaped escapeAscii
102
102
103-- | Add quotes surrounding a builder
103-- | Add quotes surrounding a builder
104quote :: Builder -> Builder
104quote :: Builder -> Builder
@@ -135,17 +135,11 @@ number s
135 where
135 where
136 e = base10Exponent s
136 e = base10Exponent s
137
137
138−emptyArray_ :: Encoding
138+emptyArray_ :: Builder
139−emptyArray_ = Encoding emptyArray__
139+emptyArray_ = BP.primBounded (ascii2 ('[',']')) ()
140
140
141−emptyArray__ :: Builder
142−emptyArray__ = BP.primBounded (ascii2 ('[',']')) ()
143−
144−emptyObject_ :: Encoding
141+emptyObject_ :: Builder
145−emptyObject_ = Encoding emptyObject__
146−
147−emptyObject__ :: Builder
148−emptyObject__ = BP.primBounded (ascii2 ('{','}')) ()
142+emptyObject_ = BP.primBounded (ascii2 ('{','}')) ()
149
143
150ascii2 :: (Char, Char) -> BP.BoundedPrim a
144ascii2 :: (Char, Char) -> BP.BoundedPrim a
151ascii2 cs = BP.liftFixedToBounded $ (const cs) BP.>$< BP.char7 >*< BP.char7
145ascii2 cs = BP.liftFixedToBounded $ (const cs) BP.>$< BP.char7 >*< BP.char7
Data/Aeson/Encode/Functions.hs+3−51
@@ -2,22 +2,14 @@
2
2
3module Data.Aeson.Encode.Functions
3module Data.Aeson.Encode.Functions
4 (
4 (
5− brackets
6− , builder
5+ builder
7− , builder'
6+ , char7 -- TODO: used by TH module
8− , char7
9 , encode
7 , encode
10− , list
11− , encodeMap
12− , encodeWithKey
13 ) where
8 ) where
14
9
15−import Data.Aeson.Encode.Builder
10+import Data.Aeson.Encoding
16import Data.Aeson.Types.Class
11import Data.Aeson.Types.Class
17−import Data.Aeson.Types.Internal
18import Data.ByteString.Builder (Builder, char7)
12import Data.ByteString.Builder (Builder, char7)
19−import Data.ByteString.Builder.Prim (primBounded)
20−import Data.Foldable (toList)
21import Data.Monoid ((<>))
13import Data.Monoid ((<>))
22import qualified Data.ByteString.Builder as B
14import qualified Data.ByteString.Builder as B
23import qualified Data.ByteString.Lazy as L
15import qualified Data.ByteString.Lazy as L
@@ -27,53 +19,13 @@ import Data.Foldable (Foldable, foldMap)
27import Data.Monoid (mempty)
19import Data.Monoid (mempty)
28#endif
20#endif
29
21
30−list :: (a -> Encoding) -> [a] -> Encoding
31−list = listEncoding
32−
33builder :: ToJSON a => a -> Builder
22builder :: ToJSON a => a -> Builder
34builder = fromEncoding . toEncoding
23builder = fromEncoding . toEncoding
35{-# INLINE builder #-}
24{-# INLINE builder #-}
36
25
37−builder' :: (a -> Encoding) -> a -> Builder
38−builder' f = fromEncoding . f
39−{-# INLINE builder' #-}
40−
41-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
26-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
42--
27--
43-- This is implemented in terms of the 'ToJSON' class's 'toEncoding' method.
28-- This is implemented in terms of the 'ToJSON' class's 'toEncoding' method.
44encode :: ToJSON a => a -> L.ByteString
29encode :: ToJSON a => a -> L.ByteString
45encode = B.toLazyByteString . builder
30encode = B.toLazyByteString . builder
46{-# INLINE encode #-}
31{-# INLINE encode #-}
47−
48−brackets :: Char -> Char -> Series -> Encoding
49−brackets begin end (Value v) = Encoding $
50− char7 begin <> fromEncoding v <> char7 end
51−brackets begin end Empty = Encoding (primBounded (ascii2 (begin,end)) ())
52−
53−
54−
55−encodeMap :: (k -> Encoding)
56− -> (v -> Encoding)
57− -> (m -> Maybe ((k,v), m))
58− -> ((k -> v -> B.Builder -> B.Builder) -> B.Builder -> m -> B.Builder)
59− -> m -> Encoding
60−encodeMap encodeKey encodeVal minViewWithKey foldrWithKey xs =
61− case minViewWithKey xs of
62− Nothing -> Encoding $ primBounded (ascii2 ('{', '}')) ()
63− Just ((k,v),ys) -> Encoding $
64− B.char7 '{' <> encodeKV encodeKey encodeVal k v <>
65− foldrWithKey go (B.char7 '}') ys
66− where go k v b = B.char7 ',' <> encodeKV encodeKey encodeVal k v <> b
67−{-# INLINE encodeMap #-}
68−
69−encodeWithKey :: (k -> Encoding)
70− -> (v -> Encoding)
71− -> ((k -> v -> Series -> Series) -> Series -> m -> Series)
72− -> m -> Encoding
73−encodeWithKey encodeKey encodeVal foldrWithKey = brackets '{' '}' . foldrWithKey go mempty
74− where go k v c = Value (Encoding $ encodeKV encodeKey encodeVal k v) <> c
75−{-# INLINE encodeWithKey #-}
76−
77−encodeKV :: (k -> Encoding) -> (v -> Encoding) -> k -> v -> B.Builder
78−encodeKV encodeKey encodeVal k v = fromEncoding (encodeKey k) <> B.char7 ':' <> builder' encodeVal v
79−{-# INLINE encodeKV #-}
Data/Aeson/Encoding.hsnew file+128
@@ -0,0 +1,128 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable, RankNTypes #-}
2+module Data.Aeson.Encoding
3+ (
4+ -- * Encoding
5+ Encoding (..) -- TODO: export fromEncoding for now
6+ , fromEncoding
7+ , unsafeToEncoding
8+ , Series (..) -- TODO: don't export constructor
9+ , pairs
10+ -- * Encoding constructors
11+ , emptyArray_
12+ , emptyObject_
13+ , list
14+ , dict
15+ , tuple
16+ , (>*<)
17+ ) where
18+
19+import Data.ByteString.Builder (Builder, char7, toLazyByteString)
20+import Data.Semigroup (Semigroup((<>)))
21+import Data.ByteString.Builder.Prim (primBounded)
22+import Data.Typeable (Typeable)
23+
24+import qualified Data.Aeson.Encode.Builder as B
25+
26+-- | An encoding of a JSON value.
27+newtype Encoding = Encoding {
28+ fromEncoding :: Builder
29+ -- ^ Acquire the underlying bytestring builder.
30+ } deriving (Semigroup,Monoid,Typeable)
31+
32+-- | Make Encoding from Builder.
33+--
34+-- Use with care! You have to make sure that the passed Builder
35+-- is a valid JSON Encoding!
36+unsafeToEncoding :: Builder -> Encoding
37+unsafeToEncoding = Encoding
38+
39+-------------------------------------------------------------------------------
40+-- Encoding instances
41+-------------------------------------------------------------------------------
42+
43+instance Show Encoding where
44+ show (Encoding e) = show (toLazyByteString e)
45+
46+instance Eq Encoding where
47+ Encoding a == Encoding b = toLazyByteString a == toLazyByteString b
48+
49+instance Ord Encoding where
50+ compare (Encoding a) (Encoding b) =
51+ compare (toLazyByteString a) (toLazyByteString b)
52+
53+-- | A series of values that, when encoded, should be separated by
54+-- commas. Since 0.11.0.0, the '.=' operator is overloaded to create
55+-- either @(Text, Value)@ or 'Series'. You can use Series when
56+-- encoding directly to a bytestring builder as in the following
57+-- example:
58+--
59+-- > toEncoding (Person name age) = pairs ("name" .= name <> "age" .= age)
60+data Series = Empty
61+ | Value Encoding
62+ deriving (Typeable)
63+
64+instance Semigroup Series where
65+ Empty <> a = a
66+ Value a <> b =
67+ Value $
68+ a <> case b of
69+ Empty -> mempty
70+ Value c -> Encoding (char7 ',') <> c
71+
72+instance Monoid Series where
73+ mempty = Empty
74+ mappend = (<>)
75+
76+emptyArray_ :: Encoding
77+emptyArray_ = Encoding B.emptyArray_
78+
79+emptyObject_ :: Encoding
80+emptyObject_ = Encoding B.emptyObject_
81+
82+-- | Encode a series of key/value pairs, separated by commas.
83+pairs :: Series -> Encoding
84+pairs = brackets '{' '}'
85+{-# INLINE pairs #-}
86+
87+brackets :: Char -> Char -> Series -> Encoding
88+brackets begin end (Value v) = Encoding $
89+ char7 begin <> fromEncoding v <> char7 end
90+brackets begin end Empty = Encoding (primBounded (B.ascii2 (begin,end)) ())
91+
92+list :: (a -> Encoding) -> [a] -> Encoding
93+list _ [] = emptyArray_
94+list to' (x:xs) = Encoding $
95+ char7 '[' <> fromEncoding (to' x) <> commas xs <> char7 ']'
96+ where
97+ commas = foldr (\v vs -> char7 ',' <> fromEncoding (to' v) <> vs) mempty
98+{-# INLINE list #-}
99+
100+-- | Encode as JSON object
101+dict
102+ :: (k -> Encoding) -- ^ key encoding
103+ -> (v -> Encoding) -- ^ value encoding
104+ -> (forall a. (k -> v -> a -> a) -> a -> m -> a) -- ^ @foldrWithKey@ - indexed fold
105+ -> m -- ^ container
106+ -> Encoding
107+dict encodeKey encodeVal foldrWithKey = brackets '{' '}' . foldrWithKey go mempty
108+ where
109+ go k v c = Value (encodeKV k v) <> c
110+ encodeKV k v = encodeKey k <> Encoding (char7 ':') <> encodeVal v
111+{-# INLINE dict #-}
112+
113+infixr 6 >*<
114+-- | See 'tuple'.
115+(>*<) :: Encoding -> Encoding -> Encoding
116+Encoding a >*< Encoding b = Encoding $ a <> char7 ',' <> b
117+{-# INLINE (>*<) #-}
118+
119+-- | Encode as a tuple.
120+--
121+-- @
122+-- toEncoding (X a b c) = tuple $
123+-- toEncoding a >*<
124+-- toEncoding b >*<
125+-- toEncoding c
126+tuple :: Encoding -> Encoding
127+tuple b = Encoding (char7 '[' <> fromEncoding b <> char7 ']')
128+{-# INLINE tuple #-}
Data/Aeson/TH.hs+2−1
@@ -107,7 +107,8 @@ import Data.Aeson.Types ( Value(..), Parser
107 , defaultOptions
107 , defaultOptions
108 , defaultTaggedObject
108 , defaultTaggedObject
109 )
109 )
110−import Data.Aeson.Types.Internal (Encoding(..), (<?>), JSONPathElement(Key))
110+import Data.Aeson.Types.Internal ((<?>), JSONPathElement(Key))
111+import Data.Aeson.Encoding ( Encoding(..) )
111import Control.Monad ( liftM2, return, mapM, fail )
112import Control.Monad ( liftM2, return, mapM, fail )
112import Data.Bool ( Bool(False, True), otherwise, (&&), not )
113import Data.Bool ( Bool(False, True), otherwise, (&&), not )
113import Data.Either ( Either(Left, Right) )
114import Data.Either ( Either(Left, Right) )
Data/Aeson/Types.hs+1−6
@@ -97,16 +97,11 @@ module Data.Aeson.Types
97import Data.Aeson.Types.Generic ()
97import Data.Aeson.Types.Generic ()
98import Data.Aeson.Types.Instances
98import Data.Aeson.Types.Instances
99import Data.Aeson.Types.Internal
99import Data.Aeson.Types.Internal
100+import Data.Aeson.Encoding (Encoding, unsafeToEncoding, fromEncoding, Series, pairs)
100
101
101import Data.Foldable (Foldable, toList)
102import Data.Foldable (Foldable, toList)
102−import Data.Aeson.Encode.Functions (brackets)
103
103
104-- | Encode a 'Foldable' as a JSON array.
104-- | Encode a 'Foldable' as a JSON array.
105foldable :: (Foldable t, ToJSON a) => t a -> Encoding
105foldable :: (Foldable t, ToJSON a) => t a -> Encoding
106foldable = toEncoding . toList
106foldable = toEncoding . toList
107{-# INLINE foldable #-}
107{-# INLINE foldable #-}
108−
109−-- | Encode a series of key/value pairs, separated by commas.
110−pairs :: Series -> Encoding
111−pairs = brackets '{' '}'
112−{-# INLINE pairs #-}
Data/Aeson/Types/Class.hs+15−8
@@ -1,5 +1,5 @@
1−{-# LANGUAGE CPP, GADTs, DefaultSignatures, FlexibleContexts, DeriveFunctor,
1+{-# LANGUAGE CPP, GADTs, DefaultSignatures, FlexibleInstances, FlexibleContexts, DeriveFunctor,
2− ScopedTypeVariables #-}
2+ ScopedTypeVariables, TypeSynonymInstances #-}
3
3
4-- |
4-- |
5-- Module: Data.Aeson.Types.Class
5-- Module: Data.Aeson.Types.Class
@@ -58,7 +58,7 @@ import Data.Aeson.Types.Internal
58import Data.Monoid ((<>))
58import Data.Monoid ((<>))
59import Data.Text (Text)
59import Data.Text (Text)
60import GHC.Generics (Generic, Rep, from, to)
60import GHC.Generics (Generic, Rep, from, to)
61−import Data.Aeson.Encode.Builder (emptyArray_)
61+import Data.Aeson.Encoding (Encoding (..), Series (..), emptyArray_, list)
62import qualified Data.ByteString.Builder as B
62import qualified Data.ByteString.Builder as B
63import qualified Data.Aeson.Encode.Builder as E
63import qualified Data.Aeson.Encode.Builder as E
64import qualified Data.Vector as V
64import qualified Data.Vector as V
@@ -325,6 +325,17 @@ class KeyValue kv where
325 (.=) :: ToJSON v => Text -> v -> kv
325 (.=) :: ToJSON v => Text -> v -> kv
326 infixr 8 .=
326 infixr 8 .=
327
327
328+instance KeyValue Series where
329+ name .= value = Value . Encoding $
330+ E.text name <> B.char7 ':' <> builder value
331+ where
332+ builder = fromEncoding . toEncoding
333+ {-# INLINE (.=) #-}
334+
335+instance KeyValue Pair where
336+ name .= value = (name, toJSON value)
337+ {-# INLINE (.=) #-}
338+
328-------------------------------------------------------------------------------
339-------------------------------------------------------------------------------
329-- Classes and types for map keys
340-- Classes and types for map keys
330-------------------------------------------------------------------------------
341-------------------------------------------------------------------------------
@@ -525,11 +536,7 @@ toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingLis
525-------------------------------------------------------------------------------
536-------------------------------------------------------------------------------
526
537
527listEncoding :: (a -> Encoding) -> [a] -> Encoding
538listEncoding :: (a -> Encoding) -> [a] -> Encoding
528−listEncoding _ [] = emptyArray_
539+listEncoding = list
529−listEncoding to' (x:xs) = Encoding $
530− B.char7 '[' <> fromEncoding (to' x) <> commas xs <> B.char7 ']'
531− where commas = foldr (\v vs -> B.char7 ',' <> fromEncoding (to' v) <> vs) mempty
532−{-# INLINE listEncoding #-}
533
540
534listValue :: (a -> Value) -> [a] -> Value
541listValue :: (a -> Value) -> [a] -> Value
535listValue f = Array . V.fromList . map f
542listValue f = Array . V.fromList . map f
Data/Aeson/Types/Generic.hs+4−3
@@ -24,9 +24,10 @@ module Data.Aeson.Types.Generic ( ) where
24import Control.Applicative ((<|>))
24import Control.Applicative ((<|>))
25import Control.Monad ((<=<))
25import Control.Monad ((<=<))
26import Control.Monad.ST (ST)
26import Control.Monad.ST (ST)
27−import Data.Aeson.Encode.Builder (emptyArray_)
27+import Data.Aeson.Encoding (Encoding (..), emptyArray_)
28import Data.Aeson.Encode.Functions (builder)
28import Data.Aeson.Encode.Functions (builder)
29import Data.Aeson.Types.Instances
29import Data.Aeson.Types.Instances
30+import Data.Aeson.Encoding ((>*<), tuple)
30import Data.Aeson.Types.Internal
31import Data.Aeson.Types.Internal
31import Data.Bits (unsafeShiftR)
32import Data.Bits (unsafeShiftR)
32import Data.ByteString.Builder as B
33import Data.ByteString.Builder as B
@@ -299,8 +300,8 @@ instance (TwoElemArrayEnc a, TwoElemArrayEnc b) => TwoElemArrayEnc (a :+: b) whe
299instance ( GToEncoding a, ConsToEncoding a
300instance ( GToEncoding a, ConsToEncoding a
300 , Constructor c ) => TwoElemArrayEnc (C1 c a) where
301 , Constructor c ) => TwoElemArrayEnc (C1 c a) where
301 twoElemArrayEnc opts x = fromEncoding . tuple $
302 twoElemArrayEnc opts x = fromEncoding . tuple $
302− builder (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
303+ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p))) >*<
303− gbuilder opts x
304+ gToEncoding opts x
304
305
305--------------------------------------------------------------------------------
306--------------------------------------------------------------------------------
306
307
Data/Aeson/Types/Instances.hs+13−22
@@ -78,8 +78,6 @@ module Data.Aeson.Types.Instances
78 , (.:?)
78 , (.:?)
79 , (.:!)
79 , (.:!)
80 , (.!=)
80 , (.!=)
81− , tuple
82− , (>*<)
83 , typeMismatch
81 , typeMismatch
84
82
85 , listEncoding
83 , listEncoding
@@ -87,10 +85,12 @@ module Data.Aeson.Types.Instances
87 , listParser
85 , listParser
88 ) where
86 ) where
89
87
90−import Data.Aeson.Types.Instances.Tuple (tuple, (>*<))
88+import Data.Aeson.Types.Instances.Tuple ()
89+
90+import Data.Aeson.Encoding (Encoding (..), emptyArray_, dict, tuple, (>*<))
91
91
92import Control.Applicative (Const(..))
92import Control.Applicative (Const(..))
93−import Data.Aeson.Encode.Functions (builder, builder', encode, list, encodeWithKey, encodeMap)
93+import Data.Aeson.Encode.Functions (builder, encode)
94import Data.Aeson.Functions (mapHashKeyVal, mapKey, mapKeyVal)
94import Data.Aeson.Functions (mapHashKeyVal, mapKey, mapKeyVal)
95import Data.Aeson.Types.Class
95import Data.Aeson.Types.Class
96import Data.Aeson.Types.Internal
96import Data.Aeson.Types.Internal
@@ -398,7 +398,7 @@ instance ToJSON () where
398 toJSON _ = emptyArray
398 toJSON _ = emptyArray
399 {-# INLINE toJSON #-}
399 {-# INLINE toJSON #-}
400
400
401− toEncoding _ = E.emptyArray_
401+ toEncoding _ = emptyArray_
402 {-# INLINE toEncoding #-}
402 {-# INLINE toEncoding #-}
403
403
404instance FromJSON () where
404instance FromJSON () where
@@ -823,11 +823,11 @@ instance ToJSONKey k => ToJSON1 (M.Map k) where
823
823
824 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
824 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
825 liftToEncoding g _ = case toJSONKey of
825 liftToEncoding g _ = case toJSONKey of
826− ToJSONKeyText (_,f) -> encodeMap f g M.minViewWithKey M.foldrWithKey
826+ ToJSONKeyText (_,f) -> dict f g M.foldrWithKey
827− ToJSONKeyValue (_,f) -> list (pairEncoding f) . M.toList
827+ ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . M.toList
828 where
828 where
829 -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
829 -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
830− pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder' g b
830+ pairEncoding f (a, b) = tuple $ f a >*< g b
831 {-# INLINE liftToEncoding #-}
831 {-# INLINE liftToEncoding #-}
832
832
833instance (FromJSONKey k, Ord k) => FromJSON1 (M.Map k) where
833instance (FromJSONKey k, Ord k) => FromJSON1 (M.Map k) where
@@ -902,7 +902,7 @@ instance ToJSON1 Vector where
902 {-# INLINE liftToJSON #-}
902 {-# INLINE liftToJSON #-}
903
903
904 liftToEncoding to _ xs
904 liftToEncoding to _ xs
905− | V.null xs = E.emptyArray_
905+ | V.null xs = emptyArray_
906 | otherwise = Encoding $
906 | otherwise = Encoding $
907 B.char7 '[' <> fromEncoding (to (V.unsafeHead xs)) <>
907 B.char7 '[' <> fromEncoding (to (V.unsafeHead xs)) <>
908 V.foldr go (B.char7 ']') (V.unsafeTail xs)
908 V.foldr go (B.char7 ']') (V.unsafeTail xs)
@@ -919,7 +919,7 @@ instance (ToJSON a) => ToJSON (Vector a) where
919
919
920encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
920encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
921encodeVector xs
921encodeVector xs
922− | VG.null xs = E.emptyArray_
922+ | VG.null xs = emptyArray_
923 | otherwise = Encoding $
923 | otherwise = Encoding $
924 B.char7 '[' <> builder (VG.unsafeHead xs) <>
924 B.char7 '[' <> builder (VG.unsafeHead xs) <>
925 VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
925 VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
@@ -1006,11 +1006,11 @@ instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1006
1006
1007 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1007 -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1008 liftToEncoding g _ = case toJSONKey of
1008 liftToEncoding g _ = case toJSONKey of
1009− ToJSONKeyText (_,f) -> encodeWithKey f g H.foldrWithKey
1009+ ToJSONKeyText (_,f) -> dict f g H.foldrWithKey
1010− ToJSONKeyValue (_,f) -> list (pairEncoding f) . H.toList
1010+ ToJSONKeyValue (_,f) -> listEncoding (pairEncoding f) . H.toList
1011 where
1011 where
1012 -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1012 -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1013− pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder' g b
1013+ pairEncoding f (a, b) = tuple $ f a >*< g b
1014 {-# INLINE liftToEncoding #-}
1014 {-# INLINE liftToEncoding #-}
1015
1015
1016instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1016instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
@@ -1367,15 +1367,6 @@ withBool _ f (Bool arr) = f arr
1367withBool expected _ v = typeMismatch expected v
1367withBool expected _ v = typeMismatch expected v
1368{-# INLINE withBool #-}
1368{-# INLINE withBool #-}
1369
1369
1370−instance KeyValue Pair where
1371− name .= value = (name, toJSON value)
1372− {-# INLINE (.=) #-}
1373−
1374−instance KeyValue Series where
1375− name .= value = Value . Encoding $
1376− E.text name <> B.char7 ':' <> builder value
1377− {-# INLINE (.=) #-}
1378−
1379-- | Convert a value from JSON, failing if the types do not match.
1370-- | Convert a value from JSON, failing if the types do not match.
1380fromJSON :: (FromJSON a) => Value -> Result a
1371fromJSON :: (FromJSON a) => Value -> Result a
1381fromJSON = parse parseJSON
1372fromJSON = parse parseJSON
Data/Aeson/Types/Instances/Tuple.hs+121−133
@@ -10,16 +10,15 @@
10-- Portability: portable
10-- Portability: portable
11--
11--
12-- Tuple instances
12-- Tuple instances
13−module Data.Aeson.Types.Instances.Tuple (tuple, (>*<)) where
13+module Data.Aeson.Types.Instances.Tuple () where
14
14
15−import Data.Aeson.Encode.Functions (builder)
15+import Data.Aeson.Encoding (tuple, (>*<))
16import Data.Aeson.Types.Class
16import Data.Aeson.Types.Class
17import Data.Aeson.Types.Internal
17import Data.Aeson.Types.Internal
18import Data.Monoid ((<>))
18import Data.Monoid ((<>))
19
19
20import qualified Data.Vector as V
20import qualified Data.Vector as V
21import qualified Data.Vector.Mutable as VM (unsafeNew, unsafeWrite)
21import qualified Data.Vector.Mutable as VM (unsafeNew, unsafeWrite)
22−import qualified Data.ByteString.Builder as B
23
22
24#if MIN_VERSION_base(4,8,0)
23#if MIN_VERSION_base(4,8,0)
25#else
24#else
@@ -29,15 +28,6 @@ import Control.Applicative ((<$>), (<*>))
29parseJSONElemAtIndex :: (Value -> Parser a) -> Int -> V.Vector Value -> Parser a
28parseJSONElemAtIndex :: (Value -> Parser a) -> Int -> V.Vector Value -> Parser a
30parseJSONElemAtIndex p idx ary = p (V.unsafeIndex ary idx) <?> Index idx
29parseJSONElemAtIndex p idx ary = p (V.unsafeIndex ary idx) <?> Index idx
31
30
32−(>*<) :: B.Builder -> B.Builder -> B.Builder
33−a >*< b = a <> B.char7 ',' <> b
34−{-# INLINE (>*<) #-}
35−infixr 6 >*<
36−
37−tuple :: B.Builder -> Encoding
38−tuple b = Encoding (B.char7 '[' <> b <> B.char7 ']')
39−{-# INLINE tuple #-}
40−
41-- Local copy of withArray
31-- Local copy of withArray
42withArray :: String -> (Array -> Parser a) -> Value -> Parser a
32withArray :: String -> (Array -> Parser a) -> Value -> Parser a
43withArray _ f (Array arr) = f arr
33withArray _ f (Array arr) = f arr
@@ -57,8 +47,8 @@ instance ToJSON2 ((,) ) where
57 {-# INLINE liftToJSON2 #-}
47 {-# INLINE liftToJSON2 #-}
58
48
59 liftToEncoding2 toA _ toB _ (a, b) = tuple $
49 liftToEncoding2 toA _ toB _ (a, b) = tuple $
60− fromEncoding (toA a) >*<
50+ toA a >*<
61− fromEncoding (toB b)
51+ toB b
62 {-# INLINE liftToEncoding2 #-}
52 {-# INLINE liftToEncoding2 #-}
63
53
64instance (ToJSON a) => ToJSON1 ((,) a) where
54instance (ToJSON a) => ToJSON1 ((,) a) where
@@ -102,9 +92,9 @@ instance (ToJSON a) => ToJSON2 ((,,) a) where
102 {-# INLINE liftToJSON2 #-}
92 {-# INLINE liftToJSON2 #-}
103
93
104 liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
94 liftToEncoding2 toB _ toC _ (a, b, c) = tuple $
105− builder a >*<
95+ toEncoding a >*<
106− fromEncoding (toB b) >*<
96+ toB b >*<
107− fromEncoding (toC c)
97+ toC c
108 {-# INLINE liftToEncoding2 #-}
98 {-# INLINE liftToEncoding2 #-}
109
99
110instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
100instance (ToJSON a, ToJSON b) => ToJSON1 ((,,) a b) where
@@ -150,10 +140,10 @@ instance (ToJSON a, ToJSON b) => ToJSON2 ((,,,) a b) where
150 {-# INLINE liftToJSON2 #-}
140 {-# INLINE liftToJSON2 #-}
151
141
152 liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
142 liftToEncoding2 toC _ toD _ (a, b, c, d) = tuple $
153− builder a >*<
143+ toEncoding a >*<
154− builder b >*<
144+ toEncoding b >*<
155− fromEncoding (toC c) >*<
145+ toC c >*<
156− fromEncoding (toD d)
146+ toD d
157 {-# INLINE liftToEncoding2 #-}
147 {-# INLINE liftToEncoding2 #-}
158
148
159instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
149instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON1 ((,,,) a b c) where
@@ -201,11 +191,11 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON2 ((,,,,) a b c) where
201 {-# INLINE liftToJSON2 #-}
191 {-# INLINE liftToJSON2 #-}
202
192
203 liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
193 liftToEncoding2 toD _ toE _ (a, b, c, d, e) = tuple $
204− builder a >*<
194+ toEncoding a >*<
205− builder b >*<
195+ toEncoding b >*<
206− builder c >*<
196+ toEncoding c >*<
207− fromEncoding (toD d) >*<
197+ toD d >*<
208− fromEncoding (toE e)
198+ toE e
209 {-# INLINE liftToEncoding2 #-}
199 {-# INLINE liftToEncoding2 #-}
210
200
211instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
201instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 ((,,,,) a b c d) where
@@ -255,12 +245,12 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 ((,,,,,) a b c d) w
255 {-# INLINE liftToJSON2 #-}
245 {-# INLINE liftToJSON2 #-}
256
246
257 liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
247 liftToEncoding2 toE _ toF _ (a, b, c, d, e, f) = tuple $
258− builder a >*<
248+ toEncoding a >*<
259− builder b >*<
249+ toEncoding b >*<
260− builder c >*<
250+ toEncoding c >*<
261− builder d >*<
251+ toEncoding d >*<
262− fromEncoding (toE e) >*<
252+ toE e >*<
263− fromEncoding (toF f)
253+ toF f
264 {-# INLINE liftToEncoding2 #-}
254 {-# INLINE liftToEncoding2 #-}
265
255
266instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
256instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 ((,,,,,) a b c d e) where
@@ -312,13 +302,13 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 ((,,,,,,)
312 {-# INLINE liftToJSON2 #-}
302 {-# INLINE liftToJSON2 #-}
313
303
314 liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
304 liftToEncoding2 toF _ toG _ (a, b, c, d, e, f, g) = tuple $
315− builder a >*<
305+ toEncoding a >*<
316− builder b >*<
306+ toEncoding b >*<
317− builder c >*<
307+ toEncoding c >*<
318− builder d >*<
308+ toEncoding d >*<
319− builder e >*<
309+ toEncoding e >*<
320− fromEncoding (toF f) >*<
310+ toF f >*<
321− fromEncoding (toG g)
311+ toG g
322 {-# INLINE liftToEncoding2 #-}
312 {-# INLINE liftToEncoding2 #-}
323
313
324instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
314instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 ((,,,,,,) a b c d e f) where
@@ -372,14 +362,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2
372 {-# INLINE liftToJSON2 #-}
362 {-# INLINE liftToJSON2 #-}
373
363
374 liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
364 liftToEncoding2 toG _ toH _ (a, b, c, d, e, f, g, h) = tuple $
375− builder a >*<
365+ toEncoding a >*<
376− builder b >*<
366+ toEncoding b >*<
377− builder c >*<
367+ toEncoding c >*<
378− builder d >*<
368+ toEncoding d >*<
379− builder e >*<
369+ toEncoding e >*<
380− builder f >*<
370+ toEncoding f >*<
381− fromEncoding (toG g) >*<
371+ toG g >*<
382− fromEncoding (toH h)
372+ toH h
383 {-# INLINE liftToEncoding2 #-}
373 {-# INLINE liftToEncoding2 #-}
384
374
385instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
375instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 ((,,,,,,,) a b c d e f g) where
@@ -435,15 +425,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g)
435 {-# INLINE liftToJSON2 #-}
425 {-# INLINE liftToJSON2 #-}
436
426
437 liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
427 liftToEncoding2 toH _ toI _ (a, b, c, d, e, f, g, h, i) = tuple $
438− builder a >*<
428+ toEncoding a >*<
439− builder b >*<
429+ toEncoding b >*<
440− builder c >*<
430+ toEncoding c >*<
441− builder d >*<
431+ toEncoding d >*<
442− builder e >*<
432+ toEncoding e >*<
443− builder f >*<
433+ toEncoding f >*<
444− builder g >*<
434+ toEncoding g >*<
445− fromEncoding (toH h) >*<
435+ toH h >*<
446− fromEncoding (toI i)
436+ toI i
447 {-# INLINE liftToEncoding2 #-}
437 {-# INLINE liftToEncoding2 #-}
448
438
449instance (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
439instance (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
@@ -501,16 +491,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
501 {-# INLINE liftToJSON2 #-}
491 {-# INLINE liftToJSON2 #-}
502
492
503 liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
493 liftToEncoding2 toI _ toJ _ (a, b, c, d, e, f, g, h, i, j) = tuple $
504− builder a >*<
494+ toEncoding a >*<
505− builder b >*<
495+ toEncoding b >*<
506− builder c >*<
496+ toEncoding c >*<
507− builder d >*<
497+ toEncoding d >*<
508− builder e >*<
498+ toEncoding e >*<
509− builder f >*<
499+ toEncoding f >*<
510− builder g >*<
500+ toEncoding g >*<
511− builder h >*<
501+ toEncoding h >*<
512− fromEncoding (toI i) >*<
502+ toI i >*<
513− fromEncoding (toJ j)
503+ toJ j
514 {-# INLINE liftToEncoding2 #-}
504 {-# INLINE liftToEncoding2 #-}
515
505
516instance (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
506instance (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
@@ -570,17 +560,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
570 {-# INLINE liftToJSON2 #-}
560 {-# INLINE liftToJSON2 #-}
571
561
572 liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
562 liftToEncoding2 toJ _ toK _ (a, b, c, d, e, f, g, h, i, j, k) = tuple $
573− builder a >*<
563+ toEncoding a >*<
574− builder b >*<
564+ toEncoding b >*<
575− builder c >*<
565+ toEncoding c >*<
576− builder d >*<
566+ toEncoding d >*<
577− builder e >*<
567+ toEncoding e >*<
578− builder f >*<
568+ toEncoding f >*<
579− builder g >*<
569+ toEncoding g >*<
580− builder h >*<
570+ toEncoding h >*<
581− builder i >*<
571+ toEncoding i >*<
582− fromEncoding (toJ j) >*<
572+ toJ j >*<
583− fromEncoding (toK k)
573+ toK k
584 {-# INLINE liftToEncoding2 #-}
574 {-# INLINE liftToEncoding2 #-}
585
575
586instance (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
576instance (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
@@ -642,18 +632,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
642 {-# INLINE liftToJSON2 #-}
632 {-# INLINE liftToJSON2 #-}
643
633
644 liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
634 liftToEncoding2 toK _ toL _ (a, b, c, d, e, f, g, h, i, j, k, l) = tuple $
645− builder a >*<
635+ toEncoding a >*<
646− builder b >*<
636+ toEncoding b >*<
647− builder c >*<
637+ toEncoding c >*<
648− builder d >*<
638+ toEncoding d >*<
649− builder e >*<
639+ toEncoding e >*<
650− builder f >*<
640+ toEncoding f >*<
651− builder g >*<
641+ toEncoding g >*<
652− builder h >*<
642+ toEncoding h >*<
653− builder i >*<
643+ toEncoding i >*<
654− builder j >*<
644+ toEncoding j >*<
655− fromEncoding (toK k) >*<
645+ toK k >*<
656− fromEncoding (toL l)
646+ toL l
657 {-# INLINE liftToEncoding2 #-}
647 {-# INLINE liftToEncoding2 #-}
658
648
659instance (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
649instance (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
@@ -717,19 +707,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
717 {-# INLINE liftToJSON2 #-}
707 {-# INLINE liftToJSON2 #-}
718
708
719 liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
709 liftToEncoding2 toL _ toM _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = tuple $
720− builder a >*<
710+ toEncoding a >*<
721− builder b >*<
711+ toEncoding b >*<
722− builder c >*<
712+ toEncoding c >*<
723− builder d >*<
713+ toEncoding d >*<
724− builder e >*<
714+ toEncoding e >*<
725− builder f >*<
715+ toEncoding f >*<
726− builder g >*<
716+ toEncoding g >*<
727− builder h >*<
717+ toEncoding h >*<
728− builder i >*<
718+ toEncoding i >*<
729− builder j >*<
719+ toEncoding j >*<
730− builder k >*<
720+ toEncoding k >*<
731− fromEncoding (toL l) >*<
721+ toL l >*<
732− fromEncoding (toM m)
722+ toM m
733 {-# INLINE liftToEncoding2 #-}
723 {-# INLINE liftToEncoding2 #-}
734
724
735instance (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
725instance (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
@@ -795,20 +785,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
795 {-# INLINE liftToJSON2 #-}
785 {-# INLINE liftToJSON2 #-}
796
786
797 liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
787 liftToEncoding2 toM _ toN _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = tuple $
798− builder a >*<
788+ toEncoding a >*<
799− builder b >*<
789+ toEncoding b >*<
800− builder c >*<
790+ toEncoding c >*<
801− builder d >*<
791+ toEncoding d >*<
802− builder e >*<
792+ toEncoding e >*<
803− builder f >*<
793+ toEncoding f >*<
804− builder g >*<
794+ toEncoding g >*<
805− builder h >*<
795+ toEncoding h >*<
806− builder i >*<
796+ toEncoding i >*<
807− builder j >*<
797+ toEncoding j >*<
808− builder k >*<
798+ toEncoding k >*<
809− builder l >*<
799+ toEncoding l >*<
810− fromEncoding (toM m) >*<
800+ toM m >*<
811− fromEncoding (toN n)
801+ toN n
812 {-# INLINE liftToEncoding2 #-}
802 {-# INLINE liftToEncoding2 #-}
813
803
814instance (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
804instance (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
@@ -876,21 +866,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
876 {-# INLINE liftToJSON2 #-}
866 {-# INLINE liftToJSON2 #-}
877
867
878 liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
868 liftToEncoding2 toN _ toO _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = tuple $
879− builder a >*<
869+ toEncoding a >*<
880− builder b >*<
870+ toEncoding b >*<
881− builder c >*<
871+ toEncoding c >*<
882− builder d >*<
872+ toEncoding d >*<
883− builder e >*<
873+ toEncoding e >*<
884− builder f >*<
874+ toEncoding f >*<
885− builder g >*<
875+ toEncoding g >*<
886− builder h >*<
876+ toEncoding h >*<
887− builder i >*<
877+ toEncoding i >*<
888− builder j >*<
878+ toEncoding j >*<
889− builder k >*<
879+ toEncoding k >*<
890− builder l >*<
880+ toEncoding l >*<
891− builder m >*<
881+ toEncoding m >*<
892− fromEncoding (toN n) >*<
882+ toN n >*<
893− fromEncoding (toO o)
883+ toO o
894 {-# INLINE liftToEncoding2 #-}
884 {-# INLINE liftToEncoding2 #-}
895
885
896instance (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
886instance (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
@@ -935,5 +925,3 @@ instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f
935instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) where
925instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) where
936 parseJSON = parseJSON2
926 parseJSON = parseJSON2
937 {-# INLINE parseJSON #-}
927 {-# INLINE parseJSON #-}
938−
939−
Data/Aeson/Types/Internal.hs−50
@@ -24,9 +24,6 @@ module Data.Aeson.Types.Internal
24 (
24 (
25 -- * Core JSON types
25 -- * Core JSON types
26 Value(..)
26 Value(..)
27− , Encoding(..)
28− , unsafeToEncoding
29− , Series(..)
30 , Array
27 , Array
31 , emptyArray, isEmptyArray
28 , emptyArray, isEmptyArray
32 , Pair
29 , Pair
@@ -67,7 +64,6 @@ import Control.Applicative
67import Control.DeepSeq (NFData(..))
64import Control.DeepSeq (NFData(..))
68import Control.Monad (MonadPlus(..), ap)
65import Control.Monad (MonadPlus(..), ap)
69import qualified Control.Monad.Fail as Fail
66import qualified Control.Monad.Fail as Fail
70−import Data.ByteString.Builder (Builder, char7, toLazyByteString)
71import Data.Char (isLower, isUpper, toLower, isAlpha, isAlphaNum)
67import Data.Char (isLower, isUpper, toLower, isAlpha, isAlphaNum)
72import Data.Data (Data)
68import Data.Data (Data)
73import Data.Foldable (foldl')
69import Data.Foldable (foldl')
@@ -338,52 +334,6 @@ data Value = Object !Object
338 | Null
334 | Null
339 deriving (Eq, Read, Show, Typeable, Data)
335 deriving (Eq, Read, Show, Typeable, Data)
340
336
341−-- | An encoding of a JSON value.
342−newtype Encoding = Encoding {
343− fromEncoding :: Builder
344− -- ^ Acquire the underlying bytestring builder.
345− } deriving (Semigroup,Monoid)
346−
347−-- | Make Encoding from Builder.
348−--
349−-- Use with care! You have to make sure that the passed Builder
350−-- is a valid JSON Encoding!
351−unsafeToEncoding :: Builder -> Encoding
352−unsafeToEncoding = Encoding
353−
354−instance Show Encoding where
355− show (Encoding e) = show (toLazyByteString e)
356−
357−instance Eq Encoding where
358− Encoding a == Encoding b = toLazyByteString a == toLazyByteString b
359−
360−instance Ord Encoding where
361− compare (Encoding a) (Encoding b) =
362− compare (toLazyByteString a) (toLazyByteString b)
363−
364−-- | A series of values that, when encoded, should be separated by
365−-- commas. Since 0.11.0.0, the '.=' operator is overloaded to create
366−-- either @(Text, Value)@ or 'Series'. You can use Series when
367−-- encoding directly to a bytestring builder as in the following
368−-- example:
369−--
370−-- > toEncoding (Person name age) = pairs ("name" .= name <> "age" .= age)
371−data Series = Empty
372− | Value Encoding
373− deriving (Typeable)
374−
375−instance Semigroup Series where
376− Empty <> a = a
377− Value a <> b =
378− Value $
379− a <> case b of
380− Empty -> mempty
381− Value c -> Encoding (char7 ',') <> c
382−
383−instance Monoid Series where
384− mempty = Empty
385− mappend = (<>)
386−
387-- | A newtype wrapper for 'UTCTime' that uses the same non-standard
337-- | A newtype wrapper for 'UTCTime' that uses the same non-standard
388-- serialization format as Microsoft .NET, whose
338-- serialization format as Microsoft .NET, whose
389-- <https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx System.DateTime>
339-- <https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx System.DateTime>
aeson.cabal+1
@@ -70,6 +70,7 @@ library
70 exposed-modules:
70 exposed-modules:
71 Data.Aeson
71 Data.Aeson
72 Data.Aeson.Encode
72 Data.Aeson.Encode
73+ Data.Aeson.Encoding
73 Data.Aeson.Internal
74 Data.Aeson.Internal
74 Data.Aeson.Internal.Time
75 Data.Aeson.Internal.Time
75 Data.Aeson.Parser
76 Data.Aeson.Parser
benchmarks/aeson-benchmarks.cabal+1
@@ -15,6 +15,7 @@ library
15
15
16 exposed-modules:
16 exposed-modules:
17 Data.Aeson
17 Data.Aeson
18+ Data.Aeson.Encoding
18 Data.Aeson.Encode
19 Data.Aeson.Encode
19 Data.Aeson.Encode.Builder
20 Data.Aeson.Encode.Builder
20 Data.Aeson.Encode.Functions
21 Data.Aeson.Encode.Functions
tuple-instances.hs+4−4
@@ -1,5 +1,5 @@
1#!/usr/bin/env stack
1#!/usr/bin/env stack
2−{- stack --resolver=lts-5.9 runghc
2+{- stack --resolver=lts-6.0 runghc
3 --package mtl
3 --package mtl
4 --package dlist
4 --package dlist
5-}
5-}
@@ -45,9 +45,9 @@ tuple n = fold $ flip execState DList.empty $ do
45 t [" liftToEncoding2 ", toPrev, " _ ", toLast, " _ ", vs', " = tuple $" ]
45 t [" liftToEncoding2 ", toPrev, " _ ", toLast, " _ ", vs', " = tuple $" ]
46 forM_ (zip [0..] vs) $ \(i, v) -> t . (" " :) $ case i of
46 forM_ (zip [0..] vs) $ \(i, v) -> t . (" " :) $ case i of
47
47
48− _ | i == n - 1 -> [ "fromEncoding (", toLast, " ", v, ")" ]
48+ _ | i == n - 1 -> [ toLast, " ", v ]
49− _ | i == n - 2 -> [ "fromEncoding (", toPrev, " ", v, ") >*<" ]
49+ _ | i == n - 2 -> [ toPrev, " ", v, " >*<" ]
50− _ | otherwise -> [ "builder ", v, " >*<" ]
50+ _ | otherwise -> [ "toEncoding ", v, " >*<" ]
51 t [" {-# INLINE liftToEncoding2 #-}" ]
51 t [" {-# INLINE liftToEncoding2 #-}" ]
52 t []
52 t []
53
53