]> Cypherpunks repositories - gostls13.git/commitdiff
json: do not Marshal unexported struct fields
authorAndrew Gerrand <adg@golang.org>
Wed, 12 Jan 2011 00:59:33 +0000 (11:59 +1100)
committerAndrew Gerrand <adg@golang.org>
Wed, 12 Jan 2011 00:59:33 +0000 (11:59 +1100)
R=r, cw, niemeyer, rsc
CC=golang-dev
https://golang.org/cl/3952041

src/pkg/json/decode_test.go
src/pkg/json/encode.go

index 2a18a62268c5767a1014b796232729ffef9bed49..68cdea051ea5747fb58862ee9e8a8f8c05c957a8 100644 (file)
@@ -270,6 +270,8 @@ type All struct {
 
        Interface  interface{}
        PInterface *interface{}
+
+       unexported int
 }
 
 type Small struct {
index e043a317e34e29c0a9e26844ae7e7dd5c36a83e6..759b49dbeb4e02e033b41de1ef47f339f1f6c25a 100644 (file)
@@ -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 {