Created
May 14, 2015 15:36
-
-
Save cuevasclemente/394ca55e1d605f466644 to your computer and use it in GitHub Desktop.
Custom JSON Unmarshaller
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 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