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

encoding/json: trouble with Marshal of Chinese word on Windows #3005

Closed
gopherbot opened this issue Feb 13, 2012 · 10 comments

Comments

Projects
None yet
3 participants
@gopherbot
Copy link

commented Feb 13, 2012

by yunge.fu:

What steps will reproduce the problem?

package main

import (
    "fmt"
    "encoding/json"
)

type Resp struct {
    Errno uint
    Error string
    Data interface{}
}

func main() {
    resp := Resp{
        Errno : 0,
        Error : "错误测试!", //some chinese word.
        Data : nil,
    }
    
    bs, err := json.Marshal(resp)
    if err != nil { panic(err) }
    
    fmt.Println(string(bs), len(bs))
}


What is the expected output?
{"Errno":0,"Error":"错误测试!","Data":null}

What do you see instead?
Marshal failed, even no error return.

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
The issue is just happened in windows xp chinese version(8g), linux(6g) is working well.

Please provide any additional information below.
Chinese version windows xp use gbk as default charset, not utf8.

The real problem is no error return(var err returned is nil), so people would hard to
debug.
@gopherbot

This comment has been minimized.

Copy link
Author

commented Feb 13, 2012

Comment 1 by hongruiqi:

What does len(bs) report? I guess that the marshal isn't failed, so no error return(the
file is save in utf8, right?). But the utf8 words can't be print on windows, try strconv
or others?
@gopherbot

This comment has been minimized.

Copy link
Author

commented Feb 13, 2012

Comment 2 by hongruiqi:

Tested in my winxp, only chinese field are messy. So it's not the problem of json
package. 
A typo fix, the strconv above is actually go-iconv..
@gopherbot

This comment has been minimized.

Copy link
Author

commented Feb 13, 2012

Comment 3 by hongruiqi:

Tested in my winxp, only chinese field is messy. So it's not the problem of json
package. 
A typo fix, the strconv above is actually go-iconv..
@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 13, 2012

Comment 4:

Please attach the program that is failing to this issue.
Attaching the file should let us check whether this is
an encoding issue that is being hidden when you paste directly
into the text box.
Thanks.
Russ
@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 14, 2012

Comment 5:

Labels changed: added priority-later, removed priority-triage.

Status changed to WaitingForReply.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Feb 16, 2012

Comment 6 by yunge.fu:

Hi hongruiqi/Russ,
First, the len(bs) is 49, and the file is saved as utf-8 file.
I attached the source file and the printscreen img, it shows that there missed a double
quote just before `,"data"`, pls check it, thanks.
Sorry for late reply!

Attachments:

  1. test.go (440 bytes)
  2. issue3005.png (4878 bytes)
@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 16, 2012

Comment 7:

Thanks for the test case.  I think this is working correctly and
that your terminal is getting confused by the full-width
exclamation mark followed by an ASCII quotation mark.
Note that in the screen shot it says ? where the full-width
exclamation mark and " should be.
$ cat x.go
package main
import (
    "encoding/json"
    "fmt"
    "strconv"
)
type Resp struct {
    Errno uint
    Error string
    Data  interface{}
}
func main() {
    resp := Resp{
        Errno: 0,
        Error: "\u9519\u8bef\u6d4b\u8bd5\uff01", //some chinese word.
        Data:  nil,
    }
    bs, err := json.Marshal(resp)
    if err != nil {
        panic(err)
    }
    fmt.Printf("JSON: %s\n", bs)
    fmt.Printf("Error: %q\n", resp.Error)
    fmt.Printf("%q\n", bs)
    fmt.Printf("\n")
    fmt.Printf("Error: %s\n", strconv.QuoteToASCII(resp.Error))
    fmt.Printf("%s\n", strconv.QuoteToASCII(string(bs)))
}
$ go run x.go
JSON: {"Errno":0,"Error":"错误测试!","Data":null}
Error: "错误测试!"
"{\"Errno\":0,\"Error\":\"错误测试!\",\"Data\":null}"
Error: "\u9519\u8bef\u6d4b\u8bd5\uff01"
"{\"Errno\":0,\"Error\":\"\u9519\u8bef\u6d4b\u8bd5\uff01\",\"Data\":null}"
$
@gopherbot

This comment has been minimized.

Copy link
Author

commented Feb 17, 2012

Comment 8 by yunge.fu:

Thanks, got it. Pls close the issue.
I just change dev os from centos into windows, sorry for the inconvenience.
Yunge
@rsc

This comment has been minimized.

Copy link
Contributor

commented Feb 17, 2012

Comment 9:

Status changed to Retraced.

@robpike

This comment has been minimized.

Copy link
Contributor

commented Apr 9, 2012

Comment 10:

Status changed to Retracted.

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.