fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] Serialization of Timestamp

Open Cooperzzy opened this issue 1 year ago • 0 comments

问题描述

序列化timestamp时,当毫秒位为0时会默认舍去,不为0时则保留。虽然可以通过@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")格式化,但能否统一默认格式?

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson2 2.0.49/2.0.50-SNAPSHOT]

重现步骤


import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;
import org.joda.time.DateTime;
import java.sql.Timestamp;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MutatedIssue2460_351_1 {
    @Test
    public void mutatedTest() {
        Timestamp ts1 = Timestamp.valueOf("2024-04-19 10:47:16.000");
        String jsonDateString = "\"2024-04-19 10:47:16.000\"";
        Object o = JSON.parse(jsonDateString);
        String str1 = JSON.toJSONString(o);
        String str2 = JSON.toJSONString(ts1);
        assertEquals(str1, jsonDateString);
        assertEquals(str2, jsonDateString);
        assertEquals(str1, str2);
    }

    @Test
    public void mutatedTest2() {
        String s = "{\"time\":\"2024-04-19 10:47:16.000\"}";
        Bean bean = JSON.parseObject(s, Bean.class);
        String s1 = JSON.toJSONString(bean);
        assertEquals("{\"time\":\"2024-04-19 10:47:16.000\"}", s1);
    }

    public static class Bean {
        public Timestamp time;
    }
}

期待的正确结果

Expected :{"time":"2024-04-19 10:47:16.000"} Actual :{"time":"2024-04-19 10:47:16"}

Cooperzzy avatar May 10 '24 03:05 Cooperzzy