Java Code Examples for com.alibaba.fastjson.annotation.JSONField#format()

The following examples show how to use com.alibaba.fastjson.annotation.JSONField#format() . 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: Rap2Generator.java    From rap2-generator with Apache License 2.0 6 votes vote down vote up
/**
 * <p class="detail">
 * 功能:判断字段类型是否为时间类型,若字段写了多个注解则优先级为DateTimeFormat(spring)>JSONField(fastjson)>JsonFormat(jackson),异常就返回""
 * </p>
 *
 * @param className :类名
 * @param fieldName :字段名
 * @return boolean
 * @author Kings
 * @date 2019.11.02
 */
private String getDayPattern(String className, String fieldName) {
    try {
        Class<?> parseClass = Class.forName(className);
        DateTimeFormat dateTimeFormat = parseClass.getDeclaredField(fieldName).getAnnotation(DateTimeFormat.class);
        if (dateTimeFormat != null) {
            return dateTimeFormat.pattern();
        }
        JSONField jsonField = parseClass.getDeclaredField(fieldName).getAnnotation(JSONField.class);
        if (jsonField != null) {
            return jsonField.format();
        }
        JsonFormat jsonFormat = parseClass.getDeclaredField(fieldName).getAnnotation(JsonFormat.class);
        if (jsonFormat != null) {
            return jsonFormat.pattern();
        }
        return "";
    } catch (Exception e) {
        return "";
    }
}
 
Example 2
Source File: Rap2WebGenerator.java    From rap2-generator with Apache License 2.0 6 votes vote down vote up
/**
 * <p class="detail">
 * 功能:判断字段类型是否为时间类型,若字段写了多个注解则优先级为DateTimeFormat(spring)>JSONField(fastjson)>JsonFormat(jackson),异常就返回""
 * </p>
 *
 * @param className :类名
 * @param fieldName :字段名
 * @return boolean
 * @author Kings
 * @date 2019.11.02
 */
private String getDayPattern(String javaDirPath,String className, String fieldName) {
    try {
        Class<?> parseClass = getCompileClass(javaDirPath,className);
        DateTimeFormat dateTimeFormat = parseClass.getDeclaredField(fieldName).getAnnotation(DateTimeFormat.class);
        if (dateTimeFormat != null) {
            return dateTimeFormat.pattern();
        }
        JSONField jsonField = parseClass.getDeclaredField(fieldName).getAnnotation(JSONField.class);
        if (jsonField != null) {
            return jsonField.format();
        }
        JsonFormat jsonFormat = parseClass.getDeclaredField(fieldName).getAnnotation(JsonFormat.class);
        if (jsonFormat != null) {
            return jsonFormat.pattern();
        }
        
        return "";
    } catch (Exception e) {
        return "";
    }
}
 
Example 3
Source File: MyObjectFieldSerializer.java    From dpCms with Apache License 2.0 5 votes vote down vote up
public MyObjectFieldSerializer(FieldInfo fieldInfo){
    super(fieldInfo);

    JSONField annotation = fieldInfo.getAnnotation(JSONField.class);

    if (annotation != null) {
        format = annotation.format();

        if (format.trim().length() == 0) {
            format = null;
        }

        for (SerializerFeature feature : annotation.serialzeFeatures()) {
            if (feature == SerializerFeature.WriteNullNumberAsZero) {
                writeNumberAsZero = true;
            } else if (feature == SerializerFeature.WriteNullStringAsEmpty) {
                writeNullStringAsEmpty = true;
            } else if (feature == SerializerFeature.WriteNullBooleanAsFalse) {
                writeNullBooleanAsFalse = true;
            } else if (feature == SerializerFeature.WriteNullListAsEmpty) {
                writeNullListAsEmpty = true;
            } else if (feature == SerializerFeature.WriteEnumUsingToString) {
                writeEnumUsingToString = true;
            }else if(feature == SerializerFeature.WriteEnumUsingName){
                writeEnumUsingName = true;
            }
        }
    }
}
 
Example 4
Source File: BuildParams.java    From Mars-Java with MIT License 4 votes vote down vote up
/**
 * 给参数赋值
 * @param field 字段
 * @param obj 对象
 * @param valList 数据
 * @throws Exception 异常
 */
private static void putAttr(Field field, Object obj, String[] valList) throws Exception{
    String fieldTypeName = field.getType().getSimpleName().toUpperCase();
    String valStr = valList[0];
    switch (fieldTypeName){
        case DataType.INT:
        case DataType.INTEGER:
            field.set(obj,Integer.parseInt(valStr));
            break;
        case DataType.BYTE:
            field.set(obj,Byte.parseByte(valStr));
            break;
        case DataType.STRING:
            field.set(obj,valStr);
            break;
        case DataType.CHAR:
        case DataType.CHARACTER:
            field.set(obj,valStr.charAt(0));
            break;
        case DataType.DOUBLE:
            field.set(obj,Double.parseDouble(valStr));
            break;
        case DataType.FLOAT:
            field.set(obj,Float.parseFloat(valStr));
            break;
        case DataType.LONG:
            field.set(obj,Long.parseLong(valStr));
            break;
        case DataType.SHORT:
            field.set(obj,Short.valueOf(valStr));
            break;
        case DataType.BOOLEAN:
            field.set(obj,Boolean.parseBoolean(valStr));
            break;
        case DataType.DATE:
            String fmt = "yyyy-MM-dd HH:mm:ss";
            JSONField jsonField = field.getAnnotation(JSONField.class);
            if(jsonField != null && !StringUtil.isNull(jsonField.format())){
                fmt = jsonField.format();
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
            field.set(obj,simpleDateFormat.parse(valStr));
            break;
        default:
            if (field.getType().equals(String[].class)){
                field.set(obj,valList);
            }
            break;
    }
}