JuliaIO / JSON.jl

JSON parsing and printing
Other
311 stars 100 forks source link

Misaligned character/byte when throwing an error #351

Open Timeroot opened 1 year ago

Timeroot commented 1 year ago

The following misformed JSON (missing a comma between fields) is supposed to give an error like this:

>> JSON.parse("{\"a\":\"foob\"      \"b\":5}")
Expected ',' here
Line: 0
Around: ...{"a":"foob"      "b":5}...
                            ^
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] _error(message::String, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser C:\Users\AlexanderMeiburg\.julia\packages\JSON\NeJ9k\src\Parser.jl:140
 [3] _error_expected_char(c::UInt8, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser C:\Users\AlexanderMeiburg\.julia\packages\JSON\NeJ9k\src\Parser.jl:83

But with the right multibyte UTF-8 characters in the right places, it throws an error while trying to throw the error:

>> JSON.parse("{\"a\":\"0⊆⊆0\"      \"b\":5}")
StringIndexError: invalid index [13], valid nearby indices [11]=>'⊆', [14]=>'0'

Stacktrace:
 [1] string_index_err(s::String, i::Int64)
   @ Base .\strings\string.jl:12
 [2] getindex
   @ .\strings\string.jl:262 [inlined]
 [3] _error(message::String, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser C:\Users\AlexanderMeiburg\.julia\packages\JSON\NeJ9k\src\Parser.jl:140
 [4] _error_expected_char(c::UInt8, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser C:\Users\AlexanderMeiburg\.julia\packages\JSON\NeJ9k\src\Parser.jl:83

It's not very critical because it's just JSON.jl failing to handle bad JSON gracefully. But, when parsing very large JSON and trying to see what the error is, this becomes pretty frustrating because it just gives a byte-offset into the file that I need to scan to.