json-tools 0.2.2 → 0.2.3
raw patch · 3 files changed
+31/−18 lines, 3 files
Files
- json-lines.hs +26/−6
- json-tools.cabal +1/−1
- json-unlines.hs +4/−11
json-lines.hs view
@@ -1,15 +1,35 @@ import Text.JSON.AttoJSON import qualified Data.ByteString.Char8 as S+import Control.Monad (when)+import System.Environment (getArgs)+import System.Exit (exitFailure)+import System.IO (hPutStrLn,stderr)+import Utils (readFilesS) jsonUnlines :: JSValue -> [JSValue] jsonUnlines (JSArray xs) = xs jsonUnlines _ = error "Unexpected JSON value, only an array was expected" +usage :: IO a+usage = mapM_ (hPutStrLn stderr)+ ["Usage: json-line [--help] <file>*"+ ,""+ ,"Reads the given files as JSON arrays (or standard input, if '-' or"+ ,"no file is given). Writes on standard output the elements of these"+ ,"arrays as one JSON document per line."]+ >> exitFailure+ main :: IO ()-main = S.putStr -- printing the output- . S.unlines -- joining lines- . map showJSON -- showing each item- . jsonUnlines -- main processing- . either err id -- error handling- . parseJSON =<< S.getContents -- reading the input+main =+ do args <- getArgs+ when ("--help"`elem`args) usage+ S.putStr -- printing the output+ . S.unlines -- joining lines+ . map showJSON -- showing each item+ . concat -- merge lines from all files+ . map ( -- for each file+ jsonUnlines -- main processing+ . either err id -- error handling+ . parseJSON -- parsing each file as JSON+ ) =<< readFilesS args -- reading the input where err msg = error $ "JSON parsing: "++msg
json-tools.cabal view
@@ -1,6 +1,6 @@ Name: json-tools Cabal-Version: >=1.6-Version: 0.2.2+Version: 0.2.3 License: BSD3 License-File: LICENSE Copyright: (c) Nicolas Pouillard
json-unlines.hs view
@@ -1,17 +1,10 @@ {-# LANGUAGE OverloadedStrings #-}-import qualified Data.ByteString.Char8 as L+import qualified Data.ByteString.Lazy.Char8 as L import Control.Monad (when) import System.Environment (getArgs) import System.Exit (exitFailure) import System.IO (hPutStrLn,stderr)---- utility function-getAllContents :: [String] -> IO L.ByteString-getAllContents args- | null args = L.getContents- | otherwise = fmap L.concat . mapM file $ args- where file "-" = L.getContents- file xs = L.readFile xs+import Utils (readFilesL) usage :: IO a usage = mapM_ (hPutStrLn stderr)@@ -26,6 +19,6 @@ main = do args <- getArgs when ("--help"`elem`args) usage- L.putStrLn . L.cons '[' . L.intercalate "\n," . L.lines- =<< getAllContents args+ L.putStrLn . L.cons '[' . L.intercalate "\n," . L.lines . L.concat+ =<< readFilesL args L.putStrLn "]"