-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add json ASCII string render #1358
Changes from 5 commits
f71e543
98acb01
7c8d633
e8f5cc0
5ac9a80
f1787f6
64d474b
551810c
fe1abc5
f299ac2
5748abf
9effe91
33eb8b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package render | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
|
||
|
@@ -30,10 +31,15 @@ type JsonpJSON struct { | |
Data interface{} | ||
} | ||
|
||
type AsciiJSON struct { | ||
Data interface{} | ||
} | ||
|
||
type SecureJSONPrefix string | ||
|
||
var jsonContentType = []string{"application/json; charset=utf-8"} | ||
var jsonpContentType = []string{"application/javascript; charset=utf-8"} | ||
var jsonAsciiContentType = []string{"application/json"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, needn't. it's ascii. |
||
|
||
func (r JSON) Render(w http.ResponseWriter) (err error) { | ||
if err = WriteJSON(w, r.Data); err != nil { | ||
|
@@ -112,3 +118,29 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) { | |
func (r JsonpJSON) WriteContentType(w http.ResponseWriter) { | ||
writeContentType(w, jsonpContentType) | ||
} | ||
|
||
func (r AsciiJSON) Render(w http.ResponseWriter) (err error) { | ||
r.WriteContentType(w) | ||
ret, err := json.Marshal(r.Data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result := "" | ||
for _, r := range string(ret) { | ||
cvt := "" | ||
if r < 128 { | ||
cvt = string(r) | ||
} else { | ||
cvt = fmt.Sprintf("\\u%04x", int64(r)) | ||
} | ||
result = result + cvt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
w.Write([]byte(result)) | ||
return nil | ||
} | ||
|
||
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) { | ||
writeContentType(w, jsonAsciiContentType) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change
204
tohttp
status code in HTTP package for maintainable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which status code should i use? you mean
http.StatusNoContent
or using other status codeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http.StatusNoContent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done