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: Change of behaviour when decoding json null values into RawMessage #11937

Closed
gsalgado opened this issue Jul 30, 2015 · 5 comments

Comments

Projects
None yet
5 participants
@gsalgado
Copy link

commented Jul 30, 2015

1a99ba5 seems to have changed how json null values are decoded into json.RawMessages. After that commit a null value is decoded into an empty byte array whereas before they were decoded into []byte("null"), as can be seen here:

https://play.golang.org/p/m4ubMyAcqL

@mikioh mikioh changed the title Change of behaviour when decoding json null values into RawMessage encoding/json: Change of behaviour when decoding json null values into RawMessage Jul 30, 2015

@mikioh mikioh added this to the Go1.5 milestone Jul 30, 2015

@dspezia

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2015

Confirmed. This CL has changed the behavior with json.RawMessages.

@dspezia

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2015

The behavior change with json.RawMessage was not intended.

But the documentation says: "The JSON null value unmarshals into an interface, map, pointer, or slice by setting that Go value to nil." A json.RawMessage is a []byte, so setting it to nil (instead of [110 117 108 108]) seems to be in line with the documentation.

I'm not sure the original authors of this package consider a json.RawMessage should always have the same behavior than a []byte though.

@dspezia

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2015

The CL has just been reverted anyway.

@gopherbot

This comment has been minimized.

Copy link

commented Jul 30, 2015

CL https://golang.org/cl/12893 mentions this issue.

@rsc rsc closed this in 80e6d63 Jul 30, 2015

@OttoAllmendinger

This comment has been minimized.

Copy link

commented Jul 31, 2015

Some of my code has been hit by this bug.

I'm relying on the fact that json.RawMessage decodes to the raw json literal, as explained in the Godoc

[RawMessage] implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.

If null decodes to []byte(), that is no longer true.

@golang golang locked and limited conversation to collaborators Aug 5, 2016

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.