Skip to content

Commit 52f122d

Browse files
bpowersrobpike
authored andcommitted
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
1 parent b252fe7 commit 52f122d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/pkg/encoding/json/encode.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"runtime"
1818
"sort"
1919
"strconv"
20+
"strings"
2021
"sync"
2122
"unicode"
2223
"unicode/utf8"
@@ -415,9 +416,11 @@ func isValidTag(s string) bool {
415416
return false
416417
}
417418
for _, c := range s {
418-
switch c {
419-
case '$', '-', '_', '/', '%':
420-
// Acceptable
419+
switch {
420+
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~", c):
421+
// Backslash and quote chars are reserved, but
422+
// otherwise any punctuation chars are allowed
423+
// in a tag name.
421424
default:
422425
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
423426
return false

src/pkg/encoding/json/tagkey_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type percentSlashTag struct {
4040
V string `json:"text/html%"` // http://golang.org/issue/2718
4141
}
4242

43+
type punctuationTag struct {
44+
V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // http://golang.org/issue/3546
45+
}
46+
4347
type emptyTag struct {
4448
W string
4549
}
@@ -73,6 +77,7 @@ var structTagObjectKeyTests = []struct {
7377
{badFormatTag{"Orfevre"}, "Orfevre", "Y"},
7478
{badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
7579
{percentSlashTag{"brut"}, "brut", "text/html%"},
80+
{punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~"},
7681
}
7782

7883
func TestStructTagObjectKey(t *testing.T) {

0 commit comments

Comments
 (0)