Open fengxuway opened 3 years ago
nilAny没有实现ToVal方法,直接继承的baseAny的ToVal https://github.com/json-iterator/go/blob/e6b9536d3649bda3e8842bb7e4fab489d79a97ea/any.go#L51-L53 为啥这里要直接panic呢,返回个error不好吗? @taowen
先判断是不是为null:j.ValueType() == jsoniter.NilValue 要使用一个 any 的value,预期就是应该先判断是否是 nil。panic 可以提前暴露错误
I have encountered the same problem. There is my solution:
val := jsoniter.Get(b, "some_field")
err := val.LastError()
if err != nil {
return fmt.Errorf("failed to unmarshal, %s", err)
}
fmt.Println("size", val.Size())
if val.Size() == 0 {
return fmt.Errorf("failed to unmarshal")
}
I found that baseAny
s size is always equal to zero:
func (any *baseAny) Size() int {
return 0
}
If a json string has null element, expect a slice or struct, but json.LastError() have no error and any.ToVal() panic.
How to fix?
Can this function return error?