-
Notifications
You must be signed in to change notification settings - Fork 18k
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: Unmarshal isn't recognizing endashes in keys / struct tags #35287
Comments
So you have a struct with 2 fields with the same json tag, am I reading it correctly? |
Good question. The struct tag on Hyphen Minus is the dash we're used to seeing on a typical keyboard, between |
Oh... (The information that you have provided should be a part of the ticket description (imho) - I missed unicode's thing.) Is possible that editor (or source control) that you have used at some point got too smart, and replaced en dash with minus because it thought you are opening an ascii file instead of unicode? Like, visually they are (almost) the same... If anything, there are more than one intermediary in between code and running binary - editors, compiler that understands the tags, json library itself, maybe some other. Sorry, I don't know how json is treated (by JSON spec, or by libraries) - does it allow to have UTF-8 tags? |
I realized my ticket was too vague and added notes about the codepoints to the ticket. Thank you for the feedback. If you run https://play.golang.org/p/VxJcyJXHyAd, I have it dump the raw bytes of the JSON. The en dash is UTF-8 encoded, and comes up as According to the JSON spec at https://www.json.org/, the key to a JSON object must be a
However, if we look at https://golang.org/src/encoding/json/encode.go?s=6471:6514#L837, there's an |
This is documented in the json.Marshal docs (referenced by the Unmarshal ones):
|
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yep
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/El7R8jXFCRA
I have some JSON data that looks like this:
and a type called
SomeStruct
defined like this:where the struct tag on SomeString contains an En Dash, U+2013, and the struct tag on AnotherString contains a Hyphen Minus, U+002D.
I'm calling
json.Unmarshal()
on my JSON data, trying to unmarshal it into aSomeStruct
.What did you expect to see?
I'd expect to see a SomeStruct equal to
What did you see instead?
Instead, I see a SomeStruct equal to
I suspect this is because
json.Unmarshal
is having trouble with the endash in the keystruct – tag
or the Struct Tagjson:"struct – tag"
.The text was updated successfully, but these errors were encountered: