-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
encoding/json: cannot call UnmarshalJSON on value receiver through interface #27722
Comments
In the pointer case, is pre-existing data kept? If I understand addressability correctly, in the non-pointer interface field we cannot modify the existing data in-place. Perhaps we could simply create a new value via reflection, but if the pointer field doesn't get zeroed before decoding, that would be inconsistent. |
Yes. The behavior of |
… struct field When a field in a struct is declared as an interface and an instance of a struct is assigned to it, the json Unmarshaler while unmarshaling, replaces the field with an instance of the type of the actual JSON value instead of calling the UnmarshalJSON method (if the struct implements the json.Unmarshaler interface). The fix checks if the field is an interface and an Unmarshaler and calls the UnmarshalJSON method on the field if it is one. Fixes golang#27722
… struct field When a field in a struct is declared as an interface and an instance of a struct is assigned to it, the json Unmarshaler while unmarshaling, replaces the field with an instance of the type of the actual JSON value instead of calling the UnmarshalJSON method (if the struct implements the json.Unmarshaler interface). The fix checks if the field is an interface and an Unmarshaler and calls the UnmarshalJSON method on the field if it is one. Fixes golang#27722
Change https://golang.org/cl/146318 mentions this issue: |
It looks like a solution was proposed, but never got merged in. I ran into this bug last night and it would be great to fix it. |
Noticed this inconsistency when poking around the code:
This current prints:
I expect it to print:
In this case,
UnmarshalJSON
is a method on the value receiver. This is a rare situation for methods that mutate the receiver, but is valid. I expect the method to still be called. That is, thejson
package should not assume that the receiver must be a pointer.This is a very obscure use-case and I personally don't have a horse in the game to address this.
The text was updated successfully, but these errors were encountered: