go icon indicating copy to clipboard operation
go copied to clipboard

Nil RawMessage unmarshal error

Open bynil opened this issue 3 years ago • 2 comments

Try unmarshal nil RawMessage to a slice, v1.1.10 is ok, but v1.1.11 will return error.

POC:

package main

import (
	"fmt"

	"github.com/json-iterator/go"
)

type commonResponse struct {
	Code  int                 `json:"code"`
	Error string              `json:"error"`
	Data  jsoniter.RawMessage `json:"data"`
}

type Recommend struct {
	A string
}

func main() {
	str := `{"code":0,"error":"","data":null}`
	var commonResp commonResponse
	err := jsoniter.Unmarshal([]byte(str), &commonResp)
	if err != nil {
		panic(err)
	}

	var recommends []Recommend
	if err = jsoniter.Unmarshal(commonResp.Data, &recommends); err != nil {
		panic(err)
	}
	fmt.Println(recommends)
}

v1.1.10:

[]

v1.1.11 / master@c6661824eb80:

panic: []main.Recommend: decode slice: expect [ or n, but found  , error found in #0 byte of ...||..., bigger context ...||...

goroutine 1 [running]:
main.main()
	/Users/gexiao/repo/playground/main.go:29 +0x1eb

bynil avatar Aug 27 '21 08:08 bynil

@AllenX2018 It seems that this issue is caused by https://github.com/json-iterator/go/commit/5bce16d299eb21bb5a80aa37151d805c37acd79d

bynil avatar Aug 27 '21 08:08 bynil

https://play.golang.org/p/xDrMSSKyisZ

NOMORECOFFEE avatar Sep 21 '21 20:09 NOMORECOFFEE