mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
Merge branch 'olegshaldybin-skip-unexported-fields'
This commit is contained in:
commit
fdfe0b9a69
@ -269,7 +269,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
|||||||
if decoder == nil {
|
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 len(fieldNames) > 0 && err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -277,11 +277,11 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
|||||||
if encoder == nil {
|
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 len(fieldNames) > 0 && err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// map is stored as pointer in the struct
|
// map is stored as pointer in the struct
|
||||||
if field.Type.Kind() == reflect.Map {
|
if encoder != nil && field.Type.Kind() == reflect.Map {
|
||||||
encoder = &optionalEncoder{encoder}
|
encoder = &optionalEncoder{encoder}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,12 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_customize_type_decoder(t *testing.T) {
|
func Test_customize_type_decoder(t *testing.T) {
|
||||||
@ -82,7 +83,7 @@ func Test_customize_field_decoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TestObject1 struct {
|
type TestObject1 struct {
|
||||||
field1 string
|
Field1 string
|
||||||
}
|
}
|
||||||
|
|
||||||
type testExtension struct {
|
type testExtension struct {
|
||||||
@ -93,7 +94,7 @@ func (extension *testExtension) UpdateStructDescriptor(structDescriptor *StructD
|
|||||||
if structDescriptor.Type.String() != "jsoniter.TestObject1" {
|
if structDescriptor.Type.String() != "jsoniter.TestObject1" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
binding := structDescriptor.GetField("field1")
|
binding := structDescriptor.GetField("Field1")
|
||||||
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *Stream) {
|
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
str := *((*string)(ptr))
|
str := *((*string)(ptr))
|
||||||
val, _ := strconv.Atoi(str)
|
val, _ := strconv.Atoi(str)
|
||||||
@ -112,7 +113,7 @@ func Test_customize_field_by_extension(t *testing.T) {
|
|||||||
obj := TestObject1{}
|
obj := TestObject1{}
|
||||||
err := UnmarshalFromString(`{"field-1": 100}`, &obj)
|
err := UnmarshalFromString(`{"field-1": 100}`, &obj)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal("100", obj.field1)
|
should.Equal("100", obj.Field1)
|
||||||
str, err := MarshalToString(obj)
|
str, err := MarshalToString(obj)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"field-1":100}`, str)
|
should.Equal(`{"field-1":100}`, str)
|
||||||
|
52
jsoniter_struct_encoder_test.go
Normal file
52
jsoniter_struct_encoder_test.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_encode_unexported_field(t *testing.T) {
|
||||||
|
type TestData struct {
|
||||||
|
a int
|
||||||
|
b <-chan int
|
||||||
|
C int
|
||||||
|
d *time.Timer
|
||||||
|
}
|
||||||
|
|
||||||
|
should := require.New(t)
|
||||||
|
|
||||||
|
testChan := make(<-chan int, 10)
|
||||||
|
testTimer := time.NewTimer(10 * time.Second)
|
||||||
|
|
||||||
|
obj := &TestData{
|
||||||
|
a: 42,
|
||||||
|
b: testChan,
|
||||||
|
C: 21,
|
||||||
|
d: testTimer,
|
||||||
|
}
|
||||||
|
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
should.NoError(err)
|
||||||
|
should.Equal([]byte(`{"C":21}`), jb)
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(`{"a": 444, "b":"bad", "C":55, "d":{"not": "a timer"}}`), obj)
|
||||||
|
should.NoError(err)
|
||||||
|
should.Equal(42, obj.a)
|
||||||
|
should.Equal(testChan, obj.b)
|
||||||
|
should.Equal(55, obj.C)
|
||||||
|
should.Equal(testTimer, obj.d)
|
||||||
|
|
||||||
|
jb, err = Marshal(obj)
|
||||||
|
should.NoError(err)
|
||||||
|
should.Equal(jb, []byte(`{"C":55}`))
|
||||||
|
|
||||||
|
err = Unmarshal([]byte(`{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`), obj)
|
||||||
|
should.NoError(err)
|
||||||
|
should.Equal(42, obj.a)
|
||||||
|
should.Equal(testChan, obj.b)
|
||||||
|
should.Equal(256, obj.C)
|
||||||
|
should.Equal(testTimer, obj.d)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user