-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
version 1.9
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (go env
)?
windows/amd64
What did you do?
package main
import (
"encoding/json"
"fmt"
)
type A struct {
X int `json:",string"`
Y uintptr `json:",string"`
Z B
}
type B map[uintptr]string
func main() {
s := new(A)
s.Z = make(B)
s.Z[0] = ""
r, _ := json.Marshal(s)
fmt.Println(string(r))
}
What did you expect to see?
{"X":"0","Y":"0","Z":{"0":""}}
What did you see instead?
{"X":"0","Y":0,"Z":{"0":""}}
Issue
As required by the
json.Marshal
documentation, theY
field ofuintptr
type, marked with the json tag "string" option, should be marshalled as JSON string, not JSON number.
Please note that the X field, of integer int
type, and the map's key of the Z field, of integer uintptr
type, are both marshalled as JSON string, as expected by the documentation, that states:
-
The "string" option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types.
-
The map's key type must either be a string, an integer type, or implement encoding.TextMarshaler.
Proposal
At line 1138 of of encoding/json/encode.go, please add
reflect.Uintptr
to the list of types that can be encoded to JSON string by the "string" option.