Skip to content

Commit

Permalink
Merge pull request #24 from pugwoo/develop-daily
Browse files Browse the repository at this point in the history
release 1.1.7
  • Loading branch information
pugwoo authored Jan 17, 2023
2 parents f315672 + 62a296c commit 2e121d5
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 57 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2023年1月17日
v1.1.7 - [add] ListUtils新增concatArray(Object[] ...objs)方法,将多个数组合并成一个数组
- [add] StringTools新增nthIndexOf和nthLastIndexOf方法
- [add] 增加LocalDateTime/LocalDate/LocalTime多种日期格式的解析支持
- [enhance] 优化JSON解析,当传入null/空字符串时,解析为null

2022年11月21日
v1.1.6 - [fix] 修复DateUtils.format(LocalDate)抛出Unsupport field: HourOfDay的异常

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<dependency>
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<version>1.1.6</version>
<version>1.1.7</version>
</dependency>
```

Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<packaging>jar</packaging>
<version>1.1.6</version>
<version>1.1.7</version>

<name>woo-utils</name>
<description>the common utils</description>
Expand Down Expand Up @@ -50,19 +50,19 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.4</version>
<version>2.14.1</version>
</dependency>

<!-- yaml相关功能 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.4</version>
<version>2.14.1</version>
</dependency>

<!-- optional依赖:servlet相关功能需要 -->
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/pugwoo/wooutils/collect/ListUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,30 @@ public static <E> List<E> flat(List<List<E>> list) {
.collect(Collectors.toList());
}

/**
* 将多个数组合并成一个数组
*/
public static Object[] concatArray(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
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/pugwoo/wooutils/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,6 +166,9 @@ public static Object parse(String str) {
* @return t
*/
public static <T> T parse(String str, Class<T> clazz) {
if (StringTools.isBlank(str)) {
return null;
}
return parse(om -> om.readValue(str,clazz));
}

Expand All @@ -178,6 +182,9 @@ public static <T> T parse(String str, Class<T> clazz) {
* @return t
*/
public static <T> T parse(String str, Class<T> clazz, Class<?>... genericClasses) {
if (StringTools.isBlank(str)) {
return null;
}
return parse(om -> {
JavaType type = om.getTypeFactory().constructParametricType(clazz, genericClasses);
return om.readValue(str, type);
Expand All @@ -191,6 +198,9 @@ public static <T> T parse(String str, Class<T> clazz, Class<?>... genericClasses
* @return t
*/
public static <T> T parse(String str, TypeReference<T> typeReference) {
if (StringTools.isBlank(str)) {
return null;
}
return parse(om -> om.readValue(str, typeReference));
}

Expand Down Expand Up @@ -218,6 +228,9 @@ public static List<Map<String, Object>> parseToListMap(String str) {
* @return map
*/
public static <T> List<T> parseToList(String str, Class<T> itemClazz) {
if (StringTools.isBlank(str)) {
return null;
}
return parseToList(str, typeFactory ->
typeFactory.constructParametricType(List.class, itemClazz)
);
Expand All @@ -231,6 +244,9 @@ public static <T> List<T> parseToList(String str, Class<T> itemClazz) {
* @return List {@literal <} T {@literal >}
*/
public static <T> List<T> parseToList(String str, TypeReference<T> itemTypeRef) {
if (StringTools.isBlank(str)) {
return null;
}
return parseToList(str, typeFactory -> {
JavaType itemType = typeFactory.constructType(itemTypeRef);
return typeFactory.constructParametricType(List.class, itemType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LocalDate> {

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;
}

}
Original file line number Diff line number Diff line change
@@ -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<LocalDateTime> {

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;
}

}
Original file line number Diff line number Diff line change
@@ -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<LocalTime> {

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;
}

}
46 changes: 39 additions & 7 deletions src/main/java/com/pugwoo/wooutils/json/MyObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

}
8 changes: 4 additions & 4 deletions src/main/java/com/pugwoo/wooutils/lang/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 2e121d5

Please sign in to comment.