Skip to content

Commit 8d5437a

Browse files
zhengyuefeng鄭 躍峰
andauthored
feat: - compatible with unknown enum - print error stack message (#77)
Co-authored-by: 鄭 躍峰 <zheng.yuefeng@smartcompany.jp>
1 parent 0de0c84 commit 8d5437a

3 files changed

Lines changed: 210 additions & 142 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.thunderz99</groupId>
55
<artifactId>java-cosmos</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.5.21</version>
7+
<version>0.5.22</version>
88
<name>${project.groupId}:${project.artifactId}$</name>
99
<description>A lightweight Azure CosmosDB client for Java</description>
1010
<url>https://github.com/thunderz99/java-cosmos</url>

src/main/java/io/github/thunderz99/cosmos/util/JsonUtil.java

Lines changed: 118 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22

33
import java.io.InputStream;
44
import java.io.Reader;
5+
import java.lang.reflect.Modifier;
56
import java.util.ArrayList;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
910

10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
12-
11+
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
1312
import com.fasterxml.jackson.annotation.JsonInclude;
1413
import com.fasterxml.jackson.annotation.PropertyAccessor;
15-
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
1614
import com.fasterxml.jackson.core.JsonProcessingException;
1715
import com.fasterxml.jackson.core.type.TypeReference;
18-
import com.fasterxml.jackson.databind.DeserializationFeature;
19-
import com.fasterxml.jackson.databind.JavaType;
20-
import com.fasterxml.jackson.databind.JsonNode;
21-
import com.fasterxml.jackson.databind.ObjectMapper;
22-
import com.fasterxml.jackson.databind.SerializationFeature;
16+
import com.fasterxml.jackson.databind.*;
17+
import org.apache.commons.lang3.builder.ToStringBuilder;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
2320

2421
/**
2522
* Simple Json Converter based on jackson. Do common initialization and wrap
@@ -51,10 +48,11 @@ public class JsonUtil {
5148
* @param mapper original mapper
5249
*/
5350
private static void init(ObjectMapper mapper) {
54-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) //
55-
.setSerializationInclusion(JsonInclude.Include.NON_NULL) //
56-
.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
57-
}
51+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) //
52+
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
53+
.setSerializationInclusion(JsonInclude.Include.NON_NULL) //
54+
.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
55+
}
5856

