Skip to content

Commit 191c8c2

Browse files
committed
LocalDateTime & LocalDate parse support more cases
1 parent 239795a commit 191c8c2

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,29 @@ public final LocalDate readLocalDate() {
49464946
}
49474947
}
49484948
}
4949+
4950+
int nextQuoteOffset = -1;
4951+
for (int i = offset, end = Math.min(i + 17, this.end); i < end; ++i) {
4952+
if (chars[i] == quote) {
4953+
nextQuoteOffset = i;
4954+
}
4955+
}
4956+
if (nextQuoteOffset != -1
4957+
&& nextQuoteOffset - offset > 10
4958+
&& chars[nextQuoteOffset - 6] == '-'
4959+
&& chars[nextQuoteOffset - 3] == '-'
4960+
) {
4961+
int year = TypeUtils.parseInt(chars, offset, nextQuoteOffset - offset - 6);
4962+
int month = TypeUtils.parseInt(chars, nextQuoteOffset - 5, 2);
4963+
int dayOfMonth = TypeUtils.parseInt(chars, nextQuoteOffset - 2, 2);
4964+
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
4965+
this.offset = nextQuoteOffset + 1;
4966+
next();
4967+
if (comma = (this.ch == ',')) {
4968+
next();
4969+
}
4970+
return localDate;
4971+
}
49494972
}
49504973
}
49514974
return super.readLocalDate();

core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6048,6 +6048,29 @@ public final LocalDate readLocalDate() {
60486048
}
60496049
}
60506050
}
6051+
6052+
int nextQuoteOffset = -1;
6053+
for (int i = offset, end = Math.min(i + 17, this.end); i < end; ++i) {
6054+
if (bytes[i] == quote) {
6055+
nextQuoteOffset = i;
6056+
}
6057+
}
6058+
if (nextQuoteOffset != -1
6059+
&& nextQuoteOffset - offset > 10
6060+
&& bytes[nextQuoteOffset - 6] == '-'
6061+
&& bytes[nextQuoteOffset - 3] == '-'
6062+
) {
6063+
int year = TypeUtils.parseInt(bytes, offset, nextQuoteOffset - offset - 6);
6064+
int month = TypeUtils.parseInt(bytes, nextQuoteOffset - 5, 2);
6065+
int dayOfMonth = TypeUtils.parseInt(bytes, nextQuoteOffset - 2, 2);
6066+
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
6067+
this.offset = nextQuoteOffset + 1;
6068+
next();
6069+
if (comma = (this.ch == ',')) {
6070+
next();
6071+
}
6072+
return localDate;
6073+
}
60516074
}
60526075
}
60536076
return super.readLocalDate();

core/src/main/java/com/alibaba/fastjson2/util/DateUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5900,6 +5900,16 @@ public static LocalDateTime parseLocalDateTimeX(char[] str, int offset, int len)
59005900
S6 = c26;
59015901
S7 = c27;
59025902
S8 = c28;
5903+
} else if (str[offset + len - 15] == '-' && str[offset + len - 12] == '-'
5904+
&& (str[offset + len - 9] == ' ' || str[offset + len - 9] == 'T')
5905+
&& str[offset + len - 6] == ':' && str[offset + len - 3] == ':') {
5906+
int year = TypeUtils.parseInt(str, offset, len - 15);
5907+
int month = TypeUtils.parseInt(str, offset + len - 14, 2);
5908+
int dayOfMonth = TypeUtils.parseInt(str, offset + len - 11, 2);
5909+
int hour = TypeUtils.parseInt(str, offset + len - 8, 2);
5910+
int minute = TypeUtils.parseInt(str, offset + len - 5, 2);
5911+
int second = TypeUtils.parseInt(str, offset + len - 2, 2);
5912+
return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
59035913
} else {
59045914
return null;
59055915
}
@@ -6034,6 +6044,16 @@ public static LocalDateTime parseLocalDateTimeX(byte[] str, int offset, int len)
60346044
S6 = c26;
60356045
S7 = c27;
60366046
S8 = c28;
6047+
} else if (str[offset + len - 15] == '-' && str[offset + len - 12] == '-'
6048+
&& (str[offset + len - 9] == ' ' || str[offset + len - 9] == 'T')
6049+
&& str[offset + len - 6] == ':' && str[offset + len - 3] == ':') {
6050+
int year = TypeUtils.parseInt(str, offset, len - 15);
6051+
int month = TypeUtils.parseInt(str, offset + len - 14, 2);
6052+
int dayOfMonth = TypeUtils.parseInt(str, offset + len - 11, 2);
6053+
int hour = TypeUtils.parseInt(str, offset + len - 8, 2);
6054+
int minute = TypeUtils.parseInt(str, offset + len - 5, 2);
6055+
int second = TypeUtils.parseInt(str, offset + len - 2, 2);
6056+
return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
60376057
} else {
60386058
return null;
60396059
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.alibaba.fastjson2.issues_2800;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.nio.charset.StandardCharsets;
7+
import java.time.LocalDate;
8+
import java.time.LocalDateTime;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class Issue2859 {
13+
@Test
14+
public void test() {
15+
String str = "{\"time\":\"-999999999-11-12 13:14:15\"}";
16+
{
17+
Bean bean = JSON.parseObject(str, Bean.class);
18+
assertEquals(str, JSON.toJSONString(bean));
19+
}
20+
{
21+
Bean bean = JSON.parseObject(str.getBytes(StandardCharsets.UTF_8), Bean.class);
22+
assertEquals(str, JSON.toJSONString(bean));
23+
}
24+
{
25+
Bean bean = JSON.parseObject(str.toCharArray(), Bean.class);
26+
assertEquals(str, JSON.toJSONString(bean));
27+
}
28+
}
29+
30+
static class Bean {
31+
public LocalDateTime time;
32+
}
33+
34+
@Test
35+
public void test1() {
36+
String str = "{\"date\":\"-999999999-11-12\"}";
37+
{
38+
Bean1 bean = JSON.parseObject(str, Bean1.class);
39+
assertEquals(str, JSON.toJSONString(bean));
40+
}
41+
{
42+
Bean1 bean = JSON.parseObject(str.getBytes(StandardCharsets.UTF_8), Bean1.class);
43+
assertEquals(str, JSON.toJSONString(bean));
44+
}
45+
{
46+
Bean1 bean = JSON.parseObject(str.toCharArray(), Bean1.class);
47+
assertEquals(str, JSON.toJSONString(bean));
48+
}
49+
}
50+
51+
static class Bean1 {
52+
public LocalDate date;
53+
}
54+
}

0 commit comments

Comments
 (0)