Skip to content

Commit

Permalink
improved LocalTime deserialize, for issue #2310
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Mar 23, 2024
1 parent ea2a032 commit 534063d
Show file tree
Hide file tree
Showing 6 changed files with 473 additions and 2 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,10 @@ public LocalTime readLocalTime() {
switch (len) {
case 5:
return readLocalTime5();
case 6:
return readLocalTime6();
case 7:
return readLocalTime7();
case 8:
return readLocalTime8();
case 9:
Expand Down Expand Up @@ -1662,6 +1666,10 @@ public final long readMillisFromString() {

protected abstract LocalTime readLocalTime5();

protected abstract LocalTime readLocalTime6();

protected abstract LocalTime readLocalTime7();

protected abstract LocalTime readLocalTime8();

protected abstract LocalTime readLocalTime9();
Expand Down
28 changes: 28 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -4912,6 +4912,10 @@ public LocalTime readLocalTime() {
switch (len) {
case 5:
return readLocalTime5();
case 6:
return readLocalTime6();
case 7:
return readLocalTime7();
case 8:
return readLocalTime8();
case 9:
Expand Down Expand Up @@ -5429,6 +5433,30 @@ protected LocalTime readLocalTime5() {
return time;
}

@Override
protected LocalTime readLocalTime6() {
LocalTime time;
if (bytes[offset] != BC_STR_ASCII_FIX_MIN + 6
|| (time = DateUtils.parseLocalTime6(bytes, offset + 1)) == null
) {
throw new JSONException("date only support string input");
}
offset += 7;
return time;
}

@Override
protected LocalTime readLocalTime7() {
LocalTime time;
if (bytes[offset] != BC_STR_ASCII_FIX_MIN + 7
|| (time = DateUtils.parseLocalTime7(bytes, offset + 1)) == null
) {
throw new JSONException("date only support string input");
}
offset += 8;
return time;
}

@Override
protected LocalTime readLocalTime8() {
LocalTime time;
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -4679,6 +4679,46 @@ protected final LocalTime readLocalTime5() {
return time;
}

@Override
protected final LocalTime readLocalTime6() {
if (ch != '"' && ch != '\'') {
throw new JSONException("localTime only support string input");
}

LocalTime time = DateUtils.parseLocalTime6(chars, offset);
if (time == null) {
return null;
}

offset += 7;
next();
if (comma = (ch == ',')) {
next();
}

return time;
}

@Override
protected final LocalTime readLocalTime7() {
if (ch != '"' && ch != '\'') {
throw new JSONException("localTime only support string input");
}

LocalTime time = DateUtils.parseLocalTime7(chars, offset);
if (time == null) {
return null;
}

offset += 8;
next();
if (comma = (ch == ',')) {
next();
}

return time;
}

@Override
protected final LocalTime readLocalTime8() {
if (ch != '"' && ch != '\'') {
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -6427,6 +6427,46 @@ protected final LocalTime readLocalTime5() {
return time;
}

@Override
protected final LocalTime readLocalTime6() {
if (ch != '"' && ch != '\'') {
throw new JSONException("localTime only support string input");
}

LocalTime time = DateUtils.parseLocalTime6(bytes, offset);
if (time == null) {
return null;
}

offset += 7;
next();
if (comma = (ch == ',')) {
next();
}

return time;
}

@Override
protected final LocalTime readLocalTime7() {
if (ch != '"' && ch != '\'') {
throw new JSONException("localTime only support string input");
}

LocalTime time = DateUtils.parseLocalTime7(bytes, offset);
if (time == null) {
return null;
}

offset += 8;
next();
if (comma = (ch == ',')) {
next();
}

return time;
}

@Override
protected final LocalTime readLocalTime8() {
if (ch != '"' && ch != '\'') {
Expand Down
Loading

0 comments on commit 534063d

Please sign in to comment.