Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: integer type uintptr not marshalled with "string" option #22629

Closed
larhun opened this issue Nov 8, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@larhun
Copy link

commented Nov 8, 2017

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, the Y field of uintptr 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.

@gbbr gbbr changed the title integer type uintptr not marshalled to JSON string with "string" option encoding/json: integer type uintptr not marshalled with "string" option Nov 8, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Nov 8, 2017

Change https://golang.org/cl/76530 mentions this issue: encoding/json: permit encoding uintptr as a string

@odeke-em odeke-em added the NeedsFix label Nov 8, 2017

@odeke-em odeke-em added this to the Go1.10 milestone Nov 8, 2017

@gopherbot gopherbot closed this in ed3d672 Nov 8, 2017

@golang golang locked and limited conversation to collaborators Nov 8, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.