Skip to content

Commit 3bd056c

Browse files
committed
fix: 修复jsonCodec无法解析多层嵌套泛型问题
1 parent 1499eda commit 3bd056c

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/codec/JsonValueCodec.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.JavaType;
66
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.type.TypeFactory;
78
import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream;
89
import lombok.Setter;
910
import lombok.SneakyThrows;
@@ -34,9 +35,9 @@ public class JsonValueCodec implements ValueCodec<Object, Object> {
3435

3536
static {
3637
defaultMapper = new ObjectMapper()
37-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
38-
.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
39-
.setTimeZone(TimeZone.getDefault());
38+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
39+
.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
40+
.setTimeZone(TimeZone.getDefault());
4041
}
4142

4243
private final JavaType jacksonType;
@@ -51,48 +52,44 @@ public static JsonValueCodec of(Class<?> targetType) {
5152
}
5253

5354
public static JsonValueCodec ofCollection(Class<? extends Collection> targetType
54-
, Class<?> elementType) {
55+
, Class<?> elementType) {
5556
return new JsonValueCodec(targetType, defaultMapper
56-
.getTypeFactory()
57-
.constructCollectionType(targetType, elementType));
57+
.getTypeFactory()
58+
.constructCollectionType(targetType, elementType));
5859
}
5960

6061
public static JsonValueCodec ofMap(Class<? extends Map> targetType, Class<?> keyType, Class<?> valueType) {
6162
return new JsonValueCodec(targetType, defaultMapper.getTypeFactory()
6263
.constructMapType(targetType, keyType, valueType));
6364
}
6465

66+
@SuppressWarnings("all")
6567
public static JsonValueCodec ofField(Field field) {
6668
Class type = field.getType();
6769
Class targetType = type;
6870
Type genericType = field.getGenericType();
6971
JavaType jacksonType = null;
7072

7173
if (type == Mono.class || type == Flux.class) {
72-
targetType = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
74+
targetType = (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];
7375
}
76+
TypeFactory factory = defaultMapper.getTypeFactory();
7477
if (Map.class.isAssignableFrom(targetType)) {
7578
if (genericType instanceof ParameterizedType) {
7679
Type[] types = ((ParameterizedType) genericType).getActualTypeArguments();
77-
jacksonType = defaultMapper
78-
.getTypeFactory()
79-
.constructMapType(targetType, (Class) types[0], (Class) types[1]);
80+
jacksonType = factory.constructMapType(targetType, factory.constructType(types[0]), factory.constructType(types[1]));
8081
}
8182

8283
} else if (Collection.class.isAssignableFrom(targetType)) {
8384
if (genericType instanceof ParameterizedType) {
8485
Type[] types = ((ParameterizedType) genericType).getActualTypeArguments();
85-
jacksonType = defaultMapper
86-
.getTypeFactory()
87-
.constructCollectionType(targetType, (Class) types[0]);
86+
jacksonType = factory.constructCollectionType(targetType, factory.constructType(types[0]));
8887
}
8988
} else if (targetType.isArray()) {
90-
jacksonType = defaultMapper
91-
.getTypeFactory()
92-
.constructArrayType(targetType.getComponentType());
89+
jacksonType = factory.constructArrayType(targetType.getComponentType());
9390
}
9491
if (jacksonType == null) {
95-
jacksonType = defaultMapper.getTypeFactory().constructType(targetType);
92+
jacksonType = factory.constructType(targetType);
9693
}
9794

9895
return new JsonValueCodec(type, jacksonType);

hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/codec/JsonValueCodecTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void testList() {
3131

3232
Assert.assertTrue(arr instanceof List);
3333

34-
Assert.assertEquals(((List) arr).size(), 2);
34+
Assert.assertEquals(2, ((List) arr).size());
3535

3636
}
3737

@@ -44,7 +44,7 @@ public void testByteBuffer() {
4444

4545
Assert.assertTrue(arr instanceof Set);
4646

47-
Assert.assertEquals(((Set) arr).size(), 2);
47+
Assert.assertEquals(2, ((Set) arr).size());
4848

4949
}
5050

@@ -76,6 +76,23 @@ public void testMap() {
7676
Assert.assertEquals(val.get("b"), Integer.valueOf(2));
7777
}
7878

79+
@Test
80+
@SneakyThrows
81+
public void testMapField() {
82+
JsonValueCodec codec = JsonValueCodec.ofField(JsonCodecEntity.class.getDeclaredField("nestMap"));
83+
84+
Object arr = codec.decode("{\"name\":{ \"key\":\"1\" }}");
85+
System.out.println(arr);
86+
Assert.assertTrue(arr instanceof Map);
87+
Map<String, Map<String,Integer>> val = ((Map) arr);
88+
89+
Map<String,Integer> vls = val.get("name");
90+
assertNotNull(vls);
91+
92+
assertEquals((Object) 1, vls.get("key"));
93+
94+
}
95+
7996
@Test
8097
public void testEntity() {
8198
JsonValueCodec codec = JsonValueCodec.of(JsonCodecEntity.class);
@@ -226,6 +243,8 @@ public static class JsonCodecEntity {
226243

227244
private Flux<JsonCodecEntity> flux;
228245

246+
private Map<String, Map<String, Integer>> nestMap;
247+
229248
}
230249

231250
}

0 commit comments

Comments
 (0)