@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, OverloadedStrings #-}
1+{-# LANGUAGE CPP, BangPatterns, OverloadedStrings #-}
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
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)
36+#if MIN_VERSION_bytestring(0,10,2)
37+import Data.ByteString.Builder
39+import Data.ByteString.Lazy.Builder
41+ (Builder, byteString, toLazyByteString, charUtf8, word8)
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"
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))
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))
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
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"
@@ -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
319+#if defined(USE_BLAZE_BUILDER)
320+byteString :: ByteString -> Builder
321+byteString = fromByteString
322+{-# INLINE byteString #-}
324+charUtf8 :: Char -> Builder
326+{-# INLINE charUtf8 #-}
328+word8 :: Word8 -> Builder
333+toByteString :: Builder -> ByteString
334+toByteString = L.toStrict . toLazyByteString
335+{-# INLINE toByteString #-}