From fc7226ea63018497719033a6c8f5f364348c5536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E7=BB=8D=E9=94=A6?= Date: Mon, 30 May 2022 10:29:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=87=E7=BA=A72.0.4?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=85=BC=E5=AE=B9=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#4202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../otter/canal/common/utils/JsonUtils.java | 27 ++++++++----------- .../otter/canal/common/JsonUtilsTest.java | 21 +++++++++++++++ pom.xml | 2 +- 3 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 common/src/test/java/com/alibaba/otter/canal/common/JsonUtilsTest.java diff --git a/common/src/main/java/com/alibaba/otter/canal/common/utils/JsonUtils.java b/common/src/main/java/com/alibaba/otter/canal/common/utils/JsonUtils.java index fe2bdcee..f43a356f 100644 --- a/common/src/main/java/com/alibaba/otter/canal/common/utils/JsonUtils.java +++ b/common/src/main/java/com/alibaba/otter/canal/common/utils/JsonUtils.java @@ -1,13 +1,12 @@ package com.alibaba.otter.canal.common.utils; import java.lang.reflect.Type; -import java.net.Inet4Address; -import java.net.Inet6Address; import java.net.InetAddress; import java.util.Arrays; import java.util.List; import com.alibaba.fastjson2.*; +import com.alibaba.fastjson2.filter.Filter; import com.alibaba.fastjson2.filter.PropertyFilter; import com.alibaba.fastjson2.writer.ObjectWriter; @@ -18,22 +17,18 @@ import com.alibaba.fastjson2.writer.ObjectWriter; * @author jianghang */ public class JsonUtils { - - static { - JSON.register(InetAddress.class, InetAddressWriter.instance); - JSON.register(Inet4Address.class, InetAddressWriter.instance); - JSON.register(Inet6Address.class, InetAddressWriter.instance); - - JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept("com.alibaba.otter."); - JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept("com.taobao.tddl.dbsync."); - } + static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter( + "com.alibaba.otter.", + "com.taobao.tddl.dbsync." + ); public static T unmarshalFromByte(byte[] bytes, Class targetClass) { - return (T) JSON.parseObject(bytes, targetClass, JSONReader.Feature.SupportAutoType);// 默认为UTF-8 + return (T) JSON.parseObject(bytes, targetClass, AUTO_TYPE_FILTER);// 默认为UTF-8 } public static T unmarshalFromByte(byte[] bytes, TypeReference type) { - return (T) JSON.parseObject(bytes, type.getType(), JSONReader.Feature.SupportAutoType); + + return (T) JSON.parseObject(bytes, type.getType(), AUTO_TYPE_FILTER); } public static byte[] marshalToByte(Object obj) { @@ -45,11 +40,11 @@ public class JsonUtils { } public static T unmarshalFromString(String json, Class targetClass) { - return (T) JSON.parseObject(json, targetClass, JSONReader.Feature.SupportAutoType);// 默认为UTF-8 + return (T) JSON.parseObject(json, targetClass, AUTO_TYPE_FILTER);// 默认为UTF-8 } public static T unmarshalFromString(String json, TypeReference type) { - return (T) JSON.parseObject(json, type, JSONReader.Feature.SupportAutoType);// 默认为UTF-8 + return (T) JSON.parseObject(json, type.getType(), AUTO_TYPE_FILTER);// 默认为UTF-8 } public static String marshalToString(Object obj) { @@ -68,7 +63,7 @@ public class JsonUtils { return JSON.toJSONString(obj, new PropertyFilter() { @Override - public boolean process(Object object, String name, Object value) { + public boolean apply(Object object, String name, Object value) { return !propertyFliters.contains(name); } }); diff --git a/common/src/test/java/com/alibaba/otter/canal/common/JsonUtilsTest.java b/common/src/test/java/com/alibaba/otter/canal/common/JsonUtilsTest.java new file mode 100644 index 00000000..b913062f --- /dev/null +++ b/common/src/test/java/com/alibaba/otter/canal/common/JsonUtilsTest.java @@ -0,0 +1,21 @@ +package com.alibaba.otter.canal.common; + +import com.alibaba.otter.canal.common.utils.JsonUtils; +import org.junit.Test; + +import java.net.InetAddress; + +import static org.junit.Assert.assertEquals; + +public class JsonUtilsTest { + @Test + public void marshalToString() throws Exception { + InetAddress address = InetAddress.getByName("localhost"); + + String json = JsonUtils.marshalToString(address); + assertEquals("\"localhost\"", json); + + InetAddress address1 = JsonUtils.unmarshalFromString(json, InetAddress.class); + assertEquals(address, address1); + } +} diff --git a/pom.xml b/pom.xml index 20800155..e5763e4f 100644 --- a/pom.xml +++ b/pom.xml @@ -222,7 +222,7 @@ com.alibaba.fastjson2 fastjson2 - 2.0.4 + 2.0.5 com.google.guava