Because the underlying type of bson.ObjectID is string, the map marshaller uses the underlying string (coerced to valid utf-8) as the JSON map key directly. In the case of ObjectID that's a bad thing because many ObjectIDs contain bytes that are not valid utf-8 and get replaced with the unicode replacement rune, causing loss of data. I think it must be this way to satisfy the Go 1 promise, because originally map keys had to be string or integer types, and even though later the encoding.TextMarshaller interface came along, they couldn't change the behavior for string and integer types including those that implement encoding.TextMarshaller. I suppose the behavior could be changed if it were controlled by a flag or something.
I wonder whether the mgo maintainers could be convinced to change the underlying type of bson.ObjectID to [12]byte instead. This would still work as a map key and would cause json.Marshaller to use the MarshalText method, giving you the results you were looking for automatically. On Thu, Dec 22, 2016 at 8:20 AM Vincent Jouglard <jougla...@gmail.com> wrote: Hi Jon, Thanks for the explanation ; i already used strings as indexes to make this work; but I was wondering if I was doing something wrong :) Still, I wonder if this behavior is wanted and if so, why that ? Regards, -- 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. -- 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.