mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
support private fields
This commit is contained in:
parent
29a928e1d2
commit
3333ec11a0
@ -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
extra/privat_fields.go
Normal file
24
extra/privat_fields.go
Normal file
@ -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
extra/private_fields_test.go
Normal file
18
extra/private_fields_test.go
Normal file
@ -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)
|
||||
}
|
@ -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]))
|
||||
|
Loading…
Reference in New Issue
Block a user