Skip to content

Commit 36b1496

Browse files
committed
#153 fix invalid utf8 using same implementation as the standard library
1 parent f706335 commit 36b1496

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

feature_stream_string.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,11 @@ func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen
286286
}
287287
c, size := utf8.DecodeRuneInString(s[i:])
288288
if c == utf8.RuneError && size == 1 {
289+
if start < i {
290+
stream.WriteRaw(s[start:i])
291+
}
292+
stream.WriteRaw(`\ufffd`)
289293
i++
290-
stream.WriteRaw(s[start:i])
291294
start = i
292295
continue
293296
}

jsoniter_invalid_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ func Test_invalid_number(t *testing.T) {
122122
obj := Message{}
123123
decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
124124
err := decoder.Decode(&obj)
125-
result, err := ConfigCompatibleWithStandardLibrary.Marshal(err.Error())
125+
invalidStr := err.Error()
126+
result, err := ConfigCompatibleWithStandardLibrary.Marshal(invalidStr)
126127
should := require.New(t)
127128
should.Nil(err)
128-
should.Contains(string(result), "\xff")
129+
result2, err := json.Marshal(invalidStr)
130+
should.Nil(err)
131+
should.Equal(string(result2), string(result))
129132
}

0 commit comments

Comments
 (0)