@@ -145,13 +145,16 @@ import Text.Pandoc.Templates
145145import Text.Pandoc.Options
146146import Text.Pandoc.Shared (safeRead, warn)
147147import Data.ByteString.Lazy (ByteString)
148+import qualified Data.ByteString.Lazy as BL
148149import Data.List (intercalate, isSuffixOf)
149150import Data.Version (showVersion)
150−import Text.JSON.Generic
151+import Data.Aeson.Generic
151152import Data.Set (Set)
153+import Data.Data
152154import qualified Data.Set as Set
153155import Text.Parsec
154156import Text.Parsec.Error
157+import qualified Text.Pandoc.UTF8 as UTF8
155158import Paths_pandoc (version)
156159
157160-- | Version number of pandoc library.
@@ -188,7 +191,8 @@ markdown o s = do
188191-- | Association list of formats and readers.
189192readers :: [(String, ReaderOptions -> String -> IO Pandoc)]
190193readers = [("native" , \_ s -> return $ readNative s)
191− ,("json" , \_ s -> return $ decodeJSON s)
194+ ,("json" , \_ s -> return $ checkJSON
195+ $ decode $ UTF8.fromStringLazy s)
192196 ,("markdown" , markdown)
193197 ,("markdown_strict" , markdown)
194198 ,("markdown_phpextra" , markdown)
@@ -212,7 +216,7 @@ data Writer = PureStringWriter (WriterOptions -> Pandoc -> String)
212216writers :: [ ( String, Writer ) ]
213217writers = [
214218 ("native" , PureStringWriter writeNative)
215− ,("json" , PureStringWriter $ \_ -> encodeJSON)
219+ ,("json" , PureStringWriter $ \_ -> UTF8.toStringLazy . encode)
216220 ,("docx" , IOByteStringWriter writeDocx)
217221 ,("odt" , IOByteStringWriter writeODT)
218222 ,("epub" , IOByteStringWriter $ \o ->
@@ -304,7 +308,7 @@ getWriter s =
304308-- that reads and writes a JSON-encoded string. This is useful
305309-- for writing small scripts.
306310jsonFilter :: (Pandoc -> Pandoc) -> String -> String
307−jsonFilter f = encodeJSON . f . decodeJSON
311+jsonFilter f = UTF8.toStringLazy . encode . f . checkJSON . decode . UTF8.fromStringLazy
308312
309313-- | 'toJsonFilter' convert a function into a filter that reads pandoc's json output
310314-- from stdin, transforms it by walking the AST and applying the specified
@@ -333,18 +337,25 @@ class ToJsonFilter a where
333337 toJsonFilter :: a -> IO ()
334338
335339instance (Data a) => ToJsonFilter (a -> a) where
336− toJsonFilter f = getContents
337− >>= putStr . encodeJSON . (bottomUp f :: Pandoc -> Pandoc) . decodeJSON
340+ toJsonFilter f = BL.getContents >>=
341+ BL.putStr . encode . (bottomUp f :: Pandoc -> Pandoc) . checkJSON . decode
338342
339343instance (Data a) => ToJsonFilter (a -> IO a) where
340− toJsonFilter f = getContents >>= (bottomUpM f :: Pandoc -> IO Pandoc) . decodeJSON
341− >>= putStr . encodeJSON
344+ toJsonFilter f = BL.getContents >>=
345+ (bottomUpM f :: Pandoc -> IO Pandoc) . checkJSON . decode >>=
346+ BL.putStr . encode
342347
343348instance (Data a) => ToJsonFilter (a -> [a]) where
344− toJsonFilter f = getContents
345− >>= putStr . encodeJSON . (bottomUp (concatMap f) :: Pandoc -> Pandoc) . decodeJSON
349+ toJsonFilter f = BL.getContents >>=
350+ BL.putStr . encode . (bottomUp (concatMap f) :: Pandoc -> Pandoc) .
351+ checkJSON . decode
346352
347353instance (Data a) => ToJsonFilter (a -> IO [a]) where
348− toJsonFilter f = getContents
349− >>= (bottomUpM (fmap concat . mapM f) :: Pandoc -> IO Pandoc) . decodeJSON
350− >>= putStr . encodeJSON
354+ toJsonFilter f = BL.getContents >>=
355+ (bottomUpM (fmap concat . mapM f) :: Pandoc -> IO Pandoc)
356+ . checkJSON . decode >>=
357+ BL.putStr . encode
358+
359+checkJSON :: Maybe a -> a
360+checkJSON Nothing = error "Error parsing JSON"
361+checkJSON (Just r) = r