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

Comments

Projects
None yet
3 participants
@qJkee
Copy link

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

This comment has been minimized.

Copy link
Member

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 changed the title encoding/json encoding/json: RawMessage.MarshalJSON should error if it is nil Nov 13, 2017

@qJkee

This comment has been minimized.

Copy link
Author

commented Nov 13, 2017

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

@mvdan

This comment has been minimized.

Copy link
Member

commented Nov 13, 2017

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

@qJkee

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Member

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 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.
You can’t perform that action at this time.