Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
encoding/json: unmarshalling same field into two struct fields #15127
Please answer these questions before submitting your issue. Thanks!
I took a look at this. I don't have a complete answer for it, but want to document my findings here.
This behavior is caused by the "annihilation" logic shared between encoding and decoding. For encoding, this makes sense: since the two sources might have different value, we might as well not write anything. One might argue that this should be an error. I'm not sure what the rationale is, but the doc on dominantField function ( https://golang.org/src/encoding/json/encode.go#L1139 ) and a test (TestDuplicatedFieldDisappears) indicate that it's a deliberate decision.
Since encode makes a decision to not error, I think we should refrain from returning an error from the decode side. One way to solve this issue is to give decode its own version of dominantField that does "union" instead of "annihilation", setting all "conflicted" fields to the same value.
Of course, another way out is to document this behavior and keep the implementation as is. What do you think?
(Please let me know if golang-dev is a better place for this discussion.)
I thought a little more about this. Currently