isNothing在解析JSON文件时抛出异常

时间:2015-01-03 00:40:23

标签: json exception haskell aeson

我有以下函数使用Data.Aeson库解码JSON文件:

data SearchResult = SearchResult {
  items :: [Item]
} deriving (Show)

instance FromJSON SearchResult where
  parseJSON :: Value -> Parser SearchResult
  parseJSON (Object v) = SearchResult <$>
    parseJSON (fromJust $ HM.lookup "items" v)
  parseJSON _ = mzero

data Item = Item {
  volumeInfo :: VolumeInfo
} deriving (Show)

instance FromJSON Item where
  parseJSON :: Value -> Parser Item
  parseJSON (Object v) = Item <$>
    parseJSON (fromJust $ HM.lookup "volumeInfo" v)
  parseJSON _ = mzero

data VolumeInfo = VolumeInfo {
  title :: String,
  authors :: [String],
  publisher :: String,
  publishedDate :: String,
  industryIdentifiers :: [IndustryIdentifier],
  pageCount :: Int,
  categories :: [String]
} deriving (Show)

instance FromJSON VolumeInfo where
  parseJSON :: Value -> Parser VolumeInfo
  parseJSON (Object v) = VolumeInfo <$>
    v .: "title" <*>
    v .: "authors" <*>
    v .: "publisher" <*>
    v .: "publishedDate" <*>
    parseJSON (fromJust $ HM.lookup "industryIdentifiers" v) <*>
    v .: "pageCount" <*>
    v .: "categories"
  parseJSON _ = mzero

data IndustryIdentifier = IndustryIdentifier {
  identifierType :: String,
  identifier :: String
} deriving (Show)

instance FromJSON IndustryIdentifier where
  parseJSON :: Value -> Parser IndustryIdentifier
  parseJSON (Object v) = IndustryIdentifier <$>
    v .: "type" <*>
    v .: "identifier"
  parseJSON _ = mzero

这个功能:

getBook content = do
  putStrLn (Data.ByteString.Lazy.Char8.unpack content)
  let searchResult = decode content :: Maybe SearchResult
  print (isNothing searchResult)
  print searchResult

函数getBook适用于许多JSON文件。这是一个例子:

False
Just (SearchResult {items = [Item {volumeInfo = VolumeInfo {title = "A Memoir of Jane Austen", authors = ["James Edward Austen-Leigh","Jane Austen, James Austen-Leigh"], publisher = "Wordsworth Editions", publishedDate = "2007", industryIdentifiers = [IndustryIdentifier {identifierType = "ISBN_10", identifier = "1840225602"},IndustryIdentifier {identifierType = "ISBN_13", identifier = "9781840225600"}], pageCount = 256, categories = ["Novelists, English"]}}]})

JSON内容已成功解码,因此isNothing在第一行返回False,然后是解码内容。如果我使用this JSON file作为content再次运行该函数,我会得到以下输出:

True
Nothing

无法对文件进行解码(因为JSON文件中没有字段categories),因此isNothing会返回True,并且会打印Nothing屏幕。现在的问题是当我使用this JSON file content运行它时。我明白了:

*** Exception: Maybe.fromJust: Nothing

执行print (isNothing searchResult)时抛出异常,我不明白为什么{1}}没有像前面的例子那样被返回(因为在这种情况下没有字段{{1} }, 例如)。我错过了什么或做错了什么?

修改

我发现每次JSON文件不包含字段True时都会出现问题。它失败了:

industryIdentifiers

1 个答案:

答案 0 :(得分:2)

github包定义了一个方便运算符来定义数组字段:

-- | A slightly more generic version of Aeson's @(.:?)@, using `mzero' instead
-- of `Nothing'.
(.:<) :: (FromJSON a) => Object -> T.Text -> Parser [a]
obj .:< key = case Map.lookup key obj of
                   Nothing -> pure mzero
                   Just v  -> parseJSON v

(此处MapData.HashMap.Lazy)的别名

然后,VolumeInfo的FromJSON实例将如下定义:

instance FromJSON VolumeInfo
  v .: "title" <*>
  ...
  v .:< "industryIdentifiers" <*>
  ...