go icon indicating copy to clipboard operation
go copied to clipboard

error in `objectLazyAny.ToVal` is not reported

Open JasonMing opened this issue 9 months ago • 0 comments

See the code in any_object.go

func (any *objectLazyAny) ToVal(obj interface{}) {
	iter := any.cfg.BorrowIterator(any.buf)
	defer any.cfg.ReturnIterator(iter)
	iter.ReadVal(obj)
}

When iter.ReadVal(obj) failed, the error did not propagate to the objectLazyAny.err.

Reproducing:

func TestJsoniter(t *testing.T) {
	api := jsoniter.Config{
		DisallowUnknownFields: true,
	}.Froze()

	var v struct{ Foo string }

	// using Any.ToVal cannot see any errors
	obj := api.BorrowIterator([]byte(`{"bar": true}`)).ReadAny()
	obj.ToVal(&v)
	t.Log(obj.LastError()) // this is nil, expecting "found unknown field: bar"

	// using ReadVal can see the expected errors
	iter := api.BorrowIterator([]byte(`{"bar": true}`))
	iter.ReadVal(&v)
	t.Log(iter.Error) // this is "found unknown field: bar"
}

JasonMing avatar Mar 28 '25 07:03 JasonMing