-
Notifications
You must be signed in to change notification settings - Fork 554
feat: add fastjson2-extension-vertx module #4002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jujn
wants to merge
10
commits into
alibaba:main
Choose a base branch
from
jujn:feat_3994
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+15,286
−40
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e50d110
build: add fastjson2-extension-vertx module scaffolding
jujn 93c0bd4
feat: init SPI loader, add compatible readers/writers, implement Fast…
jujn 466b678
test: add unit tests from Vertx official built-in
jujn aa54dda
test: add compatibility test and JMH performance test
jujn de7bfe4
test: move benchmark tests to extension-vertx(need jdk17)
jujn 63052e5
fix: allow custom ByteArrayWriter
jujn 790e33b
fix: when creator=reflect, alphabetic=false does not take effect
jujn 1253dfa
Merge remote-tracking branch 'upstream/main' into feat_3994
jujn 48e869a
optimize: add URL Safe (no padding) Base64 parsing feature
jujn 88a523b
update
jujn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -451,17 +451,18 @@ boolean record = BeanUtils.isRecord(objectClass); | |
| } | ||
|
|
||
| long writerFieldFeatures = features | beanFeatures; | ||
| boolean fieldBased = (writerFieldFeatures & JSONWriter.Feature.FieldBased.mask) != 0; | ||
| boolean fieldBased = ((writerFieldFeatures & JSONWriter.Feature.FieldBased.mask) != 0 && !objectClass.isInterface()) | ||
| || !beanInfo.alphabetic; | ||
|
Comment on lines
-454
to
+455
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里修复 creator=reflect 时,设置 alphabetic=false(序列化对象字段的输出顺序)不生效的问题;同时调整了其它地方,与 creator=asm 保持一致 |
||
|
|
||
| if (fieldBased && (record || objectClass.isInterface())) { | ||
| if (fieldBased && record) { | ||
| fieldBased = false; | ||
| } | ||
|
|
||
| List<FieldWriter> fieldWriters; | ||
| Map<String, FieldWriter> fieldWriterMap = new LinkedHashMap<>(); | ||
| final FieldInfo fieldInfo = new FieldInfo(); | ||
|
|
||
| if (fieldBased) { | ||
| Map<String, FieldWriter> fieldWriterMap = new TreeMap<>(); | ||
| BeanUtils.declaredFields(objectClass, field -> { | ||
| fieldInfo.init(); | ||
| FieldWriter fieldWriter = createFieldWriter(objectClass, writerFieldFeatures, provider, beanInfo, fieldInfo, field); | ||
|
|
@@ -484,8 +485,6 @@ boolean record = BeanUtils.isRecord(objectClass); | |
| } | ||
|
|
||
| if (!fieldWritersCreated) { | ||
| Map<String, FieldWriter> fieldWriterMap = new LinkedHashMap<>(); | ||
|
|
||
| if (!record) { | ||
| BeanUtils.declaredFields(objectClass, field -> { | ||
| fieldInfo.init(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.alibaba.fastjson2</groupId> | ||
| <artifactId>fastjson2-parent</artifactId> | ||
| <version>2.0.62-SNAPSHOT</version> | ||
| <relativePath>../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>fastjson2-extension-vertx</artifactId> | ||
| <name>fastjson2-extension-vertx</name> | ||
| <description>Fastjson is a JSON processor (JSON parser + JSON generator) written in Java</description> | ||
| <packaging>jar</packaging> | ||
| <url>https://github.com/alibaba/fastjson2</url> | ||
| <inceptionYear>2026</inceptionYear> | ||
|
|
||
| <licenses> | ||
| <license> | ||
| <name>Apache 2</name> | ||
| <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url> | ||
| <distribution>repo</distribution> | ||
| <comments>A business-friendly OSS license</comments> | ||
| </license> | ||
| </licenses> | ||
| <scm> | ||
| <url>https://github.com/alibaba/fastjson2</url> | ||
| <connection>scm:git:https://git@github.com/alibaba/fastjson2.git</connection> | ||
| </scm> | ||
|
|
||
| <properties> | ||
| <java.version>17</java.version> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <organization> | ||
| <name>Alibaba Group</name> | ||
| <url>https://github.com/alibaba</url> | ||
| </organization> | ||
| <developers> | ||
| <developer> | ||
| <id>wenshao</id> | ||
| <name>wenshao</name> | ||
| <email>shaojin.wensj(at)alibaba-inc.com</email> | ||
| <roles> | ||
| <role>Developer</role> | ||
| <role>Tech Leader</role> | ||
| </roles> | ||
| <timezone>+8</timezone> | ||
| <url>https://github.com/wenshao</url> | ||
| </developer> | ||
| <developer> | ||
| <id>jujn</id> | ||
| <name>jujn</name> | ||
| <email>jth_xdu(at)qq.com</email> | ||
| <roles> | ||
| <role>Developer</role> | ||
| </roles> | ||
| <timezone>+8</timezone> | ||
| <url>https://github.com/jujn</url> | ||
| </developer> | ||
| </developers> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.vertx</groupId> | ||
| <artifactId>vertx-dependencies</artifactId> | ||
| <type>pom</type> | ||
| <version>${vertx5.version}</version> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.alibaba.fastjson2</groupId> | ||
| <artifactId>fastjson2-extension</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>io.vertx</groupId> | ||
| <artifactId>vertx-core</artifactId> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.datatype</groupId> | ||
| <artifactId>jackson-datatype-jsr310</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>commons-io</groupId> | ||
| <artifactId>commons-io</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openjdk.jmh</groupId> | ||
| <artifactId>jmh-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openjdk.jmh</groupId> | ||
| <artifactId>jmh-generator-annprocess</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- 消除测试时警告:No SLF4J providers were found --> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-nop</artifactId> | ||
| <version>2.0.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <configuration> | ||
| <includes> | ||
| <include>com/alibaba/fastjson2/**/*.java</include> | ||
| </includes> | ||
| <systemPropertyVariables> | ||
| <user.timezone>Asia/Shanghai</user.timezone> | ||
| </systemPropertyVariables> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
22 changes: 22 additions & 0 deletions
22
extension-vertx/src/main/java/com/alibaba/fastjson2/support/vertx/BufferReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.alibaba.fastjson2.support.vertx; | ||
|
|
||
| import com.alibaba.fastjson2.JSONReader; | ||
| import com.alibaba.fastjson2.reader.ObjectReader; | ||
| import io.vertx.core.buffer.Buffer; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.Base64; | ||
|
|
||
| public class BufferReader implements ObjectReader<Buffer> { | ||
| public static final BufferReader INSTANCE = new BufferReader(); | ||
|
|
||
| @Override | ||
| public Buffer readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { | ||
| if (jsonReader.nextIfNull()) { | ||
| return null; | ||
| } | ||
|
|
||
| String base64Str = jsonReader.readString(); | ||
| return base64Str == null ? null : Buffer.buffer(Base64.getUrlDecoder().decode(base64Str)); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
extension-vertx/src/main/java/com/alibaba/fastjson2/support/vertx/BufferWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.alibaba.fastjson2.support.vertx; | ||
|
|
||
| import com.alibaba.fastjson2.JSONWriter; | ||
| import com.alibaba.fastjson2.writer.ObjectWriter; | ||
| import io.vertx.core.buffer.Buffer; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.Base64; | ||
|
|
||
| public class BufferWriter implements ObjectWriter<Buffer> { | ||
| public static final BufferWriter INSTANCE = new BufferWriter(); | ||
|
|
||
| @Override | ||
| public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { | ||
| if (object == null) { | ||
| jsonWriter.writeNull(); | ||
| return; | ||
| } | ||
|
|
||
| Buffer buffer = (Buffer) object; | ||
| String base64Str = Base64.getUrlEncoder().withoutPadding().encodeToString(buffer.getBytes()); // 无填充的 Base64 | ||
| jsonWriter.writeString(base64Str); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
extension-vertx/src/main/java/com/alibaba/fastjson2/support/vertx/ByteArrayReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.alibaba.fastjson2.support.vertx; | ||
|
|
||
| import com.alibaba.fastjson2.JSONReader; | ||
| import com.alibaba.fastjson2.reader.ObjectReader; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.Base64; | ||
|
|
||
| public class ByteArrayReader implements ObjectReader<byte[]> { | ||
| public static final ByteArrayReader INSTANCE = new ByteArrayReader(); | ||
|
|
||
| @Override | ||
| public byte[] readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { | ||
| if (jsonReader.nextIfNull()) { | ||
| return null; | ||
| } | ||
|
|
||
| String base64Str = jsonReader.readString(); | ||
| return base64Str == null ? null : Base64.getUrlDecoder().decode(base64Str); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
extension-vertx/src/main/java/com/alibaba/fastjson2/support/vertx/ByteArrayWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.alibaba.fastjson2.support.vertx; | ||
|
|
||
| import com.alibaba.fastjson2.JSONWriter; | ||
| import com.alibaba.fastjson2.writer.ObjectWriter; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.Base64; | ||
|
|
||
| public class ByteArrayWriter implements ObjectWriter<byte[]> { | ||
| public static final ByteArrayWriter INSTANCE = new ByteArrayWriter(); | ||
|
|
||
| @Override | ||
| public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { | ||
| if (object == null) { | ||
| jsonWriter.writeNull(); | ||
| return; | ||
| } | ||
|
|
||
| byte[] bytes = (byte[]) object; | ||
| String base64Str = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes); // 无填充的 Base64 | ||
| jsonWriter.writeString(base64Str); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.