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: RawMessage.MarshalJSON should error if it is nil #22686

Closed
qJkee opened this issue Nov 13, 2017 · 5 comments
Closed

encoding/json: RawMessage.MarshalJSON should error if it is nil #22686

qJkee opened this issue Nov 13, 2017 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@qJkee
Copy link

qJkee commented Nov 13, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

What did you do?

Just opened source code :D

What did you see instead?

Open encoding/json/stream.go at 250th line.
In function, MarshalJSON

func (m RawMessage) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	return m, nil
}

That means, that error always will be nil. But i think that if m == nil should return error that raw message is empty(or nill).
In that case, we have to ignore returned error(it's always nill), and check returned array for
if arr == []byte("null")

@mvdan
Copy link
Member

mvdan commented Nov 13, 2017

What makes you think that m == nil should error? Note that the definition is type RawMessage []byte, and it's very common for uses of []byte to also accept nil without any errors.

This seems more like a question than a bug report to me, unless there is an actual problem you ran into because of this behavior.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 13, 2017
@mvdan mvdan changed the title encoding/json encoding/json: RawMessage.MarshalJSON should error if it is nil Nov 13, 2017
@qJkee
Copy link
Author

qJkee commented Nov 13, 2017

So, if you always return nill error, maybe make func return only []byte to avoid unnecessary error checking?

@mvdan
Copy link
Member

mvdan commented Nov 13, 2017

Then it wouldn't implement the interface: https://golang.org/pkg/encoding/json/#Marshaler

@qJkee
Copy link
Author

qJkee commented Nov 13, 2017

As a way, we can return m, nill without checks, and don't allocate a new byte array with "null" word inside. It's will be strange, if I will be sure that my array is not empty, but it will contain "null" word instead of my data.

@mvdan
Copy link
Member

mvdan commented Nov 13, 2017

Sorry, I don't understand what you're saying.

I still don't see a bug here, so I'm closing this. The issue tracker is not used for questions and discussion, so I suggest you take this to https://github.com/golang/go/wiki/Questions.

@mvdan mvdan closed this as completed Nov 13, 2017
@golang golang locked and limited conversation to collaborators Nov 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants