diff --git a/context/context.go b/context/context.go index e3101c5d..18e57cf5 100644 --- a/context/context.go +++ b/context/context.go @@ -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 }