Skip to content

Commit

Permalink
bug fix for Date Field input empty string, fix issue #550
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 13, 2022
1 parent c62b819 commit cdc8b9f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ public void readFieldValue(JSONReader jsonReader, T object) {
}
fieldValue = new java.util.Date(millis);
}
} else if (jsonReader.nextIfEmptyString()) {
fieldValue = null;
} else {
long millis = jsonReader.readMillisFromString();
fieldValue = new java.util.Date(millis);
Expand Down
32 changes: 32 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue550.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.fastjson2.issues;

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

import java.util.Date;

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

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

public static class Bean {
public Date date;
}

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

private static class Bean1 {
public Date date;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.util.Date;

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

public class Issue550 {
@Test
public void test() {
Bean bean = JSON.parseObject("{\"date\":\"\"}", Bean.class);
assertNull(bean.date);
assertNull(JSON.parseObject("{\"date\":\"\"}").toJavaObject(Bean.class).date);
}

public static class Bean {
public Date date;
}
}

0 comments on commit cdc8b9f

Please sign in to comment.