1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-24 23:16:47 +02:00

#57 copy bytes

This commit is contained in:
Tao Wen
2017-06-17 14:36:05 +08:00
parent 17bd91fd71
commit 952a42af6c
2 changed files with 49 additions and 5 deletions

View File

@ -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 {