-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: behaves incorrectly on nested structs with json.RawMessage #10551
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
Labels
Comments
I think it's a misuse of json.RawMessage.
what do you expect the output to be for the first marshal call?
{"tasks":[{"creds":null}]} is not the correct answer.
|
Can you explain why? I'd expect similar output to |
The content of RawMessage is the raw json data,
so it's impossible for a RawMessage to be nil or []byte{}.
Think how you can parse a valid json into a RawMessage
so that it's nil or []byte{}.
|
Then why does the last example run fine? There should be some consistency, at the very least. json.Marshal(json.RawMessage(nil)) Outputs |
That's because the MarshalJSON method of json.RawMessage
is defined on a pointer receiver, so if you pass a json.RawMessage
to Marshal, it will be treated the same as a []byte and not as
a RawMessage.
https://play.golang.org/p/_253EEfKke
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Example here: https://play.golang.org/p/lozTmQr8pR
I'd expect all three outputs to be sane json. There should be no errors here.
The text was updated successfully, but these errors were encountered: