Skip to content

Commit

Permalink
fix support read java.sql.Date, for issue #2623
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed May 31, 2024
1 parent 8c59a01 commit c04070e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 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 @@ -1104,6 +1104,14 @@ public LocalDateTime readLocalDateTime() {
return zdt.toLocalDateTime();
}

if (isTypeRedirect() && nextIfMatchIdent('"', 'v', 'a', 'l', '"')) {
nextIfMatch(':');
LocalDateTime dateTime = readLocalDateTime();
nextIfObjectEnd();
setTypeRedirect(false);
return dateTime;
}

if (context.dateFormat == null
|| context.formatyyyyMMddhhmmss19
|| context.formatyyyyMMddhhmmssT19
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.fastjson2.issues_2600;

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

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

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

public class Issue2623 {
@Test
public void test() {
Map<String, Object> map = new HashMap<>();
map.put("a", 1L);
map.put("b", 2);
map.put("c", 3F);
map.put("d", new Date(System.currentTimeMillis()));
map.put("e", new java.util.Date());

String s = JSON.toJSONString(map, JSONWriter.Feature.WriteClassName);
Map<String, Object> map2 = JSON.parseObject(s, new TypeReference<Map<String, Object>>() {
}, JSONReader.Feature.SupportAutoType);
assertEquals(map.get("d").toString(), map2.get("d").toString());
assertEquals(map.get("e").toString(), map2.get("e").toString());
}
}

0 comments on commit c04070e

Please # to comment.