Skip to content

Commit

Permalink
Fix .:? behavior for haskell#83
Browse files Browse the repository at this point in the history
It makes (.:?) return Just value if the key is present for values of type Maybe a.
Earlier {"key": null} .:? "key" would return Nothing although the key is there. It was not possible to distinguish the cases when key is absent and when value is null.
Now it treats Maybe same way as other types and returns Just Nothing.
  • Loading branch information
Boris Lykah committed Dec 18, 2014
1 parent 599dc77 commit d0414be
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Data/Aeson/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ import Data.Aeson.Types ( Value(..), Parser
, defaultTaggedObject
)
import Data.Aeson.Types.Internal (Encoding(..))
import Data.Bool ( Bool(False, True), otherwise, (&&) , not)
import Control.Monad ( return, mapM, liftM2, fail, join )
import Data.Bool ( Bool(False, True), otherwise, (&&), not )
import Data.Either ( Either(Left, Right) )
import Data.Eq ( (==) )
import Data.Function ( ($), (.), flip )
Expand Down Expand Up @@ -953,7 +954,7 @@ instance (FromJSON a) => LookupField a where
Just v -> parseJSON v

instance (FromJSON a) => LookupField (Maybe a) where
lookupField _ _ = (.:?)
lookupField _ _ obj key = join <$> obj .:? key

unknownFieldFail :: String -> String -> String -> Parser fail
unknownFieldFail tName rec key =
Expand Down
6 changes: 3 additions & 3 deletions Data/Aeson/Types/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
module Data.Aeson.Types.Generic ( ) where

import Control.Applicative ((<*>), (<$>), (<|>), pure)
import Control.Monad ((<=<))
import Control.Monad ((<=<), join)
import Control.Monad.ST (ST)
import Data.Aeson.Encode.Builder (emptyArray_)
import Data.Aeson.Encode.Functions (builder)
Expand Down Expand Up @@ -656,8 +656,8 @@ instance (Selector s, GFromJSON a) => FromRecord (S1 s a) where
{-# INLINE parseRecord #-}

instance (Selector s, FromJSON a) => FromRecord (S1 s (K1 i (Maybe a))) where
parseRecord _ (Just lab) obj = (M1 . K1) <$> obj .:? lab
parseRecord opts Nothing obj = (M1 . K1) <$> obj .:? pack label
parseRecord _ (Just lab) obj = (M1 . K1) . join <$> obj .:? lab
parseRecord opts Nothing obj = (M1 . K1) . join <$> obj .:? pack label
where
label = fieldLabelModifier opts $
selName (undefined :: t s (K1 i (Maybe a)) p)
Expand Down
2 changes: 1 addition & 1 deletion Data/Aeson/Types/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ obj .: key = case H.lookup key obj of
(.:?) :: (FromJSON a) => Object -> Text -> Parser (Maybe a)
obj .:? key = case H.lookup key obj of
Nothing -> pure Nothing
Just v -> parseJSON v <?> Key key
Just v -> Just <$> parseJSON v <?> Key key
{-# INLINE (.:?) #-}

-- | Helper for use in combination with '.:?' to provide default
Expand Down

0 comments on commit d0414be

Please sign in to comment.