@@ -1,3 +1,4 @@
1+{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DefaultSignatures #-}
4{-# LANGUAGE DefaultSignatures #-}
@@ -96,6 +97,7 @@ import Foreign.Storable (Storable)
96import GHC.Generics
97import GHC.Generics
97import Numeric.Natural (Natural)
98import Numeric.Natural (Natural)
100+import qualified Data.ByteString as S
99import qualified Data.ByteString.Lazy as L
101import qualified Data.ByteString.Lazy as L
100import qualified Data.DList as DList
102import qualified Data.DList as DList
101import qualified Data.HashMap.Strict as H
103import qualified Data.HashMap.Strict as H
@@ -118,6 +120,15 @@ import qualified Data.Vector.Primitive as VP
118import qualified Data.Vector.Storable as VS
120import qualified Data.Vector.Storable as VS
119import qualified Data.Vector.Unboxed as VU
121import qualified Data.Vector.Unboxed as VU
123+#if !(MIN_VERSION_bytestring(0,10,0))
124+import Foreign.ForeignPtr (withForeignPtr)
125+import Foreign.Marshal.Utils (copyBytes)
126+import Foreign.Ptr (plusPtr)
128+import qualified Data.ByteString.Internal as S
129+import qualified Data.ByteString.Lazy.Internal as L
121toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
132toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
122toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
133toJSONPair a b = liftToJSON2 a (listValue a) b (listValue b)
123{-# INLINE toJSONPair #-}
134{-# INLINE toJSONPair #-}
@@ -448,7 +459,11 @@ toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
448toJSONKeyTextEnc e = ToJSONKeyText tot e
459toJSONKeyTextEnc e = ToJSONKeyText tot e
450 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
461 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
462+ tot = T.dropAround (== '"')
464+ . lazyToStrictByteString
451− tot = T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . E.encodingToLazyByteString . e
465+ . E.encodingToLazyByteString
453-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
468-- | Contravariant map, as 'ToJSONKeyFunction' is a contravariant functor.
454contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
469contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
@@ -1943,7 +1958,7 @@ stringEncoding :: Encoding' Text -> Value
1943stringEncoding = String
1958stringEncoding = String
1944 . T.dropAround (== '"')
1959 . T.dropAround (== '"')
1945 . T.decodeLatin1
1960 . T.decodeLatin1
1946− . L.toStrict
1961+ . lazyToStrictByteString
1947 . E.encodingToLazyByteString
1962 . E.encodingToLazyByteString
1948{-# INLINE stringEncoding #-}
1963{-# INLINE stringEncoding #-}
@@ -2583,3 +2598,28 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g,
2583 {-# INLINE toJSON #-}
2598 {-# INLINE toJSON #-}
2584 toEncoding = toEncoding2
2599 toEncoding = toEncoding2
2585 {-# INLINE toEncoding #-}
2600 {-# INLINE toEncoding #-}
2602+-------------------------------------------------------------------------------
2603+-- pre-bytestring-0.10 compatibility
2604+-------------------------------------------------------------------------------
2606+{-# INLINE lazyToStrictByteString #-}
2607+lazyToStrictByteString :: L.ByteString -> S.ByteString
2608+#if MIN_VERSION_bytestring(0,10,0)
2609+lazyToStrictByteString = L.toStrict
2611+lazyToStrictByteString = packChunks
2613+-- packChunks is taken from the blaze-builder package.
2615+-- | Pack the chunks of a lazy bytestring into a single strict bytestring.
2616+packChunks :: L.ByteString -> S.ByteString
2618+ S.unsafeCreate (fromIntegral $ L.length lbs) (copyChunks lbs)
2620+ copyChunks !L.Empty !_pf = return ()
2621+ copyChunks !(L.Chunk (S.PS fpbuf o l) lbs') !pf = do
2622+ withForeignPtr fpbuf $ \pbuf ->
2623+ copyBytes pf (pbuf `plusPtr` o) l
2624+ copyChunks lbs' (pf `plusPtr` l)