Last active
November 16, 2015 04:00
-
-
Save erantapaa/4bf55d38865863529659 to your computer and use it in GitHub Desktop.
Pipes.Aeson examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings, DeriveGeneric, NoMonomorphismRestriction, RankNTypes, BangPatterns #-} | |
-- build-depends: base >= 4.7 && < 5, bytestring, pipes-aeson, pipes-parse, pipes, aeson, vector, text | |
-- This file contains examples of how to use functions in Pipes.Aeson. | |
module Lib | |
where | |
import GHC.Generics | |
import Data.Text | |
import Data.Aeson | |
import Pipes | |
import Pipes.Parse | |
import qualified Pipes.Aeson as PA | |
import qualified Pipes.Prelude as P | |
import qualified Data.Vector as V | |
import qualified Data.ByteString as BS | |
import Control.Monad | |
import qualified System.IO as IO | |
data Person = Person { | |
name :: Text | |
, age :: Int | |
} deriving (Generic, Show) | |
instance ToJSON Person | |
instance FromJSON Person | |
people = [ Person "asd" 21, Person "qwe" 22, Person "sdf" 23 ] | |
Array peopleVector = toJSON people :: Value | |
-- example of how to use `encodeArray` | |
test = runEffect $ PA.encodeArray peopleVector >-> (forever $ await >>= (lift . BS.putStr) ) | |
-- example of how to use `decode` | |
test2 = do | |
Just ma <- evalStateT PA.decode (do mapM_ yield [" { \"age\": 234, \"name\": \"", "Steve\" } "] ) | |
let _ = ma :: Either PA.DecodingError Person | |
case ma of | |
Left e -> error $ "parse error: " ++ show e | |
Right a -> putStrLn $ "got a Person: " ++ show a | |
-- Produce the lines of a byte as ByteStrings | |
test2a str = do | |
let input = yield str | |
ma <- evalStateT PA.decode input | |
case ma of | |
Nothing -> putStrLn "got Nothing" | |
Just (Left e) -> putStrLn "got a parse error" | |
Just (Right x) -> let _ = x :: Value in putStrLn "got a value" | |
bsFromHandle :: MonadIO m => (IO.Handle -> IO BS.ByteString) -> IO.Handle -> Producer' BS.ByteString m () | |
bsFromHandle reader h = go | |
where | |
go = do | |
eof <- liftIO $ IO.hIsEOF h | |
unless eof $ do | |
str <- liftIO $ reader h | |
yield str | |
go | |
{-# INLINABLE bsFromHandle #-} | |
bsLines path = do | |
h <- lift $ IO.openFile path IO.ReadMode | |
bsFromHandle BS.hGetLine h | |
bsBlocks path = do | |
h <- lift $ IO.openFile path IO.ReadMode | |
bsFromHandle (\h -> BS.hGet h 16384) h | |
-- read a line-oriented JSON file, counting parsing errors | |
test3 path = do | |
let doit !good !bad = do | |
m <- PA.decode | |
let _ = m :: Maybe (Either PA.DecodingError Value) | |
case m of | |
Nothing -> return (good,bad) | |
Just x -> do | |
case x of | |
Left e -> do {- lift $ putStrLn "parse error"; -} doit good (bad+1) | |
Right a -> do {- lift $ putStrLn "ok"; -} doit (good+1) bad | |
(good,bad) <- evalStateT (doit 0 0) (bsLines path) | |
print (good, bad) | |
test4 path = do | |
let doit !good !bad = do | |
m <- PA.decode | |
let _ = m :: Maybe (Either PA.DecodingError Value) | |
case m of | |
Nothing -> return (good,bad) | |
Just x -> do | |
case x of | |
Left e -> do lift $ putStrLn "parse error"; doit good (bad+1) | |
Right a -> do lift $ putStrLn "ok"; doit (good+1) bad | |
(good,bad) <- evalStateT (doit 0 0) (bsBlocks path) | |
print (good, bad) | |
test5 path = | |
runEffect $ bsBlocks path >-> (forever $ await >> liftIO (putStrLn "read a block")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment