You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
encoding/json: use the Error method to marshal an error value? #5161
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?
Reacted by oneumyvakin, Ian Bishop, Jeffrey Stiles, Andrew, nick anderson, Sudip Bhandari, BMO568, Ainsley Clark, ldmi3i, Liviu Andron and 4 more
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
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.
Hi! I hate to be the one who breaks the almost-a-decade silence, but after several years of using golang I've stumbled uppon a piece of code that uses JSON marshaling of an error interface value. Guess, it is a rare scenario. 😄 Well, result of {"err": {}} wasn't very informative. I realise, that errors interface puprpose is to provide a method to give stringified message of error (with according format for logging even), which doesn't mean it's meant to be marshaled into JSON the same way.
I think adding a configuration option (not default) to json.Encoder to enable using Error() method to encode value if there is no MarshalJSON() method will help everyone to achieve desired behavior without the need of creating a special type for that.
Reacted by Denis Timofeev, Anthony De Meulemeester, Yury Kozlov, Hsing-Yu (David) Chen, Nitjsefni7, Septem, Dario Emerson, David Newhall II, Geoffroy Vincent, BMO568 and 3 more
I don't think we should provide native support for error types since the JSON representation is only unidirectional (i.e., we can marshal, but can't unmarshal). That said, with #5901, you should be able to accomplish to general gist of what's being asked for here. See this example.
Reacted by Jackie Li, Lennart Elsen and Jameel Al-Aziz
Closing as I don't believe this can practically be implemented by default as it's only a unidirectional operation. There is no equivalent unmarshal operation since we do not know the concrete type to use. However, for the marshal case, you can now use MarshalFunc to specify how to marshal error types.
I think main issue is that by default error interface without any additional marshaler interface, gets marshaled to {} when it could be nicely marshaled to {"error": "...error message"}. So if error value does also not implement any of other marshalling interfaces, why not marshal to {"error": "...error message"}?
That doesn't resolve my primary concern about this being a unidirectional operation.
Also, the representation as {"error": "...error message"} is an arbitrary choice of representation. Why not just "... error message"? The former can lead to redundant {"error": ...} nesting if the outer layer already puts the error in a field. Both are valid representations depending on usage. Rather than choosing one, we explicitly avoid it and let the user choose one with the new MarshalFunc or MarshalToFunc feature.
Activity
remyoudompheng commentedon Mar 30, 2013
Comment 1:
gopherbot commentedon Jul 27, 2013
Comment 2 by pongad:
adg commentedon Jul 29, 2013
Comment 3:
Labels changed: added priority-later, removed priority-triage.
Status changed to Thinking.
rsc commentedon Nov 27, 2013
Comment 4:
Labels changed: added go1.3maybe.
rsc commentedon Dec 4, 2013
Comment 5:
Labels changed: added release-none, removed go1.3maybe.
rsc commentedon Dec 4, 2013
Comment 6:
Labels changed: added repo-main.
dimandzhi commentedon Mar 16, 2021
Hi! I hate to be the one who breaks the almost-a-decade silence, but after several years of using golang I've stumbled uppon a piece of code that uses JSON marshaling of an error interface value. Guess, it is a rare scenario. 😄 Well, result of
{"err": {}}wasn't very informative. I realise, that errors interface puprpose is to provide a method to give stringified message of error (with according format for logging even), which doesn't mean it's meant to be marshaled into JSON the same way.I think adding a configuration option (not default) to
json.Encoderto enable using Error() method to encode value if there is no MarshalJSON() method will help everyone to achieve desired behavior without the need of creating a special type for that.1 remaining item
deployed-labeler: serialise errors as strings
deployed-labeler: serialise errors as strings
deployed-labeler: serialise errors as strings
lrweck commentedon Jul 3, 2023
just stumbled on this too. I guess changing it now would be a breaking change
mitar commentedon Aug 22, 2023
The
gitlab.com/tozd/go/errorserror package provides JSON marshal of errors which uses some standard interfaces to extract data from the error.mitar commentedon Aug 22, 2023
But I would suggest that any error marshaling in Go has at least this basic structure:
{"error": "...error message"}.dsnet commentedon Oct 6, 2023
Hi all, we kicked off a discussion for a possible "encoding/json/v2" package that addresses the spirit of this proposal.
I don't think we should provide native support for
errortypes since the JSON representation is only unidirectional (i.e., we can marshal, but can't unmarshal). That said, with #5901, you should be able to accomplish to general gist of what's being asked for here. See this example.change Err field in PeerStatus to string
Handle connection to torrent peer status update messages (#4)
dsnet commentedon Aug 20, 2026
Closing as I don't believe this can practically be implemented by default as it's only a unidirectional operation. There is no equivalent unmarshal operation since we do not know the concrete type to use. However, for the marshal case, you can now use MarshalFunc to specify how to marshal
errortypes.mitar commentedon Aug 21, 2026
I think main issue is that by default
errorinterface without any additional marshaler interface, gets marshaled to{}when it could be nicely marshaled to{"error": "...error message"}. So iferrorvalue does also not implement any of other marshalling interfaces, why not marshal to{"error": "...error message"}?dsnet commentedon Aug 21, 2026
That doesn't resolve my primary concern about this being a unidirectional operation.
Also, the representation as
{"error": "...error message"}is an arbitrary choice of representation. Why not just"... error message"? The former can lead to redundant{"error": ...}nesting if the outer layer already puts the error in a field. Both are valid representations depending on usage. Rather than choosing one, we explicitly avoid it and let the user choose one with the newMarshalFuncorMarshalToFuncfeature.The v2 docs contains an example of how to serialize errors: https://pkg.go.dev/encoding/json/v2#example-WithMarshalers-Errors
mitar commentedon Aug 21, 2026
Thanks! This is great to know!
And thank you for all amazing work you have done on this v2!