本文整理汇总了Java中com.alibaba.fastjson.util.TypeUtils.castToJavaBean方法的典型用法代码示例。如果您正苦于以下问题:Java TypeUtils.castToJavaBean方法的具体用法?Java TypeUtils.castToJavaBean怎么用?Java TypeUtils.castToJavaBean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.util.TypeUtils
的用法示例。
在下文中一共展示了TypeUtils.castToJavaBean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_bean_3
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_bean_3() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
PO vo = TypeUtils.castToJavaBean(map, PO.class);
Assert.assertEquals(123, vo.id);
SerializeWriter out = new SerializeWriter();
try {
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", (String) null)));
serializer.write(vo);
Assert.assertEquals("{}", out.toString());
} finally {
out.close();
}
}
示例2: test_bean_2
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_bean_2() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
PO vo = TypeUtils.castToJavaBean(map, PO.class);
Assert.assertEquals(123, vo.id);
SerializeWriter out = new SerializeWriter();
try {
SerializeConfig config = new SerializeConfig();
JSONSerializer serializer = new JSONSerializer(out, config);
config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", "ID")));
serializer.write(vo);
Assert.assertEquals("{\"ID\":123}", out.toString());
} finally {
out.close();
}
}
示例3: getObject
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public <T> T getObject(int index, Type type) {
Object obj = list.get(index);
if (type instanceof Class) {
return (T) TypeUtils.castToJavaBean(obj, (Class) type);
} else {
String json = JSON.toJSONString(obj);
return (T) JSON.parseObject(json, type);
}
}
示例4: test_castToJavaBean_StackTraceElement
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_castToJavaBean_StackTraceElement() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("className", "java.lang.Object");
map.put("methodName", "hashCode");
StackTraceElement element = TypeUtils.castToJavaBean(map, StackTraceElement.class, null);
Assert.assertEquals("java.lang.Object", element.getClassName());
Assert.assertEquals("hashCode", element.getMethodName());
Assert.assertEquals(null, element.getFileName());
}
示例5: test_castToJavaBean_StackTraceElement_1
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_castToJavaBean_StackTraceElement_1() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("className", "java.lang.Object");
map.put("methodName", "hashCode");
map.put("lineNumber", 12);
StackTraceElement element = TypeUtils.castToJavaBean(map, StackTraceElement.class, null);
Assert.assertEquals("java.lang.Object", element.getClassName());
Assert.assertEquals("hashCode", element.getMethodName());
Assert.assertEquals(null, element.getFileName());
Assert.assertEquals(12, element.getLineNumber());
}
示例6: test_castToJavaBean_type
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_castToJavaBean_type() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("@type", "java.lang.StackTraceElement");
map.put("className", "java.lang.Object");
map.put("methodName", "hashCode");
map.put("lineNumber", 12);
StackTraceElement element = (StackTraceElement) TypeUtils.castToJavaBean(map, Object.class, null);
Assert.assertEquals("java.lang.Object", element.getClassName());
Assert.assertEquals("hashCode", element.getMethodName());
Assert.assertEquals(null, element.getFileName());
Assert.assertEquals(12, element.getLineNumber());
}
示例7: test_error
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_error() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("@type", "xxx");
Exception error = null;
try {
TypeUtils.castToJavaBean(map, Object.class, null);
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
示例8: test_error2
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_error2() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("@type", "");
Exception error = null;
try {
TypeUtils.castToJavaBean(map, Object.class, null);
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
示例9: test_bean
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_bean() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
Entity vo = TypeUtils.castToJavaBean(map, Entity.class);
Assert.assertEquals(123, vo.getId());
Assert.assertEquals("{\"id\":123}", JSON.toJSONString(vo));
}
示例10: test_deny
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_deny() throws Exception {
JSONObject object = new JSONObject();
object.put("@type", "com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase");
Throwable error = null;
try {
TypeUtils.castToJavaBean(object, Object.class);
} catch (Exception ex) {
error = ex;
}
assertNotNull(error);
}
示例11: test_error
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_error() throws Exception {
JSONObject json = new JSONObject();
json.put("id", 1);
JSONException error = null;
try {
TypeUtils.castToJavaBean(json, C.class, ParserConfig.getGlobalInstance());
} catch (JSONException e) {
error = e;
}
Assert.assertNotNull(error);
}
示例12: test_castToJavaBean
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_castToJavaBean() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
map.put("name", "abc");
VO vo = TypeUtils.castToJavaBean(map, VO.class, null);
Assert.assertEquals(123, vo.getId());
Assert.assertEquals("abc", vo.getName());
Assert.assertEquals("{\"name\":\"abc\",\"id\":123}", JSON.toJSONString(vo));
}
示例13: test_castToJavaBean_v2
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_castToJavaBean_v2() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
map.put("name", "abc");
V2 vo = TypeUtils.castToJavaBean(map, V2.class, null);
Assert.assertEquals(123, vo.getId());
Assert.assertEquals("abc", vo.getName());
Assert.assertEquals("{\"name\":\"abc\",\"id\":123}", JSON.toJSONString(vo));
}
示例14: test_castToJavaBean_v3
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public void test_castToJavaBean_v3() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 123);
map.put("name", "abc");
V3 vo = TypeUtils.castToJavaBean(map, V3.class, null);
Assert.assertEquals(123, vo.getId());
Assert.assertEquals("abc", vo.getName());
Assert.assertEquals("{\"name\":\"abc\",\"id\":123}", JSON.toJSONString(vo));
}
示例15: getObject
import com.alibaba.fastjson.util.TypeUtils; //导入方法依赖的package包/类
public <T> T getObject(String key, Class<T> clazz){
Object obj = get(key);
return TypeUtils.castToJavaBean(obj, clazz);
}