Created
July 3, 2013 17:10
-
-
Save smagch/5920597 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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