diff --git a/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java b/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java index a12790a216..0647fc979c 100755 --- a/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java +++ b/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java @@ -463,7 +463,14 @@ protected T deserialze(DefaultJSONParser parser, // Class userType = null; if (deserizer == null) { userType = TypeUtils.loadClass(typeName, config.getDefaultClassLoader()); - deserizer = parser.getConfig().getDeserializer(userType); + + Class expectClass = TypeUtils.getClass(type); + if (expectClass == null || + (userType != null && expectClass.isAssignableFrom(userType))) { + deserizer = parser.getConfig().getDeserializer(userType); + } else { + throw new JSONException("type not match"); + } } return (T) deserizer.deserialze(parser, userType, fieldName); diff --git a/src/test/java/com/alibaba/json/bvt/parser/JSONLexerTest_2.java b/src/test/java/com/alibaba/json/bvt/parser/JSONLexerTest_2.java index 9417037101..77ac62ef98 100644 --- a/src/test/java/com/alibaba/json/bvt/parser/JSONLexerTest_2.java +++ b/src/test/java/com/alibaba/json/bvt/parser/JSONLexerTest_2.java @@ -7,6 +7,7 @@ import org.junit.Assert; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.TypeReference; public class JSONLexerTest_2 extends TestCase { @@ -17,11 +18,23 @@ public void test_0() throws Exception { } public void test_1() throws Exception { - JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO1\"}", VO.class); + Exception error = null; + try { + JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$VO1\"}", VO.class); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); } public void test_2() throws Exception { - JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$A\"}", VO.class); + Exception error = null; + try { + JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.JSONLexerTest_2$A\"}", VO.class); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); } public void test_a() throws Exception { diff --git a/src/test/java/com/alibaba/json/bvt/parser/error/TypeNotMatchError.java b/src/test/java/com/alibaba/json/bvt/parser/error/TypeNotMatchError.java new file mode 100644 index 0000000000..5db33d4a88 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/parser/error/TypeNotMatchError.java @@ -0,0 +1,40 @@ +package com.alibaba.json.bvt.parser.error; + +import org.junit.Assert; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONException; + +import junit.framework.TestCase; + +public class TypeNotMatchError extends TestCase { + + public void test_0() throws Exception { + JSON.parseObject("{\"value\":{\"@type\":\"com.alibaba.json.bvt.parser.error.TypeNotMatchError$AA\"}}", Model.class); + + Exception error = null; + try { + JSON.parseObject("{\"value\":{\"@type\":\"com.alibaba.json.bvt.parser.error.TypeNotMatchError$B\"}}", Model.class); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + private static class Model { + + public A value; + } + + private static class A { + + } + + private static class AA extends A { + + } + + private static class B { + + } +} diff --git a/src/test/java/com/alibaba/json/test/jackson/JacksonTest.java b/src/test/java/com/alibaba/json/test/jackson/JacksonTest.java index e8e731a596..400b12d9c9 100755 --- a/src/test/java/com/alibaba/json/test/jackson/JacksonTest.java +++ b/src/test/java/com/alibaba/json/test/jackson/JacksonTest.java @@ -15,4 +15,16 @@ public void test_0() throws Exception { mapper.readValue("{a:3}", Map.class); } + + public void test_1() throws Exception { + Model model = new Model(); + model.id = 1001; + ObjectMapper mapper = new ObjectMapper(); + String text = mapper.writeValueAsString(model); + System.out.println(text); + } + + public static class Model { + public int id; + } }