-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Describe the bug
When unmarshaling into a pre-initialized map[string]any, if the input starts with the letter n, jsoniter.Unmarshal unexpectedly sets the map to nil.
This happens even when the input is not valid JSON (nan, none, nil, notjson...).
This behavior causes runtime panics such as:
panic: assignment to entry in nil map
because after unmarshaling, the map is unexpectedly nil.
This does not happen with Go’s standard encoding/json for invalid JSON input (standard json returns an error but does not overwrite the map).
To Reproduce
package main
import (
"fmt"
jsoniter "github.com/json-iterator/go"
)
func main() {
data := map[string]any{} // pre-initialized map
input := []byte("none") // starts with 'n' but not valid JSON
err := jsoniter.Unmarshal(input, &data)
fmt.Println("error:", err)
fmt.Println("is nil:", data == nil) // ❌ becomes nil unexpectedly
data["a"] = 1 // ❌ panic: assignment to entry in nil map
}Expected behavior
If the input is invalid JSON (e.g., "none", "nan", "nil", "notjson"),
Unmarshal should return an error without modifying the existing map.
A pre-initialized map should never be overwritten with nil unless the input is literally JSON null.
This is how encoding/json behaves.
Actual behavior
For any input beginning with n, jsoniter attempts partial parsing and sets the target map to nil.
This breaks existing Go code that assumes a pre-initialized map remains non-nil after a failed unmarshal.