aeson

How to deliver JSON over HTTP using Warp with Aeson

北战南征 提交于 2019-12-01 08:55:18
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

Haskell Aeson: How to convert Value into custom type?

安稳与你 提交于 2019-12-01 05:18:49
Can't find a good example. Appreciate any help. The JSON is as follows: [{ "EXIF:Make": "Canon", "EXIF:Model": "Canon PowerShot S95", "EXIF:Orientation": "Horizontal (normal)", "EXIF:XResolution": 180, "EXIF:YResolution": 180, "EXIF:ResolutionUnit": "inches" }] The code I used is as follows: import Data.Aeson import Data.Attoparsec import Data.ByteString x <- fmap (parse json) (Data.ByteString.readFile "json.txt") How do I define & use the FromJSON type to convert from x into: data Exif = Exif [[(String, String)]] or similar data structure? Note the [[]] -- I'm expecting the JSON to have

Haskell :: Aeson :: parse ADT based on field value

时光怂恿深爱的人放手 提交于 2019-11-30 09:10:56
I'm using an external API which returns JSON responses. One of the responses is an array of objects and these objects are identified by the field value inside them. I'm having some trouble understanding how the parsing of such JSON response could be done with Aeson. Here is a simplified version of my problem: newtype Content = Content { content :: [Media] } deriving (Generic) instance FromJSON Content data Media = Video { objectClass :: Text , title :: Text } | AudioBook { objectClass :: Text , title :: Text } In API documentation it is said that the object can be identified by the field

How to deal with Haskell's keywords in record fields?

夙愿已清 提交于 2019-11-30 03:45:14
问题 JSON in response of Github Gists Rest API contains Haskell's keyword type . But type couldn't be used as a record field. Thus it couldn't be used in implementation of Aeson's Generic FromJSON/ToJSON instances. import Data.Text (Text) import GHC.Generics (Generic) type URL = Text data OwnerType = User deriving (Show) data Owner = Owner { id :: Int, gravatar_id :: Text, login :: Text, avatar_url :: Text, events_url :: URL, followers_url :: URL, following_url :: URL, gists_url :: URL, html_url :

Haskell, Aeson & JSON parsing into custom type

旧巷老猫 提交于 2019-11-29 17:28:09
问题 Following on from a previous post, I've found I'm totally stuck. I'm trying to parse a JSON structure into my own type, and not only am I stuck on how to parse the Array, I'm not even sure if I'm using the Aeson library as intended. Any help would be greatly appreciated. The code: data Exif = Exif [(T.Text, ExifValue)] deriving (Show) data ExifValue = ExifText T.Text | ExifInt Integer | ExifDouble Double | ExifBool Bool | ExifArray [ExifValue] deriving (Show) instance FromJSON ExifValue where

Haskell :: Aeson :: parse ADT based on field value

烈酒焚心 提交于 2019-11-29 13:00:05
问题 I'm using an external API which returns JSON responses. One of the responses is an array of objects and these objects are identified by the field value inside them. I'm having some trouble understanding how the parsing of such JSON response could be done with Aeson. Here is a simplified version of my problem: newtype Content = Content { content :: [Media] } deriving (Generic) instance FromJSON Content data Media = Video { objectClass :: Text , title :: Text } | AudioBook { objectClass :: Text