Merge pull request #1496 from bestgopher/update

modify:add stringToBytes
Former-commit-id: fc34eb51edc1460f1e44107902698aa9f89c8d42
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-04-27 17:35:44 +03:00 committed by GitHub
commit b02706f207

View File

@ -3501,7 +3501,7 @@ func WriteJSON(writer io.Writer, v interface{}, options JSON, optimize bool) (in
} }
if prefix := options.Prefix; prefix != "" { if prefix := options.Prefix; prefix != "" {
result = append([]byte(prefix), result...) result = append(stringToBytes(prefix), result...)
} }
return writer.Write(result) return writer.Write(result)
@ -3512,6 +3512,10 @@ func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b)) return *(*string)(unsafe.Pointer(&b))
} }
func stringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
// DefaultJSONOptions is the optional settings that are being used // DefaultJSONOptions is the optional settings that are being used
// inside `ctx.JSON`. // inside `ctx.JSON`.
var DefaultJSONOptions = 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. // 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) { func WriteJSONP(writer io.Writer, v interface{}, options JSONP, optimize bool) (int, error) {
if callback := options.Callback; callback != "" { if callback := options.Callback; callback != "" {
n, err := writer.Write([]byte(callback + "(")) n, err := writer.Write(stringToBytes(callback + "("))
if err != nil { if err != nil {
return n, err 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. // 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) { func WriteXML(writer io.Writer, v interface{}, options XML, optimize bool) (int, error) {
if prefix := options.Prefix; prefix != "" { if prefix := options.Prefix; prefix != "" {
n, err := writer.Write([]byte(prefix)) n, err := writer.Write(stringToBytes(prefix))
if err != nil { if err != nil {
return n, err return n, err
} }