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

[FEATURE]支持MongoDB的Date类型和java.util.Date类型的转换 #2614

Closed
ocean23 opened this issue May 22, 2024 · 5 comments
Closed

[FEATURE]支持MongoDB的Date类型和java.util.Date类型的转换 #2614

ocean23 opened this issue May 22, 2024 · 5 comments
Labels
enhancement New feature or request fixed
Milestone

Comments

@ocean23
Copy link
Contributor

ocean23 commented May 22, 2024

请描述您的需求或者改进建议

背景如下:
由于我们非常重度的依赖MongoDB数据库,而MongoDB返回日期(Date)的JSON字符串并不是一个标准的JSON,所以fastjson2需要适配才行

  1. 以下是MongoDB存储日期数据ttl的内容
{ 
    "ttl" : ISODate("2024-05-21T12:08:45.461+0000")
}
  1. 以下是通过org.bson.Document获取到json字符串的内容
Document document = event.getFullDocument();
document.toJson()
{"ttl": {"$date": "2024-05-21T16:15:19.371Z"}}
  1. 这时当我们执行转换时由于fastjson2没有对mongodb的日期类型做特别的适配,所以就会导致报错
    @Getter
    @Setter
    public class Message {
        private Date ttl;
    }

Message  data = JSON.parseObject(document.toJson(), Message.class);

请描述你建议的实现方案

我已经写好的PR, 让fastjson2支持mongodb的日期类型转换为java.util.Date类型,一会我把PR发出来

@ocean23 ocean23 added the enhancement New feature or request label May 22, 2024
@ocean23
Copy link
Contributor Author

ocean23 commented May 22, 2024

这时Message的内容:

    @Getter
    @Setter
    public class Message {
        private Date ttl;
    }

@wenshao wenshao added this to the 2.0.51 milestone May 22, 2024
@wenshao
Copy link
Member

wenshao commented May 22, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证

@wenshao wenshao added the fixed label May 22, 2024
@ocean23
Copy link
Contributor Author

ocean23 commented May 22, 2024

@wenshao 温少,你的手速太快了,我已经测试过没问题了,我看了你刚刚优化的代码,比我的版本更优美, 我是修改ObjectReaderAdapter这个class, 我这个版本感觉有一点硬编码。

                if (fieldValue.getClass() == fieldReader.fieldType) {
                    fieldReader.accept(object, fieldValue);
                } else {
                    if ((fieldReader instanceof FieldReaderList)
                            && fieldValue instanceof JSONArray
                    ) {
                        ObjectReader objectReader = fieldReader.getObjectReader(provider);
                        Object fieldValueList = objectReader.createInstance((JSONArray) fieldValue);
                        fieldReader.accept(object, fieldValueList);
                        continue;
                    } else if (fieldValue instanceof JSONObject && fieldReader.fieldType == Date.class) {
                        JSONObject fieldJSONObject = (JSONObject)fieldValue;
                        Object dateString = fieldJSONObject.get("$date");
                        if (dateString != null) {
                            fieldReader.accept(object, dateString.toString());
                        }
                        continue;
                    }
                    else if (fieldValue instanceof JSONObject
                            && fieldReader.fieldType != JSONObject.class
                    ) {
                        JSONObject jsonObject = (JSONObject) fieldValue;
                        Object fieldValueJavaBean = provider
                                .getObjectReader(fieldReader.fieldType, fieldBased)
                                .createInstance(jsonObject, features);
                        fieldReader.accept(object, fieldValueJavaBean);
                        continue;
                    }

                    fieldReader.acceptAny(object, fieldValue, features);
                }

@wenshao
Copy link
Member

wenshao commented May 22, 2024

@ocean23 欢迎后续参与进来!

@ocean23 ocean23 closed this as completed May 24, 2024
@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed
Projects
None yet
Development

No branches or pull requests

2 participants