Skip to content

Commit

Permalink
Fastjson 2.0.51 + fastjson features (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao authored Jun 11, 2024
1 parent 4e95229 commit 64083f7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ dependencies {
implementation group: 'com.dslplatform', name: 'dsl-json', version: "${dslJsonVersion}"
annotationProcessor group: 'com.dslplatform', name: 'dsl-json', version: "${dslJsonVersion}"
// FastJson
implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: '2.0.48'
implementation group: 'com.alibaba.fastjson2', name: 'fastjson2-incubator-vector', version: '2.0.48'
implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: '2.0.51'
implementation group: 'com.alibaba.fastjson2', name: 'fastjson2-incubator-vector', version: '2.0.51'
// FlexJson
implementation group: 'net.sf.flexjson', name: 'flexjson', version: '3.3'
// GENSON
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/github/fabienrenaud/jjb/JsonBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public Object fastjson() throws Exception {
return null;
}

public Object fastjson_features() throws Exception {
return null;
}

public Object jsonio() throws Exception {
return null;
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.github.fabienrenaud.jjb.data;

import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.reader.ObjectReaderProvider;
import com.alibaba.fastjson2.writer.ObjectWriterProvider;
import com.github.fabienrenaud.jjb.RandomUtils;
import com.github.fabienrenaud.jjb.data.gen.DataGenerator;
import com.github.fabienrenaud.jjb.provider.JsonProvider;
Expand Down Expand Up @@ -29,6 +34,8 @@ public abstract class JsonSource<T> {
private final StreamSerializer<T> streamSerializer;
private final StreamDeserializer<T> streamDeserializer;

private final FastjsonProvider fastjsonFeatures;

JsonSource(int quantity, int individualSize, JsonProvider provider, DataGenerator<T> dataGenerator, StreamSerializer<T> streamSerializer, StreamDeserializer<T> streamDeserializer) {
this.provider = provider;

Expand All @@ -49,6 +56,24 @@ public abstract class JsonSource<T> {
}
return arr;
});

ObjectWriterProvider featuresWriterProvider = new ObjectWriterProvider();
featuresWriterProvider.setDisableAutoType(true);
featuresWriterProvider.setDisableArrayMapping(true);
featuresWriterProvider.setDisableJSONB(true);
featuresWriterProvider.setDisableReferenceDetect(true);
JSONFactory.createWriteContext(featuresWriterProvider);

ObjectReaderProvider featuresReaderProvider = new ObjectReaderProvider();
featuresReaderProvider.setDisableArrayMapping(true);
featuresReaderProvider.setDisableAutoType(true);
featuresReaderProvider.setDisableJSONB(true);
featuresReaderProvider.setDisableReferenceDetect(true);
featuresReaderProvider.setDisableSmartMatch(true);

fastjsonFeatures = new FastjsonProvider(
JSONFactory.createReadContext(featuresReaderProvider),
JSONFactory.createWriteContext(featuresWriterProvider));
}

private void populateFields(int quantity, int individualSize) {
Expand Down Expand Up @@ -124,4 +149,10 @@ public StreamDeserializer<T> streamDeserializer() {
private int index(int bound) {
return bound == 1 ? 0 : RandomUtils.nextInt(bound);
}

public FastjsonProvider fastjsonFeatures() {
return fastjsonFeatures;
}

public record FastjsonProvider (JSONReader.Context readerContext, JSONWriter.Context writerContext) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @author Fabien Renaud
*/
public class Deserialization extends JsonBench {

public JsonSource JSON_SOURCE() {
return CLI_JSON_SOURCE;
}
Expand Down Expand Up @@ -62,6 +61,12 @@ public Object fastjson() {
return JSON.parseObject(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType());
}

@Benchmark
@Override
public Object fastjson_features() {
return JSON.parseObject(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType(), JSON_SOURCE().fastjsonFeatures().readerContext());
}

@Benchmark
@Override
public Object flexjson() throws JsonSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.ByteArrayOutputStream;

public class Serialization extends JsonBench {

public JsonSource JSON_SOURCE() {
return CLI_JSON_SOURCE;
}
Expand Down Expand Up @@ -74,6 +73,14 @@ public Object fastjson() throws Exception {
return baos;
}

@Benchmark
@Override
public Object fastjson_features() throws Exception {
ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream();
JSON.writeTo(baos, JSON_SOURCE().nextPojo(), JSON_SOURCE().fastjsonFeatures().writerContext());
return baos;
}

@Benchmark
@Override
public Object flexjson() {
Expand Down

0 comments on commit 64083f7

Please sign in to comment.