Hi,

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 
behavior. 

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` 
struct.

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:

{"Items":[{"SD":"2009-11-10T23:00"}]} 

{"Item":{"SD":"2009-11-10T23:00:00Z"}}


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 ?

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

Reply via email to