aeson

How to use Parsers from Aeson with IO

匆匆过客 提交于 2019-12-10 10:15:16
问题 I have data types with many fields that, if not being manually specified by a JSON configuration file, should be randomly set. I am using Aeson to parse the config file. What is the best way to do this? Currently, I am setting values equal to some impossible value, and then later checking for said value to edit. data Example = Example { a :: Int, b :: Int } default = Example 1 2 instance FromJSON Example where parseJSON = withObject "Example" $ \v -> Example <$> (v .: "a" <|> return (a

Aeson: parsing dynamic keys as type field

 ̄綄美尐妖づ 提交于 2019-12-10 09:34:47
问题 Let's say there is a JSON like: { "bob_id" : { "name": "bob", "age" : 20 }, "jack_id" : { "name": "jack", "age" : 25 } } Is it possible to parse it to [Person] with Person defined like below? data Person = Person { id :: Text ,name :: Text ,age :: Int } 回答1: You cannot define an instance for [Person] literally, because aeson already includes an instance for [a] , however you can create a newtype, and provide an instance for that. Aeson also includes the instance FromJSON a => FromJSON (Map

Haskell, Aeson - Is there a better way of parsing historical data?

怎甘沉沦 提交于 2019-12-09 13:38:22
问题 By 'historical data' I just mean dates as key, and value on that day as value. For example, often govt institutes or uni's research division compile date about earthquakes, rainfalls, market movement, etc. in this format { "Meta Data": { "1: Country": "SomeCountry", "2: Region": "SomeRegion", "3: Latest Recording": "2018-11-16" }, "EarthQuakes": { "2018-11-16": { "Richter": "5.2508" }, "2018-11-09": { "Richter": "4.8684" }, "2018-11-02": { "Richter": "1.8399" }, ... ... ... "1918-11-02": {

Type signature needs a type that isn't exported by the library

我只是一个虾纸丫 提交于 2019-12-08 16:51:12
问题 So I was using the aeson library, and thought it would be very useful to have the following function: v .:! f = liftM (fromMaybe mempty) (v .:? f) When I ask GHCi for the type, I get: (.:!) :: (Monoid r, FromJSON r) => Object -> T.Text -> aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser r However, Parser itself is not actually exported by either Data.Aeson or Data.Aeson.Types . Am I forced to not have a type signature for the function I have defined? Alternatively, if anyone knows a better way

Parse Array in nested JSON with Aeson

☆樱花仙子☆ 提交于 2019-12-08 16:25:01
问题 I'm trying to write a FromJSON function for Aeson. The JSON: { "total": 1, "movies": [ { "id": "771315522", "title": "Harry Potter and the Philosophers Stone (Wizard's Collection)", "posters": { "thumbnail": "http://content7.flixster.com/movie/11/16/66/11166609_mob.jpg", "profile": "http://content7.flixster.com/movie/11/16/66/11166609_pro.jpg", "detailed": "http://content7.flixster.com/movie/11/16/66/11166609_det.jpg", "original": "http://content7.flixster.com/movie/11/16/66/11166609_ori.jpg"

Override how Data.Aeson handles only one field of my record

一个人想着一个人 提交于 2019-12-08 04:46:33
问题 I am making a REST API for university courses: data Course = Course { id :: Maybe Text, name :: Text, deleted :: Bool } deriving(Show, Generic) instance FromJSON Course instance ToJSON Course I would like to allow deleted to be optional in the serialized JSON structure, but not in my application. I want to set deleted to False if it isn't specified when parsing. I could write a manual instance for FromJSON , but I don't want to have to write it out for all the fields. I want to declare how

Haskell Data.Decimal as Aeson type

痞子三分冷 提交于 2019-12-07 11:23:50
问题 Is it possible to parse a Data.Decimal from JSON using the Aeson package? Suppose I have the following JSON: { "foo": 5.231, "bar": "smth" } And the following record type: data test { foo :: Data.Decimal , bar :: String } deriving Generic with instance FromJSON test instance ToJSON test This would work, if it weren't for the Data.Decimal value "foo". From what I understand I would need to manually create a FromJSON and ToJSON (for converting back to JSON) instance of Data.Decimal, since it

How to use Parsers from Aeson with IO

非 Y 不嫁゛ 提交于 2019-12-05 20:01:49
I have data types with many fields that, if not being manually specified by a JSON configuration file, should be randomly set. I am using Aeson to parse the config file. What is the best way to do this? Currently, I am setting values equal to some impossible value, and then later checking for said value to edit. data Example = Example { a :: Int, b :: Int } default = Example 1 2 instance FromJSON Example where parseJSON = withObject "Example" $ \v -> Example <$> (v .: "a" <|> return (a default)) <*> (v .: "b" <|> return (b default)) initExample :: Range -> Example -> IO Example initExample

Haskell-way of modeling a type with dynamic JSON fields?

北城余情 提交于 2019-12-05 17:48:17
I am new to Haskell, coming from an imperative programming background. I would like to be able to serialize an object to JSON in the "Haskell way", but not quite sure how to do that yet. I have read Chapter 5 of RealWorldHaskell which talks about JSON a bit, and played around with Aeson. I have also looked at a few JSON API libraries written in Haskell, such as: Facebook API in Haskell Stripe API in Haskell That got me to the point of being able to create very basic JSON strings from objects (also thanks to this blog post ): {-# LANGUAGE OverloadedStrings, DeriveGeneric #-} import Data.Aeson

Parse JSON with fieldnames that contain reserved keywords

不羁岁月 提交于 2019-12-05 17:07:39
问题 I'm trying to parse the following JSON with aeson. { "data": [ { "id": "34", "type": "link", "story": "foo" }, { "id": "35", "type": "link", "story": "bar" } ] } Since there are a lot of field I'd like to ignore, it seems I should use GHC generics. But how to write a data type definition that uses Haskell keywords like data and type ? The following of course gives: parse error on input `data' data Feed = Feed {data :: [Post]} deriving (Show, Generic) data Post = Post { id :: String, type ::