Skip to content

Instantly share code, notes, and snippets.

@cuevasclemente
Created May 14, 2015 15:36
Show Gist options
  • Save cuevasclemente/394ca55e1d605f466644 to your computer and use it in GitHub Desktop.
Save cuevasclemente/394ca55e1d605f466644 to your computer and use it in GitHub Desktop.
Custom JSON Unmarshaller
package main
import (
"encoding/json"
"fmt"
)
type myStruct struct {
Field1 string
Field2 string
}
func (m myStruct) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Field1 string
Field2 string
Field3const string
}{
Field1: m.Field1,
Field2: m.Field2,
Field3const: "Bob"})
}
func main() {
m := myStruct{Field1: "Hello", Field2: "Hey"}
ba, err := m.MarshalJSON()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(ba))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment