-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
feature: add fastjson2 serializer support #6904
Open
GoodBoyCoder
wants to merge
9
commits into
apache:2.x
Choose a base branch
from
GoodBoyCoder:feature-fastjson2-serial
base: 2.x
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.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
422b58d
feature: add fastjson2 serializer support
GoodBoyCoder 3c30bc4
fix: pom配置
GoodBoyCoder d09023d
fix: code style
GoodBoyCoder 1d5f81f
fix: code style
GoodBoyCoder ccdde1a
add md
GoodBoyCoder 17cac76
test: add mock server test case
GoodBoyCoder cab182c
optimize: code style
GoodBoyCoder 5a73d7d
test: fix test fail
GoodBoyCoder 3e25a5e
test: fix test fail
GoodBoyCoder 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 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 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 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 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 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 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 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 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 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,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.apache.seata</groupId> | ||
<artifactId>seata-serializer</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>seata-serializer-fastjson2</artifactId> | ||
<packaging>jar</packaging> | ||
<name>seata-serializer-fastjson2 ${project.version}</name> | ||
<description>serializer-fastjson2 for Seata built with Maven</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>seata-common</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>seata-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.fastjson2</groupId> | ||
<artifactId>fastjson2</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
38 changes: 38 additions & 0 deletions
38
...er-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java
This file contains 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,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.serializer.fastjson2; | ||
|
||
import com.alibaba.fastjson2.JSONB; | ||
import org.apache.seata.common.loader.LoadLevel; | ||
import org.apache.seata.core.serializer.Serializer; | ||
|
||
/** | ||
* @Author GoodBoyCoder | ||
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. remove author |
||
*/ | ||
@LoadLevel(name = "FASTJSON2") | ||
public class Fastjson2Serializer implements Serializer { | ||
|
||
@Override | ||
public <T> byte[] serialize(T t) { | ||
return JSONB.toBytes(t, Fastjson2SerializerFactory.getInstance().getJsonWriterFeatureList()); | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(byte[] bytes) { | ||
return (T) JSONB.parseObject(bytes, Object.class, Fastjson2SerializerFactory.getInstance().getFilter(), Fastjson2SerializerFactory.getInstance().getJsonReaderFeatureList()); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...json2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java
This file contains 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,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.serializer.fastjson2; | ||
|
||
import com.alibaba.fastjson2.JSONReader; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import com.alibaba.fastjson2.filter.Filter; | ||
import org.apache.seata.core.serializer.SerializerSecurityRegistry; | ||
|
||
/** | ||
* @Author GoodBoyCoder | ||
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. remove author |
||
*/ | ||
public class Fastjson2SerializerFactory { | ||
private Filter autoTypeFilter; | ||
|
||
private JSONReader.Feature[] jsonReaderFeature; | ||
|
||
private JSONWriter.Feature[] jsonWriterFeature; | ||
private static final class InstanceHolder { | ||
public static final Fastjson2SerializerFactory INSTANCE = new Fastjson2SerializerFactory(); | ||
} | ||
|
||
public Fastjson2SerializerFactory() { | ||
autoTypeFilter = JSONReader.autoTypeFilter(true, SerializerSecurityRegistry.getAllowClassType().toArray(new Class[]{})); | ||
|
||
jsonReaderFeature = new JSONReader.Feature[]{ | ||
JSONReader.Feature.UseDefaultConstructorAsPossible, | ||
// If not configured, it will be serialized based on public field and getter methods by default. | ||
// After configuration, it will be deserialized based on non-static fields (including private). | ||
// It will be safer under FieldBased configuration | ||
JSONReader.Feature.FieldBased, | ||
JSONReader.Feature.IgnoreAutoTypeNotMatch, | ||
JSONReader.Feature.UseNativeObject | ||
}; | ||
|
||
jsonWriterFeature = new JSONWriter.Feature[]{ | ||
JSONWriter.Feature.WriteClassName, | ||
JSONWriter.Feature.FieldBased, | ||
JSONWriter.Feature.ReferenceDetection, | ||
JSONWriter.Feature.WriteNulls, | ||
JSONWriter.Feature.NotWriteDefaultValue, | ||
JSONWriter.Feature.NotWriteHashMapArrayListClassName, | ||
JSONWriter.Feature.WriteNameAsSymbol | ||
}; | ||
} | ||
|
||
public static Fastjson2SerializerFactory getInstance() { | ||
return Fastjson2SerializerFactory.InstanceHolder.INSTANCE; | ||
} | ||
|
||
public Filter getFilter() { | ||
return autoTypeFilter; | ||
} | ||
|
||
public JSONReader.Feature[] getJsonReaderFeatureList() { | ||
return jsonReaderFeature; | ||
} | ||
|
||
public JSONWriter.Feature[] getJsonWriterFeatureList() { | ||
return jsonWriterFeature; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...astjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer
This file contains 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,17 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
org.apache.seata.serializer.fastjson2.Fastjson2Serializer |
85 changes: 85 additions & 0 deletions
85
...astjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java
This file contains 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,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.serializer.fastjson2; | ||
|
||
import org.apache.seata.core.exception.TransactionExceptionCode; | ||
import org.apache.seata.core.model.BranchStatus; | ||
import org.apache.seata.core.model.BranchType; | ||
import org.apache.seata.core.protocol.ResultCode; | ||
import org.apache.seata.core.protocol.transaction.BranchCommitRequest; | ||
import org.apache.seata.core.protocol.transaction.BranchCommitResponse; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
public class Fastjson2SerializerTest { | ||
|
||
private static Fastjson2Serializer fastjson2Serializer; | ||
|
||
@BeforeAll | ||
public static void before() { | ||
fastjson2Serializer = new Fastjson2Serializer(); | ||
} | ||
|
||
|
||
@Test | ||
public void testBranchCommitRequest() { | ||
|
||
BranchCommitRequest branchCommitRequest = new BranchCommitRequest(); | ||
branchCommitRequest.setBranchType(BranchType.AT); | ||
branchCommitRequest.setXid("xid"); | ||
branchCommitRequest.setResourceId("resourceId"); | ||
branchCommitRequest.setBranchId(20190809); | ||
branchCommitRequest.setApplicationData("app"); | ||
|
||
byte[] bytes = fastjson2Serializer.serialize(branchCommitRequest); | ||
BranchCommitRequest t = fastjson2Serializer.deserialize(bytes); | ||
|
||
assertThat(t.getTypeCode()).isEqualTo(branchCommitRequest.getTypeCode()); | ||
assertThat(t.getBranchType()).isEqualTo(branchCommitRequest.getBranchType()); | ||
assertThat(t.getXid()).isEqualTo(branchCommitRequest.getXid()); | ||
assertThat(t.getResourceId()).isEqualTo(branchCommitRequest.getResourceId()); | ||
assertThat(t.getBranchId()).isEqualTo(branchCommitRequest.getBranchId()); | ||
assertThat(t.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData()); | ||
|
||
} | ||
|
||
@Test | ||
public void testBranchCommitResponse() { | ||
|
||
BranchCommitResponse branchCommitResponse = new BranchCommitResponse(); | ||
branchCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchTransactionNotExist); | ||
branchCommitResponse.setBranchId(20190809); | ||
branchCommitResponse.setBranchStatus(BranchStatus.PhaseOne_Done); | ||
branchCommitResponse.setMsg("20190809"); | ||
branchCommitResponse.setXid("20190809"); | ||
branchCommitResponse.setResultCode(ResultCode.Failed); | ||
|
||
byte[] bytes = fastjson2Serializer.serialize(branchCommitResponse); | ||
BranchCommitResponse t = fastjson2Serializer.deserialize(bytes); | ||
|
||
assertThat(t.getTransactionExceptionCode()).isEqualTo(branchCommitResponse.getTransactionExceptionCode()); | ||
assertThat(t.getBranchId()).isEqualTo(branchCommitResponse.getBranchId()); | ||
assertThat(t.getBranchStatus()).isEqualTo(branchCommitResponse.getBranchStatus()); | ||
assertThat(t.getMsg()).isEqualTo(branchCommitResponse.getMsg()); | ||
assertThat(t.getResultCode()).isEqualTo(branchCommitResponse.getResultCode()); | ||
|
||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.