Skip to content

Commit

Permalink
check type match
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 7, 2016
1 parent f49684b commit ce6b9e4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,14 @@ protected <T> 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);
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/com/alibaba/json/bvt/parser/JSONLexerTest_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}
}
12 changes: 12 additions & 0 deletions src/test/java/com/alibaba/json/test/jackson/JacksonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit ce6b9e4

Please sign in to comment.