This seems very over-complicated, and there's a couple confusing parts here.
- Do you really need the intermediary fooList during UnmarshalJSON? This makes the json representation look different than the types you define, which definitely confused me at first.
- At least here, there's no reason to be defining your own types for everything, and it's making the code very hard to read, as it hides the structure behind another layer of indirection.
So the crux of your problem is that the fooMap method does not have a pointer receiver. This makes it impossible to assign a non-nil map to the receiver from within your method. You do need a pointer receiver, which you need to dereference on each index operation. In your code you're simply assigning the map data structure to a temporary copy of the receiver (in a non-valid way I might add).
func (f *fooMap) UnmarshalJSON(data []byte) error {
*f = make(map[string]*foo) // required since map is not initialized
var results fooList
if err := json.Unmarshal(data, &results); err != nil {
return err
}
for i := 0; i < len(results); i++ {
result := results[i]
(*f)[result.Id] = result
}
return nil
}
And with my best guess of your json: http://play.golang.org/p/WbwZJxphTj