Bryan O'Sullivan avatar Bryan O'Sullivan committed aee8f7d Merge

Merge pull request #143 from basvandijk/bytestring-0.10

Use the Builder from bytestring >= 0.10

Comments (0)

Files changed (2)

Data/Aeson/Parser/Internal.hs

-{-# LANGUAGE BangPatterns, OverloadedStrings #-}
+{-# LANGUAGE CPP, BangPatterns, OverloadedStrings #-}
 
 -- |
 -- Module:      Data.Aeson.Parser.Internal
     , eitherDecodeStrictWith
     ) where
 
-import Blaze.ByteString.Builder (fromByteString, toByteString)
+#if defined(USE_BLAZE_BUILDER)
+import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
 import Blaze.ByteString.Builder.Char.Utf8 (fromChar)
 import Blaze.ByteString.Builder.Word (fromWord8)
+#else
+#if MIN_VERSION_bytestring(0,10,2)
+import Data.ByteString.Builder
+#else
+import Data.ByteString.Lazy.Builder
+#endif
+  (Builder, byteString, toLazyByteString, charUtf8, word8)
+#endif
+
 import Control.Applicative as A
 import Data.Aeson.Types (Result(..), Value(..))
 import Data.Attoparsec.Char8 hiding (Result)
           if slash /= backslash || escape == 255
             then fail "invalid JSON escape sequence"
             else do
-            let cont m = go (acc `mappend` fromByteString h `mappend` m)
+            let cont m = go (acc `mappend` byteString h `mappend` m)
                 {-# INLINE cont #-}
             if t /= 117 -- 'u'
-              then cont (fromWord8 (B.unsafeIndex mapping escape))
+              then cont (word8 (B.unsafeIndex mapping escape))
               else do
                    a <- hexQuad
                    if a < 0xd800 || a > 0xdfff
-                     then cont (fromChar (chr a))
+                     then cont (charUtf8 (chr a))
                      else do
                        b <- Z.string "\\u" *> hexQuad
                        if a <= 0xdbff && b >= 0xdc00 && b <= 0xdfff
                          then let !c = ((a - 0xd800) `shiftL` 10) +
                                        (b - 0xdc00) + 0x10000
-                              in cont (fromChar (chr c))
+                              in cont (charUtf8 (chr c))
                          else fail "invalid UTF-16 surrogates"
     done <- Z.atEnd
     if done
-      then return (acc `mappend` fromByteString h)
+      then return (acc `mappend` byteString h)
       else rest
   mapping = "\"\\/\n\t\b\r\f"
 
 -- end-of-input.  See also: 'json''.
 jsonEOF' :: Parser Value
 jsonEOF' = json' <* skipSpace <* endOfInput
+
+#if defined(USE_BLAZE_BUILDER)
+byteString :: ByteString -> Builder
+byteString = fromByteString
+{-# INLINE byteString #-}
+
+charUtf8 :: Char -> Builder
+charUtf8 = fromChar
+{-# INLINE charUtf8 #-}
+
+word8 :: Word8 -> Builder
+word8 = fromWord8
+{-# INLINE word8 #-}
+
+#else
+toByteString :: Builder -> ByteString
+toByteString = L.toStrict . toLazyByteString
+{-# INLINE toByteString #-}
+#endif
   description: operate in developer mode
   default: False
 
+flag blaze-builder
+  description: Use blaze-builder instead of bytestring >= 0.10
+  default: False
+
 library
   exposed-modules:
     Data.Aeson
   build-depends:
     attoparsec >= 0.8.6.1,
     base == 4.*,
-    blaze-builder >= 0.2.1.4,
     bytestring,
     containers,
     deepseq,
     unordered-containers >= 0.1.3.0,
     vector >= 0.7.1
 
+  if flag(blaze-builder)
+    build-depends: blaze-builder >= 0.2.1.4
+    cpp-options: -DUSE_BLAZE_BUILDER
+  else
+    build-depends: bytestring >= 0.10
+
   if flag(developer)
     ghc-options: -Werror
     ghc-prof-options: -auto-all
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.