go icon indicating copy to clipboard operation
go copied to clipboard

encoding/json/v2: Decoder.More() == false on syntax error

Open neild opened this issue 1 month ago • 4 comments

dec := json.NewDecoder(strings.NewReader(","))
fmt.Println(dec.More())
fmt.Println(dec.Token())
$ go run .
true
<nil> invalid character ',' looking for beginning of value
$ GOEXPERIMENT=jsonv2 go run .
false
<nil> invalid character ',' looking for beginning of value

The JSONv1 implementation of More reports false if there is an error filling its buffer, but it reports true if the next token is semantically invalid. JSONv2 appears to report false if the next token is invalid.

neild avatar Nov 25 '25 21:11 neild

Interesting. I don't know why it behaves this way, but the observable behavior seems to go against the intent of the v1 code: https://github.com/golang/go/blob/5945fc02fc575da1edc80bcdd0e88e41689552a7/src/encoding/json/stream.go#L485-L488

dsnet avatar Dec 03 '25 20:12 dsnet

jsontext.Decoder.PeekKind returns Kind(0) on both error and EOF. This makes it impossible to distinguish between an error and the natural end of input without reading the next token.

Maybe that's okay, but would it perhaps make sense to have separate Kinds for EOF and error?

neild avatar Dec 08 '25 21:12 neild

Change https://go.dev/cl/728300 mentions this issue: encoding/json: report true from v2 Decoder.More when an error is pending

gopherbot avatar Dec 08 '25 21:12 gopherbot