After thinking more about it, I start to feel that maybe using embedded 
structs is generally a bad idea with dealing with json? Is the correct 
solution here to use explicit fields?

BTW, happy New Year everyone.

On Tuesday, December 31, 2019 at 6:21:07 PM UTC+8, Glen Huang wrote:
>
> I want to unmarshal a struct that contains embedded structs:
>
> type Parent struct {
>  Child
>  P int 
> }
>
>
> type Child struct {
>  Grandchild
>  C int
> }
>
>
> type Grandchild struct {
>  G int
> }
>
> The problem is that Grandchild defines its own UnmarshalJSON method, and 
> that means Parent inherits it and breaks json.Unmarshal.
>
> I wonder if it's possible to correctly unmarshal Parent without overriding 
> UnmarshalJSON for every member in the embedding chain? It's correctly the 
> only way I know that can fix the issue, but it's very cumbersome, and there 
> are cases where I only have control over Parent, but not Child and 
> Grandchild,
>
> Thanks. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/28f19736-01df-40bf-a334-bc59662f01e5%40googlegroups.com.

Reply via email to