-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: string tag not symmetric #9812
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
Comments
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 |
@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. |
@klauspost: Thank you for the updated example and clarification. In deed your issue still stands. |
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/ |
CL https://golang.org/cl/10183 mentions this issue. |
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
The text was updated successfully, but these errors were encountered: