-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
main.go
51 lines (41 loc) · 1.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"github.com/kataras/iris/v12"
// This is a 3rd-party library, which you can use to override the default behavior of ctx.JSON method.
"github.com/bytedance/sonic"
)
func init() {
applyIrisGlobalPatches() // <- IMPORTANT.
}
func applyIrisGlobalPatches() {
var json = sonic.ConfigFastest
// Apply global modifications to the context REST writers
// without modifications to your web server's handlers code.
iris.Patches().Context().Writers().JSON(func(ctx iris.Context, v interface{}, options *iris.JSON) error {
enc := json.NewEncoder(ctx.ResponseWriter())
enc.SetEscapeHTML(!options.UnescapeHTML)
enc.SetIndent("", options.Indent)
return enc.Encode(v)
})
}
// User example struct for json.
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
City string `json:"city"`
Age int `json:"age"`
}
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
user := User{
Firstname: "Gerasimos",
Lastname: "Maropoulos",
City: "Athens",
Age: 29,
}
// Use ctx.JSON as you used to.
ctx.JSON(user)
})
app.Listen(":8080")
}