summaryrefslogtreecommitdiff
path: root/src/pkg/json
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/json')
-rw-r--r--src/pkg/json/decode.go2
-rw-r--r--src/pkg/json/decode_test.go9
-rw-r--r--src/pkg/json/encode.go16
-rw-r--r--src/pkg/json/scanner_test.go5
4 files changed, 19 insertions, 13 deletions
diff --git a/src/pkg/json/decode.go b/src/pkg/json/decode.go
index e78b60ccb..35a06b0f9 100644
--- a/src/pkg/json/decode.go
+++ b/src/pkg/json/decode.go
@@ -482,7 +482,7 @@ func (d *decodeState) object(v reflect.Value) {
if isValidTag(key) {
for i := 0; i < sv.NumField(); i++ {
f = st.Field(i)
- if f.Tag == key {
+ if f.Tag.Get("json") == key {
ok = true
break
}
diff --git a/src/pkg/json/decode_test.go b/src/pkg/json/decode_test.go
index bf8bf10bf..9b84bc76c 100644
--- a/src/pkg/json/decode_test.go
+++ b/src/pkg/json/decode_test.go
@@ -42,8 +42,9 @@ var (
type badTag struct {
X string
- Y string "y"
- Z string "@#*%(#@"
+ Y string `json:"y"`
+ Z string `x:"@#*%(#@"`
+ W string `json:"@#$@#$"`
}
type unmarshalTest struct {
@@ -68,7 +69,7 @@ var unmarshalTests = []unmarshalTest{
{`{"x": 1}`, new(tx), tx{}, &UnmarshalFieldError{"x", txType, txType.Field(0)}},
// skip invalid tags
- {`{"X":"a", "y":"b", "Z":"c"}`, new(badTag), badTag{"a", "b", "c"}, nil},
+ {`{"X":"a", "y":"b", "Z":"c", "W":"d"}`, new(badTag), badTag{"a", "b", "c", "d"}, nil},
// syntax errors
{`{"X": "foo", "Y"}`, nil, nil, &SyntaxError{"invalid character '}' after object key", 17}},
@@ -250,7 +251,7 @@ type All struct {
Float32 float32
Float64 float64
- Foo string "bar"
+ Foo string `json:"bar"`
PBool *bool
PInt *int
diff --git a/src/pkg/json/encode.go b/src/pkg/json/encode.go
index ec0a14a6a..adc0f0f37 100644
--- a/src/pkg/json/encode.go
+++ b/src/pkg/json/encode.go
@@ -36,11 +36,13 @@ import (
// Array and slice values encode as JSON arrays, except that
// []byte encodes as a base64-encoded string.
//
-// Struct values encode as JSON objects. Each struct field becomes
-// a member of the object. By default the object's key name is the
-// struct field name. If the struct field has a non-empty tag consisting
-// of only Unicode letters, digits, and underscores, that tag will be used
-// as the name instead. Only exported fields will be encoded.
+// Struct values encode as JSON objects. Each exported struct field
+// becomes a member of the object. By default the object's key string
+// is the struct field name. If the struct field's tag has a "json" key with a
+// value that is a non-empty string consisting of only Unicode letters,
+// digits, and underscores, that value will be used as the object key.
+// For example, the field tag `json:"myName"` says to use "myName"
+// as the object key.
//
// Map values encode as JSON objects.
// The map's key type must be string; the object keys are used directly
@@ -236,8 +238,8 @@ func (e *encodeState) reflectValue(v reflect.Value) {
} else {
e.WriteByte(',')
}
- if isValidTag(f.Tag) {
- e.string(f.Tag)
+ if tag := f.Tag.Get("json"); tag != "" && isValidTag(tag) {
+ e.string(tag)
} else {
e.string(f.Name)
}
diff --git a/src/pkg/json/scanner_test.go b/src/pkg/json/scanner_test.go
index 0d4de3246..023e7c81e 100644
--- a/src/pkg/json/scanner_test.go
+++ b/src/pkg/json/scanner_test.go
@@ -252,7 +252,10 @@ func genArray(n int) []interface{} {
if f > n {
f = n
}
- x := make([]interface{}, int(f))
+ if n > 0 && f == 0 {
+ f = 1
+ }
+ x := make([]interface{}, f)
for i := range x {
x[i] = genValue(((i+1)*n)/f - (i*n)/f)
}