Open mystox opened 1 month ago
@mystox 看代码走向,跟非public类有关。推荐使用 XCodeMap 走读源码,追踪对象字段的生命周期,快速定位有效分支。 https://xcodemap.tech/
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.54-SNAPSHOT/ 问题已修复,请帮忙用2.0.54-SNAPSHOT快照版本验证,2.0.54正式版本预计在月底发布
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.54-SNAPSHOT/ 问题已修复,请帮忙用2.0.54-SNAPSHOT快照版本验证,2.0.54正式版本预计在月底发布
验证符合期望,辛苦
问题描述
实体中字段为Boolean时 想将对应空值转成“”时报错, 其中active 修饰为public的时候正常, 请问这种情况是设计如此还是存在bug? 补充一下 似乎 Integer也有这个问题,自定义的ObjectWriter 不生效
@Data class User { public String name; public Integer age; private Boolean active;
}
public class Fastjson2ValueFilterExample { public static void main(String[] args) { User user = new User("Alice", null, null);
}
提示报错 Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean at com.alibaba.fastjson2.writer.ObjectWriterImplBoolean.write(ObjectWriterImplBoolean.java:25) at com.alibaba.fastjson2.writer.ObjectWriterAdapter.writeWithFilter(ObjectWriterAdapter.java:577) at com.alibaba.fastjson2.writer.ObjectWriter3.write(ObjectWriter3.java:63) at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:3083) at com.itime.debtdefuse.assets.util.Fastjson2ValueFilterExample.main(Fastjson2ValueFilterExample.java:34)
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.writer.ObjectWriter;
import java.lang.reflect.Type; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map;
/**
@author Baoyi Chen */ public class Main { public static void main(String[] args) { JSON.register(Integer.class, new ObjectWriter() {
@Override
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
Integer value = (Integer) object;
if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(Long.toString(value));
}
});
}
public static class Test { private Integer value; private BigDecimal value1;
} }