You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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:
Sorry for repeating the proposal, my internet is so slow that I thought that the proposal didn´t arrive
dsnet
changed the title
proposal:encoding/json:add a new method to escape html directly into structs
proposal: encoding/json: add a new method to escape html directly into structs
Mar 15, 2018
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: