以下是代表用户的JSON对象的一部分:
{ "image": { "url": "http://example.com" } }
我需要将其解析为User
类型:
data User = User { imgUrl :: Maybe Text }
天真的解决方案:
parseJSON (Object o) = User <$> getImgUrl o
where getImgUrl o = (o .:? "image") >>= maybe (return Nothing) (.:? "url")
但这并不比这些连锁店好多了:
case f m1 of
Nothing -> Nothing
Just m2 -> case f2 m2 of
Nothing -> Nothing
Just m3 -> case f3 m3 ....
通常在“为什么需要Monad”解释中进行演示
因此,我需要编写类似(.:? "url") :: Parser (Maybe a)
我试图用comp
函数描述该组合:
getImgUrl :: Object -> Parser (Maybe Text)
getImgUrl o = o .:? "image" >>= comp (o .:? "url")
comp :: (Monad m) => (a -> m (Maybe b)) -> Maybe a -> m (Maybe b)
comp p Nothing = return Nothing
comp p (Just o) = p o
闻起来像一个Functor,但fmap
没有帮助我。
然后我决定,组成必须继续:
getImgUrl :: Object -> Parser (Maybe Text)
getImgUrl = comp2 (.:? "image") (.:? "url") o
-- Maybe should be changed to a matching typeclass
comp2 :: (Monad m) => (a -> m (Maybe b)) -> (b -> m (Maybe c)) -> a -> m (Maybe c)
comp2 = undefined
Hoogle搜索没有帮助我,但浏览Control.Monad
文档给了我Kliesli的构图,我没有经验。我看到一些相似之处:
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
comp2 :: Monad m => (a -> m (f b)) -> (b -> m (f c)) -> a -> m (f c)
不同之处在于,合成期间Maybe
应该“展开”。
似乎我接近解决方案,但仍然无法找到它。请给我一些见解。
[更新]: 我已经确定实际问题的最佳解决方案是保留原始JSON结构并拥有嵌套的用户类型:
data User = User { image :: Maybe Image }
data Image = Image { url :: Text }
这完全消除了我的问题,并使API与原始资源更兼容。
然而,仅仅出于理论目的,看看如何解决原始问题会很棒。
答案 0 :(得分:3)
我被指出了一个很好的解决方案
首先,我们就是这样做的。
parseJSON (Object o) = User . join <$> (traverse (.:? "url") =<< (o .:? "image"))
在这里,我们得到Parser (Maybe Object)
并将其传递给下一个monadic动作,该动作与Maybe Object
一起使用。在traverse
的帮助下,如果是Just,我们会执行操作。在结果中,我们得到Parser (Maybe (Maybe Object)). What's left is to
加入that result and get
Parser(Maybe Object)`。
然而,使它更简单易用会更好。我会从@ bheklilr的答案中获取此运算符,并将其用于此解决方案。
-- The type can be much more generic, but for simplicity I would keep it in domain of the problem
(.:?>) :: FromJSON a => Parser (Maybe Object) -> Text -> Parser (Maybe a)
maybeParser .:?> key = fmap join . traverse (.:? key) =<< maybeParser
然后我们可以使用该运算符来解析可选字段的长链。
getImgUrl :: A.Object -> Parser (Maybe Text)
getImgUrl o = o .:? "image" .:?> "url" .:?> "foo" .:?> "bar"
从实际的角度来看,这个解决方案并没有比@ bheklilr的解决方案和我最初的'天真'代码示例更有用。但是,我更喜欢它,因为它不是在Just/Nothing
上匹配,而是可以转换许多其他类型(例如Either
)
答案 1 :(得分:2)
我能够基于你的>>= maybe (return Nothing) (.:? key)
模式制作一个相对简单的组合器,它可以大大简化你想做的事情:
(/?) :: FromJSON a => Parser (Maybe Object) -> Text -> Parser (Maybe a)
maybeParser /? key = maybeParser >>= maybe (return Nothing) (.:? key)
这可用于通过JSON文档将任意数量的级别链接在一起:
instance FromJSON User where
parseJSON (Object o) = User <$> o .:? "image" /? "url"
parseJSON _ = mzero
> decode "{\"image\": {\"url\": \"foobarbaz\"}}" :: Maybe User
Just (User {imgUrl = Just "foobarbaz"})
另一个例子:
data Test = Test (Maybe Int) deriving (Eq, Show)
instance FromJSON Test where
parseJSON (Object o) = Test <$> o .:? "foo" /? "bar" /? "baz" /? "qux"
parseJSON _ = mzero
> decode "{\"foo\": {\"bar\": {\"baz\": {\"qux\": 123}}}}" :: Maybe Test
Just (Test (Just 123))
这可能不是您正在寻找的,但我认为它可以解决您的直接问题。考虑到它具有非常低复杂度的1行功能,对我来说似乎很不恰当。在这种情况下,我并不认为真正需要更高级别的抽象。