summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2011-01-12 11:59:33 +1100
committerAndrew Gerrand <adg@golang.org>2011-01-12 11:59:33 +1100
commita9bdf0aaad0010fd7ad5002361c78c2be98ac4e7 (patch)
tree022ab2bc50cdbbef09cc1b261ac2632642ba2166
parent2a7751a4c3ebb7ef4e45bdcdb38e2bc151ea9f2f (diff)
downloadgo-a9bdf0aaad0010fd7ad5002361c78c2be98ac4e7.tar.gz
json: do not Marshal unexported struct fields
R=r, cw, niemeyer, rsc CC=golang-dev http://codereview.appspot.com/3952041
-rw-r--r--src/pkg/json/decode_test.go2
-rw-r--r--src/pkg/json/encode.go11
2 files changed, 11 insertions, 2 deletions
diff --git a/src/pkg/json/decode_test.go b/src/pkg/json/decode_test.go
index 2a18a6226..68cdea051 100644
--- a/src/pkg/json/decode_test.go
+++ b/src/pkg/json/decode_test.go
@@ -270,6 +270,8 @@ type All struct {
Interface interface{}
PInterface *interface{}
+
+ unexported int
}
type Small struct {
diff --git a/src/pkg/json/encode.go b/src/pkg/json/encode.go
index e043a317e..759b49dbe 100644
--- a/src/pkg/json/encode.go
+++ b/src/pkg/json/encode.go
@@ -37,6 +37,7 @@ import (
// a member of the object. By default the object's key name is the
// struct field name converted to lower case. If the struct field
// has a tag, that tag will be used as the name instead.
+// Only exported fields will be encoded.
//
// Map values encode as JSON objects.
// The map's key type must be string; the object keys are used directly
@@ -219,11 +220,17 @@ func (e *encodeState) reflectValue(v reflect.Value) {
e.WriteByte('{')
t := v.Type().(*reflect.StructType)
n := v.NumField()
+ first := true
for i := 0; i < n; i++ {
- if i > 0 {
+ f := t.Field(i)
+ if f.PkgPath != "" {
+ continue
+ }
+ if first {
+ first = false
+ } else {
e.WriteByte(',')
}
- f := t.Field(i)
if f.Tag != "" {
e.string(f.Tag)
} else {