mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +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"))
|
||||||
should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld"))
|
should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld"))
|
||||||
SetNamingStrategy(LowerCaseWithUnderscores)
|
SetNamingStrategy(LowerCaseWithUnderscores)
|
||||||
output, err := jsoniter.MarshalToString(struct {
|
output, err := jsoniter.Marshal(struct {
|
||||||
HelloWorld string
|
UserName string
|
||||||
|
FirstLanguage string
|
||||||
}{
|
}{
|
||||||
HelloWorld: "hi",
|
UserName: "taowen",
|
||||||
|
FirstLanguage: "Chinese",
|
||||||
})
|
})
|
||||||
should.Nil(err)
|
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])
|
fieldNames := calcFieldNames(field.Name, tagParts[0])
|
||||||
fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
||||||
decoder := fieldDecoders[fieldCacheKey]
|
decoder := fieldDecoders[fieldCacheKey]
|
||||||
if decoder == nil && len(fieldNames) > 0 {
|
if decoder == nil {
|
||||||
var err error
|
var err error
|
||||||
decoder, err = decoderOfType(cfg, field.Type)
|
decoder, err = decoderOfType(cfg, field.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -230,7 +230,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
encoder := fieldEncoders[fieldCacheKey]
|
encoder := fieldEncoders[fieldCacheKey]
|
||||||
if encoder == nil && len(fieldNames) > 0 {
|
if encoder == nil {
|
||||||
var err error
|
var err error
|
||||||
encoder, err = encoderOfType(cfg, field.Type)
|
encoder, err = encoderOfType(cfg, field.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -275,11 +275,6 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
|||||||
return structDescriptor, nil
|
return structDescriptor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func listStructFields(typ reflect.Type) []*reflect.StructField {
|
|
||||||
fields := []*reflect.StructField{}
|
|
||||||
return fields
|
|
||||||
}
|
|
||||||
|
|
||||||
func calcFieldNames(originalFieldName string, tagProvidedFieldName string) []string {
|
func calcFieldNames(originalFieldName string, tagProvidedFieldName string) []string {
|
||||||
// tag => exported? => original
|
// tag => exported? => original
|
||||||
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
|
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user