这个问题可能是因为您在导入JSON模块时没有使用严格标志。严格模式将强制要求所有字段都必须正确匹配类型和值,并且不允许额外的字段存在。您可以通过以下方式来解决该问题:
{-# LANGUAGE Strict #-}
{-# LANGUAGE OverloadedStrings #-} import Data.Aeson import Data.Aeson.Types (modifyFailure) import Control.Monad (foldM)
strictDecode :: FromJSON a => Value -> Parser a strictDecode = withObject "strictObject" $ \ obj -> do let objKeys = ["valid_key1", "valid_key2"]
-- Check if any extra keys are present
let extraKeys = filter (\ k -> not $ elem k objKeys) $ Object.keys obj
when (not $ null extraKeys) $ modifyFailure $ \ _ -> "Unexpected keys: " ++ show extraKeys
-- Decode known keys
validFields <- foldM (\ acc k -> do
v <- obj .: k
return $ acc <> [(k, v)])
[] objKeys
-- You might want to remove the following assertion in production
assert (length validFields == length objKeys) $ return ()
-- Return the result as an Aeson object
return $ object validFields
decodeStrict :: FromJSON a => ByteString -> Maybe a decodeStrict bytes = either (const Nothing) Just $ eitherResult $ strictDecode <$> eitherDecode' bytes
现在,您可以尝试重新编译您的代码并应该不再受到此问题的困扰。