修复升级2.0.4导致兼容的问题 (#4202)

This commit is contained in:
温绍锦
2022-05-30 10:29:38 +08:00
committed by GitHub
parent e19bf1ecff
commit 7aaee7b439
3 changed files with 33 additions and 17 deletions
@@ -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> T unmarshalFromByte(byte[] bytes, Class<T> 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> T unmarshalFromByte(byte[] bytes, TypeReference<T> 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> T unmarshalFromString(String json, Class<T> 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> T unmarshalFromString(String json, TypeReference<T> 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);
}
});
@@ -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);
}
}
+1 -1
View File
@@ -222,7 +222,7 @@
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.4</version>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>