aeson-2.0.2.0: Fast JSON parsing and encoding
Copyright (c) 2011-2016 Bryan O'Sullivan
(c) 2011 MailRank Inc.
License BSD3
Maintainer Bryan O'Sullivan <bos@serpentine.com>
Stability experimental
Portability portable
Safe Haskell None
Language Haskell2010

Data.Aeson.Types

Description

Types for working with JSON data.

Synopsis

Core JSON types

data Value Source #

A JSON value represented as a Haskell value.

Instances

Instances details
Eq Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

Data Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

Methods

gfoldl :: ( forall d b. Data d => c (d -> b) -> d -> c b) -> ( forall g. g -> c g) -> Value -> c Value Source #

gunfold :: ( forall b r. Data b => c (b -> r) -> c r) -> ( forall r. r -> c r) -> Constr -> c Value Source #

toConstr :: Value -> Constr Source #

dataTypeOf :: Value -> DataType Source #

dataCast1 :: Typeable t => ( forall d. Data d => c (t d)) -> Maybe (c Value ) Source #

dataCast2 :: Typeable t => ( forall d e. ( Data d, Data e) => c (t d e)) -> Maybe (c Value ) Source #

gmapT :: ( forall b. Data b => b -> b) -> Value -> Value Source #

gmapQl :: (r -> r' -> r) -> r -> ( forall d. Data d => d -> r') -> Value -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> ( forall d. Data d => d -> r') -> Value -> r Source #

gmapQ :: ( forall d. Data d => d -> u) -> Value -> [u] Source #

gmapQi :: Int -> ( forall d. Data d => d -> u) -> Value -> u Source #

gmapM :: Monad m => ( forall d. Data d => d -> m d) -> Value -> m Value Source #

gmapMp :: MonadPlus m => ( forall d. Data d => d -> m d) -> Value -> m Value Source #

gmapMo :: MonadPlus m => ( forall d. Data d => d -> m d) -> Value -> m Value Source #

Ord Value Source #

The 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 ( hashable and 'unordered-containers').

Since: 1.5.2.0

Instance details

Defined in Data.Aeson.Types.Internal

Read Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

Show Value Source #

Since version 1.5.6.0 version object values are printed in lexicographic key order

>>> toJSON $ H.fromList [("a", True), ("z", False)]
Object (fromList [("a",Bool True),("z",Bool False)])
>>> toJSON $ H.fromList [("z", False), ("a", True)]
Object (fromList [("a",Bool True),("z",Bool False)])
Instance details

Defined in Data.Aeson.Types.Internal

IsString Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

Generic Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

NFData Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

Hashable Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

FromJSON Value Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

KeyValue Pair Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

KeyValue Object Source #

Constructs a singleton KeyMap . For calling functions that demand an Object for constructing objects. To be used in conjunction with mconcat . Prefer to use object where possible.

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Value Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

Lift Value Source #

Since: 0.11.0.0

Instance details

Defined in Data.Aeson.Types.Internal

GToJSON' Value arity ( U1 :: Type -> Type ) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

GToJSON' Value arity ( V1 :: Type -> Type ) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

GToJSON' Encoding arity ( U1 :: Type -> Type ) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON1 f => GToJSON' Value One ( Rec1 f) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON1 f => GToJSON' Encoding One ( Rec1 f) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

(WriteProduct arity a, WriteProduct arity b, ProductSize a, ProductSize b) => GToJSON' Value arity (a :*: b) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

Methods

gToJSON :: Options -> ToArgs Value arity a0 -> (a :*: b) a0 -> Value

ToJSON a => GToJSON' Value arity ( K1 i a :: Type -> Type ) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

Methods

gToJSON :: Options -> ToArgs Value arity a0 -> K1 i a a0 -> Value

(EncodeProduct arity a, EncodeProduct arity b) => GToJSON' Encoding arity (a :*: b) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => GToJSON' Encoding arity ( K1 i a :: Type -> Type ) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( ToJSON1 f, GToJSON' Value One g) => GToJSON' Value One (f :.: g) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( ToJSON1 f, GToJSON' Encoding One g) => GToJSON' Encoding One (f :.: g) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

type Rep Value Source #
Instance details

Defined in Data.Aeson.Types.Internal

data Key Source #

Instances

Instances details
Eq Key Source #
Instance details

Defined in Data.Aeson.Key

Data Key Source #
Instance details

Defined in Data.Aeson.Key

Methods

gfoldl :: ( forall d b. Data d => c (d -> b) -> d -> c b) -> ( forall g. g -> c g) -> Key -> c Key Source #

gunfold :: ( forall b r. Data b => c (b -> r) -> c r) -> ( forall r. r -> c r) -> Constr -> c Key Source #

toConstr :: Key -> Constr Source #

dataTypeOf :: Key -> DataType Source #

dataCast1 :: Typeable t => ( forall d. Data d => c (t d)) -> Maybe (c Key ) Source #

dataCast2 :: Typeable t => ( forall d e. ( Data d, Data e) => c (t d e)) -> Maybe (c Key ) Source #

gmapT :: ( forall b. Data b => b -> b) -> Key -> Key Source #

gmapQl :: (r -> r' -> r) -> r -> ( forall d. Data d => d -> r') -> Key -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> ( forall d. Data d => d -> r') -> Key -> r Source #

gmapQ :: ( forall d. Data d => d -> u) -> Key -> [u] Source #

gmapQi :: Int -> ( forall d. Data d => d -> u) -> Key -> u Source #

gmapM :: Monad m => ( forall d. Data d => d -> m d) -> Key -> m Key Source #

gmapMp :: MonadPlus m => ( forall d. Data d => d -> m d) -> Key -> m Key Source #

gmapMo :: MonadPlus m => ( forall d. Data d => d -> m d) -> Key -> m Key Source #

Ord Key Source #
Instance details

Defined in Data.Aeson.Key

Read Key Source #
Instance details

Defined in Data.Aeson.Key

Show Key Source #
Instance details

Defined in Data.Aeson.Key

IsString Key Source #
Instance details

Defined in Data.Aeson.Key

Semigroup Key Source #
Instance details

Defined in Data.Aeson.Key

Monoid Key Source #
Instance details

Defined in Data.Aeson.Key

NFData Key Source #
Instance details

Defined in Data.Aeson.Key

Methods

rnf :: Key -> () Source #

Hashable Key Source #
Instance details

Defined in Data.Aeson.Key

FromJSONKey Key Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Key Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

ToJSONKey Key Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

KeyValue Pair Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Key Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

Lift Key Source #
Instance details

Defined in Data.Aeson.Key

FunctorWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

Methods

imap :: ( Key -> a -> b) -> KeyMap a -> KeyMap b Source #

FoldableWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

Methods

ifoldMap :: Monoid m => ( Key -> a -> m) -> KeyMap a -> m Source #

ifoldMap' :: Monoid m => ( Key -> a -> m) -> KeyMap a -> m Source #

ifoldr :: ( Key -> a -> b -> b) -> b -> KeyMap a -> b Source #

ifoldl :: ( Key -> b -> a -> b) -> b -> KeyMap a -> b Source #

ifoldr' :: ( Key -> a -> b -> b) -> b -> KeyMap a -> b Source #

ifoldl' :: ( Key -> b -> a -> b) -> b -> KeyMap a -> b Source #

TraversableWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

Methods

itraverse :: Applicative f => ( Key -> a -> f b) -> KeyMap a -> f ( KeyMap b) Source #

SemialignWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

ZipWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

Methods

izipWith :: ( Key -> a -> b -> c) -> KeyMap a -> KeyMap b -> KeyMap c Source #

FilterableWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

WitherableWithIndex Key KeyMap Source #
Instance details

Defined in Data.Aeson.KeyMap

unsafeToEncoding :: Builder -> Encoding' a Source #

Make Encoding from Builder.

Use with care! You have to make sure that the passed Builder is a valid JSON Encoding!

fromEncoding :: Encoding' tag -> Builder Source #

Acquire the underlying bytestring builder.

data Series Source #

A 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)

type Array = Vector Value Source #

A JSON "array" (sequence).

type Pair = ( Key , Value ) Source #

A key/value pair for an Object .

type Object = KeyMap Value Source #

A JSON "object" (key/value map).

Convenience types and functions

newtype DotNetTime Source #

A 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

DotNetTime

Fields

Instances

Instances details
Eq DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.Internal

Ord DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.Internal

Read DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.Internal

Show DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.Internal

FormatTime DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.Internal

FromJSON DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

ToJSON DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

typeMismatch Source #

Arguments

:: String

The name of the JSON type being parsed ( "Object" , "Array" , "String" , "Number" , "Boolean" , or "Null" ).

-> Value

The actual value encountered.

-> Parser a

Fail parsing due to a type mismatch, with a descriptive message.

The following wrappers should generally be prefered: withObject , withArray , withText , withBool .

Error message example

typeMismatch "Object" (String "oops")
-- Error: "expected Object, but encountered String"

unexpected :: Value -> Parser a Source #

Fail parsing due to a type mismatch, when the expected types are implicit.

Error message example

unexpected (String "oops")
-- Error: "unexpected String"

Type conversion

data Parser a Source #

A JSON parser. N.B. This might not fit your usual understanding of "parser". Instead you might like to think of Parser as a "parse result", i.e. a parser to which the input has already been applied.

Instances

Instances details
Monad Parser Source #
Instance details

Defined in Data.Aeson.Types.Internal

Functor Parser Source #
Instance details

Defined in Data.Aeson.Types.Internal

MonadFail Parser Source #
Instance details

Defined in Data.Aeson.Types.Internal

Applicative Parser Source #
Instance details

Defined in Data.Aeson.Types.Internal

Alternative Parser Source #
Instance details

Defined in Data.Aeson.Types.Internal

MonadPlus Parser Source #
Instance details

Defined in Data.Aeson.Types.Internal

Semigroup ( Parser a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

Monoid ( Parser a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

data Result a Source #

The result of running a Parser .

Instances

Instances details
Monad Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

Functor Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

MonadFail Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

Applicative Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

Foldable Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

Traversable Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

Alternative Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

MonadPlus Result Source #
Instance details

Defined in Data.Aeson.Types.Internal

Eq a => Eq ( Result a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

Show a => Show ( Result a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

Semigroup ( Result a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

Monoid ( Result a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

NFData a => NFData ( Result a) Source #
Instance details

Defined in Data.Aeson.Types.Internal

Methods

rnf :: Result a -> () Source #

class FromJSON a where Source #

A 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

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

Minimal complete definition

Nothing

Instances

Instances details
FromJSON Bool Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Char Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Double Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Float Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int8 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int16 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int32 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int64 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Integer Source #

This 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.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Natural Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Ordering Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word8 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word16 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word32 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word64 Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON () Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Text Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Text Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Void Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Version Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON CTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON IntSet Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Scientific Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON ShortText Source #

Since: 2.0.2.0

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON ZonedTime Source #

Supported string formats:

YYYY-MM-DD HH:MM Z YYYY-MM-DD HH:MM:SS Z YYYY-MM-DD HH:MM:SS.SSS Z

The first space may instead be a T , and the second space is optional. The Z represents UTC. The Z may be replaced with a time zone offset of the form +0000 or -08:00 , where the first two digits are hours, the : is optional and the second two digits (also optional) are minutes.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON LocalTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON TimeOfDay Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON CalendarDiffTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON UTCTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON SystemTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON NominalDiffTime Source #

This 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.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON DiffTime Source #

This 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.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON DayOfWeek Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Day Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON CalendarDiffDays Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON QuarterOfYear Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Quarter Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Month Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON UUID Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Key Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Value Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON [a] Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Maybe a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, Integral a) => FromJSON ( Ratio a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Solo a) Source #

Since: 2.0.2.0

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Min a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Max a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( First a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Last a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( WrappedMonoid a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Option a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Identity a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( First a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Last a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Dual a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( NonEmpty a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( IntMap a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON v => FromJSON ( Tree v) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Seq a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( Ord a, FromJSON a) => FromJSON ( Set a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON1 f => FromJSON ( Fix f) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON1 f, Functor f) => FromJSON ( Mu f) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON1 f, Functor f) => FromJSON ( Nu f) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( DNonEmpty a) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( DList a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( Prim a, FromJSON a) => FromJSON ( PrimArray a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( SmallArray a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Array a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Maybe a) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( Eq a, Hashable a, FromJSON a) => FromJSON ( HashSet a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( Vector Vector a, FromJSON a) => FromJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( Storable a, FromJSON a) => FromJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( Prim a, FromJSON a) => FromJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON v => FromJSON ( KeyMap v) Source #

Since: 2.0.1.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b) => FromJSON ( Either a b) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b) => FromJSON (a, b) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

HasResolution a => FromJSON ( Fixed a) Source #

This 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.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON ( Proxy a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSONKey k, Ord k, FromJSON v) => FromJSON ( Map k v) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b) => FromJSON ( These a b) Source #

Since: 1.5.1.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b) => FromJSON ( Pair a b) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b) => FromJSON ( These a b) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b) => FromJSON ( Either a b) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON ( HashMap k v) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON ( Const a b) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON b => FromJSON ( Tagged a b) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON ( These1 f g a) Source #

Since: 1.5.1.0

Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON ( Product f g a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON ( Sum f g a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON (a, b, c, d, e) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON ( Compose f g a) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

( FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON (a, b, c, d, e, f) Source #
Instance details

Defined in 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l, m) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l, m)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l, m, n) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l, m, n)] Source #

( 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) Source #
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) Source #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)] Source #

fromJSON :: FromJSON a => Value -> Result a Source #

Convert a value from JSON, failing if the types do not match.

parseEither :: (a -> Parser b) -> a -> Either String b Source #

Run a Parser with an Either result type. If the parse fails, the Left payload will contain an error message.

parseMaybe :: (a -> Parser b) -> a -> Maybe b Source #

Run a Parser with a Maybe result type.

parseFail :: String -> Parser a Source #

Raise a parsing failure with some custom message.

class ToJSON a where Source #

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

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:

  1. 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.
  2. 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 Coord would be sufficient as a generically decoding instance, but there probably exists no good reason to not specify toEncoding in new instances.)

Minimal complete definition

Nothing

Methods

toJSON :: a -> Value Source #

Convert a Haskell value to a JSON-friendly intermediate type.

toEncoding :: a -> Encoding Source #

Encode 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 DeriveGeneric extension, and then have GHC generate a method body as follows.

instance ToJSON Coord where
    toEncoding = genericToEncoding defaultOptions

toJSONList :: [a] -> Value Source #

toEncodingList :: [a] -> Encoding Source #

Instances

Instances details
ToJSON Bool Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Char Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Double Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Float Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Int Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Int8 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Int16 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Int32 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Int64 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Integer Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Natural Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Ordering Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Word Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Word8 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Word16 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Word32 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Word64 Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON () Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Text Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Text Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Number Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Void Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Version Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON CTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON IntSet Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Scientific Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON ShortText Source #

Since: 2.0.2.0

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON ZonedTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON LocalTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON TimeOfDay Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON CalendarDiffTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON UTCTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON SystemTime Source #

Encoded as number

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON NominalDiffTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON DiffTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON DayOfWeek Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Day Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON CalendarDiffDays Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON QuarterOfYear Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Quarter Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Month Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON UUID Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Key Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON DotNetTime Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON Value Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON [a] Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Maybe a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( ToJSON a, Integral a) => ToJSON ( Ratio a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Solo a) Source #

Since: 2.0.2.0

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Min a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Max a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( First a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Last a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( WrappedMonoid a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Option a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Identity a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( First a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Last a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Dual a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( NonEmpty a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( IntMap a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON v => ToJSON ( Tree v) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Seq a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Set a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON1 f => ToJSON ( Fix f) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.ToJSON

( ToJSON1 f, Functor f) => ToJSON ( Mu f) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.ToJSON

( ToJSON1 f, Functor f) => ToJSON ( Nu f) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( DNonEmpty a) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( DList a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( Prim a, ToJSON a) => ToJSON ( PrimArray a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( SmallArray a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Array a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Maybe a) Source #

Since: 1.5.3.0

Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( HashSet a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( Vector Vector a, ToJSON a) => ToJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( Storable a, ToJSON a) => ToJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

( Prim a, ToJSON a) => ToJSON ( Vector a) Source #
Instance details

Defined in Data.Aeson.Types.ToJSON

ToJSON a => ToJSON ( Vector a) Source #