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
Here is a pull request for my sandbox repository. As the code and test actions, you would easily find the JSON Marshal / Unmarshal functions for json.RawMessage fields behave differently from ones for []byte. And the weirdest point is parsing a field of null as the json.RawMessage type results in not nil but the byte array equivalent to a string "null". However, in the cases for other (Go-standard) types, it interprets them as the zero value of the specified type (like 0 for numbers, "" for string, and even nil for []byte).
What did you expect to see?
I want the json.RawMessage to behave as possible as similar to []byte because it's the type alias, but if this is hard, it's enough that the null value is parsed into nil of json.RawMessage, as above.
What did you see instead?
I'm looking for the implementation code of UnmarshalJSON and MarshalJSON for []byte type and want to see the difference from the ones for json.RawMessage, but for now, I couldn't find them. And at least, it's definitely clear this issue can be reproduced on Go Playground: https://play.golang.org/p/YTHmX5FOg_x
The text was updated successfully, but these errors were encountered:
json.RawMessage is documented to return the unparsed json and not any other representation of it
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
I'm sorry I overlooked that parsing JSON attributes of null returns the different bytes from parsing ones of "null" (Demo). So there is no inconsistency in the system thanks to referential transparency (even though I feel weird that the Unmarshal result doesn't end up a zero value).
Somehow, It was thoughtless of me to marking such a non-critical topic as a GitHub Issue and took your time 🙏
Uh oh!
There was an error while loading. Please reload this page.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, 1.16.5 is the latest version today!
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Here is a pull request for my sandbox repository. As the code and test actions, you would easily find the JSON Marshal / Unmarshal functions for
json.RawMessage
fields behave differently from ones for[]byte
. And the weirdest point is parsing a field ofnull
as thejson.RawMessage
type results in notnil
but the byte array equivalent to a string"null"
. However, in the cases for other (Go-standard) types, it interprets them as the zero value of the specified type (like0
for numbers,""
for string, and evennil
for[]byte
).What did you expect to see?
I want the
json.RawMessage
to behave as possible as similar to[]byte
because it's the type alias, but if this is hard, it's enough that thenull
value is parsed intonil
ofjson.RawMessage
, as above.What did you see instead?
I'm looking for the implementation code of UnmarshalJSON and MarshalJSON for
[]byte
type and want to see the difference from the ones forjson.RawMessage
, but for now, I couldn't find them. And at least, it's definitely clear this issue can be reproduced on Go Playground: https://play.golang.org/p/YTHmX5FOg_xThe text was updated successfully, but these errors were encountered: