Skip to content

error in objectLazyAny.ToVal is not reported #709

Open
@JasonMing

Description

@JasonMing

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"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions