encoding/json: behaves incorrectly on nested structs with json.RawMessage #10551
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: