git.iko.soy/typeable/pandoc
on master help sign in
Use aeson for json.

Benchmarked: about twice as slow as json!

commit5f4a32e46588b5a89cfa38d7ac51e192d62430cc authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 9 May 2013, 10:38 (13 years ago) parenta578a49
3 files changed+27−24
benchmark/benchmark-pandoc.hs+1−8 pandoc.cabal+2−3 src/Text/Pandoc.hs+24−13
benchmark/benchmark-pandoc.hs+1−8
@@ -1,8 +1,6 @@
11import Text.Pandoc
2−import Text.Pandoc.Shared (normalize)
32import Criterion.Main
43import Criterion.Config
5−import Text.JSON.Generic
64import System.Environment (getArgs)
75import Data.Monoid
86
@@ -26,11 +24,6 @@ writerBench :: Pandoc
2624writerBench doc (name, writer) = bench (name ++ " writer") $ nf
2725 (writer def{ writerWrapText = True }) doc
2826
29−normalizeBench :: Pandoc -> [Benchmark]
30−normalizeBench doc = [ bench "normalize - with" $ nf (encodeJSON . normalize) doc
31− , bench "normalize - without" $ nf encodeJSON doc
32− ]
33−
3427main :: IO ()
3528main = do
3629 args <- getArgs
@@ -42,5 +35,5 @@ main = do
4235 let readerBs = map (readerBench doc) readers
4336 let writers' = [(n,w) | (n, PureStringWriter w) <- writers]
4437 defaultMainWith conf (return ()) $
45− map (writerBench doc) writers' ++ readerBs ++ normalizeBench doc
38+ map (writerBench doc) writers' ++ readerBs
4639
pandoc.cabal+2−3
@@ -247,7 +247,7 @@ Library
247247 extensible-exceptions >= 0.1 && < 0.2,
248248 citeproc-hs >= 0.3.7 && < 0.4,
249249 pandoc-types >= 1.10 && < 1.11,
250− json >= 0.4 && < 0.8,
250+ aeson >= 0.6 && < 0.7,
251251 tagsoup >= 0.12.5 && < 0.13,
252252 base64-bytestring >= 0.1 && < 1.1,
253253 zlib >= 0.5 && < 0.6,
@@ -438,8 +438,7 @@ benchmark benchmark-pandoc
438438 Build-Depends: pandoc,
439439 base >= 4.2 && < 5,
440440 syb >= 0.1 && < 0.5,
441− criterion >= 0.5 && < 0.9,
442− json >= 0.4 && < 0.8
441+ criterion >= 0.5 && < 0.9
443442 if impl(ghc >= 7.0.1)
444443 Ghc-Options: -rtsopts -Wall -fno-warn-unused-do-bind
445444 else
src/Text/Pandoc.hs+24−13
@@ -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