mirror of
https://github.com/golang/go
synced 2024-11-20 08:14:41 -07:00
encoding/json: allow punctuation in tag names
everything except backslash and the quote chars is fair game. Fixes #3546. R=rsc, r CC=golang-dev https://golang.org/cl/6048047
This commit is contained in:
parent
b252fe7002
commit
52f122d72e
@ -17,6 +17,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@ -415,9 +416,11 @@ func isValidTag(s string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, c := range s {
|
for _, c := range s {
|
||||||
switch c {
|
switch {
|
||||||
case '$', '-', '_', '/', '%':
|
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~", c):
|
||||||
// Acceptable
|
// Backslash and quote chars are reserved, but
|
||||||
|
// otherwise any punctuation chars are allowed
|
||||||
|
// in a tag name.
|
||||||
default:
|
default:
|
||||||
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
|
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
|
||||||
return false
|
return false
|
||||||
|
@ -40,6 +40,10 @@ type percentSlashTag struct {
|
|||||||
V string `json:"text/html%"` // http://golang.org/issue/2718
|
V string `json:"text/html%"` // http://golang.org/issue/2718
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type punctuationTag struct {
|
||||||
|
V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // http://golang.org/issue/3546
|
||||||
|
}
|
||||||
|
|
||||||
type emptyTag struct {
|
type emptyTag struct {
|
||||||
W string
|
W string
|
||||||
}
|
}
|
||||||
@ -73,6 +77,7 @@ var structTagObjectKeyTests = []struct {
|
|||||||
{badFormatTag{"Orfevre"}, "Orfevre", "Y"},
|
{badFormatTag{"Orfevre"}, "Orfevre", "Y"},
|
||||||
{badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
|
{badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
|
||||||
{percentSlashTag{"brut"}, "brut", "text/html%"},
|
{percentSlashTag{"brut"}, "brut", "text/html%"},
|
||||||
|
{punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStructTagObjectKey(t *testing.T) {
|
func TestStructTagObjectKey(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user