git-svn-id: http://code.alibabatech.com/svn/fastjson/trunk/fastjson@1311 fb902949-7fc4-4485-a80f-4d6ff335c879
This commit is contained in:
wenshao
2011-08-13 02:56:08 +00:00
parent 2e54fd9682
commit 512e047eae
4 changed files with 192 additions and 7 deletions
@@ -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<String, Object>, 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<String, Object>, 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());
}
}
@@ -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;
@@ -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();
@@ -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);
}
}