Open 1029219814 opened 6 months ago
@Test
public void test() {
final AtomicReference<String> formatRef = new AtomicReference<>();
ContextValueFilter contextValueFilter = new ContextValueFilter() {
@Override
public Object process(BeanContext context, Object object, String name, Object value) {
formatRef.set(context.getFormat());
return value;
}
};
Bean bean = new Bean();
bean.id = LocalDate.of(2012, 3, 4);
JSON.toJSONString(bean, contextValueFilter);
assertEquals("yyyyMMdd", formatRef.get());
}
public static class Bean {
@JSONField(format = "yyyyMMdd")
public LocalDate id;
}
你看下这个ContextValueFilter是不是你想要的
不能像以前一样方便么,不去主动传入Filer,因为是统一进行JSON.toJSONString的
@Test public void test() { final AtomicReference<String> formatRef = new AtomicReference<>(); ContextValueFilter contextValueFilter = new ContextValueFilter() { @Override public Object process(BeanContext context, Object object, String name, Object value) { formatRef.set(context.getFormat()); return value; } }; Bean bean = new Bean(); bean.id = LocalDate.of(2012, 3, 4); JSON.toJSONString(bean, contextValueFilter); assertEquals("yyyyMMdd", formatRef.get()); } public static class Bean { @JSONField(format = "yyyyMMdd") public LocalDate id; }
你看下这个ContextValueFilter是不是你想要的
请描述您的问题
询问有关本项目的使用和其他方面的相关问题。 在fastjson1中,继承ContextObjectSerializer,我可以通过BeanContext context获取JSONField注解中的format字段,类似
@JSONField(serializeUsing = CustomerBigDecimalCodec.class, format = "#,###.####")
在CustomerBigDecimalCodec中,可以对不同字段实现不同精度的序列化,但是在fastjson2中。如何实现类似的需求,通过继承ObjectWriter
,JSONWriter无法获取JSONField注解上format的值