5957
/**
6058
* Object to json
@@ -409,88 +407,112 @@ public static <T> T fromJson(InputStream is, String className) {
409407
return null;
410408
}
411409

412-
/**
413-
* Json to List of bean
414-
* @param is json string inputStream
415-
* @param className className for bean
416-
* @param <T> generic param for bean
417-
* @return List of bean
418-
*/
419-
public static <T> List<T> fromJson2List(InputStream is, String className) {
420-
try {
421-
JavaType javaType = constructListType(className);
422-
return mapper.readValue(is, javaType);
423-
} catch (Exception e) {
424-
processException(is, e);
425-
}
426-
return List.of();
427-
}
428-
429-
/**
430-
* Json to List of bean
431-
* @param is json string inputStream
432-
* @param classOfT class of bean
433-
* @param <T> generic param for bean
434-
* @return List of bean
435-
*/
436-
public static <T> List<T> fromJson2List(InputStream is, Class<T> classOfT) {
437-
try {
438-
JavaType javaType = constructListType(classOfT.getName());
439-
return mapper.readValue(is, javaType);
440-
} catch (Exception e) {
441-
processException(is, e);
442-
}
443-
return List.of();
444-
}
445-
446-
/**
447-
* Json string to bean object
448-
* @param is json string inputStream
449-
* @param javaType javaType for bean
450-
* @param <T> generic for bean
451-
* @return bean object
452-
*/
453-
public static <T> T fromJson(InputStream is, JavaType javaType) {
454-
try {
455-
return mapper.readValue(is, javaType);
456-
} catch (Exception e) {
457-
processException(is, e);
458-
}
459-
return null;
460-
}
461-
462-
/**
463-
* map to bean object
464-
* @param map map representing json
465-
* @param classOfT class of bean
466-
* @param <T> generic param for bean
467-
* @return bean object
468-
*/
469-
public static <T> T fromMap(Map<String, Object> map, Class<T> classOfT) {
470-
try {
471-
return mapper.convertValue(map, classOfT);
472-
} catch (Exception e) {
473-
processException(map, e);
474-
}
475-
return null;
476-
}
477-
478-
private static void processException(Object object, Exception e) {
479-
throw new IllegalArgumentException("json process error.", e);
480-
}
481-
482-
private static ObjectMapper newObjectMapper() {
483-
ObjectMapper mapper = new ObjectMapper();
484-
init(mapper);
485-
return mapper;
486-
}
487-
488-
/**
489-
* return an objectMapper configured the same as this JsonUtil
490-
* @return objectMapper
491-
*/
492-
public static ObjectMapper getObjectMapper() {
493-
return newObjectMapper();
494-
}
410+
/**
411+
* Json to List of bean
412+
*
413+
* @param is json string inputStream
414+
* @param className className for bean
415+
* @param <T> generic param for bean
416+
* @return List of bean
417+
*/
418+
public static <T> List<T> fromJson2List(InputStream is, String className) {
419+
try {
420+
JavaType javaType = constructListType(className);
421+
return mapper.readValue(is, javaType);
422+
} catch (Exception e) {
423+
processException(is, e);
424+
}
425+
return List.of();
426+
}
427+
428+
/**
429+
* Json to List of bean
430+
*
431+
* @param is json string inputStream
432+
* @param classOfT class of bean
433+
* @param <T> generic param for bean
434+
* @return List of bean
435+
*/
436+
public static <T> List<T> fromJson2List(InputStream is, Class<T> classOfT) {
437+
try {
438+
JavaType javaType = constructListType(classOfT.getName());
439+
return mapper.readValue(is, javaType);
440+
} catch (Exception e) {
441+
processException(is, e);
442+
}
443+
return List.of();
444+
}
445+
446+
/**
447+
* Json string to bean object
448+
*
449+
* @param is json string inputStream
450+
* @param javaType javaType for bean
451+
* @param <T> generic for bean
452+
* @return bean object
453+
*/
454+
public static <T> T fromJson(InputStream is, JavaType javaType) {
455+
try {
456+
return mapper.readValue(is, javaType);
457+
} catch (Exception e) {
458+
processException(is, e);
459+
}
460+
return null;
461+
}
462+
463+
/**
464+
* map to bean object
465+
*
466+
* @param map map representing json
467+
* @param classOfT class of bean
468+
* @param <T> generic param for bean
469+
* @return bean object
470+
*/
471+
public static <T> T fromMap(Map<String, Object> map, Class<T> classOfT) {
472+
try {
473+
return mapper.convertValue(map, classOfT);
474+
} catch (Exception e) {
475+
processException(map, e);
476+
}
477+
return null;
478+
}
479+
480+
private static void processException(Object object, Exception e) {
481+
log.warn("json process error. e = \n{}", exceptionToString(e));
482+
if (Modifier.isFinal(object.getClass().getModifiers())) {
483+
log.warn("json process error. object = {}", object);
484+
} else {
485+
log.warn("json process error. object = {}", ToStringBuilder.reflectionToString(object));
486+
}
487+
throw new IllegalArgumentException("json process error.", e);
488+
}
489+
490+
static String exceptionToString(Throwable throwable) {
491+
StringBuilder sb = new StringBuilder();
492+
sb.append(throwable.getClass().getName());
493+
if (throwable.getMessage() != null) {
494+
sb.append(": ").append(throwable.getMessage());
495+
}
496+
sb.append("\n");
497+
for (StackTraceElement stackTraceElement : throwable.getStackTrace()) {
498+
sb.append("\tat ").append(stackTraceElement.toString()).append("\n");
499+
}
500+
return sb.toString();
501+
}
502+
503+
private static ObjectMapper newObjectMapper() {
504+
ObjectMapper mapper = new ObjectMapper();
505+
init(mapper);
506+
return mapper;
507+
}
508+
509+
/**
510+
* return an objectMapper configured the same as this JsonUtil
511+
*
512+
* @return objectMapper
513+
*/
514+
public static ObjectMapper getObjectMapper() {
515+
return newObjectMapper();
516+
}
495517

496518
}

0 commit comments

Comments
 (0)