New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't use json.Unmarshal on self inside an UnmarshalText function #28119
Comments
Your code can't work. You are implementing the unmarshaling of Test by unmarshaling into Test. That will simply lead to an infinite recursion. The package is saving you from that by returning an error. Do something like this instead: https://play.golang.org/p/KvZogax4MRQ |
Can you elaborate why |
It's possible that I've misdescribed what the encoding/json package is doing, but as far as I can see it's still the case that your original program can't work. |
I see no reason why |
OK, maybe I see what you mean. Given a type that implements the |
IMHO, it the latter makes more sense - when unmarshaling a JSON value, I'd assume that the decoder would look at the function that was specifically designed to handle JSON representations ( Suppose I need to provide a type implementing |
An
encoding.TextUnmarshaler
can't use theencoding/json
andencoding/xml
packages to deserialize a json/xml representation of itself. This is probably becauseencoding/json
treats types implementingencoding.TextUnmarshaler
in a special way.This simple project reproduces the issue: https://play.golang.org/p/huW7nqPqNCW
When unmarshaling, the following error is returned:
Unmarshaling the json representation into a
map[string]interface{}
works just fine.The text was updated successfully, but these errors were encountered: