Skip to content

Commit

Permalink
date deserialize support input empty string, for issue #467
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 14, 2022
1 parent 1cd7a77 commit 051ef40
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public long readFieldNameHashCode() {
if (ch == '}' || isNull()) {
return -1;
}
throw new JSONException("illegal character " + ch);
throw new JSONException(info("illegal character " + ch));
}

final char quote = ch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public Object readObject(JSONReader jsonReader, long features) {
DateTimeFormatter formatter = getDateFormatter();
if (formatter != null) {
String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

LocalDateTime ldt = LocalDateTime.parse(str, formatter);
ZonedDateTime zdt = ZonedDateTime.of(ldt, jsonReader.getContext().getZoneId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ private Object readDate(JSONReader jsonReader) {
ZonedDateTime zdt;
if (formatter != null) {
String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

LocalDateTime ldt;
if (!formatHasHour) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public Object readObject(JSONReader jsonReader, long features) {

String str = jsonReader.readString();

if (str.isEmpty()) {
return null;
}

if (formatMillis || formatUnixTime) {
long millis = Long.parseLong(str);
if (formatUnixTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public Object readObject(JSONReader jsonReader, long features) {
}

String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

if (formatMillis || formatUnixTime) {
long millis = Long.parseLong(str);
if (formatUnixTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public Object readObject(JSONReader jsonReader, long features) {
}

String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

if (formatMillis || formatUnixTime) {
long millis = Long.parseLong(str);
if (formatUnixTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public Object readObject(JSONReader jsonReader, long features) {
ZonedDateTime zdt;
if (formatter != null) {
String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

LocalDateTime ldt;
if (!formatHasHour) {
Expand Down Expand Up @@ -425,6 +428,9 @@ public Object readObject(JSONReader jsonReader, long features) {
}

String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

DateTimeFormatter dateFormatter = getDateFormatter();

Expand Down Expand Up @@ -517,6 +523,9 @@ public Object readObject(JSONReader jsonReader, long features) {
}

String str = jsonReader.readString();
if (str.isEmpty()) {
return null;
}

DateTimeFormatter dateFormatter = getDateFormatter();

Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/JodaSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public Object readObject(JSONReader jsonReader, long features) {

if (jsonReader.isString()) {
Instant jdkInstant = jsonReader.readInstant();
if (jdkInstant == null) {
return null;
}

long millis = jdkInstant.toEpochMilli();
return createInstanceFromMillis(millis);
}
Expand Down Expand Up @@ -362,6 +366,10 @@ public Object readObject(JSONReader jsonReader, long features) {
}

LocalDate localDate = jsonReader.readLocalDate();
if (localDate == null) {
return null;
}

try {
return constructor4.newInstance(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth(), null);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
Expand Down Expand Up @@ -573,6 +581,10 @@ public Class getObjectClass() {
public Object readObject(JSONReader jsonReader, long features) {
if (jsonReader.isString() || jsonReader.isInt()) {
LocalDateTime ldt = jsonReader.readLocalDateTime();
if (ldt == null) {
return null;
}

try {
return constructor7.newInstance(ldt.getYear(), ldt.getMonthValue(), ldt.getDayOfMonth(), ldt.getHour(), ldt.getMinute(), ldt.getSecond(), ldt.getNano() / 1000_000);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/date/FormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.ZonedDateTime;

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

public class FormatTest {
@Test
Expand Down Expand Up @@ -38,6 +39,8 @@ public void test() {
assertEquals("\"2017-12-13 00:00:00\"", JSON.toJSONString(date));
JSON.mixIn(LocalDate.class, null);
assertEquals("\"2017-12-13\"", JSON.toJSONString(date));

assertNull(JSON.parseObject("{\"date\":\"\"}", Bean.class).date);
}

@Test
Expand Down
145 changes: 145 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue467.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.alibaba.fastjson2.issues;

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

import java.time.*;
import java.util.Calendar;
import java.util.Date;

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

public class Issue467 {
@Test
public void test() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean.class).date);
}

public static class Bean {
@JSONField(format = "yyyy-MM-dd")
public Date date;
}

@Test
public void test1() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean1.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean1.class).date);
}

public static class Bean1 {
@JSONField(format = "yyyy-MM-dd")
public Calendar date;
}

@Test
public void test2() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean2.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean2.class).date);
}

public static class Bean2 {
@JSONField(format = "yyyy-MM-dd")
public Instant date;
}

@Test
public void test3() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean3.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean3.class).date);
}

public static class Bean3 {
@JSONField(format = "yyyy-MM-dd")
public LocalDateTime date;
}

@Test
public void test4() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean4.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean4.class).date);
}

public static class Bean4 {
@JSONField(format = "yyyy-MM-dd")
public LocalDate date;
}

@Test
public void test5() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean5.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean5.class).date);
}

public static class Bean5 {
@JSONField(format = "HH:mm:ss")
public LocalTime date;
}

@Test
public void test6() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean6.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean6.class).date);
}

public static class Bean6 {
@JSONField(format = "yyyy-MM-dd")
public java.sql.Date date;
}

@Test
public void test7() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean7.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean7.class).date);
}

public static class Bean7 {
@JSONField(format = "yyyy-MM-dd")
public java.sql.Timestamp date;
}

@Test
public void test8() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean8.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean8.class).date);
}

public static class Bean8 {
@JSONField(format = "yyyy-MM-dd")
public java.sql.Time date;
}

@Test
public void test9() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean9.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean9.class).date);
}

public static class Bean9 {
@JSONField(format = "yyyy-MM-dd")
public org.joda.time.LocalDate date;
}

@Test
public void test10() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean10.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean10.class).date);
}

public static class Bean10 {
@JSONField(format = "yyyy-MM-dd")
public org.joda.time.LocalDateTime date;
}

@Test
public void test11() {
assertNull(JSON.parseObject("{\"date\":\"\"}", Bean11.class).date);
assertNull(JSON.parseObject("{\"date\":\"\"}").to(Bean11.class).date);
}

public static class Bean11 {
@JSONField(format = "yyyy-MM-dd")
public org.joda.time.Instant date;
}
}

0 comments on commit 051ef40

Please sign in to comment.