Search code examples
haskellhaskell-pipes

Parsing a stream of JSON with pipes-aeson


Given a simple example JSON type:

data Test = Test
  { name :: Text
  , age  :: Int
  } deriving (Show, Generic)

instance FromJSON Test

How can I use pipes-aeson to decode a stream of JSON messages coming over a socket using the decoded lens? As an example I'd like to just print them out as they're parsed:

main = connect "127.0.0.1" "8000" $ \(socket, _) -> $ runEffect $
  some use of zoom decoded? view decoded? >-> P.print

Solution

  • decoded (the one from Pipes.Aeson.Unchecked) is a lens than transforms a Producer of raw bytes into a Producer of parsed FromJSON values.

    So, we must first create the Producer of bytes out of the socket using the fromSocket function from the pipes-network package. Something like this:

    -- to help with type inference
    printTest :: (MonadIO m) => Consumer' Test m r
    printTest = P.print
    
    main = do
        connect "127.0.0.1" "8000" $ \(socket, _) -> $ runEffect $
            view decoded (fromSocket socket 4096) >-> printTest
        return ()