diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplZonedDateTime.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplZonedDateTime.java index 025249237e..b32b511040 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplZonedDateTime.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplZonedDateTime.java @@ -39,7 +39,11 @@ public Class getObjectClass() { @Override public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - return jsonReader.readZonedDateTime(); + ZonedDateTime zdt = jsonReader.readZonedDateTime(); + if (builder != null && zdt != null) { + return builder.apply(zdt); + } + return zdt; } @Override diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplZonedDateTime.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplZonedDateTime.java index 78a5364142..3f6f64c15c 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplZonedDateTime.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplZonedDateTime.java @@ -28,7 +28,14 @@ public ObjectWriterImplZonedDateTime(String format, Locale locale, Function func @Override public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { - jsonWriter.writeZonedDateTime((ZonedDateTime) object); + ZonedDateTime zdt; + if (function != null) { + zdt = (ZonedDateTime) function.apply(object); + } else { + zdt = (ZonedDateTime) object; + } + + jsonWriter.writeZonedDateTime(zdt); } @Override diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2500/Issue2563.java b/core/src/test/java/com/alibaba/fastjson2/issues_2500/Issue2563.java new file mode 100644 index 0000000000..26b8a9f0d2 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2500/Issue2563.java @@ -0,0 +1,40 @@ +package com.alibaba.fastjson2.issues_2500; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONB; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.joda.time.DateTime; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2563 { + DateTime nowD = new DateTime(2018, 7, 14, 12, 13, 14, 0); + + @Test + public void mutatedTest0() { + Bean0 entity = new Bean0(); + entity.setNow(nowD); + + String json = JSON.toJSONString(entity); + Bean0 bean0 = JSON.parseObject(json, Bean0.class); + assertEquals(entity.now, bean0.now); + } + + @Test + public void mutatedTest1() { + Bean0 entity = new Bean0(); + entity.setNow(nowD); + + byte[] jsonbBytes = JSONB.toBytes(entity); + Bean0 bean0 = JSONB.parseObject(jsonbBytes, Bean0.class); + assertEquals(entity.now, bean0.now); + } + + @Data + public static class Bean0 { + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private DateTime now; + } +}