1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

examples: write-rest: json: usage of the Secure, ASCII and UnescapeHTML fields

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-01 13:28:21 +03:00
parent c7157f1c92
commit fec6783a22
3 changed files with 58 additions and 6 deletions

View File

@@ -3097,16 +3097,27 @@ func WriteJSON(writer io.Writer, v interface{}, options JSON, optimize bool) (in
return 0, err
}
prependSecure := false
if options.Secure {
if bytes.HasPrefix(result, jsonArrayPrefix) {
if options.Indent == "" {
prependSecure = bytes.HasSuffix(result, jsonArraySuffix)
} else {
prependSecure = bytes.HasSuffix(bytes.TrimRightFunc(result, func(r rune) bool {
return r == '\n' || r == '\r'
}), jsonArraySuffix)
}
}
}
if options.UnescapeHTML {
result = bytes.Replace(result, ltHex, lt, -1)
result = bytes.Replace(result, gtHex, gt, -1)
result = bytes.Replace(result, andHex, and, -1)
}
if options.Secure {
if bytes.HasPrefix(result, jsonArrayPrefix) && bytes.HasSuffix(result, jsonArraySuffix) {
result = append(secureJSONPrefix, result...)
}
if prependSecure {
result = append(secureJSONPrefix, result...)
}
if options.ASCII {