on 0f13c59 help sign in
Data/Aeson.hs+40−2
since splitsnapshots
unifiedsplit
@@ -37,12 +37,16 @@ module Data.Aeson
37 , decode'
37 , decode'
38 , eitherDecode
38 , eitherDecode
39 , eitherDecode'
39 , eitherDecode'
40+ , verboseDecode
41+ , verboseDecode'
40 , encode
42 , encode
41 -- ** Variants for strict bytestrings
43 -- ** Variants for strict bytestrings
42 , decodeStrict
44 , decodeStrict
43 , decodeStrict'
45 , decodeStrict'
44 , eitherDecodeStrict
46 , eitherDecodeStrict
45 , eitherDecodeStrict'
47 , eitherDecodeStrict'
48+ , verboseDecodeStrict
49+ , verboseDecodeStrict'
46 -- * Core JSON types
50 -- * Core JSON types
47 , Value(..)
51 , Value(..)
48 , Encoding
52 , Encoding
@@ -131,9 +135,14 @@ import Prelude.Compat
131
135
132import Data.Aeson.Types.FromJSON (ifromJSON)
136import Data.Aeson.Types.FromJSON (ifromJSON)
133import Data.Aeson.Encoding (encodingToLazyByteString)
137import Data.Aeson.Encoding (encodingToLazyByteString)
138+import Data.Aeson.Parser.Internal
139+ ( decodeWith, decodeStrictWith
134−import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, jsonEOF, json, jsonEOF', json')
140+ , eitherDecodeWith, eitherDecodeStrictWith
141+ , verboseDecodeWith, verboseDecodeStrictWith
142+ , jsonEOF, json, jsonEOF', json')
135import Data.Aeson.Types
143import 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)
137import qualified Data.ByteString as B
146import qualified Data.ByteString as B
138import qualified Data.ByteString.Lazy as L
147import qualified Data.ByteString.Lazy as L
139
148
@@ -221,6 +230,35 @@ eitherDecodeStrict' =
221 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
230 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
222{-# INLINE eitherDecodeStrict' #-}
231{-# INLINE eitherDecodeStrict' #-}
223
232
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+
224-- $use
262-- $use
225--
263--
226-- This section contains basic information on the different ways to
264-- This section contains basic information on the different ways to
Data/Aeson/Internal.hs+1
@@ -18,6 +18,7 @@ module Data.Aeson.Internal
18 , JSONPath
18 , JSONPath
19 , (<?>)
19 , (<?>)
20 , formatError
20 , formatError
21+ , formatErrors
21 , ifromJSON
22 , ifromJSON
22 , iparse
23 , iparse
23 ) where
24 ) where
Data/Aeson/Parser/Internal.hs+26−5
@@ -33,6 +33,8 @@ module Data.Aeson.Parser.Internal
33 , decodeStrictWith
33 , decodeStrictWith
34 , eitherDecodeWith
34 , eitherDecodeWith
35 , eitherDecodeStrictWith
35 , eitherDecodeStrictWith
36+ , verboseDecodeWith
37+ , verboseDecodeStrictWith
36 ) where
38 ) where
37
39
38import Prelude ()
40import Prelude ()
@@ -42,6 +44,7 @@ import Control.Applicative ((<|>))
42import Control.Monad (void, when)
44import Control.Monad (void, when)
43import Data.Aeson.Types.Internal (IResult(..), JSONPath, Result(..), Value(..))
45import Data.Aeson.Types.Internal (IResult(..), JSONPath, Result(..), Value(..))
44import Data.Attoparsec.ByteString.Char8 (Parser, char, decimal, endOfInput, isDigit_w8, signed, string)
46import Data.Attoparsec.ByteString.Char8 (Parser, char, decimal, endOfInput, isDigit_w8, signed, string)
47+import Data.List.NonEmpty (NonEmpty((:|)))
45import Data.Scientific (Scientific)
48import Data.Scientific (Scientific)
46import Data.Text (Text)
49import Data.Text (Text)
47import Data.Vector as Vector (Vector, empty, fromListN, reverse)
50import Data.Vector as Vector (Vector, empty, fromListN, reverse)
@@ -274,19 +277,37 @@ eitherDecodeWith :: Parser Value -> (Value -> IResult a) -> L.ByteString
274eitherDecodeWith p to s =
277eitherDecodeWith p to s =
275 case L.parse p s of
278 case L.parse p s of
276 L.Done _ v -> case to v of
279 L.Done _ v -> case to v of
277− ISuccess a -> Right a
280+ ISuccess a -> Right a
278− IError path msg -> Left (path, msg)
281+ IError (e :| _) -> Left e
279 L.Fail _ _ msg -> Left ([], msg)
282 L.Fail _ _ msg -> Left ([], msg)
280{-# INLINE eitherDecodeWith #-}
283{-# INLINE eitherDecodeWith #-}
281
284
282eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> B.ByteString
285eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> B.ByteString
283 -> Either (JSONPath, String) a
286 -> Either (JSONPath, String) a
284eitherDecodeStrictWith p to s =
287eitherDecodeStrictWith p to s =
285− case either (IError []) to (A.parseOnly p s) of
288+ case either (\e -> IError (([], e) :| [])) to (A.parseOnly p s) of
286− ISuccess a -> Right a
289+ ISuccess a -> Right a
287− IError path msg -> Left (path, msg)
290+ IError (e :| _) -> Left e
288{-# INLINE eitherDecodeStrictWith #-}
291{-# INLINE eitherDecodeStrictWith #-}
289
292
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+
290-- $lazy
311-- $lazy
291--
312--
292-- The 'json' and 'value' parsers decouple identification from
313-- The 'json' and 'value' parsers decouple identification from
Data/Aeson/Types.hs+3
@@ -35,9 +35,12 @@ module Data.Aeson.Types
35 , parseMaybe
35 , parseMaybe
36 , ToJSON(..)
36 , ToJSON(..)
37 , KeyValue(..)
37 , KeyValue(..)
38+ , liftP2
39+ , (<*>+)
38 , modifyFailure
40 , modifyFailure
39 , parserThrowError
41 , parserThrowError
40 , parserCatchError
42 , parserCatchError
43+ , parserCatchErrors
41
44
42 -- ** Keys for maps
45 -- ** Keys for maps
43 , ToJSONKey(..)
46 , ToJSONKey(..)
Data/Aeson/Types/Internal.hs+53−21
@@ -43,10 +43,14 @@ module Data.Aeson.Types.Internal
43 , parse
43 , parse
44 , parseEither
44 , parseEither
45 , parseMaybe
45 , parseMaybe
46+ , liftP2
47+ , (<*>+)
46 , modifyFailure
48 , modifyFailure
47 , parserThrowError
49 , parserThrowError
48 , parserCatchError
50 , parserCatchError
51+ , parserCatchErrors
49 , formatError
52 , formatError
53+ , formatErrors
50 , (<?>)
54 , (<?>)
51 -- * Constructors and accessors
55 -- * Constructors and accessors
52 , object
56 , object
@@ -87,6 +91,7 @@ import Data.Foldable (foldl')
87import Data.HashMap.Strict (HashMap)
91import Data.HashMap.Strict (HashMap)
88import Data.Hashable (Hashable(..))
92import Data.Hashable (Hashable(..))
89import Data.List (intercalate)
93import Data.List (intercalate)
94+import Data.List.NonEmpty (NonEmpty((:|)))
90import Data.Scientific (Scientific)
95import Data.Scientific (Scientific)
91import Data.Semigroup (Semigroup((<>)))
96import Data.Semigroup (Semigroup((<>)))
92import Data.String (IsString(..))
97import Data.String (IsString(..))
@@ -98,6 +103,7 @@ import Data.Vector (Vector)
98import GHC.Generics (Generic)
103import GHC.Generics (Generic)
99import qualified Control.Monad.Fail as Fail
104import qualified Control.Monad.Fail as Fail
100import qualified Data.HashMap.Strict as H
105import qualified Data.HashMap.Strict as H
106+import qualified Data.List.NonEmpty as NonEmpty
101import qualified Data.Scientific as S
107import qualified Data.Scientific as S
102import qualified Data.Vector as V
108import qualified Data.Vector as V
103import qualified Language.Haskell.TH.Syntax as TH
109import qualified Language.Haskell.TH.Syntax as TH
@@ -118,7 +124,7 @@ data JSONPathElement = Key Text
118type JSONPath = [JSONPathElement]
124type JSONPath = [JSONPathElement]
119
125
120-- | The internal result of running a 'Parser'.
126-- | The internal result of running a 'Parser'.
121−data IResult a = IError JSONPath String
127+data IResult a = IError (NonEmpty (JSONPath, String))
122 | ISuccess a
128 | ISuccess a
123 deriving (Eq, Show, Typeable)
129 deriving (Eq, Show, Typeable)
124
130
@@ -133,15 +139,15 @@ instance NFData JSONPathElement where
133
139
134instance (NFData a) => NFData (IResult a) where
140instance (NFData a) => NFData (IResult a) where
135 rnf (ISuccess a) = rnf a
141 rnf (ISuccess a) = rnf a
136− rnf (IError path err) = rnf path `seq` rnf err
142+ rnf (IError err) = rnf err
137
143
138instance (NFData a) => NFData (Result a) where
144instance (NFData a) => NFData (Result a) where
139 rnf (Success a) = rnf a
145 rnf (Success a) = rnf a
140 rnf (Error err) = rnf err
146 rnf (Error err) = rnf err
141
147
142instance Functor IResult where
148instance Functor IResult where
143− fmap f (ISuccess a) = ISuccess (f a)
149+ fmap f (ISuccess a) = ISuccess (f a)
144− fmap _ (IError path err) = IError path err
150+ fmap _ (IError err) = IError err
145 {-# INLINE fmap #-}
151 {-# INLINE fmap #-}
146
152
147instance Functor Result where
153instance Functor Result where
@@ -153,15 +159,15 @@ instance Monad IResult where
153 return = pure
159 return = pure
154 {-# INLINE return #-}
160 {-# INLINE return #-}
155
161
156− ISuccess a >>= k = k a
162+ ISuccess a >>= k = k a
157− IError path err >>= _ = IError path err
163+ IError err >>= _ = IError err
158 {-# INLINE (>>=) #-}
164 {-# INLINE (>>=) #-}
159
165
160 fail = Fail.fail
166 fail = Fail.fail
161 {-# INLINE fail #-}
167 {-# INLINE fail #-}
162
168
163instance Fail.MonadFail IResult where
169instance Fail.MonadFail IResult where
164− fail err = IError [] err
170+ fail err = IError (([], err) :| [])
165 {-# INLINE fail #-}
171 {-# INLINE fail #-}
166
172
167instance Monad Result where
173instance Monad Result where
@@ -238,11 +244,11 @@ instance Monoid (Result a) where
238 {-# INLINE mappend #-}
244 {-# INLINE mappend #-}
239
245
240instance Foldable IResult where
246instance Foldable IResult where
241− foldMap _ (IError _ _) = mempty
247+ foldMap _ (IError _) = mempty
242 foldMap f (ISuccess y) = f y
248 foldMap f (ISuccess y) = f y
243 {-# INLINE foldMap #-}
249 {-# INLINE foldMap #-}
244
250
245− foldr _ z (IError _ _) = z
251+ foldr _ z (IError _) = z
246 foldr f z (ISuccess y) = f y z
252 foldr f z (ISuccess y) = f y z
247 {-# INLINE foldr #-}
253 {-# INLINE foldr #-}
248
254
@@ -256,8 +262,8 @@ instance Foldable Result where
256 {-# INLINE foldr #-}
262 {-# INLINE foldr #-}
257
263
258instance Traversable IResult where
264instance Traversable IResult where
259− traverse _ (IError path err) = pure (IError path err)
265+ traverse _ (IError err) = pure (IError err)
260− traverse f (ISuccess a) = ISuccess <$> f a
266+ traverse f (ISuccess a) = ISuccess <$> f a
261 {-# INLINE traverse #-}
267 {-# INLINE traverse #-}
262
268
263instance Traversable Result where
269instance Traversable Result where
@@ -266,7 +272,7 @@ instance Traversable Result where
266 {-# INLINE traverse #-}
272 {-# INLINE traverse #-}
267
273
268-- | Failure continuation.
274-- | Failure continuation.
269−type Failure f r = JSONPath -> String -> f r
275+type Failure f r = NonEmpty (JSONPath, String) -> f r
270-- | Success continuation.
276-- | Success continuation.
271type Success a f r = a -> f r
277type Success a f r = a -> f r
272
278
@@ -289,7 +295,7 @@ instance Monad Parser where
289 {-# INLINE fail #-}
295 {-# INLINE fail #-}
290
296
291instance Fail.MonadFail Parser where
297instance Fail.MonadFail Parser where
292− fail msg = Parser $ \path kf _ks -> kf (reverse path) msg
298+ fail msg = Parser $ \path kf _ks -> kf ((reverse path, msg) :| [])
293 {-# INLINE fail #-}
299 {-# INLINE fail #-}
294
300
295instance Functor Parser where
301instance Functor Parser where
@@ -309,10 +315,11 @@ instance Alternative Parser where
309 (<|>) = mplus
315 (<|>) = mplus
310 {-# INLINE (<|>) #-}
316 {-# INLINE (<|>) #-}
311
317
318+{- TODO accumulate errors -}
312instance MonadPlus Parser where
319instance MonadPlus Parser where
313 mzero = fail "mzero"
320 mzero = fail "mzero"
314 {-# INLINE mzero #-}
321 {-# INLINE mzero #-}
315− mplus a b = Parser $ \path kf ks -> let kf' _ _ = runParser b path kf ks
322+ mplus a b = Parser $ \path kf ks -> let kf' _ = runParser b path kf ks
316 in runParser a path kf' ks
323 in runParser a path kf' ks
317 {-# INLINE mplus #-}
324 {-# INLINE mplus #-}
318
325
@@ -333,6 +340,22 @@ apP d e = do
333 return (b a)
340 return (b a)
334{-# INLINE apP #-}
341{-# INLINE apP #-}
335
342
343+-- | A variant of 'Control.Applicative.liftA2' that lazily accumulates errors
344+-- from both subparsers.
345+liftP2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
346+liftP2 f pa pb = Parser $ \path kf ks ->
347+ runParser pa path
348+ (\(e :| es) -> kf (e :| es ++ runParser pb path NonEmpty.toList (const [])))
349+ (\a -> runParser pb path kf (\b -> ks (f a b)))
350+{-# INLINE liftP2 #-}
351+
352+infixl 4 <*>+
353+
354+-- | A variant of ('<*>') that lazily accumulates errors from both subparsers.
355+(<*>+) :: Parser (a -> b) -> Parser a -> Parser b
356+(<*>+) = liftP2 id
357+{-# INLINE (<*>+) #-}
358+
336-- | A JSON \"object\" (key\/value map).
359-- | A JSON \"object\" (key\/value map).
337type Object = HashMap Text Value
360type Object = HashMap Text Value
338
361
@@ -423,7 +446,7 @@ emptyObject = Object H.empty
423
446
424-- | Run a 'Parser'.
447-- | Run a 'Parser'.
425parse :: (a -> Parser b) -> a -> Result b
448parse :: (a -> Parser b) -> a -> Result b
426−parse m v = runParser (m v) [] (const Error) Success
449+parse m v = runParser (m v) [] (Error . snd . NonEmpty.head) Success
427{-# INLINE parse #-}
450{-# INLINE parse #-}
428
451
429-- | Run a 'Parser'.
452-- | Run a 'Parser'.
@@ -433,14 +456,14 @@ iparse m v = runParser (m v) [] IError ISuccess
433
456
434-- | Run a 'Parser' with a 'Maybe' result type.
457-- | Run a 'Parser' with a 'Maybe' result type.
435parseMaybe :: (a -> Parser b) -> a -> Maybe b
458parseMaybe :: (a -> Parser b) -> a -> Maybe b
436−parseMaybe m v = runParser (m v) [] (\_ _ -> Nothing) Just
459+parseMaybe m v = runParser (m v) [] (const Nothing) Just
437{-# INLINE parseMaybe #-}
460{-# INLINE parseMaybe #-}
438
461
439-- | Run a 'Parser' with an 'Either' result type. If the parse fails,
462-- | Run a 'Parser' with an 'Either' result type. If the parse fails,
440-- the 'Left' payload will contain an error message.
463-- the 'Left' payload will contain an error message.
441parseEither :: (a -> Parser b) -> a -> Either String b
464parseEither :: (a -> Parser b) -> a -> Either String b
442parseEither m v = runParser (m v) [] onError Right
465parseEither m v = runParser (m v) [] onError Right
443− where onError path msg = Left (formatError path msg)
466+ where onError ((path, err) :| _) = Left (formatError path err)
444{-# INLINE parseEither #-}
467{-# INLINE parseEither #-}
445
468
446-- | Annotate an error message with a
469-- | Annotate an error message with a
@@ -471,6 +494,10 @@ formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg
471 escapeChar '\\' = "\\\\"
494 escapeChar '\\' = "\\\\"
472 escapeChar c = [c]
495 escapeChar c = [c]
473
496
497+-- | Annotate a list of error messages.
498+formatErrors :: Functor f => f (JSONPath, String) -> f String
499+formatErrors = fmap (uncurry formatError)
500+
474-- | A key\/value pair for an 'Object'.
501-- | A key\/value pair for an 'Object'.
475type Pair = (Text, Value)
502type Pair = (Text, Value)
476
503
@@ -510,21 +537,26 @@ p <?> pathElem = Parser $ \path kf ks -> runParser p (pathElem:path) kf ks
510-- Since 0.6.2.0
537-- Since 0.6.2.0
511modifyFailure :: (String -> String) -> Parser a -> Parser a
538modifyFailure :: (String -> String) -> Parser a -> Parser a
512modifyFailure f (Parser p) = Parser $ \path kf ks ->
539modifyFailure f (Parser p) = Parser $ \path kf ks ->
513− p path (\p' m -> kf p' (f m)) ks
540+ p path (\m -> kf ((fmap . fmap) f m)) ks
514
541
515-- | Throw a parser error with an additional path.
542-- | Throw a parser error with an additional path.
516--
543--
517-- @since 1.2.1.0
544-- @since 1.2.1.0
518parserThrowError :: JSONPath -> String -> Parser a
545parserThrowError :: JSONPath -> String -> Parser a
519parserThrowError path' msg = Parser $ \path kf _ks ->
546parserThrowError path' msg = Parser $ \path kf _ks ->
520− kf (reverse path ++ path') msg
547+ kf ((reverse path ++ path', msg) :| [])
521
548
522-- | A handler function to handle previous errors and return to normal execution.
549-- | A handler function to handle previous errors and return to normal execution.
523--
550--
524-- @since 1.2.1.0
551-- @since 1.2.1.0
525parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
552parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
553+parserCatchError p handler = parserCatchErrors p (\((e, msg) :| _) -> handler e msg)
554+
555+-- | A handler function to handle multiple previous errors and return to normal
556+-- execution.
557+parserCatchErrors :: Parser a -> (NonEmpty (JSONPath, String) -> Parser a) -> Parser a
526−parserCatchError (Parser p) handler = Parser $ \path kf ks ->
558+parserCatchErrors (Parser p) handler = Parser $ \path kf ks ->
527− p path (\e msg -> runParser (handler e msg) path kf ks) ks
559+ p path (\es -> runParser (handler es) path kf ks) ks
528
560
529--------------------------------------------------------------------------------
561--------------------------------------------------------------------------------
530-- Generic and TH encoding configuration
562-- Generic and TH encoding configuration
examples/Simplest.hs+7−3
@@ -8,6 +8,7 @@ import Prelude.Compat
8
8
9import Control.Applicative (empty)
9import Control.Applicative (empty)
10import Data.Aeson
10import Data.Aeson
11+import Data.Aeson.Types
11import Data.Monoid
12import Data.Monoid
12import qualified Data.ByteString.Lazy.Char8 as BL
13import qualified Data.ByteString.Lazy.Char8 as BL
13
14
@@ -28,9 +29,9 @@ instance ToJSON Coord where
28-- should match the format used by the ToJSON instance.
29-- should match the format used by the ToJSON instance.
29
30
30instance FromJSON Coord where
31instance FromJSON Coord where
31− parseJSON (Object v) = Coord <$>
32+ parseJSON (Object v) = liftP2 Coord
32− v .: "x" <*>
33+ (v .: "x")
33− v .: "y"
34+ (v .: "y")
34 parseJSON _ = empty
35 parseJSON _ = empty
35
36
36main :: IO ()
37main :: IO ()
@@ -39,3 +40,6 @@ main = do
39 print req
40 print req
40 let reply = Coord 123.4 20
41 let reply = Coord 123.4 20
41 BL.putStrLn (encode reply)
42 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 (..))
21import Data.HashMap.Strict (HashMap)
21import Data.HashMap.Strict (HashMap)
22import Data.Hashable (Hashable)
22import Data.Hashable (Hashable)
23import Data.Int (Int8)
23import Data.Int (Int8)
24−import Data.List.NonEmpty (NonEmpty)
24+import Data.List.NonEmpty (NonEmpty((:|)))
25import Data.Map (Map)
25import Data.Map (Map)
26import Data.Proxy (Proxy)
26import Data.Proxy (Proxy)
27import Data.Ratio (Ratio)
27import Data.Ratio (Ratio)
@@ -59,8 +59,8 @@ toParseJSON :: (Eq a, Show a) =>
59 (Value -> Parser a) -> (a -> Value) -> a -> Property
59 (Value -> Parser a) -> (a -> Value) -> a -> Property
60toParseJSON parsejson tojson x =
60toParseJSON parsejson tojson x =
61 case iparse parsejson . tojson $ x of
61 case iparse parsejson . tojson $ x of
62− IError path msg -> failure "parse" (formatError path msg) x
62+ IError ((path, msg) :| _) -> failure "parse" (formatError path msg) x
63− ISuccess x' -> x === x'
63+ ISuccess x' -> x === x'
64
64
65toParseJSON1
65toParseJSON1
66 :: (Eq (f Int), Show (f Int))
66 :: (Eq (f Int), Show (f Int))
@@ -78,15 +78,15 @@ roundTripEnc :: (FromJSON a, ToJSON a, Show a) =>
78roundTripEnc eq _ i =
78roundTripEnc eq _ i =
79 case fmap ifromJSON . L.parse value . encode $ i of
79 case fmap ifromJSON . L.parse value . encode $ i of
80 L.Done _ (ISuccess v) -> v `eq` i
80 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
82 L.Fail _ _ err -> failure "parse" err i
82 L.Fail _ _ err -> failure "parse" err i
83
83
84roundTripNoEnc :: (FromJSON a, ToJSON a, Show a) =>
84roundTripNoEnc :: (FromJSON a, ToJSON a, Show a) =>
85 (a -> a -> Property) -> a -> a -> Property
85 (a -> a -> Property) -> a -> a -> Property
86roundTripNoEnc eq _ i =
86roundTripNoEnc eq _ i =
87 case ifromJSON . toJSON $ i of
87 case ifromJSON . toJSON $ i of
88− (ISuccess v) -> v `eq` i
88+ ISuccess v -> v `eq` i
89− (IError path err) -> failure "fromJSON" (formatError path err) i
89+ IError ((path, err) :| _) -> failure "fromJSON" (formatError path err) i
90
90
91roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
91roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
92roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
92roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
@@ -104,7 +104,7 @@ x ==~ y =
104
104
105toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
105toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
106toFromJSON x = case ifromJSON (toJSON x) of
106toFromJSON 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
108 ISuccess x' -> x === x'
108 ISuccess x' -> x === x'
109
109
110modifyFailureProp :: String -> String -> Bool
110modifyFailureProp :: String -> String -> Bool