mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
#57 copy bytes
This commit is contained in:
parent
17bd91fd71
commit
952a42af6c
@ -149,11 +149,13 @@ func (cfg *frozenConfig) CleanEncoders() {
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
|
||||
buf, err := cfg.Marshal(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
stream := cfg.borrowStream()
|
||||
defer cfg.returnStream(stream)
|
||||
stream.WriteVal(v)
|
||||
if stream.Error != nil {
|
||||
return nil, stream.Error
|
||||
}
|
||||
return string(buf), nil
|
||||
return string(stream.Buffer()), nil
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
||||
@ -163,7 +165,10 @@ func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
||||
if stream.Error != nil {
|
||||
return nil, stream.Error
|
||||
}
|
||||
return stream.Buffer(), nil
|
||||
result := stream.Buffer()
|
||||
copied := make([]byte, len(result))
|
||||
copy(copied, result)
|
||||
return copied, nil
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||
|
39
feature_pool.go
Normal file
39
feature_pool.go
Normal file
@ -0,0 +1,39 @@
|
||||
package jsoniter
|
||||
|
||||
func (cfg *frozenConfig) borrowStream() *Stream {
|
||||
select {
|
||||
case stream := <-cfg.streamPool:
|
||||
stream.Reset(nil)
|
||||
return stream
|
||||
default:
|
||||
return NewStream(cfg, nil, 512)
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) returnStream(stream *Stream) {
|
||||
select {
|
||||
case cfg.streamPool <- stream:
|
||||
return
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) borrowIterator(data []byte) *Iterator {
|
||||
select {
|
||||
case iter := <- cfg.iteratorPool:
|
||||
iter.ResetBytes(data)
|
||||
return iter
|
||||
default:
|
||||
return ParseBytes(cfg, data)
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) returnIterator(iter *Iterator) {
|
||||
select {
|
||||
case cfg.iteratorPool <- iter:
|
||||
return
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user