Open gansvv opened 1 year ago
@dsnet @mvdan
I see what you're saying. There's no "true" in the first input, so the error can be a bit confusing.
"invalid literal true, error in character 'e'"
I don't think this error is particularly better, for what it's worth.
All in all, note that the decoder tokenizes one byte at a time. So when it sees a t
, it knows it can only be the beginning of true
; same for f
and false
, or "
and a quoted string. That's why the error messages are the way they are.
I think we could do better in terms of human-friendly messages, but I can't think of an obviously better choice right now.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
Outputhttps://go.dev/play/p/-AeaDZbeRj1
What did you do?
Json.Unmarshal tries to box a string that starts with a boolean character ('t' or 'f') into a different error compared to a string starting with any other character. This causes the json validation error to be confusing. Can the error be fixed to be similar in both cases (first character invalid) or make the error for attempted boolean parsing to be different ("invalid boolean prefix detected", etc.).
Reproduced here: https://go.dev/play/p/-AeaDZbeRj1
Code:
In the above, the error showing "invalid character 'e' in literal true" can be updated to state "invalid literal..." for clarity.
What did you expect to see?
"invalid literal true, error in character 'e'"
What did you see instead?
invalid character 'e' in literal true (expecting 'r')