aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/v2/fields.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoding/json/v2/fields.go')
-rw-r--r--src/encoding/json/v2/fields.go8
1 files changed, 8 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