aws-ec2-0.3.7: AWS EC2/VPC, ELB and CloudWatch client library for Haskell

Safe HaskellNone




module Aws.Core

module Aws.Query

data Text :: * #

A space efficient, packed, unboxed Unicode text type.

class FromJSON a #

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:

  • empty and mzero work, but are terse and uninformative
  • fail yields a custom error message
  • typeMismatch produces an informative message for cases when the value encountered is not of the expected type

An example type and instance:

-- 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 mzero to fail, but typeMismatch
  -- gives a much more informative error message.
  parseJSON invalid    = typeMismatch "Coord" invalid

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 will probably be more efficient than the following two options:
  • 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

If DefaultSignatures doesn't give exactly the results you want, you can customize the generic decoding with only a tiny amount of effort, using genericParseJSON with your preferred Options:

instance FromJSON Coord where
    parseJSON = genericParseJSON defaultOptions


FromJSON Subnet # 
FromJSON Vpc # 


parseJSON :: Value -> Parser Vpc #

FromJSON KeyPair # 
FromJSON PlacementGroup # 
FromJSON SecurityGroup # 
FromJSON AvailabilityZone # 
FromJSON StateReason # 
FromJSON Monitoring # 
FromJSON Placement # 
FromJSON InstanceState # 
FromJSON EbsInstanceBlockDevice # 
FromJSON InstanceBlockDeviceMapping # 
FromJSON Tag # 


parseJSON :: Value -> Parser Tag #

FromJSON InstanceNetworkInterface # 
FromJSON ProductCode # 
FromJSON Instance # 
FromJSON Group # 
FromJSON Reservation # 
FromJSON Region # 
FromJSON InstanceStateChange # 
FromJSON TerminateInstancesResponse # 
FromJSON ConsoleOutput # 
FromJSON TagDescription # 
FromJSON DescribeSecurityGroupsResponse # 
FromJSON DescribeInstancesResponse # 
FromJSON DescribeInstanceStatusResponse # 
FromJSON InstanceStatus # 
FromJSON InstanceStatusSummary # 
FromJSON InstanceStatusDetails # 
FromJSON CreateTagsResponse # 
FromJSON CreateSecurityGroupResponse #