From 20386691eb2b7e84845deca97ebee11833a77594 Mon Sep 17 00:00:00 2001 From: pugwoo Date: Fri, 30 Dec 2022 22:31:47 +0800 Subject: [PATCH 1/5] =?UTF-8?q?ListUtils=E6=96=B0=E5=A2=9Emerge(Object[]?= =?UTF-8?q?=20...objs)=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG | 2 ++ .../pugwoo/wooutils/collect/ListUtils.java | 24 +++++++++++++++++++ .../wooutils/collect/TestListUtils.java | 17 +++++++++++++ 3 files changed, 43 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index ab68102..b7f6a49 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +v1.1.7 - [add] ListUtils新增merge(Object[] ...objs)方法 + 2022年11月21日 v1.1.6 - [fix] 修复DateUtils.format(LocalDate)抛出Unsupport field: HourOfDay的异常 diff --git a/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java b/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java index fe3c886..8d44946 100644 --- a/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java +++ b/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java @@ -547,6 +547,30 @@ public static List flat(List> list) { .collect(Collectors.toList()); } + /** + * 将多个数组合并成一个数组 + */ + public static Object[] merge(Object[] ...objs) { + if (objs == null || objs.length == 0) { + return new Object[0]; + } + int size = 0; + for (Object[] obj : objs) { + size += obj == null ? 0 : obj.length; + } + Object[] result = new Object[size]; + int current = 0; + for (Object[] obj : objs) { + if (obj == null) { + continue; + } + for (Object o : obj) { + result[current++] = o; + } + } + return result; + } + /** * 数值求和 * @param list diff --git a/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java b/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java index cba78cb..595ed0b 100644 --- a/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java +++ b/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java @@ -116,4 +116,21 @@ public void testFlatList() { assert NumberUtils.sum(flat).intValue() == 45; } + @Test + public void testMergeArray() { + Object[] a = new Object[]{1,2,3}; + Object[] b = null; + Object[] c = new Object[]{7,8,9}; + + Object[] merge = ListUtils.merge(a, b, c); + assert merge.length == 6; + assert (int)merge[0] == 1; + assert (int)merge[1] == 2; + assert (int)merge[2] == 3; + assert (int)merge[3] == 7; + assert (int)merge[4] == 8; + assert (int)merge[5] == 9; + + } + } From 2826839556b72f31d0d0c9e7b17b6356d03c6fd8 Mon Sep 17 00:00:00 2001 From: pugwoo Date: Tue, 17 Jan 2023 18:58:23 +0800 Subject: [PATCH 2/5] =?UTF-8?q?StringTools=E6=96=B0=E5=A2=9EnthIndexOf?= =?UTF-8?q?=E5=92=8CnthLastIndexOf=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG | 3 +- .../pugwoo/wooutils/collect/ListUtils.java | 2 +- .../pugwoo/wooutils/string/StringTools.java | 28 +++++++++++++++++++ .../wooutils/collect/TestListUtils.java | 3 +- .../wooutils/string/StringToolsTests.java | 16 +++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b7f6a49..b14e007 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ -v1.1.7 - [add] ListUtils新增merge(Object[] ...objs)方法 +v1.1.7 - [add] ListUtils新增concatArray(Object[] ...objs)方法,将多个数组合并成一个数组 + - [add] StringTools新增nthIndexOf和nthLastIndexOf方法 2022年11月21日 v1.1.6 - [fix] 修复DateUtils.format(LocalDate)抛出Unsupport field: HourOfDay的异常 diff --git a/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java b/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java index 8d44946..3ceec36 100644 --- a/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java +++ b/src/main/java/com/pugwoo/wooutils/collect/ListUtils.java @@ -550,7 +550,7 @@ public static List flat(List> list) { /** * 将多个数组合并成一个数组 */ - public static Object[] merge(Object[] ...objs) { + public static Object[] concatArray(Object[] ...objs) { if (objs == null || objs.length == 0) { return new Object[0]; } diff --git a/src/main/java/com/pugwoo/wooutils/string/StringTools.java b/src/main/java/com/pugwoo/wooutils/string/StringTools.java index ef1df16..36ad8c1 100644 --- a/src/main/java/com/pugwoo/wooutils/string/StringTools.java +++ b/src/main/java/com/pugwoo/wooutils/string/StringTools.java @@ -103,6 +103,34 @@ public static boolean isAnyBlank(List strs) { return false; } + /** + * 返回str中第nth个substr的index + * @param str 被查找的字符串 + * @param substr 要查找的字符串 + * @param nth 从1开始 + * @return index,从0开始 + */ + public static int nthIndexOf(String str, String substr, int nth) { + int pos = str.indexOf(substr); + while (--nth > 0 && pos != -1) + pos = str.indexOf(substr, pos + 1); + return pos; + } + + /** + * 返回str中第nth个substr的index + * @param str 被查找的字符串 + * @param substr 要查找的字符串 + * @param nth 从1开始 + * @return index,从0开始 + */ + public static int nthLastIndexOf(String str, String substr, int nth) { + int pos = str.lastIndexOf(substr); + while (--nth > 0 && pos != -1) + pos = str.lastIndexOf(substr, pos - 1); + return pos; + } + /** * 返回两个字符串的共同前缀 * @param str1 diff --git a/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java b/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java index 595ed0b..f3c634f 100644 --- a/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java +++ b/src/test/java/com/pugwoo/wooutils/collect/TestListUtils.java @@ -122,7 +122,7 @@ public void testMergeArray() { Object[] b = null; Object[] c = new Object[]{7,8,9}; - Object[] merge = ListUtils.merge(a, b, c); + Object[] merge = ListUtils.concatArray(a, b, c); assert merge.length == 6; assert (int)merge[0] == 1; assert (int)merge[1] == 2; @@ -130,7 +130,6 @@ public void testMergeArray() { assert (int)merge[3] == 7; assert (int)merge[4] == 8; assert (int)merge[5] == 9; - } } diff --git a/src/test/java/com/pugwoo/wooutils/string/StringToolsTests.java b/src/test/java/com/pugwoo/wooutils/string/StringToolsTests.java index ff252ea..fa3a607 100644 --- a/src/test/java/com/pugwoo/wooutils/string/StringToolsTests.java +++ b/src/test/java/com/pugwoo/wooutils/string/StringToolsTests.java @@ -86,4 +86,20 @@ public void testJoin() { assert "a,b,c".equals(StringTools.join(new String[]{"a", "b", "c"}, ",")); assert "a,b,c".equals(StringTools.join( ListUtils.newList("a", "b", "c"), ",")); } + + @Test + public void testNthIndexOf() { + String a = "a1a2a3a4"; + assert StringTools.nthIndexOf(a, "a", 1) == 0; + assert StringTools.nthIndexOf(a, "a", 2) == 2; + assert StringTools.nthIndexOf(a, "a", 3) == 4; + assert StringTools.nthIndexOf(a, "a", 4) == 6; + assert StringTools.nthIndexOf(a, "a", 5) == -1; + + assert StringTools.nthLastIndexOf(a, "a", 1) == 6; + assert StringTools.nthLastIndexOf(a, "a", 2) == 4; + assert StringTools.nthLastIndexOf(a, "a", 3) == 2; + assert StringTools.nthLastIndexOf(a, "a", 4) == 0; + assert StringTools.nthLastIndexOf(a, "a", 5) == -1; + } } From b8278561e11bf3ff953c5a520b7c6c7051cd9886 Mon Sep 17 00:00:00 2001 From: pugwoo Date: Tue, 17 Jan 2023 21:52:32 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BC=98=E5=8C=96JSON=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=EF=BC=8C=E5=BD=93=E4=BC=A0=E5=85=A5null/=E7=A9=BA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=97=B6=EF=BC=8C=E8=A7=A3=E6=9E=90=E4=B8=BA?= =?UTF-8?q?null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG | 1 + .../java/com/pugwoo/wooutils/json/JSON.java | 16 ++++++ .../wooutils/json/MultiDateDeserializer.java | 24 +-------- .../com/pugwoo/wooutils/lang/DateUtils.java | 8 +-- src/test/java/com/pugwoo/wooutils/Test.java | 54 +++++++++++++++++++ .../com/pugwoo/wooutils/date/TestDate.java | 2 + .../com/pugwoo/wooutils/json/TestJson.java | 29 ++++++---- .../com/pugwoo/wooutils/net/TestBrowser.java | 8 +-- 8 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 src/test/java/com/pugwoo/wooutils/Test.java diff --git a/CHANGELOG b/CHANGELOG index b14e007..c590137 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ v1.1.7 - [add] ListUtils新增concatArray(Object[] ...objs)方法,将多个数组合并成一个数组 - [add] StringTools新增nthIndexOf和nthLastIndexOf方法 + - [enhance] 优化JSON解析,当传入null/空字符串时,解析为null 2022年11月21日 v1.1.6 - [fix] 修复DateUtils.format(LocalDate)抛出Unsupport field: HourOfDay的异常 diff --git a/src/main/java/com/pugwoo/wooutils/json/JSON.java b/src/main/java/com/pugwoo/wooutils/json/JSON.java index 799ab5f..1206e4c 100644 --- a/src/main/java/com/pugwoo/wooutils/json/JSON.java +++ b/src/main/java/com/pugwoo/wooutils/json/JSON.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.pugwoo.wooutils.string.StringTools; import java.io.IOException; import java.util.List; @@ -165,6 +166,9 @@ public static Object parse(String str) { * @return t */ public static T parse(String str, Class clazz) { + if (StringTools.isBlank(str)) { + return null; + } return parse(om -> om.readValue(str,clazz)); } @@ -178,6 +182,9 @@ public static T parse(String str, Class clazz) { * @return t */ public static T parse(String str, Class clazz, Class... genericClasses) { + if (StringTools.isBlank(str)) { + return null; + } return parse(om -> { JavaType type = om.getTypeFactory().constructParametricType(clazz, genericClasses); return om.readValue(str, type); @@ -191,6 +198,9 @@ public static T parse(String str, Class clazz, Class... genericClasses * @return t */ public static T parse(String str, TypeReference typeReference) { + if (StringTools.isBlank(str)) { + return null; + } return parse(om -> om.readValue(str, typeReference)); } @@ -218,6 +228,9 @@ public static List> parseToListMap(String str) { * @return map */ public static List parseToList(String str, Class itemClazz) { + if (StringTools.isBlank(str)) { + return null; + } return parseToList(str, typeFactory -> typeFactory.constructParametricType(List.class, itemClazz) ); @@ -231,6 +244,9 @@ public static List parseToList(String str, Class itemClazz) { * @return List {@literal <} T {@literal >} */ public static List parseToList(String str, TypeReference itemTypeRef) { + if (StringTools.isBlank(str)) { + return null; + } return parseToList(str, typeFactory -> { JavaType itemType = typeFactory.constructType(itemTypeRef); return typeFactory.constructParametricType(List.class, itemType); diff --git a/src/main/java/com/pugwoo/wooutils/json/MultiDateDeserializer.java b/src/main/java/com/pugwoo/wooutils/json/MultiDateDeserializer.java index 0051215..fd412ea 100644 --- a/src/main/java/com/pugwoo/wooutils/json/MultiDateDeserializer.java +++ b/src/main/java/com/pugwoo/wooutils/json/MultiDateDeserializer.java @@ -2,12 +2,9 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -import com.fasterxml.jackson.databind.node.IntNode; -import com.fasterxml.jackson.databind.node.LongNode; import com.pugwoo.wooutils.lang.DateUtils; import java.io.IOException; @@ -26,28 +23,9 @@ public MultiDateDeserializer(Class vc) { } @Override - public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode node = jp.getCodec().readTree(jp); - - // 针对时间戳做优化 - if(node instanceof LongNode || node instanceof IntNode) { - long timestamp = node.asLong(); - if(timestamp < 4200000000L) { // 小于42亿认为是秒 - return new Date(timestamp * 1000L); - } else { - return new Date(timestamp); - } - } - String date = node.asText(); - if(date == null) { - return null; - } - date = date.trim(); - if(date.isEmpty()) { - return null; - } - try { return DateUtils.parseThrowException(date); } catch (ParseException e) { diff --git a/src/main/java/com/pugwoo/wooutils/lang/DateUtils.java b/src/main/java/com/pugwoo/wooutils/lang/DateUtils.java index 287c032..f2d18ff 100644 --- a/src/main/java/com/pugwoo/wooutils/lang/DateUtils.java +++ b/src/main/java/com/pugwoo/wooutils/lang/DateUtils.java @@ -95,9 +95,9 @@ public static Date parse(String date) { String pattern = determineDateFormat(date); if(pattern == null) { // 检查是否是时间戳 - Date _date = tryParseTimestamp(date); - if(_date != null) { - return _date; + Date date2 = tryParseTimestamp(date); + if(date2 != null) { + return date2; } LOGGER.error("date parse, pattern not support, date:{}", date); @@ -129,7 +129,7 @@ private static Date tryParseTimestamp(String date) { return null; } - // 时间戳小于42亿则认为是秒,否则是毫秒 + // 时间戳小于42亿则认为是秒(此时已经是2103-02-04),否则是毫秒 if(timestamp < 4200000000L) { return new Date(timestamp * 1000L); } else { diff --git a/src/test/java/com/pugwoo/wooutils/Test.java b/src/test/java/com/pugwoo/wooutils/Test.java new file mode 100644 index 0000000..cfa0187 --- /dev/null +++ b/src/test/java/com/pugwoo/wooutils/Test.java @@ -0,0 +1,54 @@ +package com.pugwoo.wooutils; + +import com.pugwoo.wooutils.json.JSON; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +public class Test { + + public static class A { + private String a; + private LocalDate ld; + private LocalDateTime ldt; + + public String getA() { + return a; + } + + public void setA(String a) { + this.a = a; + } + + public LocalDate getLd() { + return ld; + } + + public void setLd(LocalDate ld) { + this.ld = ld; + } + + public LocalDateTime getLdt() { + return ldt; + } + + public void setLdt(LocalDateTime ldt) { + this.ldt = ldt; + } + } + + public static void main(String[] args) { + +// LocalDateTime localDateTime = DateUtils.parseLocalDateTime("2021-02-03T04:05:06"); +// System.out.println(localDateTime); + + String json = "{\"a\":\"name\",\"ld\":\"2022-03-04\",\"ldt\":\"2021-02-03 04:05:06\"}"; + System.out.println(json); + + A a = JSON.parse(json, A.class); + + System.out.println(JSON.toJson(a)); + + } + +} \ No newline at end of file diff --git a/src/test/java/com/pugwoo/wooutils/date/TestDate.java b/src/test/java/com/pugwoo/wooutils/date/TestDate.java index cb45609..c0493de 100644 --- a/src/test/java/com/pugwoo/wooutils/date/TestDate.java +++ b/src/test/java/com/pugwoo/wooutils/date/TestDate.java @@ -40,6 +40,8 @@ public void test() { assert DateUtils.parse("0000-00-00") == null; assert DateUtils.parse("0000-00-00 00:00:00") == null; + assert DateUtils.parse(null) == null; + assert DateUtils.parse("1673961578").equals(DateUtils.parse("2023-01-17 21:19:38")); for(String str : testDates) { Date date = DateUtils.parse(str); diff --git a/src/test/java/com/pugwoo/wooutils/json/TestJson.java b/src/test/java/com/pugwoo/wooutils/json/TestJson.java index 4ee1073..769108f 100644 --- a/src/test/java/com/pugwoo/wooutils/json/TestJson.java +++ b/src/test/java/com/pugwoo/wooutils/json/TestJson.java @@ -85,16 +85,27 @@ public void toJsonTest() { @Test public void parseTest() { - System.out.println("\n================ 解析json示例"); - - System.out.println(JSON.parse("\"2017-03-03 15:34\"", Date.class)); - System.out.println(JSON.parse("\"2017年3月30日\"", Date.class)); - System.out.println(JSON.parse("\" \"", Date.class)); - - System.out.println(); - + // 测试解析日期 + assert JSON.parse("\"2017-03-03 15:34\"", Date.class).equals(DateUtils.parse("2017-03-03 15:34")); + assert JSON.parse("\"2017年3月30日\"", Date.class).equals(DateUtils.parse("2017年3月30日")); + assert JSON.parse("1673961578", Date.class).equals(DateUtils.parse("2023-01-17 21:19:38")); + assert JSON.parse("\" \"", Date.class) == null; + assert JSON.parse(null, Date.class) == null; + assert JSON.parse("", Date.class) == null; + assert JSON.parse(" ", Date.class) == null; + assert JSON.parse(" \t\n\r ", Date.class) == null; + + // 测试解析日期异常的场景 + boolean hasEx = false; + try { + JSON.parse("\" 123 \"", Date.class); + } catch (Exception e) { + hasEx =true; + } + assert hasEx; + MyClass myclass = JSON.parse("{\"map\":\"\"}", MyClass.class); - System.out.println(JSON.toJson(myclass)); + assert myclass.getMap() == null; } @Test diff --git a/src/test/java/com/pugwoo/wooutils/net/TestBrowser.java b/src/test/java/com/pugwoo/wooutils/net/TestBrowser.java index 9f34347..261b763 100644 --- a/src/test/java/com/pugwoo/wooutils/net/TestBrowser.java +++ b/src/test/java/com/pugwoo/wooutils/net/TestBrowser.java @@ -30,8 +30,8 @@ public void testBasicPost() throws Exception { //browser.addRequestHeader("Content-Type", "text/plain"); - browser.post("http://127.0.0.1:8080/post", - "content".getBytes(), out); +// browser.post("http://127.0.0.1:8080/post", +// "content".getBytes(), out); } // 测试上传文件 @@ -46,8 +46,8 @@ public void testPost() throws Exception { // new FileInputStream("C:\\Users\\nickt\\Desktop\\1.png")); // params.put("file2", file2); params.put("info", "myinfo"); - browser.post("http://127.0.0.1:8080/admin/admin_upload/upload", - params); +// browser.post("http://127.0.0.1:8080/admin/admin_upload/upload", +// params); } @Test From 9d709fdbd25490c1f3f36da25f36d90c05725335 Mon Sep 17 00:00:00 2001 From: pugwoo Date: Tue, 17 Jan 2023 22:52:48 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0LocalDateTime/LocalDate/L?= =?UTF-8?q?ocalTime=E5=A4=9A=E7=A7=8D=E6=97=A5=E6=9C=9F=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E8=A7=A3=E6=9E=90=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG | 1 + .../json/MultiLocalDateDeserializer.java | 42 +++++++++++++++ .../json/MultiLocalDateTimeDeserializer.java | 42 +++++++++++++++ .../json/MultiLocalTimeDeserializer.java | 42 +++++++++++++++ .../pugwoo/wooutils/json/MyObjectMapper.java | 46 +++++++++++++--- src/test/java/com/pugwoo/wooutils/Test.java | 54 ------------------- .../com/pugwoo/wooutils/json/TestJson.java | 16 ++++-- 7 files changed, 177 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/pugwoo/wooutils/json/MultiLocalDateDeserializer.java create mode 100644 src/main/java/com/pugwoo/wooutils/json/MultiLocalDateTimeDeserializer.java create mode 100644 src/main/java/com/pugwoo/wooutils/json/MultiLocalTimeDeserializer.java delete mode 100644 src/test/java/com/pugwoo/wooutils/Test.java diff --git a/CHANGELOG b/CHANGELOG index c590137..fabaa12 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ v1.1.7 - [add] ListUtils新增concatArray(Object[] ...objs)方法,将多个数组合并成一个数组 - [add] StringTools新增nthIndexOf和nthLastIndexOf方法 + - [add] 增加LocalDateTime/LocalDate/LocalTime多种日期格式的解析支持 - [enhance] 优化JSON解析,当传入null/空字符串时,解析为null 2022年11月21日 diff --git a/src/main/java/com/pugwoo/wooutils/json/MultiLocalDateDeserializer.java b/src/main/java/com/pugwoo/wooutils/json/MultiLocalDateDeserializer.java new file mode 100644 index 0000000..e59f40d --- /dev/null +++ b/src/main/java/com/pugwoo/wooutils/json/MultiLocalDateDeserializer.java @@ -0,0 +1,42 @@ +package com.pugwoo.wooutils.json; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.pugwoo.wooutils.lang.DateUtils; +import com.pugwoo.wooutils.string.StringTools; + +import java.io.IOException; +import java.time.LocalDate; + +public class MultiLocalDateDeserializer extends StdDeserializer { + + private static final long serialVersionUID = 1L; + + public MultiLocalDateDeserializer() { + this(null); + } + public MultiLocalDateDeserializer(Class vc) { + super(vc); + } + + @Override + public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { + JsonNode node = jp.getCodec().readTree(jp); + String date = node.asText(); + if (StringTools.isBlank(date)) { // 这个判空是因为parseLocalDate中没有判空 + return null; + } + + LocalDate localDate = DateUtils.parseLocalDate(date); + if (localDate == null) { + throw new JsonParseException(jp, + "Unparseable localDate: \"" + date + "\". Supported formats: " + + DateUtils.DATE_FORMAT_REGEXPS.values()); + } + return localDate; + } + +} \ No newline at end of file diff --git a/src/main/java/com/pugwoo/wooutils/json/MultiLocalDateTimeDeserializer.java b/src/main/java/com/pugwoo/wooutils/json/MultiLocalDateTimeDeserializer.java new file mode 100644 index 0000000..de971e3 --- /dev/null +++ b/src/main/java/com/pugwoo/wooutils/json/MultiLocalDateTimeDeserializer.java @@ -0,0 +1,42 @@ +package com.pugwoo.wooutils.json; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.pugwoo.wooutils.lang.DateUtils; +import com.pugwoo.wooutils.string.StringTools; + +import java.io.IOException; +import java.time.LocalDateTime; + +public class MultiLocalDateTimeDeserializer extends StdDeserializer { + + private static final long serialVersionUID = 1L; + + public MultiLocalDateTimeDeserializer() { + this(null); + } + public MultiLocalDateTimeDeserializer(Class vc) { + super(vc); + } + + @Override + public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { + JsonNode node = jp.getCodec().readTree(jp); + String date = node.asText(); + if (StringTools.isBlank(date)) { // 这个判空是因为parseLocalDate中没有判空 + return null; + } + + LocalDateTime localDateTime = DateUtils.parseLocalDateTime(date); + if (localDateTime == null) { + throw new JsonParseException(jp, + "Unparseable localDateTime: \"" + date + "\". Supported formats: " + + DateUtils.DATE_FORMAT_REGEXPS.values()); + } + return localDateTime; + } + +} \ No newline at end of file diff --git a/src/main/java/com/pugwoo/wooutils/json/MultiLocalTimeDeserializer.java b/src/main/java/com/pugwoo/wooutils/json/MultiLocalTimeDeserializer.java new file mode 100644 index 0000000..1659687 --- /dev/null +++ b/src/main/java/com/pugwoo/wooutils/json/MultiLocalTimeDeserializer.java @@ -0,0 +1,42 @@ +package com.pugwoo.wooutils.json; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.pugwoo.wooutils.lang.DateUtils; +import com.pugwoo.wooutils.string.StringTools; + +import java.io.IOException; +import java.time.LocalTime; + +public class MultiLocalTimeDeserializer extends StdDeserializer { + + private static final long serialVersionUID = 1L; + + public MultiLocalTimeDeserializer() { + this(null); + } + public MultiLocalTimeDeserializer(Class vc) { + super(vc); + } + + @Override + public LocalTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { + JsonNode node = jp.getCodec().readTree(jp); + String date = node.asText(); + if (StringTools.isBlank(date)) { // 这个判空是因为parseLocalDate中没有判空 + return null; + } + + LocalTime localTime = DateUtils.parseLocalTime(date); + if (localTime == null) { + throw new JsonParseException(jp, + "Unparseable localTime: \"" + date + "\". Supported formats: " + + DateUtils.DATE_FORMAT_REGEXPS.values()); + } + return localTime; + } + +} \ No newline at end of file diff --git a/src/main/java/com/pugwoo/wooutils/json/MyObjectMapper.java b/src/main/java/com/pugwoo/wooutils/json/MyObjectMapper.java index 56c96cf..a5d80d0 100644 --- a/src/main/java/com/pugwoo/wooutils/json/MyObjectMapper.java +++ b/src/main/java/com/pugwoo/wooutils/json/MyObjectMapper.java @@ -9,6 +9,9 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.Date; public class MyObjectMapper extends ObjectMapper { @@ -32,15 +35,44 @@ public MyObjectMapper() { configure(JsonParser.Feature.ALLOW_MISSING_VALUES, true); //允许[]中有多个,,, configure(JsonParser.Feature.ALLOW_TRAILING_COMMA, true); //允许[]最后带多一个, configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true); // 允许字符串值出现反斜杠\ - - MultiDateDeserializer deserializer = new MultiDateDeserializer(); - SimpleModule module = new SimpleModule("DateDeserializerModule", - new Version(1, 0, 0, "", "", "")); - module.addDeserializer(Date.class, deserializer); - - registerModule(module); + + // 自定义Date解析器 + { + MultiDateDeserializer deserializer = new MultiDateDeserializer(); + SimpleModule module = new SimpleModule("DateDeserializerModule", + new Version(1, 0, 0, "", "", "")); + module.addDeserializer(Date.class, deserializer); + registerModule(module); + } registerModule(new JavaTimeModule()); // 解析LocalDate等 + + // 自定义LocalDate解析器,这个要放在JavaTimeModule后面,以覆盖JavaTimeModule对LocalDate的默认解析 + { + MultiLocalDateDeserializer deserializer = new MultiLocalDateDeserializer(); + SimpleModule module = new SimpleModule("LocalDateDeserializerModule", + new Version(1, 0, 0, "", "", "")); + module.addDeserializer(LocalDate.class, deserializer); + registerModule(module); + } + + // 自定义LocalDateTime解析器,这个要放在JavaTimeModule后面,以覆盖JavaTimeModule对LocalDateTime的默认解析 + { + MultiLocalDateTimeDeserializer deserializer = new MultiLocalDateTimeDeserializer(); + SimpleModule module = new SimpleModule("LocalDateTimeDeserializerModule", + new Version(1, 0, 0, "", "", "")); + module.addDeserializer(LocalDateTime.class, deserializer); + registerModule(module); + } + + // 自定义LocalTime解析器,这个要放在JavaTimeModule后面,以覆盖JavaTimeModule对LocalTime的默认解析 + { + MultiLocalTimeDeserializer deserializer = new MultiLocalTimeDeserializer(); + SimpleModule module = new SimpleModule("LocalTimeDeserializerModule", + new Version(1, 0, 0, "", "", "")); + module.addDeserializer(LocalTime.class, deserializer); + registerModule(module); + } } } diff --git a/src/test/java/com/pugwoo/wooutils/Test.java b/src/test/java/com/pugwoo/wooutils/Test.java deleted file mode 100644 index cfa0187..0000000 --- a/src/test/java/com/pugwoo/wooutils/Test.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.pugwoo.wooutils; - -import com.pugwoo.wooutils.json.JSON; - -import java.time.LocalDate; -import java.time.LocalDateTime; - -public class Test { - - public static class A { - private String a; - private LocalDate ld; - private LocalDateTime ldt; - - public String getA() { - return a; - } - - public void setA(String a) { - this.a = a; - } - - public LocalDate getLd() { - return ld; - } - - public void setLd(LocalDate ld) { - this.ld = ld; - } - - public LocalDateTime getLdt() { - return ldt; - } - - public void setLdt(LocalDateTime ldt) { - this.ldt = ldt; - } - } - - public static void main(String[] args) { - -// LocalDateTime localDateTime = DateUtils.parseLocalDateTime("2021-02-03T04:05:06"); -// System.out.println(localDateTime); - - String json = "{\"a\":\"name\",\"ld\":\"2022-03-04\",\"ldt\":\"2021-02-03 04:05:06\"}"; - System.out.println(json); - - A a = JSON.parse(json, A.class); - - System.out.println(JSON.toJson(a)); - - } - -} \ No newline at end of file diff --git a/src/test/java/com/pugwoo/wooutils/json/TestJson.java b/src/test/java/com/pugwoo/wooutils/json/TestJson.java index 769108f..47cf32e 100644 --- a/src/test/java/com/pugwoo/wooutils/json/TestJson.java +++ b/src/test/java/com/pugwoo/wooutils/json/TestJson.java @@ -13,12 +13,10 @@ import org.junit.Test; import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.ZoneId; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -95,6 +93,14 @@ public void parseTest() { assert JSON.parse(" ", Date.class) == null; assert JSON.parse(" \t\n\r ", Date.class) == null; + // 解析LocalDateTime + assert JSON.parse("\"2022-03-04 05:06:07\"", LocalDateTime.class) + .equals(DateUtils.parseLocalDateTime("2022-03-04 05:06:07")); + assert JSON.parse("\"2022-03-04 05:06:07\"", LocalDate.class) + .equals(DateUtils.parseLocalDate("2022-03-04 05:06:07")); + assert JSON.parse("\"2022-03-04 05:06:07\"", LocalTime.class) + .equals(DateUtils.parseLocalTime("2022-03-04 05:06:07")); + // 测试解析日期异常的场景 boolean hasEx = false; try { From 62a296cc2951f24aa2247e6700ff5f8fc819a676 Mon Sep 17 00:00:00 2001 From: pugwoo Date: Tue, 17 Jan 2023 23:00:34 +0800 Subject: [PATCH 5/5] release 1.1.7 --- CHANGELOG | 1 + README.md | 2 +- pom.xml | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index fabaa12..8df851b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +2023年1月17日 v1.1.7 - [add] ListUtils新增concatArray(Object[] ...objs)方法,将多个数组合并成一个数组 - [add] StringTools新增nthIndexOf和nthLastIndexOf方法 - [add] 增加LocalDateTime/LocalDate/LocalTime多种日期格式的解析支持 diff --git a/README.md b/README.md index fb567d4..267ad65 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ com.pugwoo woo-utils - 1.1.6 + 1.1.7 ``` diff --git a/pom.xml b/pom.xml index 009e72c..4783d93 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.pugwoo woo-utils jar - 1.1.6 + 1.1.7 woo-utils the common utils @@ -50,19 +50,19 @@ com.fasterxml.jackson.core jackson-databind - 2.13.4.2 + 2.14.1 com.fasterxml.jackson.datatype jackson-datatype-jsr310 - 2.13.4 + 2.14.1 com.fasterxml.jackson.dataformat jackson-dataformat-yaml - 2.13.4 + 2.14.1