Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: encoding/json: add a new method to escape html directly into structs #24409

Closed
johnbalvin opened this issue Mar 15, 2018 · 2 comments
Closed

Comments

@johnbalvin
Copy link
Contributor

johnbalvin commented 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)

@gopherbot gopherbot added this to the Proposal milestone Mar 15, 2018
@johnbalvin
Copy link
Contributor Author

Sorry for repeating the proposal, my internet is so slow that I thought that the proposal didn´t arrive

@dsnet 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
@dsnet
Copy link
Member

dsnet commented Mar 15, 2018

Closing as duplicate of #24408

@dsnet dsnet closed this as completed Mar 15, 2018
@golang golang locked and limited conversation to collaborators Mar 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants