Skip to content

Unexpected behavior of .:? #83

Closed
Closed

Description

@lykahb

I want to have distinction between absence of field and its null value.

data Blah = Blah (Maybe (Maybe String))
instance FromJSON Blah where
  parseJSON (Object v) = Blah <$> v .:? "blah"
Blah Nothing -- if field not mentioned
Blah (Just Nothing) -- if field has null value

However, the result of parsing is never Blah (Just Nothing) because .:? uses (Maybe a) instance directly.

(.:?) :: (FromJSON a) => Object -> Text -> Parser (Maybe a)
obj .:? key = case H.lookup key obj of
               Nothing -> pure Nothing
               Just v  -> parseJSON v
            -- Just v  -> Just <$> parseJSON v would have expected behavior

Activity

  1. bos commented on Sep 4, 2013

    @bos
    Collaborator

    The obvious fix for this breaks some of the test suite, and I haven't had a chance to diagnose that yet. Looking.

  2. bos commented on Sep 4, 2013

    @bos
    Collaborator
  3. added a commit that references this issue on Dec 18, 2014
  4. added a commit that references this issue on Aug 11, 2015
  5. lykahb commented on Aug 11, 2015

    @lykahb
    Author

    Writing a testcase was harder than I expected since monadic Parser does not play well with properties.

    isExistingKeyJust :: Value -> Bool
    isExistingKeyJust (Object obj) = all (\k -> isJust $ obj .:? k) $ H.keys obj -- does not typecheck
    isExistingKeyJust _ = False

  6. gregwebs commented on Sep 5, 2015

    @gregwebs
    Contributor

    This is committed as d0414be
    However, it is not in the Changelog even though this is a huge breaking change.
    I personally like the change, however, I think it may cause so much breakage that the existing behavior should be maintained and a different operator should be added instead., perhaps .:??

  7. gregwebs commented on Sep 21, 2015

    @gregwebs
    Contributor

    Should this be closed now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions