Just a friendly reminder. When sending code to this list, please use a link to the Go playground or use plain text. The colored text with a black background is unreadable. Thanks.
On Wednesday, October 23, 2019 at 11:45:24 PM UTC-4, Barakat Barakat wrote: > > I'm using macOS Mojave, go 1.12.4 > > Example: https://play.golang.org/p/uluBecqL6QF > I would expect the key and value in the example to be the same. > > If you implement TextMarshaler on a custom string type, the encoder does > not use the marshaler when encoding map keys of that type. I ran into this > trying to use bson.ObjectId from the globalsign mgo repo as a key in a map > and trying to serialize it to JSON. The JSON encoder uses reflection to > check that the 'kind' of the value is a string, which is true. But it does > not check if it is a custom string type that has TextMarshaler implemented. > The JSON encoder code: > > encoding/json/encode.go 865-884 > > func (w *reflectWithString) resolve() error { > if w.v.Kind() == reflect.String { > w.s = w.v.String() > return nil > } > if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok { > buf, err := tm.MarshalText() > w.s = string(buf) > return err > } > switch w.v.Kind() { > case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, > reflect.Int64: > w.s = strconv.FormatInt(w.v.Int(), 10) > return nil > case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, > reflect.Uint64, reflect.Uintptr: > w.s = strconv.FormatUint(w.v.Uint(), 10) > return nil > } > panic("unexpected map key type") > } > > > Is this intentional? > -- 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/5c1ff625-186e-48c6-95c9-a7b8fba7111f%40googlegroups.com.