diff --git a/src/main/java/com/alibaba/fastjson/JSONObject.java b/src/main/java/com/alibaba/fastjson/JSONObject.java index 51db38969..80ddc3f2c 100644 --- a/src/main/java/com/alibaba/fastjson/JSONObject.java +++ b/src/main/java/com/alibaba/fastjson/JSONObject.java @@ -42,6 +42,7 @@ import java.util.Map; import java.util.Set; import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.fastjson.parser.ParserConfig; import com.alibaba.fastjson.util.TypeUtils; /** @@ -365,7 +366,7 @@ public class JSONObject extends JSON implements Map, JSONAware, if (parameterTypes.length == 0) { Class returnType = method.getReturnType(); - if (returnType == Void.class) { + if (returnType == void.class) { throw new JSONException("illegal getter"); } @@ -396,9 +397,10 @@ public class JSONObject extends JSON implements Map, JSONAware, } } - return map.get(name); + Object value = map.get(name); + return TypeUtils.cast(value, method.getGenericReturnType(), ParserConfig.getGlobalInstance()); } - return null; + throw new UnsupportedOperationException(method.toGenericString()); } } 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 922807084..b280702c3 100644 --- a/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java +++ b/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java @@ -1,8 +1,5 @@ package com.alibaba.fastjson.parser.deserializer; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.ArrayList; @@ -13,7 +10,6 @@ import java.util.Map; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; -import com.alibaba.fastjson.annotation.JSONCreator; import com.alibaba.fastjson.parser.DefaultExtJSONParser; import com.alibaba.fastjson.parser.DefaultExtJSONParser.ResolveTask; import com.alibaba.fastjson.parser.Feature; diff --git a/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest.java b/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest.java index abd3b0dd5..4311099e6 100644 --- a/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest.java +++ b/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest.java @@ -168,6 +168,11 @@ public class JSONObjectTest extends TestCase { Assert.assertTrue(json.getJSONObject("obj") == null); } + + public void test_bytes () throws Exception { + JSONObject object = new JSONObject(); + Assert.assertNull(object.getBytes("bytes")); + } public void test_getObject() throws Exception { JSONObject json = new JSONObject(); diff --git a/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest3.java b/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest3.java new file mode 100644 index 000000000..275082cc0 --- /dev/null +++ b/src/test/java/com/alibaba/json/test/bvt/JSONObjectTest3.java @@ -0,0 +1,182 @@ +package com.alibaba.json.test.bvt; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONException; +import com.alibaba.fastjson.annotation.JSONField; + +public class JSONObjectTest3 extends TestCase { + + public void test_0() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + Assert.assertEquals("123", bean.getValue()); + Assert.assertEquals(false, bean.isBig()); + Assert.assertEquals(123, bean.getIntValue()); + + bean.setBig(true); + Assert.assertEquals(true, bean.isBig()); + + bean.setID(567); + Assert.assertEquals(567, bean.getID()); + + } + + public void test_error_0() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.f(); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_1() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.f(1); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_2() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.get(); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_3() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.is(); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_4() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + Exception error = null; + try { + bean.f(1, 2); + } catch (UnsupportedOperationException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_5() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.getA(); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_6() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.f1(1); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_7() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.set(1); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public void test_error_8() throws Exception { + String text = "{value:'123',big:false}"; + Bean bean = JSON.parseObject(text, Bean.class); + + JSONException error = null; + try { + bean.xx(); + } catch (JSONException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + public static interface Bean { + + String getValue(); + + void setValue(String value); + + boolean isBig(); + + @JSONField + void setBig(boolean value); + + @JSONField(name = "value") + int getIntValue(); + + @JSONField(name = "id") + void setID(int value); + + @JSONField(name = "id") + int getID(); + + Object get(); + + Object xx(); + + void set(int i); + + boolean is(); + + void getA(); + + void f(); + + Object f(int a); + + void f1(int a); + + void f(int a, int b); + } +}