encoding/json: wrong encoding for json.Number field with string option (struct tag) #34268
Comments
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. Fixes golang#34268
Change https://golang.org/cl/195043 mentions this issue: |
The fact that you can encode but then not decode back seems to point clearly at a bug here; thanks for spotting this. |
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:
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: