Search in sources :

Example 1 with SerializeBeanInfo

use of com.alibaba.fastjson.serializer.SerializeBeanInfo in project fastjson by alibaba.

the class TypeUtils method buildBeanInfo.

public static //
SerializeBeanInfo buildBeanInfo(//
Class<?> beanType, //
Map<String, String> aliasMap, PropertyNamingStrategy propertyNamingStrategy) {
    JSONType jsonType = beanType.getAnnotation(JSONType.class);
    // fieldName,field ,先生成fieldName的快照,减少之后的findField的轮询
    Map<String, Field> fieldCacheMap = new HashMap<String, Field>();
    ParserConfig.parserAllFieldToCache(beanType, fieldCacheMap);
    List<FieldInfo> fieldInfoList = computeGetters(beanType, jsonType, aliasMap, fieldCacheMap, false, propertyNamingStrategy);
    FieldInfo[] fields = new FieldInfo[fieldInfoList.size()];
    fieldInfoList.toArray(fields);
    String[] orders = null;
    final int features;
    String typeName = null;
    if (jsonType != null) {
        orders = jsonType.orders();
        typeName = jsonType.typeName();
        if (typeName.length() == 0) {
            typeName = null;
        }
        features = SerializerFeature.of(jsonType.serialzeFeatures());
    } else {
        features = 0;
    }
    FieldInfo[] sortedFields;
    List<FieldInfo> sortedFieldList;
    if (orders != null && orders.length != 0) {
        sortedFieldList = TypeUtils.computeGetters(beanType, jsonType, aliasMap, fieldCacheMap, true, propertyNamingStrategy);
    } else {
        sortedFieldList = new ArrayList<FieldInfo>(fieldInfoList);
        Collections.sort(sortedFieldList);
    }
    sortedFields = new FieldInfo[sortedFieldList.size()];
    sortedFieldList.toArray(sortedFields);
    if (Arrays.equals(sortedFields, fields)) {
        sortedFields = fields;
    }
    return new SerializeBeanInfo(beanType, jsonType, typeName, features, fields, sortedFields);
}
Also used : JSONField(com.alibaba.fastjson.annotation.JSONField) Field(java.lang.reflect.Field) SerializeBeanInfo(com.alibaba.fastjson.serializer.SerializeBeanInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JSONType(com.alibaba.fastjson.annotation.JSONType)

Aggregations

JSONField (com.alibaba.fastjson.annotation.JSONField)1 JSONType (com.alibaba.fastjson.annotation.JSONType)1 SerializeBeanInfo (com.alibaba.fastjson.serializer.SerializeBeanInfo)1 Field (java.lang.reflect.Field)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1