diff --git a/java/src/json/ext/SWARBasicStringEncoder.java b/java/src/json/ext/SWARBasicStringEncoder.java index 02a3bfcd..a6695d99 100644 --- a/java/src/json/ext/SWARBasicStringEncoder.java +++ b/java/src/json/ext/SWARBasicStringEncoder.java @@ -27,9 +27,10 @@ void encode(ByteList src) throws IOException { // slice a string, the underlying byte array is the same, but the // begin index and real size are different. When reading from the ptrBytes // array, we need to always add ptr to the index. - ByteBuffer bb = ByteBuffer.wrap(ptrBytes, ptr, len); + ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, ptr + len); + while (pos + 8 <= len) { - long x = bb.getLong(pos); + long x = bb.getLong(ptr + pos); if (skipChunk(x)) { pos += 8; continue; @@ -48,7 +49,7 @@ void encode(ByteList src) throws IOException { } if (pos + 4 <= len) { - int x = bb.getInt(pos); + int x = bb.getInt(ptr + pos); if (skipChunk(x)) { pos += 4; } diff --git a/test/json/json_encoding_test.rb b/test/json/json_encoding_test.rb index 8dce81da..2789e94b 100644 --- a/test/json/json_encoding_test.rb +++ b/test/json/json_encoding_test.rb @@ -35,6 +35,8 @@ def test_generate_shared_string # Ref: https://github.com/ruby/json/issues/859 s = "01234567890" assert_equal '"234567890"', JSON.dump(s[2..-1]) + s = '01234567890123456789"a"b"c"d"e"f"g"h' + assert_equal '"\"a\"b\"c\"d\"e\"f\"g\""', JSON.dump(s[20, 15]) end def test_unicode