Skip to content
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

Unexpected end of input error when parsing input streams #314

Open
julbinb opened this issue Sep 16, 2020 · 9 comments · May be fixed by #321
Open

Unexpected end of input error when parsing input streams #314

julbinb opened this issue Sep 16, 2020 · 9 comments · May be fixed by #321

Comments

@julbinb
Copy link

julbinb commented Sep 16, 2020

When parsing JSON values from an input stream like this

using JSON

while !eof(stdin)
  JSON.parse(stdin)
end

depending on the type of a value in the input, parsing either succeeds or fails.

For example, if the code above is in parse.jl, and file 1.in contains just 5 (no spaces before or after the number), this works:

$ julia parse.jl < 1.in

But if 2.in contains [5] instead (no spaces before or after the list), then there is an error:

$ julia parse.jl < 2.in
ERROR: LoadError: Unexpected end of input
 ...when parsing byte with value '10'
@martinkjlarsson
Copy link

A minimal working example is simply

using JSON
JSON.parse("")
ERROR: Unexpected end of input
Line: 0
Around: ......
           ^

JSON.parse("5"), JSON.parse("[5]"), JSON.parse("[]"), JSON.parse("{}") and similar inputs seem to work as expected. Perhaps an empty string should return nothing rather than throwing an exception as it is not invalid JSON.

@KristofferC
Copy link
Member

Perhaps an empty string should return nothing rather than throwing an exception as it is not invalid JSON.

What makes you say it is valid JSON? Also, returning nothing seems odd to me since that is not a json value.

@martinkjlarsson
Copy link

I was too hasty with commenting. An empty string is not valid JSON and the current behavior seems to be correct. My bad.

@wardlem
Copy link

wardlem commented May 7, 2021

I am having a similar issue.

#  This works fine
JSON.parse("123") 

# This errors with "Unexpected end of input"
JSON.parse(IOBuffer("123"))

@wardlem
Copy link

wardlem commented May 7, 2021

Curiously,

JSON.parse(IOBuffer("123\n"))

works just fine.

@KristofferC
Copy link
Member

Maybe

@inline hasmore(ps::StreamingParserState) = true # no more now ≠ no more ever
needs an eof check.

@wardlem wardlem linked a pull request May 7, 2021 that will close this issue
@wardlem
Copy link

wardlem commented May 7, 2021

Created #321 for this.

@NHDaly
Copy link

NHDaly commented Jan 11, 2022

Related to the above, I just wanted to comment that the current error message for the very simple reproducer is not helpful:

julia> using JSON

julia> JSON.parse("")
ERROR: Unexpected end of input
Line: 0
Around: ......
           ^

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] _error(message::String, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:140
 [3] byteat
   @ ~/.julia/packages/JSON/QXB8U/src/Parser.jl:49 [inlined]
 [4] parse_value(pc::JSON.Parser.ParserContext{Dict{String, Any}, Int64, true, nothing}, ps::JSON.Parser.MemoryParserState)
 [5] parse(str::String; dicttype::Type, inttype::Type{Int64}, allownan::Bool, null::Nothing)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:450
 [6] parse(str::String)
   @ JSON.Parser ~/.julia/packages/JSON/QXB8U/src/Parser.jl:448

We saw this error message in the wild, and it wasn't at all clear to me what it was indicating. My two requests for improvement:

  1. Could the error message indicate that the error happened when attempting to parse json? (ERROR: Failed to parse JSON: Unexpected end of input)
  2. It was not at all clear to me from the message that the problem was that the data was empty. Could we fix whatever is going wrong with the Around: ......, so that it showed the empty string? Something like this instead?:
    Line: 0
    Around: ""
            ^
    

@X3Coyote
Copy link

Should you not export this function to access this?

export

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants