Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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