aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/v2
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoding/json/v2')
-rw-r--r--src/encoding/json/v2/fields.go8
-rw-r--r--src/encoding/json/v2/fields_test.go13
2 files changed, 21 insertions, 0 deletions
diff --git a/src/encoding/json/v2/fields.go b/src/encoding/json/v2/fields.go
index 9413189c08..4a02be7327 100644
--- a/src/encoding/json/v2/fields.go
+++ b/src/encoding/json/v2/fields.go
@@ -404,6 +404,7 @@ type fieldOptions struct {
// the JSON member name and other features.
func parseFieldOptions(sf reflect.StructField) (out fieldOptions, ignored bool, err error) {
tag, hasTag := sf.Tag.Lookup("json")
+ tagOrig := tag
// Check whether this field is explicitly ignored.
if tag == "-" {
@@ -453,6 +454,13 @@ func parseFieldOptions(sf reflect.StructField) (out fieldOptions, ignored bool,
err = cmp.Or(err, fmt.Errorf("Go struct field %s has JSON object name %q with invalid UTF-8", sf.Name, name))
name = string([]rune(name)) // replace invalid UTF-8 with utf8.RuneError
}
+ if name == "-" && tag[0] == '-' {
+ defer func() { // defer to let other errors take precedence
+ err = cmp.Or(err, fmt.Errorf("Go struct field %s has JSON object name %q; either "+
+ "use `json:\"-\"` to ignore the field or "+
+ "use `json:\"'-'%s` to specify %q as the name", sf.Name, out.name, strings.TrimPrefix(strconv.Quote(tagOrig), `"-`), name))
+ }()
+ }
if err2 == nil {
out.hasName = true
out.name = name
diff --git a/src/encoding/json/v2/fields_test.go b/src/encoding/json/v2/fields_test.go
index 1c36f80905..ae58182f29 100644
--- a/src/encoding/json/v2/fields_test.go
+++ b/src/encoding/json/v2/fields_test.go
@@ -503,6 +503,19 @@ func TestParseTagOptions(t *testing.T) {
wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`},
wantErr: errors.New("Go struct field V has malformed `json` tag: invalid trailing ',' character"),
}, {
+ name: jsontest.Name("DashCommaOmitEmpty"),
+ in: struct {
+ V int `json:"-,omitempty"`
+ }{},
+ wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`, omitempty: true},
+ wantErr: errors.New("Go struct field V has JSON object name \"-\"; either use `json:\"-\"` to ignore the field or use `json:\"'-',omitempty\"` to specify \"-\" as the name"),
+ }, {
+ name: jsontest.Name("QuotedDashCommaOmitEmpty"),
+ in: struct {
+ V int `json:"'-',omitempty"`
+ }{},
+ wantOpts: fieldOptions{hasName: true, name: "-", quotedName: `"-"`, omitempty: true},
+ }, {
name: jsontest.Name("QuotedDashName"),
in: struct {
V int `json:"'-'"`