modify:add stringToBytes

Former-commit-id: 471bb65ed19ca1fa07f77575e9f11bf0e6fbbec8
This commit is contained in:
bestgopher 2020-04-27 22:25:53 +08:00
parent d1f18501e8
commit 4e68a382b7

View File

@ -3501,7 +3501,7 @@ func WriteJSON(writer io.Writer, v interface{}, options JSON, optimize bool) (in
}
if prefix := options.Prefix; prefix != "" {
result = append([]byte(prefix), result...)
result = append(stringToBytes(prefix), result...)
}
return writer.Write(result)
@ -3512,6 +3512,10 @@ func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func stringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
// DefaultJSONOptions is the optional settings that are being used
// inside `ctx.JSON`.
var DefaultJSONOptions = JSON{}
@ -3564,7 +3568,7 @@ var finishCallbackB = []byte(");")
// WriteJSONP marshals the given interface object and writes the JSON response to the writer.
func WriteJSONP(writer io.Writer, v interface{}, options JSONP, optimize bool) (int, error) {
if callback := options.Callback; callback != "" {
n, err := writer.Write([]byte(callback + "("))
n, err := writer.Write(stringToBytes(callback + "("))
if err != nil {
return n, err
}
@ -3671,7 +3675,7 @@ func (m xmlMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// WriteXML marshals the given interface object and writes the XML response to the writer.
func WriteXML(writer io.Writer, v interface{}, options XML, optimize bool) (int, error) {
if prefix := options.Prefix; prefix != "" {
n, err := writer.Write([]byte(prefix))
n, err := writer.Write(stringToBytes(prefix))
if err != nil {
return n, err
}