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
3
votes
2 answers

Deserializing to different types at runtime in haskell

Background I'm working with the Aeson library to store and retrieve values from a file. I am using Typeable (and TypeReps) to tag the data so I have a pretty good idea that it will parse correctly. I have a Class wherein each member of the class has…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
3
votes
1 answer

Data.Aeson with ForeignFunctionInterface in ghc

I have the following haskell code: {-# LANGUAGE OverloadedStrings, DeriveGeneric, DeriveAnyClass #-} module BoardToJSON where import GHC.Generics import Data.Aeson import Data.Aeson.Text (encodeToLazyText) import Data.Text import…
Maxime VAST
  • 540
  • 6
  • 22
3
votes
1 answer

Haskell/Aseon: output JSON as one object

I have the following Haskell code which encodes a list of the data type User in JSON and prints it to the standard output: {-# LANGUAGE OverloadedStrings #-} module Main where import Data.Aeson import Data.Text import qualified…
watain
  • 4,838
  • 4
  • 34
  • 35
3
votes
1 answer

Aeson: parse JSON with unknown key in Haskell

I would like to parse the following JSON using Aeson in Haskell: { "foo": { "name": "name 1", "location": "location 1" }, "bar": { "name": "name 2", "location": "location 2" } } The keys name and…
watain
  • 4,838
  • 4
  • 34
  • 35
3
votes
1 answer

FromJSON instance with DataKinds

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 …
Yves
  • 33
  • 4
3
votes
1 answer

Aeson parsing into multiple constructors

I'm trying to parse JSON to produce a type with multiple constructors. The challenge is that the type is encoded in the name of a key which contains the required data. In theory I could use a bunch of .:? calls and then check if given key returns…
Łukasz
  • 35,061
  • 4
  • 33
  • 33
3
votes
1 answer

Getting a collection of values from a JSON ByteString using lens-aeson

Say I have a JSON ByteString that looks something like { messages: [ {...}, {...} ] } I'd like to use lens to get a list/vector of messages out of it. I have a function toMessage that can turn a Value into a Maybe…
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
3
votes
1 answer

Conditionally add fields to JSON output

I have a couple types, User and Post. A Post is created by a User. My database looks the same as my types, which are data User = { userID :: Integer, name :: String } data Post = { content :: String, authorID :: Integer } -- authorID is the…
Justin Wood
  • 9,941
  • 2
  • 33
  • 46
3
votes
1 answer

FromJSON instance for newtype HashMap key

I made a newtype UUID in my application to represent Text ids. {-# LANGUAGE GeneralizedNewtypeDeriving #-} ... newtype UUID = UUID Text deriving (Eq, Generic, FromJSON, ToJSON, FromField, ToField, FromText, Show, Read, Hashable) My…
Sean Clark Hess
  • 15,859
  • 12
  • 52
  • 100
3
votes
1 answer

Aeson: how do I parse an object with an element that is a stringified object?

I need to parse an object that has a string element where the string itself is a stringified object: { "a" : "apples", "bar" : "{\"b\":\"bananas\"}" } I would like to parse this into Just ( Foo { fooA = "apples", fooBar = Bar { barB =…
helpwithhaskell
  • 558
  • 4
  • 13
3
votes
1 answer

How can I allow array individual elements to fail parsing while retaining others?

I have a json source that is not well behaved. It too often provides unexpected JSON that contains array elements that are malformed. I would like to parse this JSON and ignore any malformed array elements. Here is my attempt: {-# LANGUAGE…
helpwithhaskell
  • 558
  • 4
  • 13
3
votes
1 answer

Is it possible to make Traversal an instance of IsString

I want to use string literal as Traversal, but I am a bit lost in types. Is it possible to create this instance? import Control.Lens import Data.Aeson import Data.Aeson.Lens import Data.String import Data.Default {- Having: key' :: AsValue t =>…
Pavel
  • 127
  • 6
3
votes
1 answer

Parsing JSON with aeson for a compound data type

I have following data type: data DocumentOrDirectory = Document DocumentName DocumentContent | Directory DirectoryName [DocumentOrDirectory] I came with with following code for toJSON. It works, but needs improvement. It…
altern
  • 5,829
  • 5
  • 44
  • 72
3
votes
1 answer

Getting Aeson to deal with a mixed-type list

This works: λ decode "[\"one\", \"two\"]" :: Maybe [Text] Just ["one","two"] This works: λ decode "[1, 2]" :: Maybe [Int] Just [1,2] This is perfectly-valid JSON but I can't make it work: λ decode "[\"one\", 2]" :: Maybe [Text] Nothing Or even: λ…
Michael Fox
  • 3,632
  • 1
  • 17
  • 27
3
votes
1 answer

Ignoring a JSON field in Haskell Aeson input

I have a Haskell record data User = User { email :: Text , token :: Text } and I want to ignore the value of "token" in any input JSON. For example, if the input JSON is { "email": "foo@bar.com", "token": "abc123" } I want the…
Ralph
  • 31,584
  • 38
  • 145
  • 282