diff --git a/src/Data/Aeson/Extended.hs b/src/Data/Aeson/Extended.hs new file mode 100644 index 0000000000..54d2a88a13 --- /dev/null +++ b/src/Data/Aeson/Extended.hs @@ -0,0 +1,23 @@ +-- | The purpose of this module is to provide better failure messages +-- When parsing a key of an object, this makes sure the key itself will show up +module Data.Aeson.Extended ( + module Export + , (.:) + , (.:?) + ) where + +import Data.Aeson as Export hiding ((.:), (.:?)) +import qualified Data.Aeson as A + +import Data.Aeson.Types hiding ((.:), (.:?)) + +import Data.Text (unpack, Text) +import Data.Monoid ((<>)) + +(.:) :: FromJSON a => Object -> Text -> Parser a +(.:) o p = modifyFailure (("failed to parse field " <> unpack p <> ": ") <>) (o A..: p) +{-# INLINE (.:) #-} + +(.:?) :: FromJSON a => Object -> Text -> Parser (Maybe a) +(.:?) o p = modifyFailure (("failed to parse field " <> unpack p <> ": ") <>) (o A..:? p) +{-# INLINE (.:?) #-} diff --git a/src/Network/HTTP/Download.hs b/src/Network/HTTP/Download.hs index 1e056955ff..0f2c11c1a7 100644 --- a/src/Network/HTTP/Download.hs +++ b/src/Network/HTTP/Download.hs @@ -26,7 +26,7 @@ import Control.Monad.Catch (MonadThrow, throwM) import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Reader (MonadReader, ReaderT, ask, runReaderT) -import Data.Aeson (FromJSON, parseJSON) +import Data.Aeson.Extended (FromJSON, parseJSON) import Data.Aeson.Parser (json') import Data.Aeson.Types (parseEither) import qualified Data.ByteString as S diff --git a/src/Stack/Build/Types.hs b/src/Stack/Build/Types.hs index b398734093..6f72d4cebf 100644 --- a/src/Stack/Build/Types.hs +++ b/src/Stack/Build/Types.hs @@ -13,7 +13,7 @@ module Stack.Build.Types where import Control.DeepSeq import Control.Exception -import Data.Aeson +import Data.Aeson.Extended import Data.Binary (Binary(..)) import qualified Data.ByteString as S import Data.Char (isSpace) diff --git a/src/Stack/BuildPlan.hs b/src/Stack/BuildPlan.hs index b42320e33d..b1b709bf36 100644 --- a/src/Stack/BuildPlan.hs +++ b/src/Stack/BuildPlan.hs @@ -36,8 +36,7 @@ import Control.Monad.Reader (asks) import Control.Monad.State.Strict (State, execState, get, modify, put) import Control.Monad.Trans.Control (MonadBaseControl) -import Data.Aeson (FromJSON (..)) -import Data.Aeson (withObject, withText, (.:)) +import Data.Aeson.Extended (FromJSON (..), withObject, withText, (.:)) import Data.Binary.VersionTagged (taggedDecodeOrLoad) import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as S8 diff --git a/src/Stack/Config.hs b/src/Stack/Config.hs index fa00c3ed0b..9210b4ee05 100644 --- a/src/Stack/Config.hs +++ b/src/Stack/Config.hs @@ -36,7 +36,7 @@ import Control.Monad.Logger hiding (Loc) import Control.Monad.Reader (MonadReader, ask, runReaderT) import Control.Monad.Trans.Control (MonadBaseControl) import qualified Crypto.Hash.SHA256 as SHA256 -import Data.Aeson +import Data.Aeson.Extended import qualified Data.ByteString.Base16 as B16 import qualified Data.ByteString.Lazy as L import Data.Either (partitionEithers) diff --git a/src/Stack/Docker.hs b/src/Stack/Docker.hs index 8d42d902fe..5bd4ce1fa6 100644 --- a/src/Stack/Docker.hs +++ b/src/Stack/Docker.hs @@ -26,7 +26,7 @@ import Control.Monad.Catch (MonadThrow, throwM) import Control.Monad.IO.Class (MonadIO,liftIO) import Control.Monad.Logger (MonadLogger,logError,logInfo,logWarn) import Control.Monad.Writer (execWriter,runWriter,tell) -import Data.Aeson (FromJSON(..),(.:),(.:?),(.!=),eitherDecode) +import Data.Aeson.Extended (FromJSON(..),(.:),(.:?),(.!=),eitherDecode) import Data.ByteString.Builder (stringUtf8,charUtf8,toLazyByteString) import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Lazy.Char8 as LBS diff --git a/src/Stack/PackageIndex.hs b/src/Stack/PackageIndex.hs index b6d014bc87..1f2628140e 100644 --- a/src/Stack/PackageIndex.hs +++ b/src/Stack/PackageIndex.hs @@ -33,7 +33,7 @@ import Control.Monad.Logger (MonadLogger, logDebug, logInfo, logWarn) import Control.Monad.Reader (asks) -import Data.Aeson +import Data.Aeson.Extended import qualified Data.Binary as Binary import Data.Binary.VersionTagged (taggedDecodeOrLoad) import Data.ByteString (ByteString) diff --git a/src/Stack/Setup.hs b/src/Stack/Setup.hs index b776a95c22..fdf1a66951 100644 --- a/src/Stack/Setup.hs +++ b/src/Stack/Setup.hs @@ -24,7 +24,7 @@ import Control.Monad.Reader (MonadReader, ReaderT (..), asks) import Control.Monad.State (get, put, modify) import Control.Monad.Trans.Control -import Data.Aeson +import Data.Aeson.Extended import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as S8 import Data.Conduit (Conduit, ($$), (=$), await, yield, awaitForever) diff --git a/src/Stack/Types/Config.hs b/src/Stack/Types/Config.hs index a198282fb1..f1557a7c98 100644 --- a/src/Stack/Types/Config.hs +++ b/src/Stack/Types/Config.hs @@ -14,7 +14,7 @@ import Control.Exception import Control.Monad (liftM, mzero) import Control.Monad.Catch (MonadThrow, throwM) import Control.Monad.Reader (MonadReader, ask, asks, MonadIO, liftIO) -import Data.Aeson (ToJSON, toJSON, FromJSON, parseJSON, withText, withObject, object +import Data.Aeson.Extended (ToJSON, toJSON, FromJSON, parseJSON, withText, withObject, object ,(.=), (.:?), (.!=), (.:), Value (String)) import Data.Binary (Binary) import Data.ByteString (ByteString) diff --git a/src/Stack/Types/Docker.hs b/src/Stack/Types/Docker.hs index c7dd381132..99d65f0532 100644 --- a/src/Stack/Types/Docker.hs +++ b/src/Stack/Types/Docker.hs @@ -5,7 +5,7 @@ module Stack.Types.Docker where import Control.Applicative -import Data.Aeson +import Data.Aeson.Extended import Data.Monoid import Data.Text (Text) import Path diff --git a/src/Stack/Types/FlagName.hs b/src/Stack/Types/FlagName.hs index 0850eedab3..fd3036b4e3 100644 --- a/src/Stack/Types/FlagName.hs +++ b/src/Stack/Types/FlagName.hs @@ -23,7 +23,7 @@ module Stack.Types.FlagName import Control.Applicative import Control.Monad.Catch -import Data.Aeson +import Data.Aeson.Extended import Data.Attoparsec.ByteString.Char8 import Data.Attoparsec.Combinators import Data.Binary (Binary) diff --git a/src/Stack/Types/GhcPkgId.hs b/src/Stack/Types/GhcPkgId.hs index 2900d474c4..9003dfe6e2 100644 --- a/src/Stack/Types/GhcPkgId.hs +++ b/src/Stack/Types/GhcPkgId.hs @@ -14,7 +14,7 @@ module Stack.Types.GhcPkgId import Control.Applicative import Control.Monad.Catch -import Data.Aeson +import Data.Aeson.Extended import Data.Attoparsec.ByteString.Char8 import Data.Binary (Binary) import Data.ByteString.Char8 (ByteString) diff --git a/src/Stack/Types/PackageIdentifier.hs b/src/Stack/Types/PackageIdentifier.hs index e5b9893695..9b606eb823 100644 --- a/src/Stack/Types/PackageIdentifier.hs +++ b/src/Stack/Types/PackageIdentifier.hs @@ -23,7 +23,7 @@ import Control.Applicative import Control.DeepSeq import Control.Exception (Exception) import Control.Monad.Catch (MonadThrow, throwM) -import Data.Aeson +import Data.Aeson.Extended import Data.Attoparsec.ByteString.Char8 import Data.Binary (Binary) import Data.ByteString (ByteString) diff --git a/src/Stack/Types/PackageName.hs b/src/Stack/Types/PackageName.hs index a3abbe7352..cf574f33ed 100644 --- a/src/Stack/Types/PackageName.hs +++ b/src/Stack/Types/PackageName.hs @@ -27,7 +27,7 @@ import Control.Applicative import Control.DeepSeq import Control.Monad import Control.Monad.Catch -import Data.Aeson +import Data.Aeson.Extended import Data.Attoparsec.ByteString.Char8 import Data.Attoparsec.Combinators import Data.Binary (Binary) diff --git a/src/Stack/Types/Version.hs b/src/Stack/Types/Version.hs index c86062eadb..bc67700410 100644 --- a/src/Stack/Types/Version.hs +++ b/src/Stack/Types/Version.hs @@ -30,7 +30,7 @@ module Stack.Types.Version import Control.Applicative import Control.DeepSeq import Control.Monad.Catch -import Data.Aeson +import Data.Aeson.Extended import Data.Attoparsec.ByteString.Char8 import Data.Binary (Binary) import Data.ByteString (ByteString) diff --git a/stack.cabal b/stack.cabal index 268d93415c..36552322d2 100644 --- a/stack.cabal +++ b/stack.cabal @@ -65,6 +65,7 @@ library Path.IO System.Process.PagerEditor Paths_stack + Data.Aeson.Extended Data.Attoparsec.Combinators Data.Binary.VersionTagged Data.Set.Monad