on master help sign in
Use the Builder from bytestring >= 0.10
497f950Bas van Dijk
Data/Aeson/Parser/Internal.hs+36−7
@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, OverloadedStrings #-}
1+{-# LANGUAGE CPP, BangPatterns, OverloadedStrings #-}
2
2
3-- |
3-- |
4-- Module: Data.Aeson.Parser.Internal
4-- Module: Data.Aeson.Parser.Internal
@@ -28,9 +28,19 @@ module Data.Aeson.Parser.Internal
28 , eitherDecodeStrictWith
28 , eitherDecodeStrictWith
29 ) where
29 ) where
30
30
31+#if defined(USE_BLAZE_BUILDER)
31−import Blaze.ByteString.Builder (fromByteString, toByteString)
32+import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
32import Blaze.ByteString.Builder.Char.Utf8 (fromChar)
33import Blaze.ByteString.Builder.Char.Utf8 (fromChar)
33import Blaze.ByteString.Builder.Word (fromWord8)
34import Blaze.ByteString.Builder.Word (fromWord8)
35+#else
36+#if MIN_VERSION_bytestring(0,10,2)
37+import Data.ByteString.Builder
38+#else
39+import Data.ByteString.Lazy.Builder
40+#endif
41+ (Builder, byteString, toLazyByteString, charUtf8, word8)
42+#endif
43+
34import Control.Applicative as A
44import Control.Applicative as A
35import Data.Aeson.Types (Result(..), Value(..))
45import Data.Aeson.Types (Result(..), Value(..))
36import Data.Attoparsec.Char8 hiding (Result)
46import Data.Attoparsec.Char8 hiding (Result)
@@ -203,24 +213,24 @@ unescape = toByteString <$> go mempty where
203 if slash /= backslash || escape == 255
213 if slash /= backslash || escape == 255
204 then fail "invalid JSON escape sequence"
214 then fail "invalid JSON escape sequence"
205 else do
215 else do
206− let cont m = go (acc `mappend` fromByteString h `mappend` m)
216+ let cont m = go (acc `mappend` byteString h `mappend` m)
207 {-# INLINE cont #-}
217 {-# INLINE cont #-}
208 if t /= 117 -- 'u'
218 if t /= 117 -- 'u'
209− then cont (fromWord8 (B.unsafeIndex mapping escape))
219+ then cont (word8 (B.unsafeIndex mapping escape))
210 else do
220 else do
211 a <- hexQuad
221 a <- hexQuad
212 if a < 0xd800 || a > 0xdfff
222 if a < 0xd800 || a > 0xdfff
213− then cont (fromChar (chr a))
223+ then cont (charUtf8 (chr a))
214 else do
224 else do
215 b <- Z.string "\\u" *> hexQuad
225 b <- Z.string "\\u" *> hexQuad
216 if a <= 0xdbff && b >= 0xdc00 && b <= 0xdfff
226 if a <= 0xdbff && b >= 0xdc00 && b <= 0xdfff
217 then let !c = ((a - 0xd800) `shiftL` 10) +
227 then let !c = ((a - 0xd800) `shiftL` 10) +
218 (b - 0xdc00) + 0x10000
228 (b - 0xdc00) + 0x10000
219− in cont (fromChar (chr c))
229+ in cont (charUtf8 (chr c))
220 else fail "invalid UTF-16 surrogates"
230 else fail "invalid UTF-16 surrogates"
221 done <- Z.atEnd
231 done <- Z.atEnd
222 if done
232 if done
223− then return (acc `mappend` fromByteString h)
233+ then return (acc `mappend` byteString h)
224 else rest
234 else rest
225 mapping = "\"\\/\n\t\b\r\f"
235 mapping = "\"\\/\n\t\b\r\f"
226
236
@@ -305,3 +315,22 @@ jsonEOF = json <* skipSpace <* endOfInput
305-- end-of-input. See also: 'json''.
315-- end-of-input. See also: 'json''.
306jsonEOF' :: Parser Value
316jsonEOF' :: Parser Value
307jsonEOF' = json' <* skipSpace <* endOfInput
317jsonEOF' = json' <* skipSpace <* endOfInput
318+
319+#if defined(USE_BLAZE_BUILDER)
320+byteString :: ByteString -> Builder
321+byteString = fromByteString
322+{-# INLINE byteString #-}
323+
324+charUtf8 :: Char -> Builder
325+charUtf8 = fromChar
326+{-# INLINE charUtf8 #-}
327+
328+word8 :: Word8 -> Builder
329+word8 = fromWord8
330+{-# INLINE word8 #-}
331+
332+#else
333+toByteString :: Builder -> ByteString
334+toByteString = L.toStrict . toLazyByteString
335+{-# INLINE toByteString #-}
336+#endif
aeson.cabal+10−1
@@ -91,6 +91,10 @@ flag developer
91 description: operate in developer mode
91 description: operate in developer mode
92 default: False
92 default: False
93
93
94+flag blaze-builder
95+ description: Use blaze-builder instead of bytestring >= 0.10
96+ default: False
97+
94library
98library
95 exposed-modules:
99 exposed-modules:
96 Data.Aeson
100 Data.Aeson
@@ -115,7 +119,6 @@ library
115 build-depends:
119 build-depends:
116 attoparsec >= 0.8.6.1,
120 attoparsec >= 0.8.6.1,
117 base == 4.*,
121 base == 4.*,
118− blaze-builder >= 0.2.1.4,
119 bytestring,
122 bytestring,
120 containers,
123 containers,
121 deepseq,
124 deepseq,
@@ -129,6 +132,12 @@ library
129 unordered-containers >= 0.1.3.0,
132 unordered-containers >= 0.1.3.0,
130 vector >= 0.7.1
133 vector >= 0.7.1
131
134
135+ if flag(blaze-builder)
136+ build-depends: blaze-builder >= 0.2.1.4
137+ cpp-options: -DUSE_BLAZE_BUILDER
138+ else
139+ build-depends: bytestring >= 0.10
140+
132 if flag(developer)
141 if flag(developer)
133 ghc-options: -Werror
142 ghc-options: -Werror
134 ghc-prof-options: -auto-all
143 ghc-prof-options: -auto-all