on 584135e help sign in
12 files changed+244−47
since splitsnapshots
unifiedsplit
Data/Aeson.hs+40−2 Data/Aeson/AccParser.hs+49 Data/Aeson/Internal.hs+1 Data/Aeson/Parser/Internal.hs+26−5 Data/Aeson/Types.hs+1 Data/Aeson/Types/FromJSON.hs+8−7 Data/Aeson/Types/Internal.hs+37−22 aeson.cabal+2 examples/Simplest.hs+7−3 tests/Properties.hs+7−7 tests/Tests.hs+8−1 tests/UnitTests/AccErrors.hs+58
Data/Aeson.hs+40−2
@@ -37,12 +37,16 @@ module Data.Aeson
3737 , decode'
3838 , eitherDecode
3939 , eitherDecode'
40+ , verboseDecode
41+ , verboseDecode'
4042 , encode
4143 -- ** Variants for strict bytestrings
4244 , decodeStrict
4345 , decodeStrict'
4446 , eitherDecodeStrict
4547 , eitherDecodeStrict'
48+ , verboseDecodeStrict
49+ , verboseDecodeStrict'
4650 -- * Core JSON types
4751 , Value(..)
4852 , Encoding
@@ -131,9 +135,14 @@ import Prelude.Compat
131135
132136import Data.Aeson.Types.FromJSON (ifromJSON)
133137import Data.Aeson.Encoding (encodingToLazyByteString)
134−import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, jsonEOF, json, jsonEOF', json')
138+import Data.Aeson.Parser.Internal
139+ ( decodeWith, decodeStrictWith
140+ , eitherDecodeWith, eitherDecodeStrictWith
141+ , verboseDecodeWith, verboseDecodeStrictWith
142+ , jsonEOF, json, jsonEOF', json')
135143import Data.Aeson.Types
136−import Data.Aeson.Types.Internal (JSONPath, formatError)
144+import Data.Aeson.Types.Internal (JSONPath, formatError, formatErrors)
145+import Data.List.NonEmpty (NonEmpty)
137146import qualified Data.ByteString as B
138147import qualified Data.ByteString.Lazy as L
139148
@@ -221,6 +230,35 @@ eitherDecodeStrict' =
221230 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
222231{-# INLINE eitherDecodeStrict' #-}
223232
233+eitherFormatErrors
234+ :: Either (NonEmpty (JSONPath, String)) a -> Either (NonEmpty String) a
235+eitherFormatErrors = either (Left . formatErrors) Right
236+{-# INLINE eitherFormatErrors #-}
237+
238+-- | Like 'decode' but returns one or more error messages when decoding fails.
239+verboseDecode :: (FromJSON a) => L.ByteString -> Either (NonEmpty String) a
240+verboseDecode = eitherFormatErrors . verboseDecodeWith jsonEOF ifromJSON
241+{-# INLINE verboseDecode #-}
242+
243+-- | Like 'decodeStrict' but returns one or more error messages when decoding
244+-- fails.
245+verboseDecodeStrict :: (FromJSON a) => B.ByteString -> Either (NonEmpty String) a
246+verboseDecodeStrict =
247+ eitherFormatErrors . verboseDecodeStrictWith jsonEOF ifromJSON
248+{-# INLINE verboseDecodeStrict #-}
249+
250+-- | Like 'decode'' but returns one or more error messages when decoding fails.
251+verboseDecode' :: (FromJSON a) => L.ByteString -> Either (NonEmpty String) a
252+verboseDecode' = eitherFormatErrors . verboseDecodeWith jsonEOF' ifromJSON
253+{-# INLINE verboseDecode' #-}
254+
255+-- | Like 'decodeStrict'' but returns one or more error messages when decoding
256+-- fails.
257+verboseDecodeStrict' :: (FromJSON a) => B.ByteString -> Either (NonEmpty String) a
258+verboseDecodeStrict' =
259+ eitherFormatErrors . verboseDecodeStrictWith jsonEOF' ifromJSON
260+{-# INLINE verboseDecodeStrict' #-}
261+
224262-- $use
225263--
226264-- This section contains basic information on the different ways to
Data/Aeson/AccParser.hsnew file+49
@@ -0,0 +1,49 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3+module Data.Aeson.AccParser
4+ (
5+ AccParser (AccParser, getParser)
6+ , accSequence
7+ , accTraverse
8+ , (<*>+)
9+ ) where
10+
11+import Prelude ()
12+import Prelude.Compat
13+
14+import Data.Aeson.Types.Internal (Parser (..), runParser)
15+import Data.List.NonEmpty (NonEmpty ((:|)))
16+import qualified Data.List.NonEmpty as NonEmpty
17+
18+newtype AccParser a = AccParser { getParser :: Parser a }
19+ deriving Functor
20+
21+instance Applicative AccParser where
22+ pure = AccParser . pure
23+ f <*> a = AccParser (getParser f <*>+ getParser a)
24+
25+-- | A variant of 'Control.Applicative.liftA2' that lazily accumulates errors
26+-- from both subparsers.
27+liftP2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
28+liftP2 f pa pb = Parser $ \path kf ks ->
29+ runParser pa path
30+ (\(e :| es) -> kf (e :| es ++ runParser pb path NonEmpty.toList (const [])))
31+ (\a -> runParser pb path kf (\b -> ks (f a b)))
32+{-# INLINE liftP2 #-}
33+
34+accSequence :: Traversable t => t (Parser a) -> Parser (t a)
35+accSequence = accTraverse id
36+
37+accTraverse :: Traversable t => (a -> Parser b) -> t a -> Parser (t b)
38+accTraverse f s = getParser $ traverse' (AccParser . f) s
39+
40+-- Making sure we are using Applicative AccParser
41+traverse' :: Traversable t => (a -> AccParser b) -> t a -> AccParser (t b)
42+traverse' = traverse
43+
44+infixl 4 <*>+
45+
46+-- | A variant of ('<*>') that lazily accumulates errors from both subparsers.
47+(<*>+) :: Parser (a -> b) -> Parser a -> Parser b
48+(<*>+) = liftP2 id
49+{-# INLINE (<*>+) #-}
Data/Aeson/Internal.hs+1
@@ -18,6 +18,7 @@ module Data.Aeson.Internal
1818 , JSONPath
1919 , (<?>)
2020 , formatError
21+ , formatErrors
2122 , ifromJSON
2223 , iparse
2324 ) where
Data/Aeson/Parser/Internal.hs+26−5
@@ -33,6 +33,8 @@ module Data.Aeson.Parser.Internal
3333 , decodeStrictWith
3434 , eitherDecodeWith
3535 , eitherDecodeStrictWith
36+ , verboseDecodeWith
37+ , verboseDecodeStrictWith
3638 ) where
3739
3840import Prelude ()
@@ -42,6 +44,7 @@ import Control.Applicative ((<|>))
4244import Control.Monad (void, when)
4345import Data.Aeson.Types.Internal (IResult(..), JSONPath, Result(..), Value(..))
4446import Data.Attoparsec.ByteString.Char8 (Parser, char, decimal, endOfInput, isDigit_w8, signed, string)
47+import Data.List.NonEmpty (NonEmpty((:|)))
4548import Data.Scientific (Scientific)
4649import Data.Text (Text)
4750import Data.Vector as Vector (Vector, empty, fromListN, reverse)
@@ -274,19 +277,37 @@ eitherDecodeWith :: Parser Value -> (Value -> IResult a) -> L.ByteString
274277eitherDecodeWith p to s =
275278 case L.parse p s of
276279 L.Done _ v -> case to v of
277− ISuccess a -> Right a
278− IError path msg -> Left (path, msg)
280+ ISuccess a -> Right a
281+ IError (e :| _) -> Left e
279282 L.Fail _ _ msg -> Left ([], msg)
280283{-# INLINE eitherDecodeWith #-}
281284
282285eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> B.ByteString
283286 -> Either (JSONPath, String) a
284287eitherDecodeStrictWith p to s =
285− case either (IError []) to (A.parseOnly p s) of
286− ISuccess a -> Right a
287− IError path msg -> Left (path, msg)
288+ case either (\e -> IError (([], e) :| [])) to (A.parseOnly p s) of
289+ ISuccess a -> Right a
290+ IError (e :| _) -> Left e
288291{-# INLINE eitherDecodeStrictWith #-}
289292
293+verboseDecodeWith :: Parser Value -> (Value -> IResult a) -> L.ByteString
294+ -> Either (NonEmpty (JSONPath, String)) a
295+verboseDecodeWith p to s =
296+ case L.parse p s of
297+ L.Done _ v -> case to v of
298+ ISuccess a -> Right a
299+ IError e -> Left e
300+ L.Fail _ _ msg -> Left (([], msg) :| [])
301+{-# INLINE verboseDecodeWith #-}
302+
303+verboseDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> B.ByteString
304+ -> Either (NonEmpty (JSONPath, String)) a
305+verboseDecodeStrictWith p to s =
306+ case either (\e -> IError (([], e) :| [])) to (A.parseOnly p s) of
307+ ISuccess a -> Right a
308+ IError e -> Left e
309+{-# INLINE verboseDecodeStrictWith #-}
310+
290311-- $lazy
291312--
292313-- The 'json' and 'value' parsers decouple identification from
Data/Aeson/Types.hs+1
@@ -38,6 +38,7 @@ module Data.Aeson.Types
3838 , modifyFailure
3939 , parserThrowError
4040 , parserCatchError
41+ , parserCatchErrors
4142
4243 -- ** Keys for maps
4344 , ToJSONKey(..)
Data/Aeson/Types/FromJSON.hs+8−7
@@ -86,6 +86,7 @@ import Data.Aeson.Internal.Functions (mapKey)
8686import Data.Aeson.Parser.Internal (eitherDecodeWith, jsonEOF)
8787import Data.Aeson.Types.Generic
8888import Data.Aeson.Types.Internal
89+import Data.Aeson.AccParser
8990import Data.Attoparsec.Number (Number(..))
9091import Data.Bits (unsafeShiftR)
9192import Data.Fixed (Fixed, HasResolution)
@@ -164,8 +165,8 @@ parseIndexedJSONPair keyParser valParser idx value = p value <?> Index idx
164165 p = withArray "(k,v)" $ \ab ->
165166 let n = V.length ab
166167 in if n == 2
167− then (,) <$> parseJSONElemAtIndex keyParser 0 ab
168− <*> parseJSONElemAtIndex valParser 1 ab
168+ then (,) <$> parseJSONElemAtIndex keyParser 0 ab
169+ <*>+ parseJSONElemAtIndex valParser 1 ab
169170 else fail $ "cannot unpack array of length " ++
170171 show n ++ " into a pair"
171172{-# INLINE parseIndexedJSONPair #-}
@@ -606,7 +607,7 @@ parseJSON2 = liftParseJSON2 parseJSON parseJSONList parseJSON parseJSONList
606607
607608-- | Helper function to use with 'liftParseJSON'. See 'Data.Aeson.ToJSON.listEncoding'.
608609listParser :: (Value -> Parser a) -> Value -> Parser [a]
609−listParser f (Array xs) = fmap V.toList (V.mapM f xs)
610+listParser f (Array xs) = getParser $ V.toList <$> traverse (AccParser . f) xs
610611listParser _ v = typeMismatch "[a]" v
611612{-# INLINE listParser #-}
612613
@@ -1413,7 +1414,7 @@ parseVersionText = go . readP_to_S parseVersion . unpack
14131414
14141415instance FromJSON1 NonEmpty where
14151416 liftParseJSON p _ = withArray "NonEmpty a" $
1416− (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1417+ (>>= ne) . accSequence . zipWith (parseIndexedJSON p) [0..] . V.toList
14171418 where
14181419 ne [] = fail "Expected a NonEmpty but got an empty list"
14191420 ne (x:xs) = pure (x :| xs)
@@ -1438,7 +1439,7 @@ instance FromJSON Scientific where
14381439instance FromJSON1 DList.DList where
14391440 liftParseJSON p _ = withArray "DList a" $
14401441 fmap DList.fromList .
1441− Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1442+ accSequence . zipWith (parseIndexedJSON p) [0..] . V.toList
14421443 {-# INLINE liftParseJSON #-}
14431444
14441445instance (FromJSON a) => FromJSON (DList.DList a) where
@@ -1529,7 +1530,7 @@ instance (FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Sum f g a) where
15291530instance FromJSON1 Seq.Seq where
15301531 liftParseJSON p _ = withArray "Seq a" $
15311532 fmap Seq.fromList .
1532− Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
1533+ accSequence . zipWith (parseIndexedJSON p) [0..] . V.toList
15331534 {-# INLINE liftParseJSON #-}
15341535
15351536instance (FromJSON a) => FromJSON (Seq.Seq a) where
@@ -1607,7 +1608,7 @@ instance FromJSONKey UUID.UUID where
16071608
16081609instance FromJSON1 Vector where
16091610 liftParseJSON p _ = withArray "Vector a" $
1610− V.mapM (uncurry $ parseIndexedJSON p) . V.indexed
1611+ accTraverse (uncurry $ parseIndexedJSON p) . V.indexed
16111612 {-# INLINE liftParseJSON #-}
16121613
16131614instance (FromJSON a) => FromJSON (Vector a) where
Data/Aeson/Types/Internal.hs+37−22
@@ -34,7 +34,8 @@ module Data.Aeson.Types.Internal
3434 , emptyObject
3535
3636 -- * Type conversion
37− , Parser
37+ , Parser (Parser)
38+ , runParser
3839 , Result(..)
3940 , IResult(..)
4041 , JSONPathElement(..)
@@ -46,7 +47,9 @@ module Data.Aeson.Types.Internal
4647 , modifyFailure
4748 , parserThrowError
4849 , parserCatchError
50+ , parserCatchErrors
4951 , formatError
52+ , formatErrors
5053 , (<?>)
5154 -- * Constructors and accessors
5255 , object
@@ -87,6 +90,7 @@ import Data.Foldable (foldl')
8790import Data.HashMap.Strict (HashMap)
8891import Data.Hashable (Hashable(..))
8992import Data.List (intercalate)
93+import Data.List.NonEmpty (NonEmpty((:|)))
9094import Data.Scientific (Scientific)
9195import Data.Semigroup (Semigroup((<>)))
9296import Data.String (IsString(..))
@@ -98,6 +102,7 @@ import Data.Vector (Vector)
98102import GHC.Generics (Generic)
99103import qualified Control.Monad.Fail as Fail
100104import qualified Data.HashMap.Strict as H
105+import qualified Data.List.NonEmpty as NonEmpty
101106import qualified Data.Scientific as S
102107import qualified Data.Vector as V
103108import qualified Language.Haskell.TH.Syntax as TH
@@ -118,7 +123,7 @@ data JSONPathElement = Key Text
118123type JSONPath = [JSONPathElement]
119124
120125-- | The internal result of running a 'Parser'.
121−data IResult a = IError JSONPath String
126+data IResult a = IError (NonEmpty (JSONPath, String))
122127 | ISuccess a
123128 deriving (Eq, Show, Typeable)
124129
@@ -133,15 +138,15 @@ instance NFData JSONPathElement where
133138
134139instance (NFData a) => NFData (IResult a) where
135140 rnf (ISuccess a) = rnf a
136− rnf (IError path err) = rnf path `seq` rnf err
141+ rnf (IError err) = rnf err
137142
138143instance (NFData a) => NFData (Result a) where
139144 rnf (Success a) = rnf a
140145 rnf (Error err) = rnf err
141146
142147instance Functor IResult where
143− fmap f (ISuccess a) = ISuccess (f a)
144− fmap _ (IError path err) = IError path err
148+ fmap f (ISuccess a) = ISuccess (f a)
149+ fmap _ (IError err) = IError err
145150 {-# INLINE fmap #-}
146151
147152instance Functor Result where
@@ -153,15 +158,15 @@ instance Monad IResult where
153158 return = pure
154159 {-# INLINE return #-}
155160
156− ISuccess a >>= k = k a
157− IError path err >>= _ = IError path err
161+ ISuccess a >>= k = k a
162+ IError err >>= _ = IError err
158163 {-# INLINE (>>=) #-}
159164
160165 fail = Fail.fail
161166 {-# INLINE fail #-}
162167
163168instance Fail.MonadFail IResult where
164− fail err = IError [] err
169+ fail err = IError (([], err) :| [])
165170 {-# INLINE fail #-}
166171
167172instance Monad Result where
@@ -238,11 +243,11 @@ instance Monoid (Result a) where
238243 {-# INLINE mappend #-}
239244
240245instance Foldable IResult where
241− foldMap _ (IError _ _) = mempty
246+ foldMap _ (IError _) = mempty
242247 foldMap f (ISuccess y) = f y
243248 {-# INLINE foldMap #-}
244249
245− foldr _ z (IError _ _) = z
250+ foldr _ z (IError _) = z
246251 foldr f z (ISuccess y) = f y z
247252 {-# INLINE foldr #-}
248253
@@ -256,8 +261,8 @@ instance Foldable Result where
256261 {-# INLINE foldr #-}
257262
258263instance Traversable IResult where
259− traverse _ (IError path err) = pure (IError path err)
260− traverse f (ISuccess a) = ISuccess <$> f a
264+ traverse _ (IError err) = pure (IError err)
265+ traverse f (ISuccess a) = ISuccess <$> f a
261266 {-# INLINE traverse #-}
262267
263268instance Traversable Result where
@@ -266,7 +271,7 @@ instance Traversable Result where
266271 {-# INLINE traverse #-}
267272
268273-- | Failure continuation.
269−type Failure f r = JSONPath -> String -> f r
274+type Failure f r = NonEmpty (JSONPath, String) -> f r
270275-- | Success continuation.
271276type Success a f r = a -> f r
272277
@@ -289,7 +294,7 @@ instance Monad Parser where
289294 {-# INLINE fail #-}
290295
291296instance Fail.MonadFail Parser where
292− fail msg = Parser $ \path kf _ks -> kf (reverse path) msg
297+ fail msg = Parser $ \path kf _ks -> kf ((reverse path, msg) :| [])
293298 {-# INLINE fail #-}
294299
295300instance Functor Parser where
@@ -309,10 +314,11 @@ instance Alternative Parser where
309314 (<|>) = mplus
310315 {-# INLINE (<|>) #-}
311316
317+{- TODO accumulate errors -}
312318instance MonadPlus Parser where
313319 mzero = fail "mzero"
314320 {-# INLINE mzero #-}
315− mplus a b = Parser $ \path kf ks -> let kf' _ _ = runParser b path kf ks
321+ mplus a b = Parser $ \path kf ks -> let kf' _ = runParser b path kf ks
316322 in runParser a path kf' ks
317323 {-# INLINE mplus #-}
318324
@@ -423,7 +429,7 @@ emptyObject = Object H.empty
423429
424430-- | Run a 'Parser'.
425431parse :: (a -> Parser b) -> a -> Result b
426−parse m v = runParser (m v) [] (const Error) Success
432+parse m v = runParser (m v) [] (Error . snd . NonEmpty.head) Success
427433{-# INLINE parse #-}
428434
429435-- | Run a 'Parser'.
@@ -433,14 +439,14 @@ iparse m v = runParser (m v) [] IError ISuccess
433439
434440-- | Run a 'Parser' with a 'Maybe' result type.
435441parseMaybe :: (a -> Parser b) -> a -> Maybe b
436−parseMaybe m v = runParser (m v) [] (\_ _ -> Nothing) Just
442+parseMaybe m v = runParser (m v) [] (const Nothing) Just
437443{-# INLINE parseMaybe #-}
438444
439445-- | Run a 'Parser' with an 'Either' result type. If the parse fails,
440446-- the 'Left' payload will contain an error message.
441447parseEither :: (a -> Parser b) -> a -> Either String b
442448parseEither m v = runParser (m v) [] onError Right
443− where onError path msg = Left (formatError path msg)
449+ where onError ((path, err) :| _) = Left (formatError path err)
444450{-# INLINE parseEither #-}
445451
446452-- | Annotate an error message with a
@@ -471,6 +477,10 @@ formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg
471477 escapeChar '\\' = "\\\\"
472478 escapeChar c = [c]
473479
480+-- | Annotate a list of error messages.
481+formatErrors :: Functor f => f (JSONPath, String) -> f String
482+formatErrors = fmap (uncurry formatError)
483+
474484-- | A key\/value pair for an 'Object'.
475485type Pair = (Text, Value)
476486
@@ -510,21 +520,26 @@ p <?> pathElem = Parser $ \path kf ks -> runParser p (pathElem:path) kf ks
510520-- Since 0.6.2.0
511521modifyFailure :: (String -> String) -> Parser a -> Parser a
512522modifyFailure f (Parser p) = Parser $ \path kf ks ->
513− p path (\p' m -> kf p' (f m)) ks
523+ p path (\m -> kf ((fmap . fmap) f m)) ks
514524
515525-- | Throw a parser error with an additional path.
516526--
517527-- @since 1.2.1.0
518528parserThrowError :: JSONPath -> String -> Parser a
519529parserThrowError path' msg = Parser $ \path kf _ks ->
520− kf (reverse path ++ path') msg
530+ kf ((reverse path ++ path', msg) :| [])
521531
522532-- | A handler function to handle previous errors and return to normal execution.
523533--
524534-- @since 1.2.1.0
525535parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
526−parserCatchError (Parser p) handler = Parser $ \path kf ks ->
527− p path (\e msg -> runParser (handler e msg) path kf ks) ks
536+parserCatchError p handler = parserCatchErrors p (\((e, msg) :| _) -> handler e msg)
537+
538+-- | A handler function to handle multiple previous errors and return to normal
539+-- execution.
540+parserCatchErrors :: Parser a -> (NonEmpty (JSONPath, String) -> Parser a) -> Parser a
541+parserCatchErrors (Parser p) handler = Parser $ \path kf ks ->
542+ p path (\es -> runParser (handler es) path kf ks) ks
528543
529544--------------------------------------------------------------------------------
530545-- Generic and TH encoding configuration
aeson.cabal+2
@@ -84,6 +84,7 @@ library
8484
8585 exposed-modules:
8686 Data.Aeson
87+ Data.Aeson.AccParser
8788 Data.Aeson.Encoding
8889 Data.Aeson.Parser
8990 Data.Aeson.Text
@@ -196,6 +197,7 @@ test-suite tests
196197 SerializationFormatSpec
197198 Types
198199 UnitTests
200+ UnitTests.AccErrors
199201 UnitTests.NullaryConstructors
200202
201203 build-depends:
examples/Simplest.hs+7−3
@@ -8,6 +8,7 @@ import Prelude.Compat
88
99import Control.Applicative (empty)
1010import Data.Aeson
11+import Data.Aeson.Types
1112import Data.Monoid
1213import qualified Data.ByteString.Lazy.Char8 as BL
1314
@@ -28,9 +29,9 @@ instance ToJSON Coord where
2829-- should match the format used by the ToJSON instance.
2930
3031instance FromJSON Coord where
31− parseJSON (Object v) = Coord <$>
32− v .: "x" <*>
33− v .: "y"
32+ parseJSON (Object v) = liftP2 Coord
33+ (v .: "x")
34+ (v .: "y")
3435 parseJSON _ = empty
3536
3637main :: IO ()
@@ -39,3 +40,6 @@ main = do
3940 print req
4041 let reply = Coord 123.4 20
4142 BL.putStrLn (encode reply)
43+ let asCoord :: f Coord -> f Coord
44+ asCoord = id
45+ print (asCoord (verboseDecode "{}"))
tests/Properties.hs+7−7
@@ -21,7 +21,7 @@ import Data.Functor.Compose (Compose (..))
2121import Data.HashMap.Strict (HashMap)
2222import Data.Hashable (Hashable)
2323import Data.Int (Int8)
24−import Data.List.NonEmpty (NonEmpty)
24+import Data.List.NonEmpty (NonEmpty((:|)))
2525import Data.Map (Map)
2626import Data.Proxy (Proxy)
2727import Data.Ratio (Ratio)
@@ -59,8 +59,8 @@ toParseJSON :: (Eq a, Show a) =>
5959 (Value -> Parser a) -> (a -> Value) -> a -> Property
6060toParseJSON parsejson tojson x =
6161 case iparse parsejson . tojson $ x of
62− IError path msg -> failure "parse" (formatError path msg) x
63− ISuccess x' -> x === x'
62+ IError ((path, msg) :| _) -> failure "parse" (formatError path msg) x
63+ ISuccess x' -> x === x'
6464
6565toParseJSON1
6666 :: (Eq (f Int), Show (f Int))
@@ -78,15 +78,15 @@ roundTripEnc :: (FromJSON a, ToJSON a, Show a) =>
7878roundTripEnc eq _ i =
7979 case fmap ifromJSON . L.parse value . encode $ i of
8080 L.Done _ (ISuccess v) -> v `eq` i
81− L.Done _ (IError path err) -> failure "fromJSON" (formatError path err) i
81+ L.Done _ (IError ((path, err) :| _)) -> failure "fromJSON" (formatError path err) i
8282 L.Fail _ _ err -> failure "parse" err i
8383
8484roundTripNoEnc :: (FromJSON a, ToJSON a, Show a) =>
8585 (a -> a -> Property) -> a -> a -> Property
8686roundTripNoEnc eq _ i =
8787 case ifromJSON . toJSON $ i of
88− (ISuccess v) -> v `eq` i
89− (IError path err) -> failure "fromJSON" (formatError path err) i
88+ ISuccess v -> v `eq` i
89+ IError ((path, err) :| _) -> failure "fromJSON" (formatError path err) i
9090
9191roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
9292roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
@@ -104,7 +104,7 @@ x ==~ y =
104104
105105toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
106106toFromJSON x = case ifromJSON (toJSON x) of
107− IError path err -> failure "fromJSON" (formatError path err) x
107+ IError ((path, err) :| _) -> failure "fromJSON" (formatError path err) x
108108 ISuccess x' -> x === x'
109109
110110modifyFailureProp :: String -> String -> Bool
tests/Tests.hs+8−1
@@ -7,8 +7,15 @@ import Test.Framework (defaultMain)
77import qualified DataFamilies.Properties as DF
88import qualified Properties
99import qualified UnitTests
10+import qualified UnitTests.AccErrors as AccErrors
1011
1112main :: IO ()
1213main = do
1314 ioTests <- UnitTests.ioTests
14− defaultMain (DF.tests : Properties.tests : UnitTests.tests : ioTests)
15+ defaultMain
16+ ( AccErrors.tests
17+ : DF.tests
18+ : Properties.tests
19+ : UnitTests.tests
20+ : ioTests
21+ )
tests/UnitTests/AccErrors.hsnew file+58
@@ -0,0 +1,58 @@
1+{-# LANGUAGE NoMonomorphismRestriction #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module UnitTests.AccErrors (tests) where
4+
5+import Prelude ()
6+import Prelude.Compat hiding (seq)
7+
8+import Data.Aeson
9+import Data.DList (DList)
10+import Data.Aeson.Parser.Internal
11+import Data.Aeson.Types ()
12+import Data.Aeson.Internal
13+import Data.List.NonEmpty (NonEmpty)
14+import Data.Semigroup
15+import Data.Vector (Vector)
16+import Test.Framework
17+import Test.Framework.Providers.HUnit
18+import Test.HUnit hiding (Test)
19+import qualified Data.ByteString.Lazy as L
20+import qualified Data.List.NonEmpty as NL
21+import qualified Data.Sequence as Seq
22+
23+tests :: Test
24+tests = testGroup "Error accumulation" [
25+ testCase "Seq" seq
26+ , testCase "Vector" vector
27+ , testCase "NonEmpty" nonEmpty
28+ , testCase "DList" dlist
29+ ]
30+
31+decoder :: FromJSON a
32+ => L.ByteString
33+ -> Either (NonEmpty (JSONPath, String)) a
34+decoder = verboseDecodeWith jsonEOF ifromJSON
35+
36+seq :: Assertion
37+seq = do
38+ let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (Seq.Seq Int)
39+ let message i s = ([Index i], "expected Int, encountered " <> s)
40+ res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])
41+
42+vector :: Assertion
43+vector = do
44+ let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (Vector Int)
45+ let message i s = ([Index i], "expected Int, encountered " <> s)
46+ res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])
47+
48+nonEmpty :: Assertion
49+nonEmpty = do
50+ let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (NL.NonEmpty Int)
51+ let message i s = ([Index i], "expected Int, encountered " <> s)
52+ res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])
53+
54+dlist :: Assertion
55+dlist = do
56+ let res = decoder "[true, null]" :: Either (NonEmpty (JSONPath, String)) (DList Int)
57+ let message i s = ([Index i], "expected Int, encountered " <> s)
58+ res @=? Left (NL.fromList [message 0 "Boolean", message 1 "Null"])