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

Comments

Projects
None yet
5 participants
@matt-duch
Copy link

commented Aug 25, 2014

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

This comment has been minimized.

Copy link
Author

commented Aug 25, 2014

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

This comment has been minimized.

Copy link
Contributor

commented Aug 25, 2014

Comment 2:

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

@bradfitz

This comment has been minimized.

Copy link
Member

commented Aug 25, 2014

Comment 3:

Owner changed to @bradfitz.

Status changed to Started.

@gopherbot

This comment has been minimized.

Copy link

commented Aug 25, 2014

Comment 4:

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

This comment has been minimized.

Copy link
Member

commented Aug 25, 2014

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 added a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018

encoding/json: make ,string work when encoding pointer fields
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 added a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018

encoding/json: make ,string work when encoding pointer fields
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.
You can’t perform that action at this time.