Skip to content

Commit

Permalink
fix serialize special string, for issue #1025
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 21, 2022
1 parent e7928a0 commit 71b7df7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,7 @@ public void writeString(String str) {
return;
}

if (escapeNoneAscii || browserSecure) {
ensureCapacity(off + strlen * 6 + 2);
} else {
ensureCapacity(off + strlen * 2 + 2);
}
ensureCapacity(off + strlen * 6 + 2);

chars[off++] = quote;
for (int i = 0; i < strlen; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ public void writeString(String str) {
return;
}

if (escapeNoneAscii) {
ensureCapacity(off + strlen * 6 + 2);
} else {
ensureCapacity(off + strlen * 2 + 2);
}
ensureCapacity(off + strlen * 6 + 2);
chars[off++] = quote;
for (int i = 0; i < strlen; ++i) {
char ch = value[i];
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@ public void writeString(String str) {
}
}

int rest = chars.length - i;
minCapacity = off + rest * 6 + 2;
if (minCapacity - this.bytes.length > 0) {
int oldCapacity = this.bytes.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
if (newCapacity - maxArraySize > 0) {
throw new OutOfMemoryError();
}

// minCapacity is usually close to size, so this is a win:
this.bytes = Arrays.copyOf(this.bytes, newCapacity);
}

for (; i < chars.length; ++i) { // ascii none special fast write
char ch = chars[i];
if ((ch >= 0x0000) && (ch <= 0x007F)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.fastjson2.issues_1000;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1025 {
@Test
public void test2000() throws Exception {
byte[] bytes = new byte[8000];
Arrays.fill(bytes, (byte) 'A');
for (int i = 0; i < 2000; i++) {
bytes[i] = 0x01;
}

String str = new String(bytes, "UTF-8");
String json = JSON.toJSONString(str);
assertEquals(str, JSON.parse(json));

byte[] jsonBytes = JSON.toJSONBytes(str);
assertEquals(str, JSON.parse(jsonBytes));
}

@Test
public void test4000() throws Exception {
byte[] bytes = new byte[8000];
Arrays.fill(bytes, (byte) 'A');
for (int i = 1000; i < 5000; i++) {
bytes[i] = 0x01;
}

String str = new String(bytes, "UTF-8");
String json = JSON.toJSONString(str);
assertEquals(str, JSON.parse(json));

byte[] jsonBytes = JSON.toJSONBytes(str);
assertEquals(str, JSON.parse(jsonBytes));
}
}

0 comments on commit 71b7df7

Please sign in to comment.