-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: JSON tags don't handle empty properties, non-standard characters #22518
Comments
This is because of |
Do other JSON encoders support field names that are not ordinary identifiers? |
@ianlancetaylor Newtonsoft.JSON for .NET handles all those cases. |
Might be worth noting that the JSON spec(s) [1] [2] allow for any unicode character in object keys. JSON.parse (ECMAScript 5.1 and up) also supports that. |
Change https://golang.org/cl/140305 mentions this issue: |
@rsc Should this issue be considered with "JSON sweep", or is it fine as-is? The PR that I submitted can be amended to remove the introduced emptykey option. |
This change seems too narrow-sighted. An A more generalized solution might be to allow a quoted string as the name argument:
However, this looks ugly since it requires double escaping. Personally, I'm okay with the fact that the |
From #29476 . I think it's a great pity that we can't use Chinese punctuation as json's key. But in fact, I still have other ways to avoid this situation. What I encountered was that I had to add |
Marking as NeedsDecision since it's not clear what is the correct solution to this issue. |
If anyone does want to take a crack at allowing arbitrary utf-8 as JSON keys, please read @dsnet's comment at #39189 (comment). I personally agree with the earlier comment here that it's okay for |
Allow |
There is also no reason why commas are not allowed in JSON keys. {"username,version1": "Foo"} There is no any way for us to correctly unmarshal the JSON above to a structure: type Data struct {
UsernameV1 string `json:"username,version1"`
// UsernameV1 string `json:"username\,version1"`
// UsernameV1 string `json:"username\\,version1"`
} Can't we support any escape seq here? |
Or we can add a new tag type Data struct {
/* Options here Key, supports comma */
UsernameV1 string `json:",omitempty" jsonkey:"username,version1"`
} |
We have run into this in opensearch-project/OpenSearch#8744. |
What did you do?
https://play.golang.org/p/RRB1VFNufW
Trying to unmarshal with tags like this:
What did you expect to see?
What did you see instead?
System details
The text was updated successfully, but these errors were encountered: