mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
#153 fix invalid utf8 using same implementation as the standard library
This commit is contained in:
parent
f706335302
commit
36b14963da
@ -286,8 +286,11 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen
|
|||||||
}
|
}
|
||||||
c, size := utf8.DecodeRuneInString(s[i:])
|
c, size := utf8.DecodeRuneInString(s[i:])
|
||||||
if c == utf8.RuneError && size == 1 {
|
if c == utf8.RuneError && size == 1 {
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
stream.WriteRaw(`\ufffd`)
|
||||||
i++
|
i++
|
||||||
stream.WriteRaw(s[start:i])
|
|
||||||
start = i
|
start = i
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -122,8 +122,11 @@ func Test_invalid_number(t *testing.T) {
|
|||||||
obj := Message{}
|
obj := Message{}
|
||||||
decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
|
decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
|
||||||
err := decoder.Decode(&obj)
|
err := decoder.Decode(&obj)
|
||||||
result, err := ConfigCompatibleWithStandardLibrary.Marshal(err.Error())
|
invalidStr := err.Error()
|
||||||
|
result, err := ConfigCompatibleWithStandardLibrary.Marshal(invalidStr)
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Contains(string(result), "\xff")
|
result2, err := json.Marshal(invalidStr)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(string(result2), string(result))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user