Tao Wen 8 лет назад
Родитель
Сommit
3333ec11a0
4 измененных файлов с 50 добавлено и 13 удалено
  1. 6 6
      extra/naming_strategy_test.go
  2. 24 0
      extra/privat_fields.go
  3. 18 0
      extra/private_fields_test.go
  4. 2 7
      feature_reflect_extension.go

+ 6 - 6
extra/naming_strategy_test.go

@@ -11,13 +11,13 @@ func Test_lower_case_with_underscores(t *testing.T) {
 	should.Equal("hello_world", LowerCaseWithUnderscores("helloWorld"))
 	should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld"))
 	SetNamingStrategy(LowerCaseWithUnderscores)
-	output, err := jsoniter.MarshalToString(struct {
-		HelloWorld string
+	output, err := jsoniter.Marshal(struct {
+		UserName      string
+		FirstLanguage string
 	}{
-		HelloWorld: "hi",
+		UserName:      "taowen",
+		FirstLanguage: "Chinese",
 	})
 	should.Nil(err)
-	should.Equal(`{"hello_world":"hi"}`, output)
+	should.Equal(`{"user_name":"taowen","first_language":"Chinese"}`, string(output))
 }
-
-

+ 24 - 0
extra/privat_fields.go

@@ -0,0 +1,24 @@
+package extra
+
+import (
+	"github.com/json-iterator/go"
+	"unicode"
+)
+
+func SupportPrivateFields() {
+	jsoniter.RegisterExtension(&privateFieldsExtension{})
+}
+
+type privateFieldsExtension struct {
+	jsoniter.DummyExtension
+}
+
+func (extension *privateFieldsExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
+	for _, binding := range structDescriptor.Fields {
+		isPrivate := unicode.IsLower(rune(binding.Field.Name[0]))
+		if isPrivate {
+			binding.FromNames = []string{binding.Field.Name}
+			binding.ToNames = []string{binding.Field.Name}
+		}
+	}
+}

+ 18 - 0
extra/private_fields_test.go

@@ -0,0 +1,18 @@
+package extra
+
+import (
+	"testing"
+	"github.com/json-iterator/go/require"
+	"github.com/json-iterator/go"
+)
+
+func Test_private_fields(t *testing.T) {
+	type TestObject struct {
+		field1 string
+	}
+	SupportPrivateFields()
+	should := require.New(t)
+	obj := TestObject{}
+	should.Nil(jsoniter.UnmarshalFromString(`{"field1":"Hello"}`, &obj))
+	should.Equal("Hello", obj.field1)
+}

+ 2 - 7
feature_reflect_extension.go

@@ -222,7 +222,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
 			fieldNames := calcFieldNames(field.Name, tagParts[0])
 			fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
 			decoder := fieldDecoders[fieldCacheKey]
-			if decoder == nil && len(fieldNames) > 0 {
+			if decoder == nil {
 				var err error
 				decoder, err = decoderOfType(cfg, field.Type)
 				if err != nil {
@@ -230,7 +230,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
 				}
 			}
 			encoder := fieldEncoders[fieldCacheKey]
-			if encoder == nil && len(fieldNames) > 0 {
+			if encoder == nil {
 				var err error
 				encoder, err = encoderOfType(cfg, field.Type)
 				if err != nil {
@@ -275,11 +275,6 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
 	return structDescriptor, nil
 }
 
-func listStructFields(typ reflect.Type) []*reflect.StructField {
-	fields := []*reflect.StructField{}
-	return fields
-}
-
 func calcFieldNames(originalFieldName string, tagProvidedFieldName string) []string {
 	// tag => exported? => original
 	isNotExported := unicode.IsLower(rune(originalFieldName[0]))