mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-09-28 16:33:21 +08:00
1.5.0-jre8
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.type.MapType;
|
||||
|
||||
@@ -42,7 +44,7 @@ public class JsonUtil {
|
||||
* @param objectMapper
|
||||
*/
|
||||
private static void initDefaultObjectMapper(ObjectMapper objectMapper) {
|
||||
objectMapper.setSerializationInclusion(Include.NON_NULL);
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
@@ -55,16 +57,17 @@ public class JsonUtil {
|
||||
//TODO 待优化
|
||||
public static void initSnakeCase() {
|
||||
//驼峰下划线互转
|
||||
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
||||
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个设置不能改变JsonUtil自带的objectMapper设置,只能修改传入objMapper的设置
|
||||
*
|
||||
* @param objMapper
|
||||
*/
|
||||
public static void initSnakeCase(ObjectMapper objMapper) {
|
||||
//驼峰下划线互转
|
||||
objMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
||||
objMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,8 +147,7 @@ public class JsonUtil {
|
||||
*/
|
||||
public static <T> T json2Obj(String json, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||
T pojo = null;
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
||||
parameterClasses);
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, javaType);
|
||||
} catch (JsonParseException e) {
|
||||
@@ -186,26 +188,18 @@ public class JsonUtil {
|
||||
/**
|
||||
* Json字符串转换为Java-Map对象
|
||||
*
|
||||
* @param json 字符串
|
||||
* @param mapClass Map 继承类
|
||||
* @param keyType Key 类
|
||||
* @param valueType Value 类
|
||||
* @param json 字符串
|
||||
* @param javaType 具体的java类型
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Obj(String json, Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) {
|
||||
T pojo = null;
|
||||
MapType mapType = objectMapper.getTypeFactory().constructMapType(mapClass, keyType, valueType);
|
||||
public static <T extends Object> T json2Obj(String json, TypeReference<T> javaType) {
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, mapType);
|
||||
} catch (JsonParseException e) {
|
||||
return objectMapper.readValue(json, javaType);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||
} catch (JsonMappingException e) {
|
||||
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||
}
|
||||
return pojo;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
@@ -7,7 +9,27 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonUtilTest {
|
||||
public static void main(String[] args) {
|
||||
@Test
|
||||
public void test2() {
|
||||
// TestA testA = new TestA("张三", 1, false);
|
||||
// List<TestA> list = new ArrayList<>();
|
||||
// list.add(testA);
|
||||
// list.add(testA);
|
||||
// Map<String, List<TestA>> data = new HashMap<>();
|
||||
// data.put("data",list);
|
||||
// System.out.println(JsonUtil.obj2Json(data));
|
||||
// System.out.println("===========================");
|
||||
String readData="{\"data\":[{\"name\":\"张三\",\"id\":1,\"sex\":false},{\"name\":\"张三\",\"id\":1,\"sex\":false}]}";
|
||||
Map<String, List<TestA>> map = JsonUtil.json2Obj(readData, new TypeReference<Map<String, List<TestA>>>() {
|
||||
});
|
||||
System.out.println(JsonUtil.obj2Json(map));
|
||||
List<TestA> testAS = map.get("data");
|
||||
System.out.println(testAS.getClass().toString());
|
||||
System.out.println(testAS.get(0).getClass().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
TestA testA = new TestA("张三", 1, false);
|
||||
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(testA), Map.class));
|
||||
|
||||
@@ -15,7 +37,7 @@ public class JsonUtilTest {
|
||||
list.add(testA);
|
||||
list.add(testA);
|
||||
list.add(testA);
|
||||
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(list), List.class,TestA.class));
|
||||
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(list), List.class, TestA.class));
|
||||
}
|
||||
|
||||
static class TestA implements Serializable {
|
||||
@@ -32,5 +54,4 @@ public class JsonUtilTest {
|
||||
this.sex = sex;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user