1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-17 11:26:35 +02:00

Merge pull request #562 from runzhi/master

fix: invalid unicode while binary to string
This commit is contained in:
Tao Wen 2021-07-22 09:00:26 +08:00 committed by GitHub
commit c6661824eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -180,9 +180,9 @@ func readHex(iter *jsoniter.Iterator, b1, b2 byte) byte {
}
ret *= 16
if b2 >= '0' && b2 <= '9' {
ret = b2 - '0'
ret += b2 - '0'
} else if b2 >= 'a' && b2 <= 'f' {
ret = b2 - 'a' + 10
ret += b2 - 'a' + 10
} else {
iter.ReportError("read hex", "expects 0~9 or a~f, but found "+string([]byte{b2}))
return 0

View File

@ -22,11 +22,11 @@ func TestBinaryAsStringCodec(t *testing.T) {
})
t.Run("non safe set", func(t *testing.T) {
should := require.New(t)
output, err := jsoniter.Marshal([]byte{1, 2, 3, 15})
output, err := jsoniter.Marshal([]byte{1, 2, 3, 23})
should.NoError(err)
should.Equal(`"\\x01\\x02\\x03\\x0f"`, string(output))
should.Equal(`"\\x01\\x02\\x03\\x17"`, string(output))
var val []byte
should.NoError(jsoniter.Unmarshal(output, &val))
should.Equal([]byte{1, 2, 3, 15}, val)
should.Equal([]byte{1, 2, 3, 23}, val)
})
}