Unmarshal should propagate errors returned by custom UnmarshalJSON funcs
I've tried to use json-iterator/go instead of encoding/json and found the following incompatibility.
When a custom UnmarshalJSON returns an error the error is propagated 1:1 to the caller of encoding/json.Unmarshal
json-iterator/go extracts the error message via .Error() and creates a new error via fmt.Errorf.
Example:
- encoding/json
err := json.Unmarshal([]byte(`{"key": "value"}`), &doc)
// Returns:
// &syntaxError{}
// errors.As can find the syntaxError
se := &syntaxError{}
ok := errors.As(err, &se)
g.Expect(ok).To(BeTrue())
-
json-iterator/go
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(`{"key": "value"}`), &doc)
// Returns:
// errors.errorString{
// s: unmarshalerDecoder: syntax error: syntax error, error found in #10 byte of ...|: "value"}|..., bigger context ...|{"key": "value"}|...
// }
(full code to reproduce in: https://github.com/sbueringer/json-iterator-tests/blob/master/conformance_err_propagation_test.go#L13)
I think json-iterator doesn't necessarily have to return the exact error returned by the custom UnmarshalJSON function, but it would be good if it would wrap the error. Thus it would be possible to check for the error type via errors.As.
Happy to contribute, if this change sounds okay.