aeson

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

爷,独闯天下 提交于 2020-01-23 08:28:25
问题 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

Using Aeson generics to construct JSON with a value as key holding another value

别等时光非礼了梦想. 提交于 2020-01-04 06:56:35
问题 Toying a bit with the github gist API while trying to get down with the Aeson JSON library. I've run into a problem with the generated ToJSON instance, and I don't know exactly how to solve it. I need to contain a value inside and the key that is associated to the value also needs to be a value and not a predefined key name. It's a bit easier to show. The desired output is, { "public": true, "description": "Something..", "files": {"This Thing.md": {"content": "Here we go!"}} } where the value

Haskell, Aeson: Parsing nested JSON with part unnecessary values

走远了吗. 提交于 2020-01-04 06:32:51
问题 I'm a beginner trying to learn more about Haskell and Aeson by parsing some json files I find online. I have a .json that looks like this "Abilities": { "Prime": { "Ammo": 210, "Available": true, "Diposition": 3, "Type": "Secondary", "Class": "Pistols", "NormalAttack": { "Chance": 0.25, "Streak": 2, "Rate": 2.67, "ShotType": "Hit-Scan", "Damage": { "Front": 15, "Back": 15, "Up": 120, "Down": 40 }, "Status": 0.25 } "Accuracy": 9.8, "Reload": 3, "Name": "Prime", "Magazine": 16, }, "Dual": {

Haskell, Aeson: Parsing nested JSON with part unnecessary values

柔情痞子 提交于 2020-01-04 06:32:29
问题 I'm a beginner trying to learn more about Haskell and Aeson by parsing some json files I find online. I have a .json that looks like this "Abilities": { "Prime": { "Ammo": 210, "Available": true, "Diposition": 3, "Type": "Secondary", "Class": "Pistols", "NormalAttack": { "Chance": 0.25, "Streak": 2, "Rate": 2.67, "ShotType": "Hit-Scan", "Damage": { "Front": 15, "Back": 15, "Up": 120, "Down": 40 }, "Status": 0.25 } "Accuracy": 9.8, "Reload": 3, "Name": "Prime", "Magazine": 16, }, "Dual": {

How to deliver JSON over HTTP using Warp with Aeson

拜拜、爱过 提交于 2019-12-30 10:55:08
问题 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

How to parse array into tuple with aeson?

半世苍凉 提交于 2019-12-24 08:19:27
问题 If I have an array ["addTask", {"id": "1", "description": "d", "dependsOn": [], "dependentTasks": []}] . data Task = Task { id :: String , description :: String , dependsOn :: [String] , dependentTasks :: [String] } deriving (Eq, Show, Generic, ToJSON, FromJSON) type Change = Storage -> Storage addTask :: Task -> Change addTask (Task id desc dep dept) = insert id (Task id desc dep dept) How can I create a parser that would produce a addTask from that? instance FromJSON (Storage -> Storage)

Haskell, Aeson - no instance for (ToJSON ByteString)

不羁岁月 提交于 2019-12-24 01:27:29
问题 So happy making it this far, encountered a new hurdle: Got this code made to be encoded to JSON. However no matter when type I use as an instance, the compiler complains. Now I am obviously doing something wrong, but it is exactly what is in the documentation (when using DeriveGeneric obviously). {-# LANGUAGE OverloadedStrings, DeriveGeneric #-} import Data.Aeson import Data.Text as T import Data.ByteString.Lazy as B import Data.ByteString.Lazy.Char8 as BC import GHC.Generics -- decode ::

FromJSON instance with DataKinds

£可爱£侵袭症+ 提交于 2019-12-23 17:26:40
问题 Trying to do the JSON de-serialisation for a data type with TypeLits, I get stuck with the following problem: Couldn't match type ‘n’ with ‘2’ ‘n’ is a rigid type variable bound by the instance declaration at test.hs:14:10 Expected type: aeson-0.11.2.1:Data.Aeson.Types.Internal.Parser (X n) Actual type: aeson-0.11.2.1:Data.Aeson.Types.Internal.Parser (X 2) How would be the correct syntax to allow Nat generically in the FromJSON instance in the following example: {-# LANGUAGE DataKinds #-} {-#

Isolate a single value from a nested JSON response in Aeson

风流意气都作罢 提交于 2019-12-23 03:09:24
问题 I'm working with a couple of JSON based APIs and the vast majority of the time I only need to extract a single value from the JSON response. E.g. with {"foo":"xyz","bar":"0.0000012"} I only need the value of bar . To accommodate this I've written functions to extract the information I need: -- | Isolate a Double based on a key from a JSON encoded ByteString isolateDouble :: String -> B.ByteString -> Maybe Double isolateDouble k bstr = isolateString k bstr >>= maybeRead -- | Isolate a String

Fault tolerant JSON parsing

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 03:52:23
问题 I'm using Data.Aeson to parse some JSON into a Record type. From time to time data is added to the JSON and this breaks my code as Aeson complains something to the effect of: expected Object with 21 name/value pairs but got 23 name/value I'd really prefer to parse the JSON in a fault tolerant way -- I don't care if more fields are added to the JSON at a later date, just parse whatever you can! Is there a way to achieve this fault tolerance? Here's my code: myRecordFromJSONString :: BS