Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JSONField(format="")设置无效 #380

Closed
youandeanyou opened this issue May 30, 2022 · 7 comments
Closed

@JSONField(format="")设置无效 #380

youandeanyou opened this issue May 30, 2022 · 7 comments
Labels
bug Something isn't working
Milestone

Comments

@youandeanyou
Copy link

2.0.5版本中,实体类中时间字段设置@JSONField(format="")注解后,只有JSON.toJSONString()可以生效,但对外输出只依赖全局设置的dateFormat。

示例代码:
https://github.com/youandeanyou/springboot-demo
访问 http://localhost:8081/test
期望结果:{"current":"2022-05-30 10:35:56","history":"2022-05-30 10","tomorrow":"2022-05-30 10:35","yesterday":"2022-05-30"}
实际结果:{"current":"2022-05-30 10:35:56","history":"2022-05-30 10:35:56","tomorrow":"2022-05-30 10:35:56","yesterday":"2022-05-30 00:00:00"}

@youandeanyou youandeanyou added the bug Something isn't working label May 30, 2022
@wenshao wenshao added this to the 2.0.6 milestone May 30, 2022
@wenshao
Copy link
Member

wenshao commented May 30, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

问题已经修复,请用2.0.6-SNAPSHOT版本验证,2.0.6正式版预计6月5日前发布

@prog-ape
Copy link

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

问题已经修复,请用2.0.6-SNAPSHOT版本验证,2.0.6正式版预计6月5日前发布

温少,我想问一下,这快照如何引用,仓库地址要改成哪个?

@wenshao
Copy link
Member

wenshao commented May 31, 2022

@youandeanyou
Copy link
Author

youandeanyou commented May 31, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

问题已经修复,请用2.0.6-SNAPSHOT版本验证,2.0.6正式版预计6月5日前发布

已验证,已解决。

我还有个疑问,关于FieldBased的。
如果我按如下设置:
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
// 这里如果设置了FieldBased,@JSONField注解就生不效了
// config.setWriterFeatures(JSONWriter.Feature.FieldBased, JSONWriter.Feature.NullAsDefaultValue);
// 如果去掉FieldBased,@JSONField又生效了
config.setWriterFeatures(JSONWriter.Feature.NullAsDefaultValue);
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setFastJsonConfig(config);
List mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON);
converter.setSupportedMediaTypes(mediaTypes);
converters.add(0, converter);
super.addDefaultHttpMessageConverters(converters);

我看文档中,关于FieldBased描述是:“基于字段反序列化,如果不配置,会默认基于public的field和getter方法序列化。配置后,会基于非static的field(包括private)做反序列化。”。为什么会有这样的问题?

@xionggit
Copy link

xionggit commented Jun 1, 2022

有一个问题,和这个bug是类似的,在fastjson1中,设置所有date 的默认格式可以这样写
JSON.DEFFAULT_DATE_FORMAT= "yyyyMMdd";
String json = JSON.toJSONString(object,SerializerFeature.WriteDateUseDateFormat);

但是在fastjson2的2.0.6中JSONWrite.Feature中好像不支持了?

@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.6
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jun 4, 2022
@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

@youandeanyou @xionggit 不同的问题不要在同一个issue里讨论,要不然会漏了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants