]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1] encoding/json: allow punctuation in tag names
authorBobby Powers <bobbypowers@gmail.com>
Wed, 25 Apr 2012 04:33:33 +0000 (14:33 +1000)
committerRob Pike <r@golang.org>
Wed, 25 Apr 2012 04:33:33 +0000 (14:33 +1000)
««« backport 233ff5d46b3d
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

»»»

src/pkg/encoding/json/encode.go
src/pkg/encoding/json/tagkey_test.go

index 14957b8487ba1cc20e432f03202fddaeb08cf74f..842672c3974509e2c70e0624ac3a268c0d2b6584 100644 (file)
@@ -17,6 +17,7 @@ import (
        "runtime"
        "sort"
        "strconv"
+       "strings"
        "sync"
        "unicode"
        "unicode/utf8"
@@ -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
index bba5730353d27f0e26d3cc4f7444da89bdb7a21b..da8b12bd8fb4a8f65c21e918cfdf6b721aec0895 100644 (file)
@@ -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
 }
@@ -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) {