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

java.util.Date字段反序列化可以支持unixtime #392

Closed
dance3366 opened this issue Jun 1, 2022 · 5 comments
Closed

java.util.Date字段反序列化可以支持unixtime #392

dance3366 opened this issue Jun 1, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@dance3366
Copy link

dance3366 commented Jun 1, 2022

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

java.util.Date字段反序列化可以支持unixtime,目前直接转换成1970年

请描述你建议的实现方案

format支持unixtime

@dance3366 dance3366 added the enhancement New feature or request label Jun 1, 2022
@wenshao
Copy link
Member

wenshao commented Jun 1, 2022

能提供一个你期待结果的代码,现在是支持的,我看哪里漏了

@dance3366
Copy link
Author

dance3366 commented Jun 1, 2022

    public static void main(String[] args) {
        String date1 = JSONObject.of("date1", 1654053162).toString();
        System.out.println("date1 = " + date1);
        Date date = JSON.parseObject(date1, Test.class).getDate1();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-HH-dd HH:mm:dd");
        System.out.println(sf.format(date));
    }
@Data
class Test{
    @JSONField(format = "unixtime")
    private Date date1;
}

如果是数值类型,直接当毫秒处理了
com.alibaba.fastjson2.reader.FieldReaderDateMethod#readFieldValue方法处理代码如下:
if (jsonReader.isInt()) {
long millis = jsonReader.readInt64Value();
fieldValue = new java.util.Date(millis);
}
如果是字符串,直接报错,毫秒字符串,比如1654053162000可以正常转换
String date1 = JSONObject.of("date1", "1654053162").toString();
com.alibaba.fastjson2.reader.FieldReaderDateMethod#readFieldValue报错代码行:
DateTimeFormatter formatter = getFormatter(locale);

还是我使用的有问题

@wenshao wenshao added this to the 2.0.6 milestone Jun 1, 2022
@wenshao wenshao added bug Something isn't working and removed enhancement New feature or request labels Jun 1, 2022
@wenshao
Copy link
Member

wenshao commented Jun 1, 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日前发布

@dance3366
Copy link
Author

dance3366 commented Jun 2, 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日前发布

验证数值型是可以成功的,比如:{"date1":1654134285}

对于unixtime反序列化为date,是否可以基于位数与是否全是数字的方式进行判断,不用format配置
但如果是字符:比如:{"date1":“1654134285”}会报错,字段配置如下
@JSONField(format = "unixtime")
private Date date1;
配置format报错位置如下:
com.alibaba.fastjson2.reader.FieldReaderDateMethod#readFieldValue报错代码行:
DateTimeFormatter formatter = getFormatter(locale);
错误提示:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown pattern letter: i

未配置format
@JSONField
private Date date1;
报错位置如下:
com.alibaba.fastjson2.JSONReader#readMillisFromString
case 10: {
LocalDateTime date = readLocalDate10();
if (date != null) {
return ZonedDateTime.of(date,
context.getZoneId())
.toInstant()
.toEpochMilli();
}
String str = readString();
if ("0000-00-00".equals(str)) {
return 0;
}
throw new JSONException("TODO : " + str);
}
unixtime刚好是10位,是否可以增加判断IOUtils.isNumber(str)

测试代码如下:
public class UnixTimeTest {

public static void main(String[] args) {
    String date1 = JSONObject.of("date1", "1654134285").toString();

    System.out.println("date1 = " + date1);
    Date date = JSON.parseObject(date1, Test.class).getDate1();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
    System.out.println(sf.format(date));
    System.out.println(sf.format(new Date(1654134285000L)));
}

}

@DaTa
class Test{
@JSONField(format = "unixtime")
private Date date1;
}

@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 wenshao reopened this Jun 4, 2022
@wenshao wenshao closed this as completed Jun 4, 2022
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

2 participants