Efficiently deserialize a JSON value from a lazy L.ByteString.
If this fails due to incomplete or invalid input, Nothing is
returned.
Moduleaeson-2.2.3.0Haskell2010
Data.Aeson
Types and functions for working efficiently with JSON data.
(A note on naming: in Greek mythology, Aeson was the father of Jason.)
- 21 types
- 14 classes
- 72 values
- Packageaeson-2.2.3.0
- Exports107
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceAeson.hs
How to use this library
0 declarationsThis section contains basic information on the different ways to work with data using this library. These range from simple but inflexible, to complex but flexible.
The most common way to use the library is to define a data type, corresponding to some JSON data you want to work with, and then write either a FromJSON instance, a ToJSON instance, or both for that type.
For example, given this JSON data:
{ "name": "Joe", "age": 12 }we create a matching data type:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Person = Person {
name :: Text
, age :: Int
} deriving (Generic, Show)The LANGUAGE pragma and Generic instance let us write empty
FromJSON and ToJSON instances for which the compiler will
generate sensible default implementations.
instance ToJSON Person where
-- No need to provide a toJSON implementation.
-- For efficiency, we write a simple toEncoding implementation, as
-- the default version uses toJSON.
toEncoding = genericToEncoding defaultOptions
instance FromJSON Person
-- No need to provide a parseJSON implementation.
We can now encode a value like so:
>>> encode (Person {name = "Joe", age = 12})
"{\"name\":\"Joe\",\"age\":12}"Writing instances by hand
When necessary, we can write ToJSON and FromJSON instances by hand. This is valuable when the JSON-on-the-wire and Haskell data are different or otherwise need some more carefully managed translation. Let's revisit our JSON data:
{ "name": "Joe", "age": 12 }We once again create a matching data type, without bothering to add
a Generic instance this time:
data Person = Person {
name :: Text
, age :: Int
} deriving ShowTo decode data, we need to define a FromJSON instance:
{-# LANGUAGE OverloadedStrings #-}
instance FromJSON Person where
parseJSON = withObject "Person" $ \v -> Person
<$> v .: "name"
<*> v .: "age"We can now parse the JSON data like so:
>>> decode "{\"name\":\"Joe\",\"age\":12}" :: Maybe Person
Just (Person {name = "Joe", age = 12})To encode data, we need to define a ToJSON instance. Let's begin with an instance written entirely by hand.
instance ToJSON Person where
-- this generates a Value
toJSON (Person name age) =
object ["name" .= name, "age" .= age]
-- this encodes directly to a bytestring Builder
toEncoding (Person name age) =
pairs ("name" .= name <> "age" .= age)
We can now encode a value like so:
>>> encode (Person {name = "Joe", age = 12})
"{\"name\":\"Joe\",\"age\":12}"There are predefined FromJSON and ToJSON instances for many types. Here's an example using lists and Ints:
>>> decode "[1,2,3]" :: Maybe [Int]
Just [1,2,3]And here's an example using the Data.Map.Map type to get a map of
Ints.
>>> decode "{\"foo\":1,\"bar\":2}" :: Maybe (Map String Int)
Just (fromList [("bar",2),("foo",1)])Working with the AST
Sometimes you want to work with JSON data directly, without first converting it to a custom data type. This can be useful if you want to e.g. convert JSON data to YAML data, without knowing what the contents of the original JSON data was. The Value type, which is an instance of FromJSON, is used to represent an arbitrary JSON AST (abstract syntax tree). Example usage:
>>> decode "{\"foo\": 123}" :: Maybe Value
Just (Object (fromList [("foo",Number 123)]))>>> decode "{\"foo\": [\"abc\",\"def\"]}" :: Maybe Value
Just (Object (fromList [("foo",Array (fromList [String "abc",String "def"]))]))Once you have a Value you can write functions to traverse it and make arbitrary transformations.
Decoding to a Haskell value
We can decode to any instance of FromJSON:
λ> decode "[1,2,3]" :: Maybe [Int]
Just [1,2,3]Alternatively, there are instances for standard data types, so you
can use them directly. For example, use the Data.Map.Map type to
get a map of Ints.
λ> import Data.Map
λ> decode "{\"foo\":1,\"bar\":2}" :: Maybe (Map String Int)
Just (fromList [("bar",2),("foo",1)])Decoding a mixed-type object
The above approach with maps of course will not work for mixed-type objects that don't follow a strict schema, but there are a couple of approaches available for these.
The Object type contains JSON objects:
λ> decode "{\"name\":\"Dave\",\"age\":2}" :: Maybe Object
Just (fromList [("name",String "Dave"),("age",Number 2)])You can extract values from it with a parser using parse, parseEither or, in this example, parseMaybe:
λ> do result <- decode "{\"name\":\"Dave\",\"age\":2}"
flip parseMaybe result $ \obj -> do
age <- obj .: "age"
name <- obj .: "name"
return (name ++ ": " ++ show (age*2))
Just "Dave: 4"Considering that any type that implements FromJSON can be used here, this is quite a powerful way to parse JSON. See the documentation in FromJSON for how to implement this class for your own data types.
The downside is that you have to write the parser yourself; the upside is that you have complete control over the way the JSON is parsed.
Encoding and decoding
0 declarationsDecoding is a two-step process.
When decoding a value, the process is reversed: the bytes are converted to a Value, then the FromJSON class is used to convert to the desired type.
There are two ways to encode a value.
Convert to a Value using toJSON, then possibly further encode. This was the only method available in aeson 0.9 and earlier.
Directly encode (to what will become a ByteString) using toEncoding. This is much more efficient (about 3x faster, and less memory intensive besides), but is only available in aeson 0.10 and newer.
For convenience, the encode and decode functions combine both steps.
Direct encoding
In older versions of this library, encoding a Haskell value involved converting to an intermediate Value, then encoding that.
A "direct" encoder converts straight from a source Haskell value
to a BL.ByteString without constructing an intermediate Value.
This approach is faster than toJSON, and allocates less memory.
The toEncoding method makes it possible to implement direct
encoding with low memory overhead.
To complicate matters, the default implementation of toEncoding uses toJSON. Why? The toEncoding method was added to this library much more recently than toJSON. Using toJSON ensures that packages written against older versions of this library will compile and produce correct output, but they will not see any speedup from direct encoding.
To write a minimal implementation of direct encoding, your type
must implement GHC's Generic class, and your code should look
like this:
toEncoding = genericToEncoding defaultOptions
What if you have more elaborate encoding needs? For example, perhaps you need to change the names of object keys, omit parts of a value.
To encode to a JSON "object", use the pairs function.
toEncoding (Person name age) =
pairs ("name" .= name <> "age" .= age)
Any container type that implements Foldable can be encoded to a JSON "array" using foldable.
> import Data.Sequence as Seq
> encode (Seq.fromList [1,2,3])
"[1,2,3]"Remarks on specific encodings
0 declarationsTime
This module contains instances of ToJSON and FromJSON for types from the time library.
Those instances encode time as JSON strings in ISO 8601 formats, with the following general form for UTCTime and ZonedTime, while other time types use subsets of those fields:
[+,-]YYYY-MM-DDThh:mm[:ss[.sss]]Zwhere
[+,-]is an optional sign,+or-.YYYYis the year, which must have at least 4 digits to prevent Y2K problems. Years from0000to0999must thus be zero-padded.MMis a two-digit month.DDis a two-digit day.Tis a literal'T'character separating the date and the time of day. It may be a space instead.hhis a two-digit hour.mmis a two-digit minute.ssis a two-digit second.sssis a decimal fraction of a second; it may have any nonzero number of digits.Zis a time zone; it may be preceded by an optional space.
For more information, see ISO 8601 time, and text-iso8601 (where the relevant parsers are defined).
Main encoding and decoding functions
6 declarationsEfficiently deserialize a JSON value from a lazy ByteString. If this fails due to incomplete or invalid input, Nothing is returned.
Since 2.2.0.0 an alias for decode.
Like decode but returns an error message when decoding fails.
Like decode' but returns an error message when decoding fails.
Since 2.2.0.0 an alias for eitherDecode.
Efficiently serialize a JSON value as a lazy ByteString.
This is implemented in terms of the ToJSON class's toEncoding method.
Efficiently serialize a JSON value as a lazy ByteString and write it to a file.
Variants for strict bytestrings
Efficiently deserialize a JSON value from a strict B.ByteString.
If this fails due to incomplete or invalid input, Nothing is
returned.
Efficiently deserialize a JSON value from a file. If this fails due to incomplete or invalid input, Nothing is returned.
The input file's content must consist solely of a JSON document, with no trailing data except for whitespace.
This function parses immediately, but defers conversion. See
json for details.
Efficiently deserialize a JSON value from a strict ByteString. If this fails due to incomplete or invalid input, Nothing is returned.
Since 2.2.0.0 an alias for decodeStrict.
Efficiently deserialize a JSON value from a file. If this fails due to incomplete or invalid input, Nothing is returned.
Since 2.2.0.0 an alias for decodeFileStrict.
Like decodeStrict but returns an error message when decoding fails.
Like decodeFileStrict but returns an error message when decoding fails.
Like decodeStrict' but returns an error message when decoding fails.
Since 2.2.0.0 an alias for eitherDecodeStrict.
Like decodeFileStrict' but returns an error message when decoding fails.
Since 2.2.0.0 an alias for eitherDecodeFileStrict'.
Variants for strict text
Like decodeStrictText but returns an error message when decoding fails.
Exception throwing variants
Exception thrown by throwDecode and variants.
Constructors
Instances2Show, Exception
Show AesonExceptionDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalException AesonExceptionDefined in aeson-2.2.3.0 · Data.Aeson.Types.Internal
Like decode but throws an AesonException when decoding fails.
throwDecode is in aeson since 2.1.2.0, but this variant is added later.
Like decodeStrict but throws an AesonException when decoding fails.
Like decodeStrictText but throws an AesonException when decoding fails.
Like decode' but throws an AesonException when decoding fails.
Since 2.2.0.0 an alias for throwDecode.
Like decodeStrict' but throws an AesonException when decoding fails.
Since 2.2.0.0 an alias for throwDecodeStrict.
Core JSON types
6 declarationsA JSON value represented as a Haskell value.
Instances37Eq, Data, Ord, Read, Show, IsString, …
Eq ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalData ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalOrd ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalThe ordering is total, consistent with Eq instance. However, nothing else about the ordering is specified, and it may change from environment to environment and version to version of either this package or its dependencies (
hashableand 'unordered-containers').Read ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalShow ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalSince version 1.5.6.0 version object values are printed in lexicographic key order
Example1 expression toJSON $ H.fromList [("a", True), ("z", False)]Object (fromList [("a",Bool True),("z",Bool False)])
Example1 expression toJSON $ H.fromList [("z", False), ("a", True)]Object (fromList [("a",Bool True),("z",Bool False)])
IsString ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalGeneric ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalNFData ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalArbitrary ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalCoArbitrary ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalFunction ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalHashable ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalFromJSON ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONToJSON ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONLift ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalKeyValue Encoding SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONKeyValueOmit Encoding SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(GToJSON' Encoding arity a, ConsToJSON Encoding arity a, Constructor c) => SumToJSON' TwoElemArray Encoding arity (C1 c a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(GToJSON' Value arity a, ConsToJSON Value arity a, Constructor c) => SumToJSON' TwoElemArray Value arity (C1 c a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Encoding arity U1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Encoding arity V1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Value arity U1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Value arity V1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => GToJSON' Encoding One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => GToJSON' Value One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => GToJSON' Encoding arity (K1 i a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => GToJSON' Value arity (K1 i a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(EncodeProduct arity a, EncodeProduct arity b) => GToJSON' Encoding arity (a :*: b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(WriteProduct arity a, WriteProduct arity b, ProductSize a, ProductSize b) => GToJSON' Value arity (a :*: b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, GToJSON' Encoding One g) => GToJSON' Encoding One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, GToJSON' Value One g) => GToJSON' Value One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONvalue ~ Value => KeyValue Value (KeyMap value)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONvalue ~ Value => KeyValueOmit Value (KeyMap value)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONFromPairs Value (DList Pair)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONv ~ Value => KeyValuePair v (DList Pair)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(key ~ Key, value ~ Value) => KeyValue Value (key, value)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONtype Rep Value = D1 ('MetaDataDefined in aeson-2.2.3.0 · Data.Aeson.Types.Internal"Value"
"Data.Aeson.Types.Internal"
"aeson-2.2.3.0-8wqDv5YwVfEFAGhRLvt8rk"
'False) ((C1 ('MetaCons"Object"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Object)) :+: (C1 ('MetaCons"Array"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Array)) :+: C1 ('MetaCons"String"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)))) :+: (C1 ('MetaCons"Number"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Scientific)) :+: (C1 ('MetaCons"Bool"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons"Null"
'PrefixI 'False) U1)))
Often used synonym for Encoding'.
Acquire the underlying bytestring builder.
A JSON "array" (sequence).
A JSON "object" (key/value map).
Instances27Eq, Data, Ord, Read, Show, IsString, …
Eq KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyData KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyOrd KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyRead KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyShow KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyIsString KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeySemigroup KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyMonoid KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyNFData KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyArbitrary KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyCoArbitrary KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyFunction KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyHashable KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyFromJSON KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONToJSON KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONLift KeyDefined in aeson-2.2.3.0 · Data.Aeson.KeyFoldableWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapFunctorWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapTraversableWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapSemialignWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapZipWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapFilterableWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapWitherableWithIndex Key KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.KeyMapFromPairs Value (DList Pair)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONv ~ Value => KeyValuePair v (DList Pair)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Convenience types
1 declarationA newtype wrapper for UTCTime that uses the same non-standard serialization format as Microsoft .NET, whose System.DateTime type is by default serialized to JSON as in the following example:
/Date(1302547608878)/The number represents milliseconds since the Unix epoch.
Constructors
DotNetTimefromDotNetTime :: UTCTimeAcquire the underlying value.
Instances7Eq, Ord, Read, Show, FormatTime, FromJSON, …
Eq DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalOrd DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalRead DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalShow DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalFormatTime DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalFromJSON DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONToJSON DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Type conversion
8 declarationsA type that can be converted from JSON, with the possibility of failure.
In many cases, you can get the compiler to generate parsing code for you (see below). To begin, let's cover writing an instance by hand.
There are various reasons a conversion could fail. For example, an Object could be missing a required key, an Array could be of the wrong size, or a value could be of an incompatible type.
The basic ways to signal a failed conversion are as follows:
fail yields a custom error message: it is the recommended way of reporting a failure;
empty (or mzero) is uninformative: use it when the error is meant to be caught by some
(<|>);typeMismatch can be used to report a failure when the encountered value is not of the expected JSON type; unexpected is an appropriate alternative when more than one type may be expected, or to keep the expected type implicit.
prependFailure (or modifyFailure) add more information to a parser's error messages.
An example type and instance using typeMismatch and prependFailure:
-- Allow ourselves to write Text literals.
{-# LANGUAGE OverloadedStrings #-}
data Coord = Coord { x :: Double, y :: Double }
instance FromJSON Coord where
parseJSON (Object v) = Coord
<$> v .: "x"
<*> v .: "y"
-- We do not expect a non-Object value here.
-- We could use empty to fail, but typeMismatch
-- gives a much more informative error message.
parseJSON invalid =
prependFailure "parsing Coord failed, "
(typeMismatch "Object" invalid)
For this common case of only being concerned with a single type of JSON value, the functions withObject, withScientific, etc. are provided. Their use is to be preferred when possible, since they are more terse. Using withObject, we can rewrite the above instance (assuming the same language extension and data type) as:
instance FromJSON Coord where
parseJSON = withObject "Coord" $ \v -> Coord
<$> v .: "x"
<*> v .: "y"
Instead of manually writing your FromJSON instance, there are two options to do it automatically:
Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
The compiler can provide a default generic implementation for parseJSON.
To use the second, simply add a deriving Generic clause to your
datatype and declare a FromJSON instance for your datatype without giving
a definition for parseJSON.
For example, the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord = Coord { x :: Double, y :: Double } deriving Generic
instance FromJSON Coord
or using the DerivingVia extension
deriving via Generically Coord instance FromJSON Coord
The default implementation will be equivalent to
parseJSON = genericParseJSON defaultOptions; if you need different
options, you can customize the generic decoding by defining:
customOptions = defaultOptions
{ fieldLabelModifier = map toUpper
}
instance FromJSON Coord where
parseJSON = genericParseJSON customOptions
Methods
parseJSON :: Value -> Parser aparseJSONList :: Value -> Parser [a]omittedField :: Maybe aDefault value for optional fields. Used by
(.:?=)operator, and Generics and TH deriving withallowOmittedFields = True(default).
Instances113FromJSON, …
FromJSON KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON IntSetDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON IntegerDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONThis instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.
FromJSON NaturalDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON VoidDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON AllDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON AnyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON VersionDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON CTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Int16Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Int32Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Int64Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Int8Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Word16Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Word32Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Word64Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON Word8Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON BoolDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON CharDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON DoubleDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON FloatDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON IntDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON OrderingDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON WordDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON ScientificDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON ShortTextDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON CalendarDiffDaysDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON DayDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON MonthDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON QuarterDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON QuarterOfYearDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON DayOfWeekDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON DiffTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONThis instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.
FromJSON NominalDiffTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONThis instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.
FromJSON SystemTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON UTCTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON CalendarDiffTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON LocalTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON TimeOfDayDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON ZonedTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONSupported string formats:
YYYY-MM-DD HH:MMZYYYY-MM-DD HH:MM:SSZYYYY-MM-DD HH:MM:SS.SSSZThe first space may instead be a
T, and the second space is optional. TheZrepresents UTC. TheZmay be replaced with a time zone offset of the form+0000or-08:00, where the first two digits are hours, the:is optional and the second two digits (also optional) are minutes.FromJSON UUIDDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON ()Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (First a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Last a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Max a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Min a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (WrappedMonoid a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (IntMap a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Seq a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (DNonEmpty a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (DList a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (NonEmpty a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Identity a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (First a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Last a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Down a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Dual a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Product a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Sum a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Maybe a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Array a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (SmallArray a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Maybe a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON [a]Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON v => FromJSON (KeyMap v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON v => FromJSON (Tree v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 f => FromJSON (Fix f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, Integral a) => FromJSON (Ratio a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, Functor f) => FromJSON (Mu f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, Functor f) => FromJSON (Nu f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Storable a, FromJSON a) => FromJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Generic a, GFromJSON Zero (Rep a)) => FromJSON (Generically a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Eq a, Hashable a, FromJSON a) => FromJSON (HashSet a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Ord a, FromJSON a) => FromJSON (Set a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Prim a, FromJSON a) => FromJSON (PrimArray a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Prim a, FromJSON a) => FromJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Vector Vector a, FromJSON a) => FromJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON (Proxy a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONHasResolution a => FromJSON (Fixed a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONThis instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.
(FromJSON a, FromJSON b) => FromJSON (Either a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON (Either a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON (These a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON (Pair a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON (These a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON (a, b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (HashMap k v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSONKey k, Ord k, FromJSON v) => FromJSON (Map k v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON (Const a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON b => FromJSON (Tagged a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (These1 f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Product f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Sum f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON (a, b, c, d, e)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Compose f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON (a, b, c, d, e, f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON (a, b, c, d, e, f, g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h) => FromJSON (a, b, c, d, e, f, g, h)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i) => FromJSON (a, b, c, d, e, f, g, h, i)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) => FromJSON (a, b, c, d, e, f, g, h, i, j)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k) => FromJSON (a, b, c, d, e, f, g, h, i, j, k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
Instances13Monad, Functor, MonadFail, Applicative, Foldable, Traversable, …
Monad ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalFunctor ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalMonadFail ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalApplicative ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalFoldable ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalTraversable ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalAlternative ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalMonadPlus ResultDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalEq a => Eq (Result a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.InternalShow a => Show (Result a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.InternalSemigroup (Result a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.InternalMonoid (Result a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.InternalNFData a => NFData (Result a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.Internal
Convert a value from JSON, failing if the types do not match.
A type that can be converted to JSON.
Instances in general must specify toJSON and should (but don't need to) specify toEncoding.
An example type and instance:
-- Allow ourselves to write Text literals.
{-# LANGUAGE OverloadedStrings #-}
data Coord = Coord { x :: Double, y :: Double }
instance ToJSON Coord where
toJSON (Coord x y) = object ["x" .= x, "y" .= y]
toEncoding (Coord x y) = pairs ("x" .= x <> "y" .= y)
Instead of manually writing your ToJSON instance, there are two options to do it automatically:
Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
The compiler can provide a default generic implementation for toJSON.
To use the second, simply add a deriving Generic clause to your
datatype and declare a ToJSON instance. If you require nothing other than
defaultOptions, it is sufficient to write (and this is the only
alternative where the default toJSON implementation is sufficient):
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord = Coord { x :: Double, y :: Double } deriving Generic
instance ToJSON Coord where
toEncoding = genericToEncoding defaultOptions
or more conveniently using the DerivingVia extension
deriving via Generically Coord instance ToJSON Coord
If on the other hand you wish to customize the generic decoding, you have to implement both methods:
customOptions = defaultOptions
{ fieldLabelModifier = map toUpper
}
instance ToJSON Coord where
toJSON = genericToJSON customOptions
toEncoding = genericToEncoding customOptions
Previous versions of this library only had the toJSON method. Adding toEncoding had two reasons:
toEncoding is more efficient for the common case that the output of toJSON is directly serialized to a
ByteString. Further, expressing either method in terms of the other would be non-optimal.The choice of defaults allows a smooth transition for existing users: Existing instances that do not define toEncoding still compile and have the correct semantics. This is ensured by making the default implementation of toEncoding use toJSON. This produces correct results, but since it performs an intermediate conversion to a Value, it will be less efficient than directly emitting an Encoding. (this also means that specifying nothing more than
instance ToJSON Coordwould be sufficient as a generically decoding instance, but there probably exists no good reason to not specify toEncoding in new instances.)
Methods
toJSON :: a -> ValueConvert a Haskell value to a JSON-friendly intermediate type.
toEncoding :: a -> EncodingEncode a Haskell value as JSON.
The default implementation of this method creates an intermediate Value using toJSON. This provides source-level compatibility for people upgrading from older versions of this library, but obviously offers no performance advantage.
To benefit from direct encoding, you must provide an implementation for this method. The easiest way to do so is by having your types implement Generic using the
DeriveGenericextension, and then have GHC generate a method body as follows.instance ToJSON Coord where toEncoding = genericToEncoding defaultOptionstoJSONList :: [a] -> ValuetoEncodingList :: [a] -> EncodingomitField :: a -> BoolDefines when it is acceptable to omit a field of this type from a record. Used by
(.?=)operator, and Generics and TH deriving withomitNothingFields = True.
Instances113ToJSON, …
ToJSON KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON DotNetTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON ValueDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON IntSetDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON IntegerDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON NaturalDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON VoidDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON AllDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON AnyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON VersionDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON CTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Int16Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Int32Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Int64Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Int8Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Word16Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Word32Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Word64Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON Word8Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON BoolDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON CharDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON DoubleDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON FloatDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON IntDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON OrderingDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON WordDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON ScientificDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON ShortTextDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON CalendarDiffDaysDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON DayDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON MonthDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON QuarterDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON QuarterOfYearDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON DayOfWeekDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON DiffTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON NominalDiffTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON SystemTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONEncoded as number
ToJSON UTCTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON CalendarDiffTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON LocalTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON TimeOfDayDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON ZonedTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON UUIDDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON ()Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (First a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Last a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Max a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Min a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (WrappedMonoid a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (IntMap a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Seq a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Set a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (DNonEmpty a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (DList a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (NonEmpty a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Identity a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (First a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Last a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Down a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Dual a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Product a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Sum a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Maybe a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Array a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (SmallArray a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Maybe a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (HashSet a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON [a]Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON v => ToJSON (KeyMap v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON v => ToJSON (Tree v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => ToJSON (Fix f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, Integral a) => ToJSON (Ratio a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, Functor f) => ToJSON (Mu f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, Functor f) => ToJSON (Nu f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Storable a, ToJSON a) => ToJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Generic a, GToJSON' Value Zero (Rep a), GToJSON' Encoding Zero (Rep a)) => ToJSON (Generically a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Prim a, ToJSON a) => ToJSON (PrimArray a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Prim a, ToJSON a) => ToJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Vector Vector a, ToJSON a) => ToJSON (Vector a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON (Proxy a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONHasResolution a => ToJSON (Fixed a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON (Either a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON (Either a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON (These a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON (Pair a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON (These a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON (a, b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON v, ToJSONKey k) => ToJSON (Map k v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON v, ToJSONKey k) => ToJSON (HashMap k v)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON (Const a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON b => ToJSON (Tagged a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c) => ToJSON (a, b, c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (These1 f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a, b, c, d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Product f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Sum f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON (a, b, c, d, e)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g, ToJSON a) => ToJSON (Compose f g a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON (a, b, c, d, e, f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON (a, b, c, d, e, f, g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h) => ToJSON (a, b, c, d, e, f, g, h)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i) => ToJSON (a, b, c, d, e, f, g, h, i)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j) => ToJSON (a, b, c, d, e, f, g, h, i, j)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) => ToJSON (a, b, c, d, e, f, g, h, i, j, k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) => ToJSON (a, b, c, d, e, f, g, h, i, j, k, l)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m) => ToJSON (a, b, c, d, e, f, g, h, i, j, k, l, m)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m, ToJSON n) => ToJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m, ToJSON n, ToJSON o) => ToJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
A key-value pair for encoding a JSON object.
Methods
(.=) :: ToJSON v => Key -> v -> kvinfixr 8explicitToField :: (v -> e) -> Key -> v -> kv
Instances3KeyValue
An optional key-value pair for envoding to a JSON object
Instances2KeyValueOmit
KeyValueOmit Encoding SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONvalue ~ Value => KeyValueOmit Value (KeyMap value)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Add JSON Path context to a parser
When parsing a complex structure, it helps to annotate (sub)parsers with context, so that if an error occurs, you can find its location.
withObject "Person" $ \o ->
Person
<$> o .: "name" <?> Key "name"
<*> o .: "age" <?> Key "age"(Standard methods like (.:) already do this.)
With such annotations, if an error occurs, you will get a JSON Path location of that error.
Since 0.10
Keys for maps
Typeclass for types that can be used as the key of a map-like container
(like Map or HashMap). For example, since Text has a ToJSONKey
instance and Char has a ToJSON instance, we can encode a value of
type Map Text Char:
LBC8.putStrLn $ encode $ Map.fromList [("foo" :: Text, 'a')]{"foo":"a"}
Since Int also has a ToJSONKey instance, we can similarly write:
LBC8.putStrLn $ encode $ Map.fromList [(5 :: Int, 'a')]{"5":"a"}
JSON documents only accept strings as object keys. For any type
from base that has a natural textual representation, it can be
expected that its ToJSONKey instance will choose that representation.
For data types that lack a natural textual representation, an alternative is provided. The map-like container is represented as a JSON array instead of a JSON object. Each value in the array is an array with exactly two values. The first is the key and the second is the value.
For example, values of type '[Text]' cannot be encoded to a
string, so a Map with keys of type '[Text]' is encoded as follows:
LBC8.putStrLn $ encode $ Map.fromList [(["foo","bar","baz" :: Text], 'a')][[["foo","bar","baz"],"a"]]
The default implementation of ToJSONKey chooses this method of encoding a key, using the ToJSON instance of the type.
To use your own data type as the key in a map, all that is needed
is to write a ToJSONKey (and possibly a FromJSONKey) instance
for it. If the type cannot be trivially converted to and from Text,
it is recommended that ToJSONKeyValue is used. Since the default
implementations of the typeclass methods can build this from a
ToJSON instance, there is nothing that needs to be written:
data Foo = Foo { fooAge :: Int, fooName :: Text }
deriving (Eq,Ord,Generic)
instance ToJSON Foo
instance ToJSONKey FooThat's it. We can now write:
let m = Map.fromList [(Foo 4 "bar",'a'),(Foo 6 "arg",'b')]LBC8.putStrLn $ encode m[[{"fooName":"bar","fooAge":4},"a"],[{"fooName":"arg","fooAge":6},"b"]]
The next case to consider is if we have a type that is a newtype wrapper around Text. The recommended approach is to use generalized newtype deriving:
newtype RecordId = RecordId { getRecordId :: Text }
deriving (Eq,Ord,ToJSONKey)Then we may write:
LBC8.putStrLn $ encode $ Map.fromList [(RecordId "abc",'a')]{"abc":"a"}
Simple sum types are a final case worth considering. Suppose we have:
data Color = Red | Green | Blue
deriving (Show,Read,Eq,Ord)It is possible to get the ToJSONKey instance for free as we did
with Foo. However, in this case, we have a natural way to go to
and from Text that does not require any escape sequences. So
ToJSONKeyText can be used instead of ToJSONKeyValue to encode maps
as objects instead of arrays of pairs. This instance may be
implemented using generics as follows:
instance ToJSONKey Color where
toJSONKey = genericToJSONKey defaultJSONKeyOptions
Low-level implementations
The Show instance can be used to help write ToJSONKey:
instance ToJSONKey Color where
toJSONKey = ToJSONKeyText f g
where f = Text.pack . show
g = text . Text.pack . show
-- text function is from Data.Aeson.EncodingThe situation of needing to turning function a -> Text into
a ToJSONKeyFunction is common enough that a special combinator
is provided for it. The above instance can be rewritten as:
instance ToJSONKey Color where
toJSONKey = toJSONKeyText (Text.pack . show)The performance of the above instance can be improved by
not using String as an intermediate step when converting to
Text. One option for improving performance would be to use
template haskell machinery from the text-show package. However,
even with the approach, the Encoding (a wrapper around a bytestring
builder) is generated by encoding the Text to a ByteString,
an intermediate step that could be avoided. The fastest possible
implementation would be:
-- Assuming that OverloadedStrings is enabled
instance ToJSONKey Color where
toJSONKey = ToJSONKeyText f g
where f x = case x of {Red -> "Red";Green ->"Green";Blue -> "Blue"}
g x = case x of {Red -> text "Red";Green -> text "Green";Blue -> text "Blue"}
-- text function is from Data.Aeson.EncodingThis works because GHC can lift the encoded values out of the case statements, which means that they are only evaluated once. This approach should only be used when there is a serious need to maximize performance.
Methods
toJSONKey :: ToJSONKeyFunction aStrategy for rendering the key for a map-like container.
toJSONKeyList :: ToJSONKeyFunction [a]
Instances43ToJSONKey, …
ToJSONKey KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey IntegerDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey NaturalDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey VoidDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey VersionDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Int16Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Int32Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Int64Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Int8Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Word16Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Word32Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Word64Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey Word8Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey BoolDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey CharDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey DoubleDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey FloatDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey IntDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey WordDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey ScientificDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey ShortTextDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey DayDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey MonthDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey QuarterDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey QuarterOfYearDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey DayOfWeekDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey UTCTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey LocalTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey TimeOfDayDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey ZonedTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey UUIDDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey a => ToJSONKey (Identity a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey a => ToJSONKey (a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSONKey a, ToJSON a) => ToJSONKey [a]Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONHasResolution a => ToJSONKey (Fixed a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSONKey (a, b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey b => ToJSONKey (Tagged a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a, b, c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSONKey a) => ToJSONKey (Const a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a, b, c, d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Constructors
ToJSONKeyText !(a -> Key) !(a -> Encoding' Key)key is encoded to string, produces object
ToJSONKeyValue !(a -> Value) !(a -> Encoding)key is encoded to value, produces array
Instances1Contravariant
Contravariant ToJSONKeyFunctionDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Read the docs for ToJSONKey first. This class is a conversion
in the opposite direction. If you have a newtype wrapper around Text,
the recommended way to define instances is with generalized newtype deriving:
newtype SomeId = SomeId { getSomeId :: Text }
deriving (Eq,Ord,Hashable,FromJSONKey)If you have a sum of nullary constructors, you may use the generic implementation:
data Color = Red | Green | Blue
deriving Generic
instance FromJSONKey Color where
fromJSONKey = genericFromJSONKey defaultJSONKeyOptions
Methods
fromJSONKey :: FromJSONKeyFunction aStrategy for parsing the key of a map-like container.
fromJSONKeyList :: FromJSONKeyFunction [a]
Instances41FromJSONKey, …
FromJSONKey KeyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey IntegerDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey NaturalDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey VoidDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey VersionDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Int16Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Int32Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Int64Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Int8Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Word16Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Word32Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Word64Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey Word8Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey BoolDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey CharDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey DoubleDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey FloatDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey IntDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey WordDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey TextDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey ShortTextDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey DayDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey MonthDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey QuarterDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey QuarterOfYearDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey DayOfWeekDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey UTCTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey LocalTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey TimeOfDayDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey ZonedTimeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey UUIDDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey a => FromJSONKey (Identity a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey a => FromJSONKey (a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSONKey a, FromJSON a) => FromJSONKey [a]Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSONKey (a, b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey b => FromJSONKey (Tagged a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c) => FromJSONKey (a, b, c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSONKey a) => FromJSONKey (Const a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSONKey (a, b, c, d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
This type is related to ToJSONKeyFunction. If FromJSONKeyValue is used in the
FromJSONKey instance, then ToJSONKeyValue should be used in the ToJSONKey
instance. The other three data constructors for this type all correspond to
ToJSONKeyText. Strictly speaking, FromJSONKeyTextParser is more powerful than
FromJSONKeyText, which is in turn more powerful than FromJSONKeyCoerce.
For performance reasons, these exist as three options instead of one.
Constructors
FromJSONKeyCoerce :: Coercible Text a => FromJSONKeyFunction aFromJSONKeyText :: !(Text -> a) -> FromJSONKeyFunction aFromJSONKeyTextParser :: !(Text -> Parser a) -> FromJSONKeyFunction aFromJSONKeyValue :: !(Value -> Parser a) -> FromJSONKeyFunction a
Instances1Functor
Functor FromJSONKeyFunctionDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONOnly law abiding up to interpretation
Generic keys
Instances1GToJSONKey
GetConName f => GToJSONKey fDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Instances1GFromJSONKey
(ConstructorNames f, SumFromString f) => GFromJSONKey fDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
fromJSONKey for Generic types. These types must be sums of nullary constructors, whose names will be used as keys for JSON objects.
See also genericToJSONKey.
Example
data Color = Red | Green | Blue
deriving Generic
instance FromJSONKey Color where
fromJSONKey = genericFromJSONKey defaultJSONKeyOptions
Liftings to unary and binary type constructors
Lifting of the FromJSON class to unary type constructors.
Instead of manually writing your FromJSON1 instance, there are two options to do it automatically:
Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
The compiler can provide a default generic implementation for liftParseJSON.
To use the second, simply add a deriving Generic1 clause to your
datatype and declare a FromJSON1 instance for your datatype without giving
a definition for liftParseJSON.
For example:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Pair a b = Pair { pairFst :: a, pairSnd :: b } deriving Generic1
instance FromJSON a => FromJSON1 (Pair a)
or
deriving via Generically1 (Pair a) instance FromJSON1 (Pair a)
If the default implementation doesn't give exactly the results you want, you can customize the generic decoding with only a tiny amount of effort, using genericLiftParseJSON with your preferred Options:
customOptions = defaultOptions
{ fieldLabelModifier = map toUpper
}
instance FromJSON a => FromJSON1 (Pair a) where
liftParseJSON = genericLiftParseJSON customOptions
Instances53FromJSON1, …
FromJSON1 KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 FirstDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 LastDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 MaxDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 MinDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 WrappedMonoidDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 IntMapDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 SeqDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 TreeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 DNonEmptyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 DListDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 NonEmptyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 IdentityDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 FirstDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 LastDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 DownDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 DualDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 ProductDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 SumDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 MaybeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 SoloDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 MaybeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 VectorDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 []Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (Either a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (Tuple2 a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (Either a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (These a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (Pair a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (These a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 ProxyDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSONKey k, Eq k, Hashable k) => FromJSON1 (HashMap k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSONKey k, Ord k) => FromJSON1 (Map k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON1 (Const a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 (Tagged a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON1 (Tuple3 a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g) => FromJSON1 (These1 f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Generic1 f, GFromJSON One (Rep1 f)) => FromJSON1 (Generically1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c) => FromJSON1 (Tuple4 a b c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g) => FromJSON1 (Product f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g) => FromJSON1 (Sum f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON1 (Tuple5 a b c d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, FromJSON1 g) => FromJSON1 (Compose f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON1 (Tuple6 a b c d e)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON1 (Tuple7 a b c d e f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON1 (Tuple8 a b c d e f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h) => FromJSON1 (Tuple9 a b c d e f g h)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i) => FromJSON1 (Tuple10 a b c d e f g h i)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) => FromJSON1 (Tuple11 a b c d e f g h i j)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k) => FromJSON1 (Tuple12 a b c d e f g h i j k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l) => FromJSON1 (Tuple13 a b c d e f g h i j k l)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m) => FromJSON1 (Tuple14 a b c d e f g h i j k l m)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n) => FromJSON1 (Tuple15 a b c d e f g h i j k l m n)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
Lift the standard parseJSON function through the type constructor.
Lifting of the FromJSON class to binary type constructors.
Instead of manually writing your FromJSON2 instance, Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time.
Methods
liftParseJSON2 :: Maybe a -> (Value -> Parser a) -> (Value -> Parser [a]) -> Maybe b -> (Value -> Parser b) -> (Value -> Parser [b]) -> Value -> Parser (f a b)liftParseJSONList2 :: Maybe a -> (Value -> Parser a) -> (Value -> Parser [a]) -> Maybe b -> (Value -> Parser b) -> (Value -> Parser [b]) -> Value -> Parser [f a b]liftOmittedField2 :: Maybe a -> Maybe b -> Maybe (f a b)
Instances21FromJSON2, …
FromJSON2 EitherDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 Tuple2Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 EitherDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 TheseDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 PairDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 TheseDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => FromJSON2 (Tuple3 a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 ConstDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON2 TaggedDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b) => FromJSON2 (Tuple4 a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c) => FromJSON2 (Tuple5 a b c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON2 (Tuple6 a b c d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON2 (Tuple7 a b c d e)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON2 (Tuple8 a b c d e f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON2 (Tuple9 a b c d e f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h) => FromJSON2 (Tuple10 a b c d e f g h)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i) => FromJSON2 (Tuple11 a b c d e f g h i)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) => FromJSON2 (Tuple12 a b c d e f g h i j)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k) => FromJSON2 (Tuple13 a b c d e f g h i j k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l) => FromJSON2 (Tuple14 a b c d e f g h i j k l)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m) => FromJSON2 (Tuple15 a b c d e f g h i j k l m)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
Lift the standard parseJSON function through the type constructor.
Lifting of the ToJSON class to unary type constructors.
Instead of manually writing your ToJSON1 instance, there are two options to do it automatically:
Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
The compiler can provide a default generic implementation for toJSON1.
To use the second, simply add a deriving Generic1 clause to your
datatype and declare a ToJSON1 instance for your datatype without giving
definitions for liftToJSON or liftToEncoding.
For example:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Pair a b = Pair { pairFst :: a, pairSnd :: b } deriving Generic1
instance ToJSON a => ToJSON1 (Pair a)
If the default implementation doesn't give exactly the results you want, you can customize the generic encoding with only a tiny amount of effort, using genericLiftToJSON and genericLiftToEncoding with your preferred Options:
customOptions = defaultOptions
{ fieldLabelModifier = map toUpper
}
instance ToJSON a => ToJSON1 (Pair a) where
liftToJSON = genericLiftToJSON customOptions
liftToEncoding = genericLiftToEncoding customOptions
See also ToJSON.
Methods
liftToJSON :: (a -> Bool) -> (a -> Value) -> ([a] -> Value) -> f a -> ValueliftToJSONList :: (a -> Bool) -> (a -> Value) -> ([a] -> Value) -> [f a] -> ValueliftToEncoding :: (a -> Bool) -> (a -> Encoding) -> ([a] -> Encoding) -> f a -> EncodingliftToEncodingList :: (a -> Bool) -> (a -> Encoding) -> ([a] -> Encoding) -> [f a] -> EncodingliftOmitField :: (a -> Bool) -> f a -> Bool
Instances55ToJSON1, …
ToJSON1 KeyMapDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 FirstDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 LastDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 MaxDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 MinDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 WrappedMonoidDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 IntMapDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 SeqDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 SetDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 TreeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 DNonEmptyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 DListDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 NonEmptyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 IdentityDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 FirstDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 LastDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 DownDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 DualDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 ProductDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 SumDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 MaybeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 SoloDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 MaybeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 HashSetDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 VectorDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 []Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (Either a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (Tuple2 a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (Either a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (These a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (Pair a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (These a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 ProxyDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey k => ToJSON1 (Map k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey k => ToJSON1 (HashMap k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON1 (Const a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 (Tagged a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON1 (Tuple3 a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g) => ToJSON1 (These1 f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Generic1 f, GToJSON' Value One (Rep1 f), GToJSON' Encoding One (Rep1 f)) => ToJSON1 (Generically1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c) => ToJSON1 (Tuple4 a b c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g) => ToJSON1 (Product f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g) => ToJSON1 (Sum f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON1 (Tuple5 a b c d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, ToJSON1 g) => ToJSON1 (Compose f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON1 (Tuple6 a b c d e)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON1 (Tuple7 a b c d e f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON1 (Tuple8 a b c d e f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h) => ToJSON1 (Tuple9 a b c d e f g h)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i) => ToJSON1 (Tuple10 a b c d e f g h i)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j) => ToJSON1 (Tuple11 a b c d e f g h i j)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) => ToJSON1 (Tuple12 a b c d e f g h i j k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) => ToJSON1 (Tuple13 a b c d e f g h i j k l)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m) => ToJSON1 (Tuple14 a b c d e f g h i j k l m)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m, ToJSON n) => ToJSON1 (Tuple15 a b c d e f g h i j k l m n)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Lift the standard toJSON function through the type constructor.
Lift the standard toEncoding function through the type constructor.
Lifting of the ToJSON class to binary type constructors.
Instead of manually writing your ToJSON2 instance, Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time.
The compiler cannot provide a default generic implementation for liftToJSON2, unlike toJSON and liftToJSON.
Methods
liftToJSON2 :: (a -> Bool) -> (a -> Value) -> ([a] -> Value) -> (b -> Bool) -> (b -> Value) -> ([b] -> Value) -> f a b -> ValueliftToJSONList2 :: (a -> Bool) -> (a -> Value) -> ([a] -> Value) -> (b -> Bool) -> (b -> Value) -> ([b] -> Value) -> [f a b] -> ValueliftToEncoding2 :: (a -> Bool) -> (a -> Encoding) -> ([a] -> Encoding) -> (b -> Bool) -> (b -> Encoding) -> ([b] -> Encoding) -> f a b -> EncodingliftToEncodingList2 :: (a -> Bool) -> (a -> Encoding) -> ([a] -> Encoding) -> (b -> Bool) -> (b -> Encoding) -> ([b] -> Encoding) -> [f a b] -> EncodingliftOmitField2 :: (a -> Bool) -> (b -> Bool) -> f a b -> Bool
Instances21ToJSON2, …
ToJSON2 EitherDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 Tuple2Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 EitherDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 TheseDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 PairDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 TheseDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => ToJSON2 (Tuple3 a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 ConstDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON2 TaggedDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b) => ToJSON2 (Tuple4 a b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c) => ToJSON2 (Tuple5 a b c)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON2 (Tuple6 a b c d)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON2 (Tuple7 a b c d e)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON2 (Tuple8 a b c d e f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON2 (Tuple9 a b c d e f g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h) => ToJSON2 (Tuple10 a b c d e f g h)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i) => ToJSON2 (Tuple11 a b c d e f g h i)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j) => ToJSON2 (Tuple12 a b c d e f g h i j)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) => ToJSON2 (Tuple13 a b c d e f g h i j k)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) => ToJSON2 (Tuple14 a b c d e f g h i j k l)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l, ToJSON m) => ToJSON2 (Tuple15 a b c d e f g h i j k l m)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Lift the standard toJSON function through the type constructor.
Lift the standard toEncoding function through the type constructor.
Generic JSON classes and options
Class of generic representation types that can be converted from JSON.
Instances7GFromJSON, …
GFromJSON One Par1Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONGFromJSON arity V1Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON1 f => GFromJSON One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSON a => GFromJSON arity (K1 i a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(GFromJSON' arity a, Datatype d) => GFromJSON arity (D1 d a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONGFromJSON arity a => GFromJSON arity (M1 i c a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, GFromJSON One g) => GFromJSON One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
Class of generic representation types that can be converted to JSON.
Instances17GToJSON', …
GToJSON' enc One Par1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Encoding arity U1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Encoding arity V1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Value arity U1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' Value arity V1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => GToJSON' Encoding One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => GToJSON' Value One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONConsToJSON enc arity a => GToJSON' enc arity (C1 c a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => GToJSON' Encoding arity (K1 i a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON a => GToJSON' Value arity (K1 i a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(AllNullary (a :+: b) allNullary, SumToJSON enc arity (a :+: b) allNullary) => GToJSON' enc arity (a :+: b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ConsToJSON enc arity a, AllNullary (C1 c a) allNullary, SumToJSON enc arity (C1 c a) allNullary) => GToJSON' enc arity (D1 d (C1 c a))Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(EncodeProduct arity a, EncodeProduct arity b) => GToJSON' Encoding arity (a :*: b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(WriteProduct arity a, WriteProduct arity b, ProductSize a, ProductSize b) => GToJSON' Value arity (a :*: b)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONGToJSON' enc arity a => GToJSON' enc arity (M1 i c a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, GToJSON' Encoding One g) => GToJSON' Encoding One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, GToJSON' Value One g) => GToJSON' Value One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
A type-level indicator that ToJSON or FromJSON is being derived generically.
A type-level indicator that ToJSON1 or FromJSON1 is being derived generically.
Instances12RecordToPairs, GToJSON', RecordFromJSON', GFromJSON, …
GFromJSON One Par1Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONGToJSON' enc One Par1Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Selector s, GToJSON' enc One Par1, KeyValuePair enc pairs) => RecordToPairs enc pairs One (S1 s Par1)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(Selector s, GToJSON' enc One (Rec1 f), KeyValuePair enc pairs, ToJSON1 f) => RecordToPairs enc pairs One (S1 s (Rec1 f))Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => GToJSON' Encoding One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSON1 f => GToJSON' Value One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, GToJSON' Encoding One g) => GToJSON' Encoding One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON(ToJSON1 f, GToJSON' Value One g) => GToJSON' Value One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONFromJSON1 f => GFromJSON One (Rec1 f)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Selector s, GFromJSON One Par1) => RecordFromJSON' One (S1 s Par1)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(Selector s, GFromJSON One (Rec1 f), FromJSON1 f) => RecordFromJSON' One (S1 s (Rec1 f))Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON(FromJSON1 f, GFromJSON One g) => GFromJSON One (f :.: g)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSON
A configurable generic JSON creator. This function applied to defaultOptions is used as the default for toJSON when the type is an instance of Generic.
A configurable generic JSON creator. This function applied to defaultOptions is used as the default for liftToJSON when the type is an instance of Generic1.
A configurable generic JSON encoder. This function applied to defaultOptions is used as the default for toEncoding when the type is an instance of Generic.
A configurable generic JSON encoder. This function applied to defaultOptions is used as the default for liftToEncoding when the type is an instance of Generic1.
A configurable generic JSON decoder. This function applied to defaultOptions is used as the default for parseJSON when the type is an instance of Generic.
A configurable generic JSON decoder. This function applied to defaultOptions is used as the default for liftParseJSON when the type is an instance of Generic1.
Generic and TH encoding configuration
Options that specify how to encode/decode your datatype to/from JSON.
Options can be set using record syntax on defaultOptions with the fields below.
Default encoding Options:
Options
{ fieldLabelModifier = id
, constructorTagModifier = id
, allNullaryToStringTag = True
, omitNothingFields = False
, allowOmittedFields = True
, sumEncoding = defaultTaggedObject
, unwrapUnaryRecords = False
, tagSingleConstructors = False
, rejectUnknownFields = False
}
Options fields
Function applied to field labels. Handy for removing common record prefixes for example.
Function applied to constructor tags which could be handy for lower-casing them for example.
If True the constructors of a datatype, with all nullary constructors, will be encoded to just a string with the constructor tag. If False the encoding will always follow the sumEncoding.
If True, record fields with a Nothing value will be
omitted from the resulting object. If False, the resulting
object will include those fields mapping to null.
In aeson-2.2 this flag is generalised to omit all values with omitField x = True.
If False, the resulting object will include those fields encoded as specified.
Note that this does not affect parsing: Maybe fields are
optional regardless of the value of omitNothingFields.
allowOmittedFieds controls parsing behavior.
Specifies how to encode constructors of a sum datatype.
Hide the field name when a record constructor has only one field, like a newtype.
Encode types with a single constructor as sums, so that allNullaryToStringTag and sumEncoding apply.
Applies only to Data.Aeson.FromJSON instances. If a field appears in
the parsed object map, but does not appear in the target object, parsing
will fail, with an error message indicating which fields were unknown.
Options utilities
Specifies how to encode constructors of a sum datatype.
Constructors
TaggedObjectA constructor will be encoded to an object with a field tagFieldName which specifies the constructor tag (modified by the constructorTagModifier). If the constructor is a record the encoded record fields will be unpacked into this object. So make sure that your record doesn't have a field with the same label as the tagFieldName. Otherwise the tag gets overwritten by the encoded value of that field! If the constructor is not a record the encoded constructor contents will be stored under the contentsFieldName field.
UntaggedValueConstructor names won't be encoded. Instead only the contents of the constructor will be encoded as if the type had a single constructor. JSON encodings have to be disjoint for decoding to work properly.
When decoding, constructors are tried in the order of definition. If some encodings overlap, the first one defined will succeed.
Note: Nullary constructors are encoded as strings (using constructorTagModifier). Having a nullary constructor alongside a single field constructor that encodes to a string leads to ambiguity.
Note: Only the last error is kept when decoding, so in the case of malformed JSON, only an error for the last constructor will be reported.
ObjectWithSingleFieldA constructor will be encoded to an object with a single field named after the constructor tag (modified by the constructorTagModifier) which maps to the encoded contents of the constructor.
TwoElemArrayA constructor will be encoded to a 2-element array where the first element is the tag of the constructor (modified by the constructorTagModifier) and the second element the encoded contents of the constructor.
Instances2Eq, Show
Eq SumEncodingDefined in aeson-2.2.3.0 · Data.Aeson.Types.InternalShow SumEncodingDefined in aeson-2.2.3.0 · Data.Aeson.Types.Internal
Better version of camelTo. Example where it works better:
camelTo '_' "CamelAPICase" == "camel_apicase"
camelTo2 '_' "CamelAPICase" == "camel_api_case"Default TaggedObject SumEncoding options:
defaultTaggedObject = TaggedObject
{ tagFieldName = "tag"
, contentsFieldName = "contents"
}
Options for object keys
Options for encoding keys with genericFromJSONKey and genericToJSONKey.
Function applied to keys. Its result is what goes into the encoded Value.
Example
The following instances encode the constructor Bar to lower-case keys
"bar".
data Foo = Bar
deriving Generic
opts :: JSONKeyOptions
opts = defaultJSONKeyOptions { keyModifier = toLower }
instance ToJSONKey Foo where
toJSONKey = genericToJSONKey opts
instance FromJSONKey Foo where
fromJSONKey = genericFromJSONKey opts
Default JSONKeyOptions:
defaultJSONKeyOptions = JSONKeyOptions
{ keyModifier = id
}
Inspecting Values
6 declarationswithObject name f value applies f to the Object when value
is an Object and fails otherwise.
Error message example
withObject "MyType" f (String "oops")
-- Error: "parsing MyType failed, expected Object, but encountered String"withScientific name f value applies f to the Scientific number
when value is a Data.Aeson.Number and fails using typeMismatch
otherwise.
Warning: If you are converting from a scientific to an unbounded
type such as Integer you may want to add a restriction on the
size of the exponent (see withBoundedScientific) to prevent
malicious input from filling up the memory of the target system.
Error message example
withScientific "MyType" f (String "oops")
-- Error: "parsing MyType failed, expected Number, but encountered String"Decode a nested JSON-encoded string.
Constructors and accessors
10 declarationsA series of values that, when encoded, should be separated by
commas. Since 0.11.0.0, the .= operator is overloaded to create
either (Text, Value) or Series. You can use Series when
encoding directly to a bytestring builder as in the following
example:
toEncoding (Person name age) = pairs ("name" .= name <> "age" .= age)Instances6Semigroup, Monoid, KeyValue, KeyValueOmit, KeyValuePair, FromPairs
Semigroup SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Encoding.InternalMonoid SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Encoding.InternalKeyValue Encoding SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONKeyValueOmit Encoding SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONe ~ Encoding => KeyValuePair e SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONa ~ Value => FromPairs (Encoding' a) SeriesDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSON
Encode a series of key/value pairs, separated by commas.
Encode a Foldable as a JSON array.
Retrieve the value associated with the given key of an Object. The result is empty if the key is not present or the value cannot be converted to the desired type.
This accessor is appropriate if the key and value must be present in an object for it to be valid. If the key and value are optional, use .:? instead.
Retrieve the value associated with the given key of an Object. The result is Nothing if the key is not present or if its value is Null, or empty if the value cannot be converted to the desired type.
This accessor is most useful if the key and value can be absent from an object without affecting its validity. If the key and value are mandatory, use .: instead.
Helper for use in combination with .:? to provide default values for optional JSON object fields.
This combinator is most useful if the key and value can be absent from an object without affecting its validity and we know a default value to assign in that case. If the key and value are mandatory, use .: instead.
Example usage:
v1 <- o .:? "opt_field_with_dfl" .!= "default_val"
v2 <- o .: "mandatory_field"
v3 <- o .:? "opt_field2"
Retrieve the value associated with the given key of an Object.
If the key is not present or the field is null and the omittedField is Just x for some x,
the result will be that x.
Retrieve the value associated with the given key of an Object.
If the key is not present and the omittedField is Just x for some x,
the result will be that x.
This differs from .:?= by attempting to parse Null the same as any other JSON value, instead of using omittedField when it's Just.