You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the string option is set on a struct field of type json.Number, the value gets encoded without quotes. If this encoded value is then unmarshaled back, it fails, because the expected quotes are missing.
What version of Go are you using (go version)?
$ go version
go version go1.12.5 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
{"JSONNumberStringOption":2}
2019/09/12 21:33:02 json: invalid use of ,string struct tag, trying to unmarshal unquoted value into json.Number
exit status 1
The text was updated successfully, but these errors were encountered:
breml
added a commit
to breml/go
that referenced
this issue
Sep 12, 2019
Add quotes if a value of type json.Number with the string option
(struct tag) set is marshaled. This ensures, that the resulting json
can be unmarshaled into the source struct without error.
Fixesgolang#34268
I added and then quickly deleted this comment, but I think a few people still saw it via email notifications, so I figured I might as well restore it for posterity:
Package json's documentation describes:
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.
So using it for json.Number is technically against the documentation.
I don't have any preference here; but if json.Number is updated to support the "string" option, then this documentation should also be updated.
I made this comment before realizing that Number is a declared type whose underlying type is string. After realizing that, however, I didn't feel confident about my interpretation and wanted to retract it, rather than add uninformed drive-by chatter. (But you can't delete anything from the Internet.)
If the
string
option is set on a struct field of typejson.Number
, the value gets encoded without quotes. If this encoded value is then unmarshaled back, it fails, because the expected quotes are missing.What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/iX9v1XWpsLG
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: