mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
add more tests
This commit is contained in:
parent
101dfdbb2a
commit
c78023531e
@ -52,8 +52,19 @@ type generalStructDecoder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *generalStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *generalStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
if !iter.readObjectStart() {
|
||||||
fieldDecoder := decoder.fields[field]
|
return
|
||||||
|
}
|
||||||
|
field := iter.readObjectField()
|
||||||
|
fieldDecoder := decoder.fields[field]
|
||||||
|
if fieldDecoder == nil {
|
||||||
|
iter.Skip()
|
||||||
|
} else {
|
||||||
|
fieldDecoder.decode(ptr, iter)
|
||||||
|
}
|
||||||
|
for iter.nextToken() == ',' {
|
||||||
|
field = iter.readObjectField()
|
||||||
|
fieldDecoder = decoder.fields[field]
|
||||||
if fieldDecoder == nil {
|
if fieldDecoder == nil {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
} else {
|
} else {
|
||||||
|
@ -68,41 +68,36 @@ func Test_reflect_four_fields_struct(t *testing.T) {
|
|||||||
should.Equal("d", obj.field4)
|
should.Equal("d", obj.field4)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_struct_string(t *testing.T) {
|
func Test_reflect_five_fields_struct(t *testing.T) {
|
||||||
type StructOfString struct {
|
should := require.New(t)
|
||||||
|
type TestObject struct {
|
||||||
field1 string
|
field1 string
|
||||||
field2 string
|
field2 string
|
||||||
|
field3 string
|
||||||
|
field4 string
|
||||||
|
field5 string
|
||||||
}
|
}
|
||||||
iter := ParseString(`{"field1": "hello", "field2": "world"}`)
|
obj := TestObject{}
|
||||||
Struct := StructOfString{}
|
should.Nil(UnmarshalString(`{}`, &obj))
|
||||||
iter.Read(&Struct)
|
should.Equal("", obj.field1)
|
||||||
if Struct.field1 != "hello" {
|
should.Nil(UnmarshalString(`{"field1": "a", "field2": "b", "field3": "c", "field4": "d", "field5": "e"}`, &obj))
|
||||||
fmt.Println(iter.Error)
|
should.Equal("a", obj.field1)
|
||||||
t.Fatal(Struct.field1)
|
should.Equal("b", obj.field2)
|
||||||
}
|
should.Equal("c", obj.field3)
|
||||||
if Struct.field2 != "world" {
|
should.Equal("d", obj.field4)
|
||||||
fmt.Println(iter.Error)
|
should.Equal("e", obj.field5)
|
||||||
t.Fatal(Struct.field2)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructOfStringPtr struct {
|
func Test_struct_with_optional_field(t *testing.T) {
|
||||||
field1 *string
|
should := require.New(t)
|
||||||
field2 *string
|
type TestObject struct {
|
||||||
}
|
field1 *string
|
||||||
|
field2 *string
|
||||||
func Test_reflect_struct_string_ptr(t *testing.T) {
|
|
||||||
iter := ParseString(`{"field1": null, "field2": "world"}`)
|
|
||||||
Struct := StructOfStringPtr{}
|
|
||||||
iter.Read(&Struct)
|
|
||||||
if Struct.field1 != nil {
|
|
||||||
fmt.Println(iter.Error)
|
|
||||||
t.Fatal(Struct.field1)
|
|
||||||
}
|
|
||||||
if *Struct.field2 != "world" {
|
|
||||||
fmt.Println(iter.Error)
|
|
||||||
t.Fatal(Struct.field2)
|
|
||||||
}
|
}
|
||||||
|
obj := TestObject{}
|
||||||
|
UnmarshalString(`{"field1": null, "field2": "world"}`, &obj)
|
||||||
|
should.Nil(obj.field1)
|
||||||
|
should.Equal("world", *obj.field2)
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructOfTag struct {
|
type StructOfTag struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user