diff --git a/feature_stream_string.go b/feature_stream_string.go index 8655f93..334282f 100644 --- a/feature_stream_string.go +++ b/feature_stream_string.go @@ -286,8 +286,11 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen } c, size := utf8.DecodeRuneInString(s[i:]) if c == utf8.RuneError && size == 1 { + if start < i { + stream.WriteRaw(s[start:i]) + } + stream.WriteRaw(`\ufffd`) i++ - stream.WriteRaw(s[start:i]) start = i continue } diff --git a/jsoniter_invalid_test.go b/jsoniter_invalid_test.go index ba864b4..338f4db 100644 --- a/jsoniter_invalid_test.go +++ b/jsoniter_invalid_test.go @@ -122,8 +122,11 @@ func Test_invalid_number(t *testing.T) { obj := Message{} decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`)) err := decoder.Decode(&obj) - result, err := ConfigCompatibleWithStandardLibrary.Marshal(err.Error()) + invalidStr := err.Error() + result, err := ConfigCompatibleWithStandardLibrary.Marshal(invalidStr) should := require.New(t) should.Nil(err) - should.Contains(string(result), "\xff") + result2, err := json.Marshal(invalidStr) + should.Nil(err) + should.Equal(string(result2), string(result)) }