fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

注解判断

Open Humor-er opened this issue 10 years ago • 2 comments

public class Order { @LongCurrency private Long fee; }

重写ObjectSerializer void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) 判断如果Field有注解LongCurrency,则格式化属性值。但是接口的参数不充足。 上层接口为什么不能把整个fieldInfo传进去?

Humor-er avatar Mar 01 '16 20:03 Humor-er

这样能解决这个需求,只是又反射了一下

@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    if (object == null) {
        serializer.getWriter().writeNull();
        return;
    }
    String text = String.valueOf(object);
    try {
        Object ctxObject = serializer.getContext().getObject();
        Field field = ctxObject.getClass().getDeclaredField(String.valueOf(fieldName));
        Annotation annotation = field.getAnnotation(LongCurrency.class);
        if (annotation != null) {
            Long longVal = (Long) object;
            text =  Money.getMoneyString(longVal);
        }
    } catch (Exception e) {
    }
    serializer.write(text);
}

Humor-er avatar Mar 02 '16 03:03 Humor-er

版本:1.2.70 现象:serializer.getContext() return null

zcwease avatar Jun 02 '21 12:06 zcwease