Skip to content

Add labels to json object and list parsing #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Data/Aeson/Parser/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ objectValues str val = do
-- Why use acc pattern here, you may ask? because 'H.fromList' use 'unsafeInsert'
-- and it's much faster because it's doing in place update to the 'HashMap'!
loop acc = do
k <- str <* skipSpace <* char ':'
v <- val <* skipSpace
ch <- A.satisfy $ \w -> w == COMMA || w == CLOSE_CURLY
k <- (str A.<?> "object key") <* skipSpace <* (char ':' A.<?> "':'")
v <- (val A.<?> "object value") <* skipSpace
ch <- A.satisfy (\w -> w == COMMA || w == CLOSE_CURLY) A.<?> "',' or '}'"
let acc' = (k, v) : acc
if ch == COMMA
then skipSpace >> loop acc'
Expand All @@ -149,8 +149,8 @@ arrayValues val = do
else loop [] 1
where
loop acc !len = do
v <- val <* skipSpace
ch <- A.satisfy $ \w -> w == COMMA || w == CLOSE_SQUARE
v <- (val A.<?> "json list value") <* skipSpace
ch <- A.satisfy (\w -> w == COMMA || w == CLOSE_SQUARE) A.<?> "',' or ']'"
if ch == COMMA
then skipSpace >> loop (v:acc) (len+1)
else return (Vector.reverse (Vector.fromListN len (v:acc)))
Expand Down Expand Up @@ -275,7 +275,12 @@ eitherDecodeWith p to s =
L.Done _ v -> case to v of
ISuccess a -> Right a
IError path msg -> Left (path, msg)
L.Fail _ _ msg -> Left ([], msg)
L.Fail _ ctx msg -> Left ([], buildMsg ctx msg)
where
buildMsg :: [String] -> String -> String
buildMsg [] msg = msg
buildMsg (expectation:_) msg =
msg ++ ". Expecting " ++ expectation
{-# INLINE eitherDecodeWith #-}

eitherDecodeStrictWith :: Parser Value -> (Value -> IResult a) -> B.ByteString
Expand Down
5 changes: 5 additions & 0 deletions tests/ErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ outputGeneric choice = concat
, "{\"record\": {}, \"W\":{}}"
, "{}"
, "[]"
, "{\"unary\""
, "{\"unary\":"
, "{\"unary\":1"
]

, testWithSomeType "SomeType (two-element array)"
Expand All @@ -129,6 +132,8 @@ outputGeneric choice = concat
, "[null, 0]"
, "[]"
, "{}"
, "[1"
, "[1,"
]

, testWith "EitherTextInt"
Expand Down
5 changes: 5 additions & 0 deletions tests/golden/generic.expected
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ Error in $: parsing Types.SomeType failed, expected an Object with a single pair
Error in $: parsing Types.SomeType failed, expected an Object with a single pair, but found 2 pairs
Error in $: parsing Types.SomeType failed, expected an Object with a single pair, but found 0 pairs
Error in $: parsing Types.SomeType failed, expected Object, but encountered Array
Error in $: not enough input. Expecting ':'
Error in $: not enough input. Expecting object value
Error in $: not enough input. Expecting ',' or '}'
SomeType (two-element array)
Error in $[1]: parsing Int failed, expected Number, but encountered Boolean
Error in $[1]: parsing Types.SomeType(Record) failed, expected Object, but encountered Null
Error in $[0]: parsing Types.SomeType failed, expected tag of the 2-element Array to be one of ["nullary","unary","product","record","list"], but found tag "X"
Error in $[0]: parsing Types.SomeType failed, tag element is not a String
Error in $: parsing Types.SomeType failed, expected a 2-element Array, but encountered an Array of length 0
Error in $: parsing Types.SomeType failed, expected Array, but encountered Object
Error in $: not enough input. Expecting ',' or ']'
Error in $: not enough input. Expecting json list value
EitherTextInt
Error in $: parsing Types.EitherTextInt(NoneNullary) failed, expected tag "nonenullary", but found tag "X"
Error in $: parsing Types.EitherTextInt(NoneNullary) failed, expected String, but encountered Array
Expand Down