Skip to content
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

Open
dsnet opened this issue Sep 18, 2018 · 5 comments
Open
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Sep 18, 2018

Noticed this inconsistency when poking around the code:

type CustomUnmarshaler struct{ p *bytes.Buffer }

func (cu CustomUnmarshaler) UnmarshalJSON(b []byte) error {
	cu.p.Write(b)
	return nil
}
func (cu CustomUnmarshaler) String() string {
	return fmt.Sprintf("custom %v", cu.p.String())
}

func main() {
	s := &struct {
		F1 CustomUnmarshaler
		F2 interface{}
		F3 interface{}
	}{
		CustomUnmarshaler{new(bytes.Buffer)},
		CustomUnmarshaler{new(bytes.Buffer)},
		&CustomUnmarshaler{new(bytes.Buffer)},
	}
	json.Unmarshal([]byte(`{"F1": "F1", "F2": "F2", "F3": "F3"}`), s)
	fmt.Println(s.F1)
	fmt.Println(s.F2)
	fmt.Println(s.F3)
}

This current prints:

custom "F1"
F2
custom "F3"

I expect it to print:

custom "F1"
custom "F2"
custom "F3"

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, the json 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.

@dsnet dsnet changed the title encoding/json: cannot call UnmarshalJSON on value receiver encoding/json: cannot call UnmarshalJSON on value receiver through interface Sep 18, 2018
@mvdan
Copy link
Member

mvdan commented Sep 18, 2018

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.

@dsnet
Copy link
Member Author

dsnet commented Sep 18, 2018

In the pointer case, is pre-existing data kept?

Yes. The behavior of json.Unmarshal is to merge into pre-allocated data structures. It is a slight inconsistency that this does not happen here.

@bcmills
Copy link
Contributor

bcmills commented Sep 22, 2018

CC @rsc @bradfitz

@bcmills bcmills added the NeedsFix The path to resolution is known, but the work has not been done. label Sep 22, 2018
@bcmills bcmills added this to the Unplanned milestone Sep 22, 2018
jamdagni86 added a commit to jamdagni86/go that referenced this issue Oct 31, 2018
… 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
jamdagni86 added a commit to jamdagni86/go that referenced this issue Oct 31, 2018
… 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
@gopherbot
Copy link

Change https://golang.org/cl/146318 mentions this issue: encoding/json: json.Unmarshal fails to unmarshal a custom Unmarshaler struct field

@rsnancollas
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants