e47a147c by astaxie

serverJson Supoort 中文编码

1 parent 4ecb9cc3
...@@ -214,13 +214,16 @@ func (c *Controller) Abort(code string) { ...@@ -214,13 +214,16 @@ func (c *Controller) Abort(code string) {
214 panic(code) 214 panic(code)
215 } 215 }
216 216
217 func (c *Controller) ServeJson() { 217 func (c *Controller) ServeJson(encoding ...bool) {
218 content, err := json.MarshalIndent(c.Data["json"], "", " ") 218 content, err := json.MarshalIndent(c.Data["json"], "", " ")
219 if err != nil { 219 if err != nil {
220 http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) 220 http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
221 return 221 return
222 } 222 }
223 c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8") 223 c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8")
224 if len(encoding) > 0 && encoding[0] == true {
225 content = []byte(stringsToJson(string(content)))
226 }
224 c.writeToWriter(content) 227 c.writeToWriter(content)
225 } 228 }
226 229
......
...@@ -232,3 +232,17 @@ func ParseForm(form url.Values, obj interface{}) error { ...@@ -232,3 +232,17 @@ func ParseForm(form url.Values, obj interface{}) error {
232 } 232 }
233 return nil 233 return nil
234 } 234 }
235
236 func stringsToJson(str string) string {
237 rs := []rune(str)
238 jsons := ""
239 for _, r := range rs {
240 rint := int(r)
241 if rint < 128 {
242 jsons += string(r)
243 } else {
244 jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
245 }
246 }
247 return jsons
248 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!