mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: coerce invalid UTF-8 to valid UTF-8 during Marshal
In practice, rejecting an entire structure due to a single invalid byte in a string is just too picky, and too hard to track down. Be consistent with the bulk of the standard library by converting invalid UTF-8 into UTF-8 with replacement runes. R=golang-dev, crawshaw CC=golang-dev https://golang.org/cl/11211045
This commit is contained in:
parent
cfefe6a763
commit
64054a40ad
3 changed files with 17 additions and 11 deletions
|
|
@ -393,15 +393,10 @@ func TestMarshal(t *testing.T) {
|
|||
|
||||
func TestMarshalBadUTF8(t *testing.T) {
|
||||
s := "hello\xffworld"
|
||||
const enc = `"hello\ufffdworld"`
|
||||
b, err := Marshal(s)
|
||||
if err == nil {
|
||||
t.Fatal("Marshal bad UTF8: no error")
|
||||
}
|
||||
if len(b) != 0 {
|
||||
t.Fatal("Marshal returned data")
|
||||
}
|
||||
if _, ok := err.(*InvalidUTF8Error); !ok {
|
||||
t.Fatalf("Marshal did not return InvalidUTF8Error: %T %v", err, err)
|
||||
if string(b) != enc || err != nil {
|
||||
t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", s, b, err, enc)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue