1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:34:48 -07:00

encoding/json: allow / and % in tag names

Fixes #2718

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5532095
This commit is contained in:
Brad Fitzpatrick 2012-01-18 19:05:15 -08:00
parent 0d8c6b4fcd
commit b39c883e29
2 changed files with 13 additions and 3 deletions

View File

@ -419,10 +419,15 @@ func isValidTag(s string) bool {
return false
}
for _, c := range s {
if c != '$' && c != '-' && c != '_' && !unicode.IsLetter(c) && !unicode.IsDigit(c) {
switch c {
case '$', '-', '_', '/', '%':
// Acceptable
default:
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
}
}
return true
}

View File

@ -36,6 +36,10 @@ type miscPlaneTag struct {
V string `json:"色は匂へど"`
}
type percentSlashTag struct {
V string `json:"text/html%"` // http://golang.org/issue/2718
}
type emptyTag struct {
W string
}
@ -68,6 +72,7 @@ var structTagObjectKeyTests = []struct {
{misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"},
{badFormatTag{"Orfevre"}, "Orfevre", "Y"},
{badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
{percentSlashTag{"brut"}, "brut", "text/html%"},
}
func TestStructTagObjectKey(t *testing.T) {
@ -88,7 +93,7 @@ func TestStructTagObjectKey(t *testing.T) {
t.Fatalf("Unexpected value: %#q, want %v", s, tt.value)
}
default:
t.Fatalf("Unexpected key: %#q", i)
t.Fatalf("Unexpected key: %#q, from %#q", i, b)
}
}
}