no longer use unsafe package for string/bytes conversion

This commit is contained in:
daguang
2022-04-19 17:06:27 +08:00
parent fd92fd2409
commit 308fdccf65
32 changed files with 103 additions and 96 deletions

View File

@ -8,8 +8,9 @@ package gtype
import (
"bytes"
"github.com/gogf/gf/util/gconv"
"sync/atomic"
"github.com/gogf/gf/util/gconv"
)
// String is a struct for concurrent-safe operation for type string.
@ -55,12 +56,12 @@ func (v *String) String() string {
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *String) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(`"` + v.Val() + `"`), nil
return []byte(`"` + v.Val() + `"`), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *String) UnmarshalJSON(b []byte) error {
v.Set(gconv.UnsafeBytesToStr(bytes.Trim(b, `"`)))
v.Set(string(bytes.Trim(b, `"`)))
return nil
}