Skip to content

Commit

Permalink
fix readZonedDateTime support read null for issue #1861
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Sep 19, 2023
1 parent 0796a9b commit 217dc8b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,11 @@ public ZonedDateTime readZonedDateTime() {

return ZonedDateTime.parse(str);
}

if (isNull()) {
readNull();
return null;
}
throw new JSONException("TODO : " + ch);
}

Expand Down
21 changes: 14 additions & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -5789,15 +5789,17 @@ public final OffsetDateTime readOffsetDateTime() {
) {
year = (y0 - '0') * 1000 + (y1 - '0') * 100 + (y2 - '0') * 10 + (y3 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

if (m0 >= '0' && m0 <= '9'
&& m1 >= '0' && m1 <= '9'
) {
month = (m0 - '0') * 10 + (m1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int dom;
Expand All @@ -5806,7 +5808,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
dom = (d0 - '0') * 10 + (d1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int hour;
Expand All @@ -5815,7 +5818,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
hour = (h0 - '0') * 10 + (h1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int minute;
Expand All @@ -5824,7 +5828,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
minute = (i0 - '0') * 10 + (i1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int second;
Expand All @@ -5833,7 +5838,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
second = (s0 - '0') * 10 + (s1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

LocalDate localDate;
Expand Down Expand Up @@ -5871,7 +5877,8 @@ public final OffsetDateTime readOffsetDateTime() {
}
}
}
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

@Override
Expand Down
21 changes: 14 additions & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -7024,15 +7024,17 @@ public final OffsetDateTime readOffsetDateTime() {
) {
year = (y0 - '0') * 1000 + (y1 - '0') * 100 + (y2 - '0') * 10 + (y3 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

if (m0 >= '0' && m0 <= '9'
&& m1 >= '0' && m1 <= '9'
) {
month = (m0 - '0') * 10 + (m1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int dom;
Expand All @@ -7041,7 +7043,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
dom = (d0 - '0') * 10 + (d1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int hour;
Expand All @@ -7050,7 +7053,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
hour = (h0 - '0') * 10 + (h1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int minute;
Expand All @@ -7059,7 +7063,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
minute = (i0 - '0') * 10 + (i1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

int second;
Expand All @@ -7068,7 +7073,8 @@ public final OffsetDateTime readOffsetDateTime() {
) {
second = (s0 - '0') * 10 + (s1 - '0');
} else {
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

LocalDate localDate;
Expand Down Expand Up @@ -7106,7 +7112,8 @@ public final OffsetDateTime readOffsetDateTime() {
}
}
}
return readZonedDateTime().toOffsetDateTime();
ZonedDateTime zdt = readZonedDateTime();
return zdt == null ? null : zdt.toOffsetDateTime();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alibaba.fastjson2.issues_1800;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.junit.jupiter.api.Test;

import javax.validation.Valid;
import java.time.OffsetDateTime;
import java.util.Map;

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

public class Issue1861 {
@Test
public void test() {
String json = "{\"myData1\":{\"myData2\":{\"key\":{\"startTime\":null}}}}";
MyData data = JSON.parseObject(json, MyData.class);
assertEquals(json, JSON.toJSONString(data, JSONWriter.Feature.WriteNulls));
}

public class MyData {
private MyData1 myData1;

public MyData1 getMyData1() {
return myData1;
}

public void setMyData1(MyData1 myData1) {
this.myData1 = myData1;
}
}

public class MyData1 {
@JsonProperty("myData2")
private @Valid Map<String, MyData2> myData2 = null;

public Map<String, MyData2> getMyData2() {
return myData2;
}

public void setMyData2(Map<String, MyData2> myData2) {
this.myData2 = myData2;
}
}

public class MyData2 {
@JsonProperty("startTime")
private OffsetDateTime startTime;

public OffsetDateTime getStartTime() {
return startTime;
}

public void setStartTime(OffsetDateTime startTime) {
this.startTime = startTime;
}
}
}

0 comments on commit 217dc8b

Please sign in to comment.