When marshalling something as body for a http.Request we can only use
BuildBytes() which always allocates a buffer for our whole body. This
pull request adds a ReadCloser() method which returns an io.ReadCloser
that can be passed as body into http.NewRequest. This ReadCloser will
read from the existing buffs and putBuf them when they are not needed
anymore.
This option is similar to json.Encoder.SetEscapeHTML. The default
behavior of false is to escape &, <, and > to \u0026, \u003c, and
\u003e. Setting this to true will not escape these.
This adds an optional parameter to BuildBytes() which allows for reusing
of a byte buffer. Because it's an optional parameter it will not break
backwards compatibility.
This allows people to use a sync.Pool for the buffers used by
BuildBytes() to reduce garbage generation.
- nil slices are encoded as JSON nulls
- empty slices are encoded as empty JSON arrays
- byte slices are encoded as base 64 encoded JSON strings
Demo: https://play.golang.org/p/Z_YE5PHS3g