-
go version: go1.12.6
-
gin version (or commit ref): 1.4.0
-
operating system: Mac + Linux
Description
Trying to use PureJSON as described in the example works fine, but when using it in my app to format a database (sqlx + sqlite) response, it will always use the default JSON behaviour of escaping the e.g. "<" in HTML as \u003c.
Working as expected:
c.PureJSON(200, gin.H{
"html": "<b>Hello, world!</b>",
})
Not working:
data := struct {
Thing model.Thing
}{
thing,
}
c.PureJSON(200, data)
Where thing is the scanned result of a sqlx db.Get
Comment From: ljluestc
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"html"
)
func main() {
// Initialize Gin
r := gin.Default()
r.GET("/test", func(c *gin.Context) {
// Simulate a database response
thing := struct {
HTMLContent string `json:"html_content"`
}{
HTMLContent: "<b>Hello, world!</b>",
}
// Convert struct to JSON manually without escaping special HTML characters
jsonData, err := json.Marshal(thing)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
// Send the response using PureJSON
// PureJSON bypasses the default escaping behavior in Gin
c.PureJSON(200, jsonData)
})
r.Run(":8080")
}