Questions tagged [aeson]

A Haskell JSON parsing and encoding library optimized for high performance and easy usage

Aeson is a Haskell JSON parsing and encoding library optimized for high performance and easy usage.

It provides a typeclass-based interface for serializiation and parsing of object hierarchies (see the FromJSON and ToJSON typeclasses) andprovides lazy or strict parsing mechanisms.

Errors can be handled by using Maybe or Either, providing typesafe parsing of any object structure.

In greek mythology, Aeson was the father of Jason, therefore relating the library's name to the format it parses.

383 questions
4
votes
2 answers

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

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…
Lance
  • 75,200
  • 93
  • 289
  • 503
4
votes
1 answer

Aeson Prisms with "free" references

Just read the brilliant "Lens/Aeson Traversals/Prisms" article and have a real-world application. Given the following anonymized JSON structure, how would I prism out a collection rather than a specific value? {"Locations" : [ {"id" : "2o8434",…
Brandon
  • 308
  • 1
  • 2
  • 10
4
votes
2 answers

Specification of default ToJson format used by Aeson

Does anybody know where I can find documentation on how ADTs are translated to Json by Aeson's ToJSON? I'm using Haskell for a backend application, and I'm trying to write the JSON decoder for another functional language on the front end, so I'd…
jmite
  • 8,171
  • 6
  • 40
  • 81
4
votes
2 answers

Data.Aeson encoding optional keys

I have the following problem, I have a JSON format with optional keys that I need to generate from my haskell code. Lets make an example {-# LANGUAGE DeriveGeneric #-} import GHC.Generics import Data.Aeson data Person = { name :: String, …
windwarrior
  • 456
  • 2
  • 11
4
votes
1 answer

Conduit with aeson / attoparsec, how to exit cleanly without exception once source has no more data

I'm using aeson / attoparsec and conduit / conduit-http connected by conduit-attoparsec to parse JSON data from a file / webserver. My problem is that my pipeline always throws this exception... ParseError {errorContexts = ["demandInput"],…
NBFGRTW
  • 459
  • 3
  • 11
4
votes
1 answer

Aeson's deriveJSON doesn't work as expected for enums

I usually write my own ToJSON and FromJSON instances, but I decided to use deriveJSON for a type because it was so simple: data Priority = HIGH | MEDIUM | LOW deriving Show $(deriveToJSON id ''Priority) main = BS.putStrLn . encode $ HIGH I…
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
4
votes
1 answer

Aeson deriveJSON combined with conduit sinkParser

Continuing my exploration of conduit and aeson, how would I go about using my own data type in stead of Value in this (slightly modified) code snippet from the Yesod book. {-# LANGUAGE OverloadedStrings, TemplateHaskell #-} import Network.Wai…
Johanna Larsson
  • 10,531
  • 6
  • 39
  • 50
4
votes
1 answer

Haskell Aeson destructuring generic parse

I have a JSON request in the style of {"command":"get","params":{"something":"something else"}} and this code snippet from the Yesod book {-# LANGUAGE OverloadedStrings #-} import Network.Wai (Response, responseLBS, Application, requestBody) import…
Johanna Larsson
  • 10,531
  • 6
  • 39
  • 50
3
votes
0 answers

Is it possible to set Aeson's Options to omit some fields?

There is a type: data HandlerH f = StreamHandler { level :: HKD T.Level f -- ^ Default is @NOTSET@. , filterer :: HKD T.Filterer f -- ^ Default is…
RandomB
  • 3,367
  • 19
  • 30
3
votes
1 answer

Prevent unknown field names in Aeson parseJSON

With the following type and instance deriving: {-# LANGUAGE RecordWildCards #-} import Data.Aeson import Data.Text data MyParams = MyParams { mpFoo :: Maybe Text, mpBar :: Maybe Text } deriving Show instance FromJSON…
Jivan
  • 21,522
  • 15
  • 80
  • 131
3
votes
2 answers

Can aeson handle JSON with imprecise types?

I have to deal with JSON from a service that sometimes gives me "123" instead of 123 as the value of field. Of course this is ugly, but I cannot change the service. Is there an easy way to derive an instance of FromJSON that can handle this? The…
tkx68
  • 69
  • 1
  • 1
  • 6
3
votes
1 answer

How do I parse polymorphic values using a type function in Haskell/Aeson?

In an attempt to improve my understanding of Haskell, I have started a personal project that allows users to combine many different predefined transforms that rely on an environment and state which are polymorphic. The core types are organized…
Jason Whittle
  • 751
  • 4
  • 23
3
votes
1 answer

pass type to decode json in haskell

I think I'm going about this problem in a fundamentally incorrect way, but I'm not sure how to make an elegant solution in Haskell. I would like to write a function which parses a string of JSON into some Haskell type. However, I don't know how to…
3
votes
1 answer

How to parse values distributed across an array with Aeson?

I have an json value of: { "name": "xyz1", "extra": [ { "this_string_A": "Hello" }, { "this_string_B": "World" } ] } And a data type of: data Abc = Abc { name :: String , a :: Maybe String , b :: Maybe…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
3
votes
2 answers

Haskell Aeson how to decode / parse based on `Value` type rather than `ByteString`?

I have the following function: parseUserBasic :: ByteString -> Either String [Either String UserBasic] parseUserBasic x = do xx <- parseItems x pure $ fmap (eitherDecode . encode) (items xx) However it's not very efficient because of pure $…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286