This is behaving as documented. The docs say "keys of any string type are
used directly".

The reason for that behaviour is probably to avoid breaking backward
compatibility, because historically encoding/json did not recognise
TextMarshaler in map keys.

You could wrap your type in a struct:

   type jsonKey struct {
       MyStringType
   }

and use that as a key instead, which should work, although I haven't tried
it.

On Thu, 24 Oct 2019, 04:44 Barakat Barakat, <baraka...@gmail.com> 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/6745c4a2-3d52-46f6-ab8f-000d8a1a3f8d%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/6745c4a2-3d52-46f6-ab8f-000d8a1a3f8d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAJhgacjUyLkqJCkuc0aej8MHYzvK14DVbpVWuDN9uAfJ0TYkeQ%40mail.gmail.com.

Reply via email to