aeson

FromJSON make a list from multiple fields

五迷三道 提交于 2019-12-20 03:09:21
问题 I have an object to parse that looks a bit like this : { "data": [ { "virtio0": "some text", "virtio1": "blah", "ide2": "some other text", "cores": 1, "mem": 512, ... }, { // The same ... } ] } Now I basically want to parse that into a [VM], but my problem is those numbered fields. Depending on the VM config, it might or might not have virtioX fields, ideX fields .. and I don't see a way to know in advance, nor to guess the numbers. I was thinking the best might be to define a Disk type that

Haskell Aeson: How to convert Value into custom type?

别来无恙 提交于 2019-12-19 06:59:09
问题 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 =

Give a default value for fields not available in json using aeson

一个人想着一个人 提交于 2019-12-14 04:11:39
问题 I am trying to load json using the Aeson library. The thing is that the datastructure that I want to load it into contains more fields than the json. data Resource = Res { name :: String, file :: FilePath, res :: Picture, loaded :: Bool } deriving (Generic, Show) Where only the name and the file fields are available in the json. Picture is a gloss Picture so that can't really be loaded from json. I can't figure out how to leave out res and loaded out of the FromJSON instance. 回答1: If you can

Extract the Text from a JSON value String Text without pattern matching

点点圈 提交于 2019-12-13 19:17:54
问题 Here is the definition for a Json Value : -- | A JSON value represented as a Haskell value. data Value = Object !Object | Array !Array | String !Text | Number !Scientific | Bool !Bool | Null deriving (Eq, Show) let value = String "myValue" looking for => fromString value == "myValue" ?? fromString :: Value -> Text I'm looking a function like where I could get the Text from String without to do some pattern matching, obviously this function will be unsafe... a fromString like fromJust in Data

Haskell Aeson to deal with missing data

China☆狼群 提交于 2019-12-13 16:11:20
问题 I have a (valid) json encoded array that has missing or malformed data. I want Aeson to turn that into Maybe [Maybe Point] and have Nothing where the array element was not a valid Point . import Data.Aeson decode "[1,2,3,4,null,\"hello\"]" :: (Maybe [Maybe Int]) => Nothing But I would much rather have it evaluate to => Just [Just 1, Just 2, Just 3, Just 4, Nothing, Nothing] If this can't be done with Aeson, is there another library that can do it? Note that the actual object is much more

Haskell Yesod - extracting a object inside a json before converting it to a model

[亡魂溺海] 提交于 2019-12-13 02:36:35
问题 Suppose I have a JSON like this: { data: {...} } and {...} represents a model of mine. How could get my model in this case in the Handler? For instance, the following will not work obviously: putMyEntityR :: Handler () putMyEntityR = do (Entity id _) <- (...) -- getting the Key e <- requireJsonBody :: Handler MyEntity runDB $ replace id e sendResponseStatus status204 ("UPDATED" :: Text) How can I read the JSON , take the data object, and only then decode it? 回答1: There was some more

Functionally changing key names in serialization to aeson with Text keys

▼魔方 西西 提交于 2019-12-12 19:30:22
问题 I have a json object with a manually crafted ToJSON instance. I would like to replace this with a function that does not require my explicit enumeration of the key names. I am using "rec*" as a prefix I would like to strip, and my fields start out as Text rather than string. Starting with minimal data: data R3 = R3 { recCode :: Code , recValue :: Value} deriving (Show, Generic) And smart constructor function: makeR3 rawcode rawval = R3 code value where code = rawcode value = rawval This

Haskell - Aeson : Getting “Nothing” when trying to decode JSON URL Req

我与影子孤独终老i 提交于 2019-12-11 19:15:45
问题 I'm relatively new to haskell and right now I'm trying to get a deeper understanding and trying to get used to different popular libraries. Right now I'm trying "aeson". What I want to do is parse MSFT quote request from https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo This is what it looks like { "Global Quote": { "01. symbol": "MSFT", "02. open": "105.3500", "03. high": "108.2400", "04. low": "105.2700", "05. price": "107.6000", "06. volume": "23308066", "07.

Haskell ADTs with aeson

£可爱£侵袭症+ 提交于 2019-12-10 17:28:37
问题 I've been fighting with a simple ADT, trying to get it to round-trip back and forth to JSON, but I've had no luck, no matter how I try to massage or modify the type. What am I missing? When it compiles, I always get the same runtime error: > let t = Fahrenheit > fromJSON $ toJSON t Error "when expecting a (), encountered Object instead" Trying this just gives me "Nothing", presumably because of the same error: decode $ encode t I've tried to follow these sources, but I can't seem to get

Aeson: derive some (but not all) fields of a struct

◇◆丶佛笑我妖孽 提交于 2019-12-10 11:03:58
问题 I have a large struct which I need to be an instance of FromJSON so that I can parse my json data into it. I would like to derive automatically, but a single field needs "special care" in that it is an object in json and I want it to be an array of the values in my struct. How can I do this without writing a huge FromJson implementation repeating all the fields? Example json: {"myobject": {"one": 1, "two": 2}, ...many_more_fields...} Example struct: data MyStruct = MyStruct { myobject :: [Int