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: document that when MarshalText is used, a JSON string is created #17743

Closed
cespare opened this issue Nov 2, 2016 · 1 comment

Comments

Projects
None yet
4 participants
@cespare
Copy link
Contributor

commented Nov 2, 2016

The behavior of JSON marshaling when the MarshalText method is used isn't well-specified:

If no MarshalJSON method is present but the value implements encoding.TextMarshaler instead, Marshal calls its MarshalText method. The nil pointer exception is not strictly necessary but mimics a similar, necessary exception in the behavior of UnmarshalJSON.

Note that it says that MarshalJSON is called "to produce JSON", but there's nothing indicating that MarshalText produces a JSON string (i.e., the value is escaped as a string and surrounding quotes are added).

Compare with the docs about UnmarshalText (docs added in 1.8):

Otherwise, if the value implements encoding.TextUnmarshaler and the input is a JSON quoted string, Unmarshal calls that value's UnmarshalText method with the unquoted form of the string.

I can send a CL later.

@quentinmit quentinmit added this to the Go1.8Maybe milestone Nov 4, 2016

@gopherbot

This comment has been minimized.

Copy link

commented Nov 11, 2016

CL https://golang.org/cl/33135 mentions this issue.

@rsc rsc modified the milestones: Go1.8, Go1.8Maybe Nov 11, 2016

@gopherbot gopherbot closed this in 9073af2 Nov 22, 2016

@golang golang locked and limited conversation to collaborators Nov 22, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.