Skip to content

encoding/json: using MarshalJSON declared on pointer receiver with non-pointer values #6468

@gopherbot

Description

@gopherbot

by Lytvynov.A.V:

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
http://play.golang.org/p/ZAsj63RNx0

What is the expected output?
{"S":"IkhlbGxvIg=="}
{"S":"IkhlbGxvIg=="}

What do you see instead?
{"S":"Hello"}
{"S":"IkhlbGxvIg=="}

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Debian Wheezy

Which version are you using?  (run 'go version')
go version devel +1607e9e9e6de Tue Sep 24 00:17:08 2013 -0400 linux/amd64
(also works on the playground)

Please provide any additional information below.
Field S in the struct is of type Str and does not have MarshalJSON in it's method set.
This means that S is not json.Marshaler.
But because reflect.Value.CanAddr for this field in the first case returns true,
json.Marshal will use address of that field to try to assert it to json.Marshaler.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions