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
encoding/json: how to marshal with unicode escape? #39137
Comments
Before we start talking about new API, we should first talk about why you need to do that in the first place. You shouldn't need to escape non-ASCII letters in json. |
@mvdan I have several program environments, go, python2, js runtime(a v8 app). they have different default character encoding, utf-8(go, python2). ucs2 (js runtime). It need to convert utf-8 to usc2 or usc2 to utf-8 when a json text transferred between them, it's a error-prone job. I think unicode escape is use for this case., it's a |
That seems like a very narrow edge case, and I don't think it should be the json package's job to support producing ASCII output alone. You could consider having a named string type that implements |
Yeah, I know your meaning. here is the e.g. : https://play.golang.org/p/YVSQzad2Z2r type UnicodeEscape string
func (ue UnicodeEscape) MarshalJSON() ([]byte, error) {
text := strconv.QuoteToASCII(string(ue))
return []byte(text), nil
}
type Object struct {
Name UnicodeEscape
} But I would to think the BTW: |
json spec says unicode https://tools.ietf.org/html/rfc7159#section-8.1 |
@seankhliao Yes, it says e.g.:
For a json text |
Maybe you could consider a |
I think the few lines of code shown in #39137 (comment) are a completely acceptable solution to this. The json API should only cover common issues and needs. Trying to avoid utf-8 altogether in favor of ascii with unicode escapes certainly feels like an edge case that we shouldn't cover, especially given how easy it is to do with MarshalJSON. |
ok, I'll do it by |
@cupen seems it doesn't work with emoji
|
strconv.QuoteToASCII produces a Go string that's not valid JSON: JSON needs a pair of UTF-16 code units: |
@Bogdaan Sorry for delay. func (ue UnicodeEscape) MarshalJSON() ([]byte, error) {
var result = []byte("\"")
for _, r := range []rune(ue) {
if r < 0x10000 {
v := "\\u" + strconv.FormatInt(int64(r), 16)
result = append(result, []byte(v)...)
continue
}
r1, r2 := utf16.EncodeRune(r)
v1 := "\\u" + strconv.FormatInt(int64(r1), 16)
v2 := "\\u" + strconv.FormatInt(int64(r2), 16)
result = append(append(result, []byte(v1)...), []byte(v2)...)
}
result = append(result, []byte("\"")...)
return result, nil
} https://play.golang.org/p/_435-VMzeY2 BTW: This is not very high efficiency, I'm just show you a case which is works. |
This is not working with emoji. Test with |
@pavelpatrin And this? #39137 (comment) |
I tested your example result with Chrome DevTools console and from IPython, and it looks fine with Emoji in both cases. Instead of the
|
@pavelpatrin Yes. That's right. |
@cupen i tested it again and looks like it is not working with ascii-compartible characters.
You may replace escape formatting with |
Is there any way I can get a valid JSON string, i am also stuck with similar Problem for non printable Unicode characters |
Use a Rune Literal: https://golang.org/ref/spec#Rune_literals
|
I also stuck with similar kind of Issue, Any help would be appreciated |
package main import ( type building struct { type encoded string func (e encoded) MarshalJSON() ([]byte, error) { func main() {
} How do we do in this case !!! |
Maybe this helps others: https://go.dev/play/p/eJfouGxeEzs |
What version of Go are you using (
go version
)?What did you expect to see?
module
json
add a API for marshal string with unicode escape would be useful.unicode escape
https://tools.ietf.org/html/rfc7159#section-7
The text was updated successfully, but these errors were encountered: