To unmarshal a JSON object into a map, Unmarshal first establishes a map to use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal reuses the existing map, keeping existing entries. Unmarshal then stores key-value pairs from the JSON object into the map. The map's key type must either be any string type, an integer, implement json.Unmarshaler, or implement encoding.TextUnmarshaler.
If a JSON value is not appropriate for a given target type, or if a JSON number overflows the target type, Unmarshal skips that field and completes the unmarshaling as best it can. If no more serious errors are encountered, Unmarshal returns an UnmarshalTypeError describing the earliest such error. In any case, it's not guaranteed that all the remaining fields following the problematic one will be unmarshaled into the target object.
It doesn't seem to me that it describes the above behaviour.
The text was updated successfully, but these errors were encountered:
changed the title
encoding/json: Unmarshal() leaves keys mapping to zeros in map[string]int when the value is not an integerNov 11, 2020
I think this is a valid bug report, and the behavior also surprises me a little. I think it might be simply because the "init" operations (creating a zero value, inserting the key to the map) are out of order.