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 · 4 comments

Comments

Projects
None yet
4 participants
@dsnet
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 from encoding/json: cannot call UnmarshalJSON on value receiver to encoding/json: cannot call UnmarshalJSON on value receiver through interface Sep 18, 2018

@mvdan

This comment has been minimized.

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

This comment has been minimized.

Member

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

This comment has been minimized.

Member

bcmills commented Sep 22, 2018

@bcmills bcmills added the NeedsFix 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

encoding/json: json.Unmarshal fails to unmarshal a custom Unmarshaler…
… 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

encoding/json: json.Unmarshal fails to unmarshal a custom Unmarshaler…
… 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

This comment has been minimized.

gopherbot commented Oct 31, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment