-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: document and limit buffering #1955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Milestone
Comments
func (*Decoder) Decode Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v. http://golang.org/pkg/json/#Decoder.Decode func (*Buffer) Read Read reads the next len(p) bytes from the buffer or until the buffer is drained. http://golang.org/pkg/bytes/#Buffer.Read When json.Decode reads from bytes.Buffer (buf.Read), the bytes in the buffer are consumed. package main import ( "bytes" "json" "log" ) func main() { buf := bytes.NewBufferString(`{"k":"v"}exdata`) m := map[string]string{} log.Println("buf.Len:", buf.Len()) err := json.NewDecoder(buf).Decode(&m) if err != nil { log.Fatalf("Couldn't even decode...") } log.Println("buf.Len:", buf.Len()) exbuff := make([]byte, 80) n, err := buf.Read(exbuff) if string(exbuff[0:n]) != "exdata" { log.Fatalf("Didn't get proper exdata, got '%s'", exbuff[0:n]) } log.Printf("Everything was A-OK") } Output: 2011/06/13 23:45:02 buf.Len: 15 2011/06/13 23:45:02 buf.Len: 0 2011/06/13 23:45:02 Didn't get proper exdata, got '' |
Comment 2 by jdnurmi@qwe.cc: I totally agree that is what's happening; The bug (as I view it) is that the json element should be decoded only to the end of the object ('}'); At that point, I should be able to resume the read and read 'exdata' (non-JSON data) if so desired. It's not the standard JSON stream -- but it doesn't seem unreasonable to read a JSON element and not 'extra' non-JSON data. Or did I misunderstand your comment? |
http.ReadRequest takes a *bufio.Reader. JSON could also? Then you could Peek at it. Perhaps add json.NewBufferedDecoder() and keep json.NewDecoder() as a wrapper. Or a fun technique I used recently: you can have json.Decoder.Decode return not just an os.Error but instead a (remaining io.Reader, err os.Error) where remaining is: remaining = io.MultiReader(bytes.NewBuffer(slop), originalReader) |
Owner changed to builder@golang.org. |
I submitted a CL to solve this issue. Ready to be reviewed: http://golang.org/cl/5623053/ |
This issue was closed by revision 49110ea. Status changed to Fixed. |
Proposed addition for this: https://golang.org/cl/7181053/ |
This issue was updated by revision 91e99c1. R=golang-dev, adg, rsc CC=golang-dev https://golang.org/cl/7181053 |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by jdnurmi@qwe.cc:
Attachments:
The text was updated successfully, but these errors were encountered: