This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new d78a5db698 JsonUtils Support Fastjson2 (#10791)
d78a5db698 is described below
commit d78a5db69881199beaf8ba47c21cde2a9da481f4
Author: Albumen Kevin <[email protected]>
AuthorDate: Sat Oct 29 10:44:28 2022 +0800
JsonUtils Support Fastjson2 (#10791)
---
dubbo-common/pom.xml | 5 +
.../dubbo/common/json/impl/FastJson2Impl.java | 53 ++++++++
.../org/apache/dubbo/common/utils/JsonUtils.java | 7 +-
.../apache/dubbo/common/utils/JsonUtilsTest.java | 136 +++++++++++++++++++--
.../apache/dubbo/common/utils/json/TestEnum.java | 21 ++++
.../dubbo/common/utils/json/TestObjectA.java | 56 +++++++++
.../dubbo/common/utils/json/TestObjectB.java | 51 ++++++++
dubbo-dependencies-bom/pom.xml | 2 +-
8 files changed, 321 insertions(+), 10 deletions(-)
diff --git a/dubbo-common/pom.xml b/dubbo-common/pom.xml
index b21501091e..b7d7724ab1 100644
--- a/dubbo-common/pom.xml
+++ b/dubbo-common/pom.xml
@@ -67,6 +67,11 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.alibaba.fastjson2</groupId>
+ <artifactId>fastjson2</artifactId>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJson2Impl.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJson2Impl.java
new file mode 100644
index 0000000000..24147dc32a
--- /dev/null
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/FastJson2Impl.java
@@ -0,0 +1,53 @@
+/*
+ * 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.dubbo.common.json.impl;
+
+import org.apache.dubbo.common.utils.ClassUtils;
+
+import com.alibaba.fastjson2.JSONWriter;
+
+import java.lang.reflect.Type;
+import java.util.List;
+
+public class FastJson2Impl extends AbstractJSONImpl {
+
+ @Override
+ public boolean isSupport() {
+ try {
+ Class<?> aClass = ClassUtils.forName("com.alibaba.fastjson2.JSON");
+ return aClass != null;
+ } catch (Throwable t) {
+ return false;
+ }
+ }
+
+ @Override
+ public <T> T toJavaObject(String json, Type type) {
+ return com.alibaba.fastjson2.JSON.parseObject(json, type);
+ }
+
+ @Override
+ public <T> List<T> toJavaList(String json, Class<T> clazz) {
+ return com.alibaba.fastjson2.JSON.parseArray(json, clazz);
+ }
+
+ @Override
+ public String toJson(Object obj) {
+ return com.alibaba.fastjson2.JSON.toJSONString(obj,
JSONWriter.Feature.WriteEnumsUsingName);
+ }
+}
+
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
index 57235b9be0..2285f1f05d 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/JsonUtils.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.common.utils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.json.JSON;
+import org.apache.dubbo.common.json.impl.FastJson2Impl;
import org.apache.dubbo.common.json.impl.FastJsonImpl;
import org.apache.dubbo.common.json.impl.GsonImpl;
@@ -36,6 +37,9 @@ public class JsonUtils {
try {
JSON instance = null;
switch (preferJsonFrameworkName) {
+ case "fastjson2":
+ instance = new FastJson2Impl();
+ break;
case "fastjson":
instance = new FastJsonImpl();
break;
@@ -52,6 +56,7 @@ public class JsonUtils {
}
if (json == null) {
List<Class<? extends JSON>> jsonClasses =
Arrays.asList(
+ FastJson2Impl.class,
FastJsonImpl.class,
GsonImpl.class);
for (Class<? extends JSON> jsonClass : jsonClasses) {
@@ -67,7 +72,7 @@ public class JsonUtils {
}
}
if (json == null) {
- throw new IllegalStateException("Dubbo unable to find
out any json framework (e.g. fastjson, gson) from jvm env. " +
+ throw new IllegalStateException("Dubbo unable to find
out any json framework (e.g. fastjson2, fastjson, gson) from jvm env. " +
"Please import at least one json framework.");
}
}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
index a04683c8d8..15f75a954e 100644
---
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/JsonUtilsTest.java
@@ -16,8 +16,12 @@
*/
package org.apache.dubbo.common.utils;
+import org.apache.dubbo.common.json.impl.FastJson2Impl;
import org.apache.dubbo.common.json.impl.FastJsonImpl;
import org.apache.dubbo.common.json.impl.GsonImpl;
+import org.apache.dubbo.common.utils.json.TestEnum;
+import org.apache.dubbo.common.utils.json.TestObjectA;
+import org.apache.dubbo.common.utils.json.TestObjectB;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -25,6 +29,7 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@@ -41,6 +46,14 @@ public class JsonUtilsTest {
Assertions.assertEquals(map,
JsonUtils.getJson().toJavaObject("{\"a\":\"a\"}", Map.class));
Assertions.assertEquals(Collections.singletonList(map),
JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
+ // prefer use fastjson2
+ JsonUtils.setJson(null);
+ System.setProperty("dubbo.json-framework.prefer", "fastjson2");
+ Assertions.assertEquals("{\"a\":\"a\"}",
JsonUtils.getJson().toJson(map));
+ Assertions.assertEquals(map,
JsonUtils.getJson().toJavaObject("{\"a\":\"a\"}", Map.class));
+ Assertions.assertEquals(Collections.singletonList(map),
JsonUtils.getJson().toJavaList("[{\"a\":\"a\"}]", Map.class));
+ System.clearProperty("dubbo.json-framework.prefer");
+
// prefer use fastjson
JsonUtils.setJson(null);
System.setProperty("dubbo.json-framework.prefer", "fastjson");
@@ -60,6 +73,94 @@ public class JsonUtilsTest {
JsonUtils.setJson(null);
}
+ @Test
+ public void consistentTest() {
+ List<Object> objs = new LinkedList<>();
+
+ {
+ objs.add(null);
+ }
+
+ {
+ Map<String, String> map = new HashMap<>();
+ map.put("a", "a");
+ objs.add(map);
+ }
+
+ {
+ TestObjectA a = new TestObjectA();
+ objs.add(a);
+ }
+
+ {
+ TestObjectA a = new TestObjectA();
+ a.setTestEnum(TestEnum.TYPE_A);
+ objs.add(a);
+ }
+
+ {
+ TestObjectB b = new TestObjectB();
+ objs.add(b);
+ }
+
+ {
+ TestObjectB b = new TestObjectB();
+ b.setInnerA(new TestObjectB.Inner());
+ b.setInnerB(new TestObjectB.Inner());
+ objs.add(b);
+ }
+
+ {
+ TestObjectB b = new TestObjectB();
+ TestObjectB.Inner inner1 = new TestObjectB.Inner();
+ TestObjectB.Inner inner2 = new TestObjectB.Inner();
+ inner1.setName("Test");
+ inner2.setName("Test");
+ b.setInnerA(inner1);
+ b.setInnerB(inner2);
+ objs.add(b);
+ }
+
+ {
+ TestObjectB b = new TestObjectB();
+ TestObjectB.Inner inner1 = new TestObjectB.Inner();
+ inner1.setName("Test");
+ b.setInnerA(inner1);
+ b.setInnerB(inner1);
+ objs.add(b);
+ }
+
+ for (Object obj : objs) {
+
+ // prefer use fastjson2
+ JsonUtils.setJson(null);
+ System.setProperty("dubbo.json-framework.prefer", "fastjson2");
+ Assertions.assertInstanceOf(FastJson2Impl.class,
JsonUtils.getJson());
+ String fromFastjson2 = JsonUtils.getJson().toJson(obj);
+ System.clearProperty("dubbo.json-framework.prefer");
+
+ // prefer use fastjson
+ JsonUtils.setJson(null);
+ System.setProperty("dubbo.json-framework.prefer", "fastjson");
+ Assertions.assertInstanceOf(FastJsonImpl.class,
JsonUtils.getJson());
+ String fromFastjson1 = JsonUtils.getJson().toJson(obj);
+ System.clearProperty("dubbo.json-framework.prefer");
+
+ // prefer use gson
+ JsonUtils.setJson(null);
+ System.setProperty("dubbo.json-framework.prefer", "gson");
+ Assertions.assertInstanceOf(GsonImpl.class, JsonUtils.getJson());
+ String fromGson = JsonUtils.getJson().toJson(obj);
+ System.clearProperty("dubbo.json-framework.prefer");
+
+ JsonUtils.setJson(null);
+
+ Assertions.assertEquals(fromFastjson1, fromFastjson2);
+ Assertions.assertEquals(fromFastjson1, fromGson);
+ Assertions.assertEquals(fromFastjson2, fromGson);
+ }
+ }
+
@Test
public void testGetJson2() {
ClassLoader originClassLoader =
Thread.currentThread().getContextClassLoader();
@@ -77,10 +178,17 @@ public class JsonUtilsTest {
};
Thread.currentThread().setContextClassLoader(newClassLoader);
- // default use fastjson
+ // default use fastjson2
JsonUtils.setJson(null);
removedPackages.set(Collections.emptyList());
- Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
+ Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
+
+ // prefer use fastjson2
+ JsonUtils.setJson(null);
+ removedPackages.set(Collections.emptyList());
+ System.setProperty("dubbo.json-framework.prefer", "fastjson2");
+ Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
+ System.clearProperty("dubbo.json-framework.prefer");
// prefer use fastjson
JsonUtils.setJson(null);
@@ -100,36 +208,48 @@ public class JsonUtilsTest {
JsonUtils.setJson(null);
removedPackages.set(Collections.emptyList());
System.setProperty("dubbo.json-framework.prefer", "notfound");
- Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
+ Assertions.assertInstanceOf(FastJson2Impl.class, JsonUtils.getJson());
System.clearProperty("dubbo.json-framework.prefer");
+ JsonUtils.setJson(null);
+ // TCCL not found fastjson2
+
removedPackages.set(Collections.singletonList("com.alibaba.fastjson2"));
+ Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
+
JsonUtils.setJson(null);
// TCCL not found fastjson
- removedPackages.set(Collections.singletonList("com.alibaba.fastjson"));
+ removedPackages.set(Arrays.asList("com.alibaba.fastjson2",
"com.alibaba.fastjson"));
Assertions.assertInstanceOf(GsonImpl.class, JsonUtils.getJson());
JsonUtils.setJson(null);
// TCCL not found gson
- removedPackages.set(Collections.singletonList("com.google.gson"));
+ removedPackages.set(Arrays.asList("com.alibaba.fastjson2",
"com.google.gson"));
Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
+ JsonUtils.setJson(null);
+ // TCCL not found fastjson2, prefer use fastjson
+
removedPackages.set(Collections.singletonList("com.alibaba.fastjson2"));
+ System.setProperty("dubbo.json-framework.prefer", "fastjson");
+ Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
+ System.clearProperty("dubbo.json-framework.prefer");
+
JsonUtils.setJson(null);
// TCCL not found fastjson, prefer use fastjson
- removedPackages.set(Collections.singletonList("com.alibaba.fastjson"));
+ removedPackages.set(Arrays.asList("com.alibaba.fastjson2",
"com.alibaba.fastjson"));
System.setProperty("dubbo.json-framework.prefer", "fastjson");
Assertions.assertInstanceOf(GsonImpl.class, JsonUtils.getJson());
System.clearProperty("dubbo.json-framework.prefer");
JsonUtils.setJson(null);
// TCCL not found gson, prefer use gson
- removedPackages.set(Collections.singletonList("com.google.gson"));
+ removedPackages.set(Arrays.asList("com.alibaba.fastjson2",
"com.google.gson"));
System.setProperty("dubbo.json-framework.prefer", "gson");
Assertions.assertInstanceOf(FastJsonImpl.class, JsonUtils.getJson());
System.clearProperty("dubbo.json-framework.prefer");
JsonUtils.setJson(null);
// TCCL not found fastjson, gson
- removedPackages.set(Arrays.asList("com.alibaba.fastjson",
"com.google.gson"));
+ removedPackages.set(Arrays.asList("com.alibaba.fastjson2",
"com.alibaba.fastjson", "com.google.gson"));
Assertions.assertThrows(IllegalStateException.class,
JsonUtils::getJson);
Thread.currentThread().setContextClassLoader(originClassLoader);
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestEnum.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestEnum.java
new file mode 100644
index 0000000000..f00aa81dd9
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestEnum.java
@@ -0,0 +1,21 @@
+/*
+ * 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.dubbo.common.utils.json;
+
+public enum TestEnum {
+ TYPE_A, TYPE_B, TYPE_C
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestObjectA.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestObjectA.java
new file mode 100644
index 0000000000..436d8a12b8
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestObjectA.java
@@ -0,0 +1,56 @@
+/*
+ * 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.dubbo.common.utils.json;
+
+public class TestObjectA {
+ private String name;
+ private int age;
+ private TestEnum testEnum;
+
+ public TestObjectA() {
+ }
+
+ public TestObjectA(String name, int age, TestEnum testEnum) {
+ this.name = name;
+ this.age = age;
+ this.testEnum = testEnum;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public TestEnum getTestEnum() {
+ return testEnum;
+ }
+
+ public void setTestEnum(TestEnum testEnum) {
+ this.testEnum = testEnum;
+ }
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestObjectB.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestObjectB.java
new file mode 100644
index 0000000000..e15a9a1e89
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/json/TestObjectB.java
@@ -0,0 +1,51 @@
+/*
+ * 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.dubbo.common.utils.json;
+
+public class TestObjectB {
+ private Inner innerA;
+ private Inner innerB;
+
+
+ public Inner getInnerA() {
+ return innerA;
+ }
+
+ public void setInnerA(Inner innerA) {
+ this.innerA = innerA;
+ }
+
+ public Inner getInnerB() {
+ return innerB;
+ }
+
+ public void setInnerB(Inner innerB) {
+ this.innerB = innerB;
+ }
+
+ public static class Inner {
+ private String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+}
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 90ec2d8d8f..e80d03f62c 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -98,7 +98,7 @@
<httpclient_version>4.5.13</httpclient_version>
<httpcore_version>4.4.6</httpcore_version>
<fastjson_version>1.2.83</fastjson_version>
- <fastjson2_version>2.0.14</fastjson2_version>
+ <fastjson2_version>2.0.16</fastjson2_version>
<zookeeper_version>3.4.14</zookeeper_version>
<curator_version>4.2.0</curator_version>
<curator_test_version>2.12.0</curator_test_version>