Skip to content

Commit

Permalink
encoding/json: allow punctuation in tag names
Browse files Browse the repository at this point in the history
everything except backslash and the quote chars is fair game.

Fixes #3546.

R=rsc, r
CC=golang-dev
https://golang.org/cl/6048047
  • Loading branch information
bpowers authored and robpike committed Apr 25, 2012
1 parent b252fe7 commit 52f122d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/pkg/encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"runtime"
"sort"
"strconv"
"strings"
"sync"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -415,9 +416,11 @@ func isValidTag(s string) bool {
return false
}
for _, c := range s {
switch c {
case '$', '-', '_', '/', '%':
// Acceptable
switch {
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~", c):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
default:
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/encoding/json/tagkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type percentSlashTag struct {
V string `json:"text/html%"` // http://golang.org/issue/2718
}

type punctuationTag struct {
V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // http://golang.org/issue/3546
}

type emptyTag struct {
W string
}
Expand Down Expand Up @@ -73,6 +77,7 @@ var structTagObjectKeyTests = []struct {
{badFormatTag{"Orfevre"}, "Orfevre", "Y"},
{badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
{percentSlashTag{"brut"}, "brut", "text/html%"},
{punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~"},
}

func TestStructTagObjectKey(t *testing.T) {
Expand Down

0 comments on commit 52f122d

Please sign in to comment.