Skip to content

Commit

Permalink
fix toJSON date field error, for issue #2550
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 10, 2024
1 parent 8dee60c commit 45cc6b3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,11 @@ public JSONObject toJSONObject(T object, long features) {
Class fieldClass = fieldWriter.fieldClass;
if (format != null) {
if (fieldClass == Date.class) {
fieldValue = DateUtils.format((Date) fieldValue, format);
if ("millis".equals(format)) {
fieldValue = ((Date) fieldValue).getTime();
} else {
fieldValue = DateUtils.format((Date) fieldValue, format);
}
} else if (fieldClass == LocalDate.class) {
fieldValue = DateUtils.format((LocalDate) fieldValue, format);
} else if (fieldClass == LocalDateTime.class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import java.util.Date;

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

public class Issue2550 {
@Test
public void mutatedTest1() {
Bean1 bean = new Bean1();
bean.date = new Date(1655097829796L);
String str = JSON.toJSON(bean).toString();
assertEquals("{\"date\":1655097829796}", str);

Bean1 bean1 = JSON.parseObject(str, Bean1.class);
assertEquals(bean.date.getTime(), bean1.date.getTime());
Bean1 bean2 = JSON.parseObject(str).toJavaObject(Bean1.class);
assertEquals(bean.date.getTime(), bean2.date.getTime());
}

public static class Bean1 {
@JSONField(format = "millis")
public Date date;
}

@Test
public void mutatedTest2() {
Bean2 bean = new Bean2();
bean.date = new Date(1655097829796L);
String str = JSON.toJSON(bean).toString();
assertEquals("{\"date\":1655097829796}", str);

Bean2 bean1 = JSON.parseObject(str, Bean2.class);
assertEquals(bean.date.getTime(), bean1.date.getTime());
Bean2 bean2 = JSON.parseObject(str).toJavaObject(Bean2.class);
assertEquals(bean.date.getTime(), bean2.date.getTime());
}

public static class Bean2 {
@JSONField(format = "millis")
public Date date;
}

@Test
public void mutatedTest1fj() {
Bean1fj bean = new Bean1fj();
bean.date = new Date(1655097829796L);
String str = com.alibaba.fastjson.JSON.toJSON(bean).toString();
assertEquals("{\"date\":1655097829796}", str);

Bean1fj bean1 = com.alibaba.fastjson.JSON.parseObject(str, Bean1fj.class);
assertEquals(bean.date.getTime(), bean1.date.getTime());
Bean1fj bean2 = com.alibaba.fastjson.JSON.parseObject(str).toJavaObject(Bean1fj.class);
assertEquals(bean.date.getTime(), bean2.date.getTime());
}

public static class Bean1fj {
@com.alibaba.fastjson.annotation.JSONField(format = "millis")
public Date date;
}

@Test
public void mutatedTest2fj() {
Bean2fj bean = new Bean2fj();
bean.date = new Date(1655097829796L);
String str = com.alibaba.fastjson.JSON.toJSON(bean).toString();
assertEquals("{\"date\":1655097829796}", str);

Bean2fj bean1 = com.alibaba.fastjson.JSON.parseObject(str, Bean2fj.class);
assertEquals(bean.date.getTime(), bean1.date.getTime());
Bean2fj bean2 = com.alibaba.fastjson.JSON.parseObject(str).toJavaObject(Bean2fj.class);
assertEquals(bean.date.getTime(), bean2.date.getTime());
}

public static class Bean2fj {
@com.alibaba.fastjson.annotation.JSONField(format = "millis")
public Date date;
}
}

0 comments on commit 45cc6b3

Please sign in to comment.