mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
reflect string
This commit is contained in:
parent
ce5b193569
commit
18a2587df6
@ -71,18 +71,18 @@ type TestObj struct {
|
||||
|
||||
func Benchmark_jsoniter_object(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
||||
obj := TestObj{}
|
||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||
switch field {
|
||||
case "field1":
|
||||
obj.Field1 = iter.ReadString()
|
||||
case "field2":
|
||||
obj.Field2 = iter.ReadUint64()
|
||||
default:
|
||||
iter.ReportError("bind object", "unexpected field")
|
||||
}
|
||||
}
|
||||
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
||||
obj := TestObj{}
|
||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||
switch field {
|
||||
case "field1":
|
||||
obj.Field1 = iter.ReadString()
|
||||
case "field2":
|
||||
obj.Field2 = iter.ReadUint64()
|
||||
default:
|
||||
iter.ReportError("bind object", "unexpected field")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
53
jsoniter_reflect.go
Normal file
53
jsoniter_reflect.go
Normal file
@ -0,0 +1,53 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Decoder interface {
|
||||
decode(iter *Iterator, obj interface{})
|
||||
}
|
||||
|
||||
type stringDecoder struct {
|
||||
}
|
||||
|
||||
func (decoder *stringDecoder) decode(iter *Iterator, obj interface{}) {
|
||||
ptr := obj.(*string)
|
||||
*ptr = iter.ReadString()
|
||||
}
|
||||
|
||||
var DECODER_STRING *stringDecoder
|
||||
|
||||
func init() {
|
||||
DECODER_STRING = &stringDecoder{}
|
||||
}
|
||||
|
||||
func (iter *Iterator) Read(obj interface{}) {
|
||||
type_ := reflect.TypeOf(obj)
|
||||
decoder, err := decoderOfType(type_)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return
|
||||
}
|
||||
decoder.decode(iter, obj)
|
||||
}
|
||||
|
||||
func decoderOfType(type_ reflect.Type) (Decoder, error) {
|
||||
switch type_.Kind() {
|
||||
case reflect.Ptr:
|
||||
return decoderOfPtr(type_.Elem())
|
||||
default:
|
||||
return nil, errors.New("expect ptr")
|
||||
}
|
||||
}
|
||||
|
||||
func decoderOfPtr(type_ reflect.Type) (Decoder, error) {
|
||||
switch type_.Kind() {
|
||||
case reflect.String:
|
||||
return DECODER_STRING, nil
|
||||
default:
|
||||
return nil, errors.New("expect string")
|
||||
}
|
||||
}
|
||||
|
14
jsoniter_reflect_test.go
Normal file
14
jsoniter_reflect_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_reflect_str(t *testing.T) {
|
||||
iter := ParseString(`"hello"`)
|
||||
str := ""
|
||||
iter.Read(&str)
|
||||
if str != "hello" {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
@ -76,13 +76,13 @@ func Test_skip_empty_object(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_nested(t *testing.T) {
|
||||
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
if iter.ReadString() != "b" {
|
||||
t.FailNow()
|
||||
}
|
||||
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
if iter.ReadString() != "b" {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
type TestResp struct {
|
||||
|
Loading…
Reference in New Issue
Block a user