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: inconsistent behavior in *(numeric type) and string tag option #8582

Closed
matt-duch opened this issue Aug 25, 2014 · 5 comments
Closed
Milestone

Comments

@matt-duch
Copy link

What does 'go version' print?
go version go1.3.1 linux/amd64

What steps reproduce the problem?
1. Create a struct type with a field of type *int64
2. Init with a non-nil value for that field
3. Encode using json.Marshal
4. Decode result using json.Unmarshal

http://play.golang.org/p/fWLSVv9hxT

Also discussed:
groups.google.com/d/msg/golang-nuts/BTNKGipB7zs/Ub3FDleTnlkJ
groups.google.com/forum/#!topic/golang-nuts/mr49_7W9CKM

What happened?
Encountered decode error unmarshalling quoted (string) field value ("err
unmarshalling json: cannot unmarshal number into Go value of type string")

What should have happened instead?
Decoded int64 string value into a *int64 without error.

Please provide any additional information below.
encoding/json/encode.go
 699    func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   700      if v.IsNil() {
   701          e.WriteString("null")
   702          return
   703      }
   704      pe.elemEnc(e, v.Elem(), false)
   705  }

If changed to (lines 699, 704)
 699    func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   700      if v.IsNil() {
   701          e.WriteString("null")
   702          return
   703      }
   704      pe.elemEnc(e, v.Elem(), quoted)
   705  }

corrects the issue. As the array, slice, struct, and map encoders ignore the quoted
parameter, this should still result in correct json encoding in all existing code, while
printing strings for numeric and bool values (as a user probably expects when adding the
string tag option)
@matt-duch
Copy link
Author

Comment 1:

Correction/clarification to "What should have happened instead?": The error is generated
during decoding, but the root of the error is the encoder does not wrap the value in
quotes.

@ianlancetaylor
Copy link
Contributor

Comment 2:

Labels changed: added repo-main, release-go1.4.

@bradfitz
Copy link
Contributor

Comment 3:

Owner changed to @bradfitz.

Status changed to Started.

@gopherbot
Copy link

Comment 4:

CL https://golang.org/cl/132960043 mentions this issue.

@bradfitz
Copy link
Contributor

Comment 5:

This issue was closed by revision df52d2e.

Status changed to Fixed.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
It was respected by unmarshal, but not marshal, so they didn't
round-trip.

Fixes golang#8582

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/132960043
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
It was respected by unmarshal, but not marshal, so they didn't
round-trip.

Fixes golang#8582

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/132960043
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants