There is a difference in the way MarshalJSON of a struct is called when an 
instance of a struct is either part of an array or embedded directly inside 
another object. I am not sure if it is a bug or if it is the intended 

Please see the Go source available at: https://play.golang.org/p/NpWj4VAjrz

I have a custom struct `CustomTime` which has a MarshalJSON and an 
UnmarshalJSON function.

Now I have a struct `Item`, which has an instance of the above `CustomTime` 

There are two other structs, ItemsArray and ItemsMember where Item is part 
of an array or a direct member.

type ItemsArray struct {
Items []Item

type ItemsMember struct {
Item Item

Now for the same, Item object, the json.Marshal function returns two 
different strings for the `Item` object.

If you run the playground url, you will find that:



are two different outputs (notice the "seconds" and the "Z" in the second 
line). I expected both the lines to print the same output for "SD" as the 
same object is re-used for them both.

Now, should I define MarshalJson and UnmarshalJSON function for the []Item 
too (after making it a custom struct) ?

Or is this a bug, or do I misunderstand anything ?


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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to