Skip to content

Instantly share code, notes, and snippets.

@dougneal
Created April 12, 2016 13:16
Show Gist options
  • Save dougneal/8fdc86516a76b69a51aa33fcadc4fdd1 to your computer and use it in GitHub Desktop.
Save dougneal/8fdc86516a76b69a51aa33fcadc4fdd1 to your computer and use it in GitHub Desktop.
Marshalling binary data to JSON results in corruption
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"io/ioutil"
)
const ec2_userdata string = `#cloud-config
bootcmd:
- echo "hello world"
`
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var buffer bytes.Buffer
gzipWriter := gzip.NewWriter(&buffer)
gzipWriter.Write([]byte(ec2_userdata))
gzipWriter.Close()
mapD := map[string]string{"gzip": buffer.String()}
mapB, _ := json.Marshal(mapD)
var err error
var raw []byte
err = ioutil.WriteFile("output.json", mapB, 0644)
check(err)
raw, err = ioutil.ReadFile("output.json")
check(err)
var unmarshalled map[string]interface{}
err = json.Unmarshal([]byte(raw), &unmarshalled)
check(err)
gzip_in := unmarshalled["gzip"].(string)
err = ioutil.WriteFile("unmarshalled.gz", []byte(gzip_in), 0644)
check(err)
}
@dougneal
Copy link
Author

Try to gunzip the resulting unmarshalled.gz file, it won't work.

$ gunzip unmarshalled.gz
gunzip: unmarshalled.gz: not in gzip format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment