-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: regression in handling of nil RawMessage #17704
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
Comments
In the first version of my fix I had modified the json encoder to special-case RawMessage. @rsc didn't like that: https://go-review.googlesource.com/c/21811/#message-f4ac3e3444c1d4a46192e36a7607c117d0ca0bb4 It might be necessary, though? |
One way to resolve this is the following change: func (m RawMessage) MarshalJSON() ([]byte, error) {
+ if m == nil {
+ return []byte("null"), nil
+ }
return m, nil
} This avoids special-casing logic being added to encode.go P.S. Maybe |
LGTM |
@dsnet, yes thank you. Let's go with just nil = null for now, not non-nil len 0. |
CL https://golang.org/cl/32472 mentions this issue. |
Can you give more details about what struct was that led to your pain? That will help guide the decision better. |
@dsnet It was something like:
There was a bunch of such structures and one of them had Address as
Both |
That would not match |
But we're not trying to do that. RawMessage has a specific semantic meaning and has specific behaviour. The bytes of a RawMessage should represent a valid JSON value to be copied verbatim into the stream. An empty byte slice clearly doesn't represent a valid value and so we either decide that's programmer error, copy zero bytes and end up with an encoding error, or we add some magic and treat empty RawMessage as representing NULL. Personally I'm slightly more in favour of the latter but would not be brave enough to change current behaviour. |
1625da2 (issue: #14493, CL: http://golang.org/cl/21811) caused a regression where the a nil RawMessage no longer gets marshaled as "null".
Consider the following:
On Go 1.7, this prints:
On 1625da2, this prints:
In light of the change to address #14493, I expect this to print:
Does anyone object if I make this change?
\cc @bradfitz @rsc
The text was updated successfully, but these errors were encountered: