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: string tag not symmetric #9812

Closed
klauspost opened this issue Feb 9, 2015 · 5 comments
Closed

encoding/json: string tag not symmetric #9812

klauspost opened this issue Feb 9, 2015 · 5 comments
Milestone

Comments

@klauspost
Copy link
Contributor

The string tag is not symmetric if the type is a base type that fulfills the json.Marshaler/json.Unmarshaler interfaces.

The tag is ignored when the json is encoded, but quotes are removed when unmarshaling the result.

Playground example

In the example, the marshaler generates this json: {"Time":"2009-11-10T23:00:00Z"} with no extra quotes. However, when the same field is unmarhalled, the quotes are removed, and the decoder fails.

Go should either a) Add quotes when marshaling field, or b) not remove quotes when unmarshaling.

go version go1.4 windows/amd64

@mikioh mikioh changed the title encoding/json string tag not symmetric encoding/json: string tag not symmetric Feb 9, 2015
@odeke-em
Copy link
Member

I don't know if this matters but if I move the closing parenthesis into the terminating double quote ie.

$ diff c7y5IMDoYT.go c7y5IMDoYT-fixed.go 
10c10
<     Time time.Time `json:",string")`
---
>     Time time.Time `json:",string)"`

The code works without error giving:

$ go run c7y5IMDoYT-fixed.go && echo -e "\n"
Encoded: {"Time":"2015-02-15T20:46:10.498513622-07:00"}
Decoded:main.A{Time:time.Time{sec:63559655170, nsec:0x1db6b6d6, loc:(*time.Location)(0x59f6c0)}}

instead of

$ go run c7y5IMDoYT.go
Encoded: {"Time":"2015-02-15T20:51:03.13257219-07:00"}
panic: parsing time "2015-02-15T20:51:03.13257219-07:00" as ""2006-01-02T15:04:05Z07:00"": cannot parse "2015-02-15T20:51:03.13257219-07:00" as """

goroutine 16 [running]:
runtime.panic(0x4dcce0, 0xc2080181e0)
    /usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.main()
    /home/emmanuel/Desktop/openSrc/bugs/golang/9812/c7y5IMDoYT.go:21 +0x27b

goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
    /usr/local/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
    /usr/local/go/src/pkg/runtime/proc.c:1445

goroutine 18 [runnable]:
bgsweep()
    /usr/local/go/src/pkg/runtime/mgc0.c:1976
runtime.goexit()
    /usr/local/go/src/pkg/runtime/proc.c:1445

goroutine 19 [runnable]:
runfinq()
    /usr/local/go/src/pkg/runtime/mgc0.c:2606
runtime.goexit()
    /usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2

@klauspost
Copy link
Contributor Author

@odeke-em : That's just because now the tag is "string)", which doesn't equal "string", so it ignores the tag.

The ')' wasn't supposed to be there. I have updated the playground example.

@odeke-em
Copy link
Member

@klauspost: Thank you for the updated example and clarification. In deed your issue still stands.

@rsc rsc added this to the Go1.5 milestone Apr 10, 2015
@larzconwell
Copy link
Contributor

This occurs because the code that checks if the "string" option is given didn't consider the type of value given.

Fixed in CL: https://go-review.googlesource.com/#/c/10183/

@gopherbot
Copy link

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

@rsc rsc closed this as completed in 1a4e177 Jul 15, 2015
@golang golang locked and limited conversation to collaborators Jul 18, 2016
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