encoding/json: Unexpected marshal behavior #21903
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
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: