Jenny Blunt Jenny Blunt - 1 year ago 212
JSON Question

Most efficient way to convert io.ReadCloser to byte array

I have a very simple Go webserver. It's job is to receive an inbound json payload. It then publishes the payload to one or more services that expect a byte array. The payload doesn't need to be checked. Just sent over.

In this case, it receives an inbound job and sends it to Google PubSub. It might be another service - it doesn't really matter. I'm trying to find the most efficient way to convert the object to a byte array without first decoding it.

Why? Seems a bit wasteful to decode and convert to JSON on one server, only to unmarshal it later. Plus, I don't want to maintain two identical structs in two packages.

How is it possible to convert the io.ReadCloser to a byte array so I only need to unmarshal once. I tried something like this answer but don't think that's the most efficient way either:

From io.Reader to string in Go

My http server code looks like this:

func Collect(d DbManager) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

code := 422
obj := Report{}
response := Response{}
response.Message = "Invalid request"

decoder := json.NewDecoder(r.Body)

if obj.Device.MachineType != "" {
msg,_ := json.Marshal(obj)
if d.Publish(msg, *Topic) {
code = 200
response.Message = "Ok"

a, _ := json.Marshal(response)

Answer Source

You convert a Reader to bytes, by reading it. There's not really a more efficient way to do it.

body, err := ioutil.ReadAll(r.Body)

If you are unconditionally transferring bytes from an io.Reader to an io.Writer, you can just use io.Copy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download