Skip to content

Commit

Permalink
speed up serialize & deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 7, 2023
1 parent e7f09f3 commit 48231de
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 39 deletions.
26 changes: 26 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -2715,6 +2715,32 @@ static byte[] toJSONBytes(Object object, String format, Filter[] filters, JSONWr
}
}

/**
* Serialize Java Object to JSON and write to {@link OutputStream}
*
* @param out {@link OutputStream} to be written
* @param object Java Object to be serialized into JSON
* @throws JSONException if an I/O error occurs. In particular, a {@link JSONException} may be thrown if the output stream has been closed
*/
static int writeTo(OutputStream out, Object object) {
try (JSONWriter writer = JSONWriter.ofUTF8()) {
if (object == null) {
writer.writeNull();
} else {
writer.rootObject = object;
writer.path = JSONWriter.Path.ROOT;

Class<?> valueClass = object.getClass();
ObjectWriter<?> objectWriter = writer.getObjectWriter(valueClass, valueClass);
objectWriter.write(writer, object, null, null, 0);
}

return writer.flushTo(out);
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}

/**
* Serialize Java Object to JSON and write to {@link OutputStream} with specified {@link JSONReader.Feature}s enabled
*
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,7 @@ public BigInteger readBigInteger() {
return getBigInteger();
}

public BigDecimal readBigDecimal() {
readNumber0();
return getBigDecimal();
}
public abstract BigDecimal readBigDecimal();

public abstract UUID readUUID();

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.*;
import java.util.*;
Expand Down Expand Up @@ -4623,6 +4624,11 @@ public final void readNull() {
}
}

public final BigDecimal readBigDecimal() {
readNumber0();
return getBigDecimal();
}

@Override
public final UUID readUUID() {
if (ch == 'n') {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -5105,6 +5106,11 @@ protected final LocalDateTime readLocalDateTimeX(int len) {
return ldt;
}

public final BigDecimal readBigDecimal() {
readNumber0();
return getBigDecimal();
}

@Override
public final UUID readUUID() {
if (ch == 'n') {
Expand Down
32 changes: 3 additions & 29 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -898,20 +898,7 @@ public void writeInt16(short value) {
writeInt32(value);
}

public void writeInt32(int[] value) {
if (value == null) {
writeNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeInt32(value[i]);
}
endArray();
}
public abstract void writeInt32(int[] value);

public abstract void writeInt32(int value);

Expand All @@ -921,20 +908,7 @@ public void writeMillis(long i) {
writeInt64(i);
}

public void writeInt64(long[] value) {
if (value == null) {
writeNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeInt64(value[i]);
}
endArray();
}
public abstract void writeInt64(long[] value);

public abstract void writeFloat(float value);

Expand Down Expand Up @@ -1208,7 +1182,7 @@ public final void writeString(Reader reader) {
}

public abstract void writeString(String str);
public abstract void writeStringLatin1(byte[] value);
protected abstract void writeStringLatin1(byte[] value);

public void writeString(List<String> list) {
startArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void writeString(char[] chars, int off, int len, boolean quote) {
writeString(new String(chars, off, len));
}

public void writeStringLatin1(final byte[] str) {
protected void writeStringLatin1(final byte[] str) {
if (str == null) {
writeStringNull();
return;
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONWriterPretty.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public void close() {
jsonWriter.close();
}

public void writeInt32(int[] value) {
jsonWriter.writeInt32(value);
}

@Override
public void writeInt32(int value) {
jsonWriter.writeInt32(value);
Expand All @@ -45,6 +49,10 @@ public void writeInt64(long i) {
jsonWriter.writeInt64(i);
}

public void writeInt64(long[] value) {
jsonWriter.writeInt64(value);
}

@Override
public void writeFloat(float value) {
jsonWriter.writeFloat(value);
Expand Down Expand Up @@ -76,7 +84,7 @@ public void writeString(String str) {
}

@Override
public void writeStringLatin1(byte[] str) {
protected void writeStringLatin1(byte[] str) {
jsonWriter.writeStringLatin1(str);
}

Expand Down
55 changes: 54 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public final void writeString(List<String> list) {
chars[off++] = ']';
}

public void writeStringLatin1(byte[] value) {
protected void writeStringLatin1(byte[] value) {
if (value == null) {
writeStringNull();
return;
Expand Down Expand Up @@ -1255,6 +1255,33 @@ final void ensureCapacity(int minCapacity) {
}
}

public final void writeInt32(int[] value) {
if (value == null) {
writeNull();
return;
}

if (off == chars.length) {
ensureCapacity(off + 1);
}
chars[off++] = '[';

for (int i = 0; i < value.length; i++) {
if (i != 0) {
if (off == chars.length) {
ensureCapacity(off + 1);
}
chars[off++] = ',';
}
writeInt32(value[i]);
}

if (off == chars.length) {
ensureCapacity(off + 1);
}
chars[off++] = ']';
}

@Override
public final void writeInt32(int i) {
boolean writeAsString = (context.features & Feature.WriteNonStringValueAsString.mask) != 0;
Expand Down Expand Up @@ -1343,6 +1370,32 @@ public final void writeInt32(int i) {
}
}

public final void writeInt64(long[] value) {
if (value == null) {
writeNull();
return;
}

if (off == chars.length) {
ensureCapacity(off + 1);
}
chars[off++] = '[';

for (int i = 0; i < value.length; i++) {
if (i != 0) {
if (off == chars.length) {
ensureCapacity(off + 1);
}
chars[off++] = ',';
}
writeInt64(value[i]);
}
if (off == chars.length) {
ensureCapacity(off + 1);
}
chars[off++] = (byte) ']';
}

@Override
public final void writeInt64(long i) {
boolean writeAsString = (context.features & (WriteNonStringValueAsString.mask | WriteLongAsString.mask)) != 0
Expand Down
55 changes: 54 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void writeString(String str) {
bytes[off++] = (byte) quote;
}

public void writeStringLatin1(byte[] value) {
protected void writeStringLatin1(byte[] value) {
if (value == null) {
writeStringNull();
return;
Expand Down Expand Up @@ -1548,6 +1548,33 @@ final void ensureCapacity(int minCapacity) {
}
}

public final void writeInt32(int[] value) {
if (value == null) {
writeNull();
return;
}

if (off == bytes.length) {
ensureCapacity(off + 1);
}
bytes[off++] = (byte) '[';

for (int i = 0; i < value.length; i++) {
if (i != 0) {
if (off == bytes.length) {
ensureCapacity(off + 1);
}
bytes[off++] = (byte) ',';
}
writeInt32(value[i]);
}

if (off == bytes.length) {
ensureCapacity(off + 1);
}
bytes[off++] = (byte) ']';
}

@Override
public final void writeInt32(int i) {
boolean writeAsString = (context.features & Feature.WriteNonStringValueAsString.mask) != 0;
Expand Down Expand Up @@ -1636,6 +1663,32 @@ public final void writeInt32(int i) {
}
}

public final void writeInt64(long[] value) {
if (value == null) {
writeNull();
return;
}

if (off == bytes.length) {
ensureCapacity(off + 1);
}
bytes[off++] = (byte) '[';

for (int i = 0; i < value.length; i++) {
if (i != 0) {
if (off == bytes.length) {
ensureCapacity(off + 1);
}
bytes[off++] = (byte) ',';
}
writeInt64(value[i]);
}
if (off == bytes.length) {
ensureCapacity(off + 1);
}
bytes[off++] = (byte) ']';
}

@Override
public final void writeInt64(long i) {
boolean writeAsString = (context.features & (WriteNonStringValueAsString.mask | WriteLongAsString.mask)) != 0
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ public void test_writeTo_0() {
assertEquals("[1]",
new String(out.toByteArray()));
}
@Test
public void test_writeTo_0_f() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JSON.writeTo(out, Collections.singleton(1), JSONWriter.Feature.WriteNulls);
assertEquals("[1]",
new String(out.toByteArray()));
}

@Test
public void test_writeTo_1() {
Expand Down Expand Up @@ -1020,6 +1027,8 @@ public void write(byte[] b, int off, int len) {
};
assertThrows(JSONException.class, () ->
JSON.writeTo(out, JSONObject.of("id", 123)));
assertThrows(JSONException.class, () ->
JSON.writeTo(out, JSONObject.of("id", 123), JSONWriter.Feature.PrettyFormat));
}
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.alibaba.fastjson2.primitves;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TestUtils;
import com.alibaba.fastjson2.reader.FieldReader;
import com.alibaba.fastjson2.reader.ObjectReader;
Expand Down Expand Up @@ -73,4 +75,18 @@ public void test_final_0() throws Exception {
}
}
}

@Test
public void test_write_0() throws Exception {
IntValueArrayField1 bean = new IntValueArrayField1();
bean.values = new int[] {101, 102, 103};
assertEquals("{\"values\":[101,102,103]}", JSON.toJSONString(bean));
assertEquals("{\"values\":[101,102,103]}", new String(JSON.toJSONBytes(bean)));
assertEquals("{\n" +
"\t\"values\":[101,102,103]\n" +
"}", JSON.toJSONString(bean, JSONWriter.Feature.PrettyFormat));
assertEquals("{\n" +
"\t\"values\":[101,102,103]\n" +
"}", new String(JSON.toJSONBytes(bean, JSONWriter.Feature.PrettyFormat)));
}
}
Loading

0 comments on commit 48231de

Please sign in to comment.