proposal: encoding/json: add a new method to escape html directly into structs #24409
Labels
Milestone
Comments
Sorry for repeating the proposal, my internet is so slow that I thought that the proposal didn´t arrive |
Closing as duplicate of #24408 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
What did you do?
I wanted to decode json with its fields being html scaped
`import (
"html/template"
"encoding/json"
"net/http"
"io"
"io/ioutil"
"log"
)
func anyFunction(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print(err)
}
ri, wo := io.Pipe()
go template.HTMLEscape(wo, body)
var t []customStruct
json.NewDecoder(ri).Decode(t) //error: Invalid character:'&' looking for beginning of object key string
...
}`
What did you expect to see?
CustumStruct to be filled with the escaped html
What did you see instead?
error: Invalid character:'&' looking for beginning of object key string
###More information
I asked on stackoverlow: https://stackoverflow.com/questions/49288607/scape-html-from-json-post-request-in-go but there is not actually an efficient solution to this issue
As far as I understand, with the tools provided right now there are 2 razonable solutions:
1-Creating a method for each struct (customMarshalJSON) as https://golang.org/pkg/encoding/json/ says so
2.-Escaping the html after unmarshal
Both of them solve the issue, but not in an efficient way because it makes to repeat the same code over and over again
Wouldn´t be more efficient escaping the html while decoding? like having a method like this:
json.UnmarshalHTMLEscape(data, &struct)
The text was updated successfully, but these errors were encountered: