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: use the Error method to marshal an error value? #5161

Open
orofarne opened this Issue Mar 30, 2013 · 6 comments

Comments

Projects
None yet
5 participants
@orofarne

orofarne commented Mar 30, 2013

What steps will reproduce the problem?
http://play.golang.org/p/epXpwk0s4s

What is the expected output?
Something like this:
http://play.golang.org/p/Dk0l6fTj01

What do you see instead?
{"bar":{},"foo":{}}

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


Which operating system are you using?
Linux (Ubuntu 12.04, 3.2.0-38-generic)

Which version are you using?  (run 'go version')
go version go1.0.3
go version devel +5260abd6df41 Sat Mar 30 19:05:00 2013 +0800 linux/amd64

Please provide any additional information below.
I think it's not intuitive behaviour. fmt prints errors without problems, why json not?
@remyoudompheng

This comment has been minimized.

Contributor

remyoudompheng commented Mar 30, 2013

Comment 1:

See also issue #3353.
@gopherbot

This comment has been minimized.

gopherbot commented Jul 27, 2013

Comment 2 by pongad:

I believe this is the expected behavior of json.Marshal(interfaceValue). According to
the documentation, if the argument is an interface value, it is encodes the value of
that interface.
The implementation of fmt.Errorf uses errors.errorString type. The type is a struct with
one unexported member 's' which holds the error. Since the member is unexported,
json.Marshal does not care to encode it.
A possible workaround is to create a new error type that both satisfies "error" and
"json.Marshaler" like 
http://play.golang.org/p/NrtoYsGzij
@adg

This comment has been minimized.

Contributor

adg commented Jul 29, 2013

Comment 3:

Not sure what the right move is, here. Or even if we can fix it at all. There may be
code that relies on the JSON marhsaller *not* using the Error method to marshal a value.

Labels changed: added priority-later, removed priority-triage.

Status changed to Thinking.

@rsc

This comment has been minimized.

Contributor

rsc commented Nov 27, 2013

Comment 4:

Labels changed: added go1.3maybe.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 5:

Labels changed: added release-none, removed go1.3maybe.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 6:

Labels changed: added repo-main.

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