-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: Unexpected marshal behavior #21903
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
Comments
Note that we don't use the issue tracker to ask questions. Please see https://golang.org/wiki/Questions . |
In my opinion, it's a bug since it works for some cases. |
I should note that Go 1.0 had the user's expected behavior: {"value":"A"}
{"value":"B"}
{"value":"C"} The current output has been the case since Go1.1. |
Maybe Go 1.0 is buggy. I feel the results of the later versions are right. func (this *B) MarshalJSON() ([]byte, error) {
type Copy B
c := (*Copy)(this)
return json.Marshal(c)
} The type The |
Doesn't seem to be anything to do here. |
I created a custom MarshalJSON for my struct and it outputs the wrong struct if I have customs MarshalJSON nested in my struct.
I'm trying to understand why it doesn't work as expected. I know that there are some limitations with type redefinition but it works well if I don't nest any struct with a custom MarshalJSON.
What version of Go are you using (
go version
)?1.8
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Linux
What did you do?
https://play.golang.org/p/Mp1ot20ILA
This example is just to be able to reproduce the problem. I know it doesn't make any sense the custom Copy type, but in a real problem, I would extend Copy type by adding custom fields.
What did you expect to see?
{"value":"A"}
{"value":"B","A":{"value":"A"}}
{"value":"C","A":{"value":"A"},"B":{"value":"B","A":{"value":"A"}}}
or at least
{"value":"A"}
{"value":"B"}
{"value":"C"}
What did you see instead?
{"value":"A"}
{"value":"A"}
{"value":"C","A":{"value":"A"},"B":{"value":"A"}}
The first line is ok.
The second line is completely wrong.
The third line is almost ok.
C has two nested objects with a custom marshal and it works almost good.
B has just one and it prints the output of A.
The text was updated successfully, but these errors were encountered: