Skip to content

Instantly share code, notes, and snippets.

@smagch
Created July 3, 2013 17:10
Show Gist options
  • Select an option

  • Save smagch/5920597 to your computer and use it in GitHub Desktop.

Select an option

Save smagch/5920597 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"log"
)
func main() {
// JSON wrapping test
var data struct {
X float64 `json:"x,string"`
Id *json.RawMessage `json:"id"`
Y float32 `json:"y"`
}
const msg = `{"x":"0.1","id":["\u0056",null],"y":0.2}`
err := json.Unmarshal([]byte(msg), &data)
if err != nil {
panic(err.Error())
}
log.Println(data)
replace := []byte(`{"msg":"this is replaced json"}`)
if err = data.Id.UnmarshalJSON(replace); err != nil {
panic(err.Error())
}
data.X = data.X * 2
log.Println(data)
b, err := json.Marshal(&data)
if err != nil {
panic(err.Error())
}
log.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment