On 9 Aug 2017, at 12:26, Sankar <sankar.curios...@gmail.com> wrote:
> 
> 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 ?

The issue is that you have defined MarshalJSON as a method with a pointer 
receiver, while the JSON marshaller will access it through an interface value 
that is not addressable. Instead, declare the MarshalJSON method with a value 
receiver:

func (ct CustomTime) MarshalJSON() ([]byte, error) { ... }

//jb

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