Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
(w http.ResponseWriter)
| 153 | |
| 154 | // Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType. |
| 155 | func (r AsciiJSON) Render(w http.ResponseWriter) error { |
| 156 | r.WriteContentType(w) |
| 157 | ret, err := json.API.Marshal(r.Data) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | |
| 162 | var buffer bytes.Buffer |
| 163 | escapeBuf := make([]byte, 0, 6) // Preallocate 6 bytes for Unicode escape sequences |
| 164 | |
| 165 | for _, r := range bytesconv.BytesToString(ret) { |
| 166 | if r > unicode.MaxASCII { |
| 167 | escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x", r) // Reuse escapeBuf |
| 168 | buffer.Write(escapeBuf) |
| 169 | } else { |
| 170 | buffer.WriteByte(byte(r)) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | _, err = w.Write(buffer.Bytes()) |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | // WriteContentType (AsciiJSON) writes JSON ContentType. |
| 179 | func (r AsciiJSON) WriteContentType(w http.ResponseWriter) { |
nothing calls this directly
no test coverage detected