diff --git a/extra/naming_strategy_test.go b/extra/naming_strategy_test.go index 1a579e8..97ddf47 100644 --- a/extra/naming_strategy_test.go +++ b/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)) } - - diff --git a/extra/privat_fields.go b/extra/privat_fields.go new file mode 100644 index 0000000..6ca7a5f --- /dev/null +++ b/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} + } + } +} diff --git a/extra/private_fields_test.go b/extra/private_fields_test.go new file mode 100644 index 0000000..eb0274f --- /dev/null +++ b/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) +} diff --git a/feature_reflect_extension.go b/feature_reflect_extension.go index a637218..c0caa48 100644 --- a/feature_reflect_extension.go +++ b/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]))