Java Code Examples for com.alibaba.fastjson.util.TypeUtils#cast()
The following examples show how to use
com.alibaba.fastjson.util.TypeUtils#cast() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityToVoMapper.java From mPaaS with Apache License 2.0 | 6 votes |
/** 单值数据类型转换 */ private Object castSingleValue(MapperContext context, Object srcValue, Class<?> targetClass) throws Exception { if (srcValue instanceof IEntity) { // 源值是Entity,拷贝一份 Object targetValue = targetClass.newInstance(); String entityClass = EntityUtil.getEntityClassName(srcValue); MetaEntity meta = MetaEntity.localEntity(entityClass); if (meta == null) { log.warn(entityClass + "的数据字典信息为空."); } MapperContext child = new MapperContext(context, meta, srcValue, targetValue); copyProperties(child); return targetValue; } else { // 其他,直接转 return TypeUtils.cast(srcValue, targetClass, null); } }
Example 2
Source File: EntityToVoMapper.java From mPass with Apache License 2.0 | 6 votes |
/** 单值数据类型转换 */ private Object castSingleValue(MapperContext context, Object srcValue, Class<?> targetClass) throws Exception { if (srcValue instanceof IEntity) { // 源值是Entity,拷贝一份 Object targetValue = targetClass.newInstance(); String entityClass = EntityUtil.getEntityClassName(srcValue); MetaEntity meta = MetaEntity.localEntity(entityClass); if (meta == null) { log.warn(entityClass + "的数据字典信息为空."); } MapperContext child = new MapperContext(context, meta, srcValue, targetValue); copyProperties(child); return targetValue; } else { // 其他,直接转 return TypeUtils.cast(srcValue, targetClass, null); } }
Example 3
Source File: TypeUtil.java From mPaaS with Apache License 2.0 | 5 votes |
/** * 转换单值 */ static Object castObject(Object value, Class<?> clazz) { if (clazz.isAssignableFrom(value.getClass())) { return value; } if (CURRENT.equals(value)) { if (Date.class.isAssignableFrom(clazz)) { return new Date(); } } if (IEntity.class.isAssignableFrom(clazz)) { return castEntity(value, clazz); } if (IEnum.class.isAssignableFrom(clazz)) { return castEnum(value, clazz); } if (String.class.equals(clazz) && value instanceof IData) { if (value instanceof IData) { return ((IData) value).getFdId(); } else if (value instanceof Map) { for (String key : IDKEYS) { Object val = ((Map<?, ?>) value).get(key); if (val != null && val instanceof String) { return (String) val; } } } } return TypeUtils.cast(value, clazz, null); }
Example 4
Source File: JSONArray.java From uavstack with Apache License 2.0 | 5 votes |
/** * @since 1.2.23 */ public <T> List<T> toJavaList(Class<T> clazz) { List<T> list = new ArrayList<T>(this.size()); ParserConfig config = ParserConfig.getGlobalInstance(); for (Object item : this) { T classItem = (T) TypeUtils.cast(item, clazz, config); list.add(classItem); } return list; }
Example 5
Source File: JSONObject.java From uavstack with Apache License 2.0 | 5 votes |
public <T> T getObject(String key, TypeReference typeReference) { Object obj = map.get(key); if (typeReference == null) { return (T) obj; } return TypeUtils.cast(obj, typeReference.getType(), ParserConfig.getGlobalInstance()); }
Example 6
Source File: ResolveFieldDeserializer.java From uavstack with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public void setValue(Object object, Object value) { if (map != null) { map.put(key, value); return; } if (collection != null) { collection.add(value); return; } list.set(index, value); if (list instanceof JSONArray) { JSONArray jsonArray = (JSONArray) list; Object array = jsonArray.getRelatedArray(); if (array != null) { int arrayLength = Array.getLength(array); if (arrayLength > index) { Object item; if (jsonArray.getComponentType() != null) { item = TypeUtils.cast(value, jsonArray.getComponentType(), parser.getConfig()); } else { item = value; } Array.set(array, index, item); } } } }
Example 7
Source File: JsonDataCodec.java From spring-boot-protocol with Apache License 2.0 | 5 votes |
protected Object cast(Object value, Class<?> type) { try { return TypeUtils.cast(value,type,parserConfig); }catch (Exception e){ return value; } }
Example 8
Source File: TypeUtil.java From mPass with Apache License 2.0 | 5 votes |
/** * 转换简单类型 */ static Object castSimple(Object value, String type) { Class<?> clazz = EntityUtil.TYPEMAP.get(type); if (clazz == null) { throw new ParamsNotValidException(StringHelper.join("无法转换类型:", value.getClass().getName(), "->", type)); } else { return TypeUtils.cast(value, clazz, null); } }
Example 9
Source File: TypeUtil.java From mPass with Apache License 2.0 | 5 votes |
/** * 转换枚举 */ @SuppressWarnings({ "unchecked", "rawtypes" }) static Object castEnum(Object value, Class<?> clazz) { Class<?> valueClass = ReflectUtil.getActualClass(clazz, IEnum.class, "V"); Object enumValue = TypeUtils.cast(value, valueClass, null); return IEnum.valueOf((Class<IEnum>) clazz, enumValue); }
Example 10
Source File: TypeUtil.java From mPass with Apache License 2.0 | 5 votes |
/** * 转换单值 */ static Object castObject(Object value, Class<?> clazz) { if (clazz.isAssignableFrom(value.getClass())) { return value; } if (CURRENT.equals(value)) { if (Date.class.isAssignableFrom(clazz)) { return new Date(); } } if (IEntity.class.isAssignableFrom(clazz)) { return castEntity(value, clazz); } if (IEnum.class.isAssignableFrom(clazz)) { return castEnum(value, clazz); } if (String.class.equals(clazz) && value instanceof IData) { if (value instanceof IData) { return ((IData) value).getFdId(); } else if (value instanceof Map) { for (String key : IDKEYS) { Object val = ((Map<?, ?>) value).get(key); if (val != null && val instanceof String) { return (String) val; } } } } return TypeUtils.cast(value, clazz, null); }
Example 11
Source File: TypeUtil.java From mPaaS with Apache License 2.0 | 5 votes |
/** * 转换简单类型 */ static Object castSimple(Object value, String type) { Class<?> clazz = EntityUtil.TYPEMAP.get(type); if (clazz == null) { throw new ParamsNotValidException(StringHelper.join("无法转换类型:", value.getClass().getName(), "->", type)); } else { return TypeUtils.cast(value, clazz, null); } }
Example 12
Source File: ExcelUtils.java From poi-excel-utils with Apache License 2.0 | 5 votes |
private static void setProperty(Object obj, String fieldName, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { PropertyDescriptor pd = getPropertyDescriptor(obj.getClass(), fieldName); if (pd == null || pd.getWriteMethod() == null) { throw new IllegalStateException("In class" + obj.getClass() + "no setter method found for field '" + fieldName + "'"); } Class<?> paramType = pd.getWriteMethod().getParameterTypes()[0]; if (value != null && !paramType.isAssignableFrom(value.getClass())) { value = TypeUtils.cast(value, paramType, null); } pd.getWriteMethod().invoke(obj, value); }
Example 13
Source File: PageQueryTemplate.java From mPass with Apache License 2.0 | 4 votes |
/** * 转VO对象 */ private <T> T toViewObject(Tuple tuple, ViewInfo viewInfo, Class<T> clazz, T vo) { for (Entry<String, ViewInfo> entry : viewInfo.children.entrySet()) { ViewInfo child = entry.getValue(); Object value = null; if (child.arrayElementType != null && child.children != null) { // 数组,先从VO中获取列表,若列表为空则创建,然后往列表中追加子 List<Object> list = null; if (vo == null) { vo = ReflectUtil.newInstance(clazz); } else { list = readProperty(vo, child.desc); } if (list == null) { list = new ArrayList<>(); writeProperty(vo, child.desc, list); } value = toViewObject(tuple, child, child.arrayElementType, null); if(value != null) { list.add(value); } } else { if (child.children != null) { // 子对象 value = toViewObject(tuple, child, child.desc.getPropertyType(), null); } else if (child.index > -1) { // 普通字段 value = tuple.get(child.index); value = TypeUtils.cast(value, child.desc.getPropertyType(), null); if (child.langIndex > -1) { Object langValue = tuple.get(child.langIndex); langValue = TypeUtils.cast(langValue, child.desc.getPropertyType(), null); if (StringUtils.isNotBlank((String) langValue)) { value = langValue; } } } if (value != null) { if (vo == null) { vo = ReflectUtil.newInstance(clazz); } writeProperty(vo, child.desc, value); } } } return vo; }
Example 14
Source File: JSONObject.java From uavstack with Apache License 2.0 | 4 votes |
public <T> T getObject(String key, Type type) { Object obj = map.get(key); return TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance()); }
Example 15
Source File: PageQueryTemplate.java From mPaaS with Apache License 2.0 | 4 votes |
/** * 转VO对象 */ private <T> T toViewObject(Tuple tuple, ViewInfo viewInfo, Class<T> clazz, T vo) { for (Entry<String, ViewInfo> entry : viewInfo.children.entrySet()) { ViewInfo child = entry.getValue(); Object value = null; if (child.arrayElementType != null && child.children != null) { // 数组,先从VO中获取列表,若列表为空则创建,然后往列表中追加子 List<Object> list = null; if (vo == null) { vo = ReflectUtil.newInstance(clazz); } else { list = readProperty(vo, child.desc); } if (list == null) { list = new ArrayList<>(); writeProperty(vo, child.desc, list); } value = toViewObject(tuple, child, child.arrayElementType, null); if(value != null) { list.add(value); } } else { if (child.children != null) { // 子对象 value = toViewObject(tuple, child, child.desc.getPropertyType(), null); } else if (child.index > -1) { // 普通字段 value = tuple.get(child.index); value = TypeUtils.cast(value, child.desc.getPropertyType(), null); if (child.langIndex > -1) { Object langValue = tuple.get(child.langIndex); langValue = TypeUtils.cast(langValue, child.desc.getPropertyType(), null); if (StringUtils.isNotBlank((String) langValue)) { value = langValue; } } } if (value != null) { if (vo == null) { vo = ReflectUtil.newInstance(clazz); } writeProperty(vo, child.desc, value); } } } return vo; }
Example 16
Source File: JSON.java From uavstack with Apache License 2.0 | 4 votes |
public static <T> T toJavaObject(JSON json, Class<T> clazz) { return TypeUtils.cast(json, clazz, ParserConfig.getGlobalInstance()); }
Example 17
Source File: JSON.java From uavstack with Apache License 2.0 | 4 votes |
/** * @since 1.2.9 */ public <T> T toJavaObject(Class<T> clazz) { return TypeUtils.cast(this, clazz, ParserConfig.getGlobalInstance()); }
Example 18
Source File: JSON.java From uavstack with Apache License 2.0 | 4 votes |
/** * @since 1.2.33 */ public <T> T toJavaObject(Type type) { return TypeUtils.cast(this, type, ParserConfig.getGlobalInstance()); }
Example 19
Source File: JSON.java From uavstack with Apache License 2.0 | 4 votes |
/** * @since 1.2.33 */ public <T> T toJavaObject(TypeReference typeReference) { Type type = typeReference != null ? typeReference.getType() : null; return TypeUtils.cast(this, type, ParserConfig.getGlobalInstance()); }
Example 20
Source File: FetchTaskProcessor.java From vscrawler with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") List<Seed> injectField(final AbstractAutoProcessModel model, AbstractSelectable abstractSelectable, final GrabResult crawlResult) { List<Seed> newSeeds = Lists.newLinkedList(); for (final FetchTaskBean fetchTaskBean : fetchTaskBeanList) { try { Field field = fetchTaskBean.getField(); final Class<?> type = field.getType(); //处理循环的model,支持子结构抽取 if (AbstractAutoProcessModel.class.isAssignableFrom(type) && annotationProcessorBuilder.findExtractor((Class<? extends AbstractAutoProcessModel>) type) != null) { AbstractSelectable subSelectable = fetchTaskBean.getModelSelector().select(abstractSelectable); field.set(model, fetchSubModel(type, subSelectable, model, crawlResult)); continue; } Object data = null; if (Collection.class.isAssignableFrom(type) && fetchTaskBean.getHelpClazz() != Object.class && annotationProcessorBuilder.findExtractor(fetchTaskBean.getHelpClazz()) != null) { List<AbstractSelectable> abstractSelectables = fetchTaskBean.getModelSelector().select(abstractSelectable).toMultiSelectable(); data = Lists.transform(abstractSelectables, new Function<AbstractSelectable, AbstractAutoProcessModel>() { @Override public AbstractAutoProcessModel apply(AbstractSelectable input) { return fetchSubModel(fetchTaskBean.getHelpClazz(), input, model, crawlResult); } }); } //非子model抽取,需要直接抽取到结果,结束抽取链,判断抽取结果类型,进行数据类型转换操作 if (data == null) { data = fetchTaskBean.getModelSelector().select(abstractSelectable).createOrGetModel(); } //特殊逻辑,因为SipNode对象同时持有字符串或者dom对象,所以需要对他进行拆箱 data = unPackSipNode(data); //如果目标类型不是集合或者数组,且源数据为集合,则进行集合拆箱 data = unpackCollection(type, data); if (data == null) { continue; } Object transformedObject = TypeCastUtils.cast(data, type, fetchTaskBean.getHelpClazz()); if (transformedObject == null) { transformedObject = TypeUtils.cast(data, type, ParserConfig.getGlobalInstance()); } field.set(model, transformedObject); if (fetchTaskBean.isNewSeed()) { newSeeds.addAll(handleNewSeed(transformedObject, model.getBaseUrl())); } } catch (Exception e) { throw new RuntimeException("can not inject data for model:" + model.getClass().getName() + " for field: " + fetchTaskBean.getField().getName(), e); } } return newSeeds; }