Skip to content

Commit

Permalink
improved date parse support. for issue #1411
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 16, 2017
1 parent aa857bc commit a5fee4c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/alibaba/fastjson/parser/JSONScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
}
}

if (rest == 8 || rest == 14 || (rest == 17 && charAt(bp + 6) != '-')) {
char c10;
if (rest == 8
|| rest == 14
|| (rest == 16 && ((c10 = charAt(bp + 10)) == 'T' || c10 == ' '))
|| (rest == 17 && charAt(bp + 6) != '-')) {
if (strict) {
return false;
}
Expand All @@ -258,8 +262,10 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
char c7 = charAt(bp + 7);
char c8 = charAt(bp + 8);

final boolean sperate17 = c4 == '-' && c7 == '-' && rest == 17;
if (sperate17) {
final boolean c_47 = c4 == '-' && c7 == '-';
final boolean sperate16 = c_47 && rest == 16;
final boolean sperate17 = c_47 && rest == 17;
if (sperate17 || sperate16) {
y0 = c0;
y1 = c1;
y2 = c2;
Expand Down Expand Up @@ -289,14 +295,15 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
int hour, minute, seconds, millis;
if (rest != 8) {
char c9 = charAt(bp + 9);
char c10 = charAt(bp + 10);
c10 = charAt(bp + 10);
char c11 = charAt(bp + 11);
char c12 = charAt(bp + 12);
char c13 = charAt(bp + 13);

char h0, h1, m0, m1, s0, s1;

if (sperate17 && c10 == 'T' && c13 == ':' && charAt(bp + 16) == 'Z') {
if ((sperate17 && c10 == 'T' && c13 == ':' && charAt(bp + 16) == 'Z')
|| (sperate16 && (c10 == ' ' || c10 == 'T') && c13 == ':')) {
h0 = c11;
h1 = c12;
m0 = charAt(bp + 14);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/alibaba/json/bvt/date/DateFieldTest10.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public void test_2() throws Exception {
assertEquals(object, model.date);
}

public void test_3() throws Exception {
String text = "{\"date\":\"2017-08-16 04:29\"}";
Model model = JSON.parseObject(text, Model.class);

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Object object = format.parse("2017-08-16 04:29");
assertEquals(object, model.date);
}

public void test_4() throws Exception {
String text = "{\"date\":\"2017-08-16T04:29\"}";
Model model = JSON.parseObject(text, Model.class);

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Object object = format.parse("2017-08-16 04:29");
assertEquals(object, model.date);
}

public static class Model {
public Date date;
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1200/Issue1298.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ public void test_for_issue() throws Exception {

assertEquals("\"2017-06-29T10:36:30+08:00\"", JSON.toJSONString(date, SerializerFeature.UseISO8601DateFormat));
}

public void test_for_issue_1() throws Exception {
JSONObject object = new JSONObject();

object.put("date", "2017-08-15 20:00:00.000");

Date date = object.getObject("date", java.util.Date.class);

assertEquals("\"2017-08-15T20:00:00+08:00\"", JSON.toJSONString(date, SerializerFeature.UseISO8601DateFormat));

JSON.parseObject("\"2017-08-15 20:00:00.000\"", java.util.Date.class);
}
}

0 comments on commit a5fee4c

Please sign in to comment.