Encoding/json: Decoding with two decoders causes error

I have reduced my situation to this play: https://play.golang.org/p/c9J334uPyo - the interesting stuff starts on line 70. Take note of the input JSON. I’m essentially trying to traverse into the JSON structure enough to read those “type” fields so that I can do some custom unmarshaling.

My strategy is to get into that part of the structure, create a new decoder with the remaining bytes, then use it to decode into a generic map[string]interface{} where I can then read the “type” field and use the first decoder to decode into a concrete type.

Calling Decode() on both decoders, in that loop, causes an error:

invalid character ‘,’ looking for beginning of value

So I thought I’d take this example from the Go docs and try to reproduce it even more minimally: https://play.golang.org/p/RmoZ90qa0B (notice line 35) - and it works without errors. (This playground confirms that commas even exist there!)

Anyone know why this is?

(As a last resort, I’ve done my problem a different way where I create two decoders and they decode the entire structure in lock-step so they stay in the same spot as we go: https://play.golang.org/p/_WpUwO-EkP - this works, but I kind of hate it.)

Wild guess: the problem is actually this: dec.Buffered().

That call creates a bytes.Reader using a slice to the decoder’s internal buffer and it does say that that io.Reader is invalidated by calling Decode.

Try copying the data before creating the new decoder?

1 Like

Indeed, copying the data into a new buffer, then giving that buffer to the new decoder works!

So I suppose, under the hood, the decoders are sharing the same buffer, even though they have different views into it…

Update: Kale provided a much cleaner solution. I had considered RawMessage but not combining it with the strategy of temporary, “placeholder” types: https://play.golang.org/p/tMInbyV-hv

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.