Skip to content
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

encoding/json: can add json.NewDecoderWithBytes function #7709

Closed
gopherbot opened this issue Apr 4, 2014 · 3 comments
Closed

encoding/json: can add json.NewDecoderWithBytes function #7709

gopherbot opened this issue Apr 4, 2014 · 3 comments

Comments

@gopherbot
Copy link

by runner.mei:

can add json.NewDecoderWithBytes function, use it like this:

  var buffer bytes.Buffer
  ch := make(chan io.Reader)
  // .....

  for _, reader := range ch {
    buffer.Reset()    // buffer is reused, redue gc
    ioutil.Copy(buffer, reader)
    dec := json.NewDecoderWithBytes(data)
    dec.UseNumber()
    dec.Decode(&m) 
  }
@gopherbot
Copy link
Author

Comment 1 by runner.mei:

Sorry, the above code was wrong.
  var buffer bytes.Buffer
  ch := make(chan io.Reader)
  // .....
  for _, reader := range ch {
    buffer.Reset()    // buffer is reused, redue gc
    ioutil.Copy(buffer, reader)
    dec := json.NewDecoderWithBytes(buffer.Bytes()) // This line of code above was wrong.
    dec.UseNumber()
    dec.Decode(&m) 
  }

@bradfitz
Copy link
Contributor

bradfitz commented Apr 6, 2014

Comment 2:

You can use bytes.NewReader with json.NewDecoder.

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Author

Comment 3 by runner.mei:

use json.NewDecoder with bytes.NewReader, should copy all bytes to Decoder, it is what I
want to avoid。I want to reuse buffer and zero copy, Reducing the number of GC

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants