Last active
August 29, 2015 13:56
-
-
Save cameronjacobson/9015638 to your computer and use it in GitHub Desktop.
simple example of how one could wrap their structs for json en/decoding
This file contains 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" | |
"fmt" | |
) | |
type blah struct { | |
Numbers []int | |
Astring string | |
} | |
func (self *blah) fromJson(data string) { | |
err := json.Unmarshal([]byte(data), self) | |
if err != nil { | |
fmt.Println("error:", err) | |
} | |
} | |
func (self *blah) toJson() string { | |
data, err := json.Marshal(self) | |
if err != nil { | |
fmt.Println("error:", err) | |
} | |
return string(data) | |
} | |
func main() { | |
ab := `{ | |
"Numbers": [1, 2, 3], | |
"Astring": "this is the string" | |
}` | |
var ac blah | |
ac.fromJson(ab) | |
fmt.Println(ac) | |
fmt.Println(ac.toJson()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment