问题描述
我想使用创建在Haskell上运行的基于HTTP的高性能API,如下所示:一个HTTP后端。
服务器应根据请求返回JSON数据。这些数据应使用进行序列化
但是, 需要一个响应对象,而Aeson会返回懒惰的 ByteString
s。 p>
如何将两个库绑定在一起?对于这个问题的范围,我对查询解析或路由不感兴趣,但是以一个如何将两个库绑定在一起以提供带有正确头的正确JSON为例。
:这个问题故意没有显示任何研究成果,因为它被回答为Q& A-style-ish。如果您需要研究工作,请参阅我的答案。 /www.haskell.org/haskellwiki/Web/Serversrel =nofollow> HaskellWiki最小变形示例。
为简单起见,我删除了任何代码如路由,通过存根和注释替换最相关的部分放置什么。
我们将在此示例中序列化的JSON数据是 [ 一, b, C]
。
连接两个库的问题是,虽然 warp 需要Blaze 来正确构建它的响应,而 Aeson 返回(如你所说)一个懒惰 ByteString
。
将两者连接在一起的适当函数称为。
{ - #LANGUAGE OverloadedStrings# - }
导入Data.Aeson
导入Data.Text(文本)
导入Network.Wai
导入Network.Wai.Handler.Warp
import Network.HTTP.Types(status200)
import Network.HTTP.Types.Header(hContentType)
import Blaze.ByteString.Builder.ByteString(fromLazyByteString)
将合格的Data.ByteString.UTF8导入为BU
main = do
let port = 3000
putStrLn $侦听端口++ show port
运行端口应用程序
应用程序::应用程序
应用程序请求f = f $
案例pathInfo请求
- 在此处放置自定义路由
_ - > anyRoute
- 将被转换为JSON的数据
jsonData = [a,b,c] :: [Text]
anyRoute = responseLBS
status200
[(hContentType,application / json)]
(encode jsonData)
2015年1月1日更新:修正Warp / WAI 3.x的示例
I want to create a high-performance HTTP-based API running on Haskell using warp as a HTTP backend.
The server shall return JSON data upon request. This data shall be serialized by using Aeson
However, warp requires a response object whereas Aeson returns lazy ByteString
s.
How can I tie both libraries together? For this question's scope I'm not interested in query parsing or routing, but in an example of how to tie both libraries together to deliver a correct JSON with correct headers.
Note: This question intentionally does not show any research effort, as it was answered Q&A-style-ish. See my answer if you require research effort.
I'll build my example on the HaskellWiki minimal warp example.
For the sake of simplicity I removed any code like routing, replacing the most relevant parts by stubs and comments where to place what.
The JSON data we will serialize in this example is the list ["a","b","c"]
. The same response (= the JSON) will be returned for any URL.
The issue in connecting both libraries is that while warp requires a Blaze Builder
to build its response properly, while Aeson returns (as you said) a lazy ByteString
.The appropriate function to connect both together is called fromLazyByteString
.
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Data.Text (Text)
import Network.Wai
import Network.Wai.Handler.Warp
import Network.HTTP.Types (status200)
import Network.HTTP.Types.Header (hContentType)
import Blaze.ByteString.Builder.ByteString (fromLazyByteString)
import qualified Data.ByteString.UTF8 as BU
main = do
let port = 3000
putStrLn $ "Listening on port " ++ show port
run port app
app :: Application
app req f = f $
case pathInfo req of
-- Place custom routes here
_ -> anyRoute
-- The data that will be converted to JSON
jsonData = ["a","b","c"] :: [Text]
anyRoute = responseLBS
status200
[(hContentType, "application/json")]
(encode jsonData)
Update 05/01/2015: Fix example for Warp/WAI 3.x
这篇关于如何使用Warp和Aeson在HTTP上传送JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!