@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, OverloadedStrings #-}
1+{-# LANGUAGE CPP, BangPatterns, OverloadedStrings #-}
22
33-- |
44-- Module: Data.Aeson.Parser.Internal
@@ -28,9 +28,19 @@ module Data.Aeson.Parser.Internal
2828 , eitherDecodeStrictWith
2929 ) where
3030
31−import Blaze.ByteString.Builder (fromByteString, toByteString)
31+#if defined(USE_BLAZE_BUILDER)
32+import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
3233import Blaze.ByteString.Builder.Char.Utf8 (fromChar)
3334import 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+
3444import Control.Applicative as A
3545import Data.Aeson.Types (Result(..), Value(..))
3646import Data.Attoparsec.Char8 hiding (Result)
@@ -203,24 +213,24 @@ unescape = toByteString <$> go mempty where
203213 if slash /= backslash || escape == 255
204214 then fail "invalid JSON escape sequence"
205215 else do
206− let cont m = go (acc `mappend` fromByteString h `mappend` m)
216+ let cont m = go (acc `mappend` byteString h `mappend` m)
207217 {-# INLINE cont #-}
208218 if t /= 117 -- 'u'
209− then cont (fromWord8 (B.unsafeIndex mapping escape))
219+ then cont (word8 (B.unsafeIndex mapping escape))
210220 else do
211221 a <- hexQuad
212222 if a < 0xd800 || a > 0xdfff
213− then cont (fromChar (chr a))
223+ then cont (charUtf8 (chr a))
214224 else do
215225 b <- Z.string "\\u" *> hexQuad
216226 if a <= 0xdbff && b >= 0xdc00 && b <= 0xdfff
217227 then let !c = ((a - 0xd800) `shiftL` 10) +
218228 (b - 0xdc00) + 0x10000
219− in cont (fromChar (chr c))
229+ in cont (charUtf8 (chr c))
220230 else fail "invalid UTF-16 surrogates"
221231 done <- Z.atEnd
222232 if done
223− then return (acc `mappend` fromByteString h)
233+ then return (acc `mappend` byteString h)
224234 else rest
225235 mapping = "\"\\/\n\t\b\r\f"
226236
@@ -305,3 +315,22 @@ jsonEOF = json <* skipSpace <* endOfInput
305315-- end-of-input. See also: 'json''.
306316jsonEOF' :: Parser Value
307317jsonEOF' = 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