It's less efficient, but you could unmarshal twice to avoid manually mapping the fields. Once to put all the properly tagged fields into the struct, and then again into a map[string]interface{}
to get everything else. If you don't care about the duplicate fields, you don't even need to filter the second map.
You can even do this in an UnmarshalJSON
method to automatically populate the struct
type S struct {
A string `json:"a"`
B string `json:"b"`
All map[string]interface{}
}
func (s *S) UnmarshalJSON(b []byte) error {
// create a new type to hide the UnmarshalJSON method
// otherwise we'll recurse indefinitely.
type ss S
err := json.Unmarshal(b, (*ss)(s))
if err != nil {
return err
}
// now unmarshal again into the All map
err = json.Unmarshal(b, &s.All)
if err != nil {
return err
}
return nil
}
http://play.golang.org/p/VBVlRjNlHy