Aesonのjsonのparseで「nullが指定されている」と「キーが存在しない」を区別する

Aesonのjsonのparseで「nullが指定されている」と「キーが存在しない」を区別する

.:!を使えば良い

{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

import           Data.Aeson  
import           GHC.Generics

data PatchTest = PatchTest  
  { f1 :: Maybe (Maybe String)
  } deriving (Show, Generic)
instance FromJSON PatchTest where  
    parseJSON (Object v) =  PatchTest <$> v .:! "f1"

f = mapM_ (putStrLn.show) (  
  [ decode "{\"f1\": \"test\"}"
  , decode "{\"f1\": null}"
  , decode "{}"] :: [Maybe PatchTest])
Just (PatchTest {f1 = Just (Just "test")})  
Just (PatchTest {f1 = Just Nothing})  
Just (PatchTest {f1 = Nothing})