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