mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +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) {
|
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
|
||||||
buf, err := cfg.Marshal(v)
|
stream := cfg.borrowStream()
|
||||||
if err != nil {
|
defer cfg.returnStream(stream)
|
||||||
return "", err
|
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) {
|
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
||||||
@ -163,7 +165,10 @@ func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
|||||||
if stream.Error != nil {
|
if stream.Error != nil {
|
||||||
return nil, stream.Error
|
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 {
|
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…
x
Reference in New Issue
Block a user