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 lazyByteString
. The appropriate function to connect both together is calledfromLazyByteString
.Update 05/01/2015: Fix example for Warp/WAI 3.x