com.alibaba.fastjson.annotation.JSONType Java Examples

The following examples show how to use com.alibaba.fastjson.annotation.JSONType. 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: SerializeBeanInfo.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public SerializeBeanInfo(Class<?> beanType, //
                         JSONType jsonType, //
                         String typeName, //
                         String typeKey,
                         int features,
                         FieldInfo[] fields, //
                         FieldInfo[] sortedFields
                         ){
    this.beanType = beanType;
    this.jsonType = jsonType;
    this.typeName = typeName;
    this.typeKey = typeKey;
    this.features = features;
    this.fields = fields;
    this.sortedFields = sortedFields;
}
 
Example #2
Source File: JavaBeanInfo.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public static Class<?> getBuilderClass(Class<?> clazz, JSONType type) {
    if (clazz != null && clazz.getName().equals("org.springframework.security.web.savedrequest.DefaultSavedRequest")) {
        return TypeUtils.loadClass("org.springframework.security.web.savedrequest.DefaultSavedRequest$Builder");
    }

    if (type == null) {
        return null;
    }

    Class<?> builderClass = type.builder();

    if (builderClass == Void.class) {
        return null;
    }

    return builderClass;
}
 
Example #3
Source File: TypeUtils.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private static boolean isJSONTypeIgnore(Class<?> clazz, String propertyName){
    JSONType jsonType = TypeUtils.getAnnotation(clazz,JSONType.class);
    if(jsonType != null){
        // 1、新增 includes 支持,如果 JSONType 同时设置了includes 和 ignores 属性,则以includes为准。
        // 2、个人认为对于大小写敏感的Java和JS而言,使用 equals() 比 equalsIgnoreCase() 更好,改动的唯一风险就是向后兼容性的问题
        // 不过,相信开发者应该都是严格按照大小写敏感的方式进行属性设置的
        String[] fields = jsonType.includes();
        if(fields.length > 0){
            for(int i = 0; i < fields.length; i++){
                if(propertyName.equals(fields[i])){
                    return false;
                }
            }
            return true;
        } else{
            fields = jsonType.ignores();
            for(int i = 0; i < fields.length; i++){
                if(propertyName.equals(fields[i])){
                    return true;
                }
            }
        }
    }
    if(clazz.getSuperclass() != Object.class && clazz.getSuperclass() != null){
        if(isJSONTypeIgnore(clazz.getSuperclass(), propertyName)){
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: TypeUtils.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated
 */
public static int getSerializeFeatures(Class<?> clazz){
    JSONType annotation = TypeUtils.getAnnotation(clazz,JSONType.class);
    if(annotation == null){
        return 0;
    }
    return SerializerFeature.of(annotation.serialzeFeatures());
}
 
Example #5
Source File: TypeUtils.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public static int getParserFeatures(Class<?> clazz){
    JSONType annotation = TypeUtils.getAnnotation(clazz,JSONType.class);
    if(annotation == null){
        return 0;
    }
    return Feature.of(annotation.parseFeatures());
}
 
Example #6
Source File: ASMSerializerFactory.java    From uavstack with Apache License 2.0 4 votes vote down vote up
private void _if_write_null(MethodVisitor mw, FieldInfo fieldInfo, Context context) {
    Class<?> propertyClass = fieldInfo.fieldClass;

    Label _if = new Label();
    Label _else = new Label();
    Label _write_null = new Label();
    Label _end_if = new Label();

    mw.visitLabel(_if);

    JSONField annotation = fieldInfo.getAnnotation();
    int features = 0;
    if (annotation != null) {
        features = SerializerFeature.of(annotation.serialzeFeatures());
    }
    JSONType jsonType = context.beanInfo.jsonType;
    if (jsonType != null) {
        features |= SerializerFeature.of(jsonType.serialzeFeatures());
    }

    int writeNullFeatures;
    if (propertyClass == String.class) {
        writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
                | SerializerFeature.WriteNullStringAsEmpty.getMask();
    } else if (Number.class.isAssignableFrom(propertyClass)) {
        writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
                | SerializerFeature.WriteNullNumberAsZero.getMask();
    } else if (Collection.class.isAssignableFrom(propertyClass)) {
        writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
                | SerializerFeature.WriteNullListAsEmpty.getMask();
    } else if (Boolean.class == propertyClass) {
        writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
                | SerializerFeature.WriteNullBooleanAsFalse.getMask();
    } else {
        writeNullFeatures = SerializerFeature.WRITE_MAP_NULL_FEATURES;
    }

    if ((features & writeNullFeatures) == 0) {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitLdcInsn(writeNullFeatures);
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
        mw.visitJumpInsn(IFEQ, _else);
    }

    mw.visitLabel(_write_null);

    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");

    _writeFieldName(mw, context);

    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitLdcInsn(features);
    // features

    if (propertyClass == String.class || propertyClass == Character.class) {
        mw.visitLdcInsn(SerializerFeature.WriteNullStringAsEmpty.mask);
    } else if (Number.class.isAssignableFrom(propertyClass)) {
        mw.visitLdcInsn(SerializerFeature.WriteNullNumberAsZero.mask);
    } else if (propertyClass == Boolean.class) {
        mw.visitLdcInsn(SerializerFeature.WriteNullBooleanAsFalse.mask);
    } else if (Collection.class.isAssignableFrom(propertyClass) || propertyClass.isArray()) {
        mw.visitLdcInsn(SerializerFeature.WriteNullListAsEmpty.mask);
    } else {
        mw.visitLdcInsn(0);
    }
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeNull", "(II)V");

    // seperator = ',';
    _seperator(mw, context);

    mw.visitJumpInsn(GOTO, _end_if);

    mw.visitLabel(_else);

    mw.visitLabel(_end_if);
}
 
Example #7
Source File: TypeCollector.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public void visitAnnotation(String desc) {
    if (JSONType.equals(desc)) {
        jsonType = true;
    }
}
 
Example #8
Source File: TypeUtils.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String,String> aliasMap, boolean sorted){
    JSONType jsonType = TypeUtils.getAnnotation(clazz,JSONType.class);
    Map<String,Field> fieldCacheMap = new HashMap<String,Field>();
    ParserConfig.parserAllFieldToCache(clazz, fieldCacheMap);
    return computeGetters(clazz, jsonType, aliasMap, fieldCacheMap, sorted, PropertyNamingStrategy.CamelCase);
}
 
Example #9
Source File: JavaBeanInfo.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
 * @deprecated
 */
public static Class<?> getBuilderClass(JSONType type) {
    return getBuilderClass(null, type);
}