S
S
SADFGHJAETJER2022-03-11 00:06:27
go
SADFGHJAETJER, 2022-03-11 00:06:27

How to escape string to insert into JSON string value?

I don’t work with Go so often, I can’t understand how can I use the built-in features of the language, without regular expressions, to convert a string so that it can be safely inserted as a JSON string? I can't google a function that escapes quotes and that's it.

You need it to implement a custom GQL scalar type:

// MarshalGQL implements the graphql.Marshaler interface
func (u Username) MarshalGQL(w io.Writer) {
  _, err := w.Write([]byte(fmt.Sprintf(`"%s"`, u)))
  if err != nil {
    return
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2022-03-11
@SADFGHJAETJER

https://golang.cafe/blog/golang-json-marshal-examp...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question