encoding/json: second decode after error impossible #31701
Comments
SyntaxError gives you an offset, so you could re-start the decoder if you want that behavior. I'm not convinced that all encoding/json users would want the decoder to skip past syntax errors. |
Yes you are right! But it only works on a very inelegant way: |
Agreed that it's inelegant, but is this really a common use case for the json package? I've used the package for many things over the last five years, and never have I needed to skip a syntax error and keep decoding some input. This seems like a feature request or proposal to me, so it should be backed by some evidence to show that it's needed and useful in many cases. If not, having the user write a bit of extra code for the rare cases where one needs this specific behavior seems fine to me. |
Imagine you are reading data from a UDP stream. There it is absolutely possible that a JSON is broken, but I want to read on. The "Decoder" have all information to do that... Yes it's absolutely a feature request. I could not find a better place for it... |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What did you do?
Read a byte stream with multiple JSON's. The first has a syntax error.
What did you expect to see?
I expected "Decode" to save the position and continue working after the error. Without a syntax error, it works as expected...
What did you see instead?
It's impossible to decode a second time after a error, e.g.:
https://play.golang.org/p/0Z-lz_Wj9D7
The text was updated successfully, but these errors were encountered: