新增jackson使用用例
This commit is contained in:
parent
ce40f51f54
commit
af81084ac0
@ -1,45 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<groupId>com.heibaiying</groupId>
|
|
||||||
<artifactId>jackson-samples</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<source>8</source>
|
|
||||||
<target>8</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<!--jackson 依赖包 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-databind</artifactId>
|
|
||||||
<version>2.9.8</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
<version>1.18.4</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.12</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.heibaiying.bean;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author : heibaiying
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private int age;
|
|
||||||
|
|
||||||
private float salary;
|
|
||||||
|
|
||||||
private Date birthday;
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
package com.heibaiying.jackson;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author : heibaiying
|
|
||||||
* @description : jackson 的使用
|
|
||||||
*/
|
|
||||||
public class JacksonUtils {
|
|
||||||
|
|
||||||
private static ObjectMapper mapper = new ObjectMapper();
|
|
||||||
|
|
||||||
/***
|
|
||||||
* spring 对象转换为json
|
|
||||||
*/
|
|
||||||
public static String objectToJson(Object object) throws JsonProcessingException {
|
|
||||||
return mapper.writeValueAsString(object);
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
|
||||||
* spring json 转换为对象
|
|
||||||
*/
|
|
||||||
public static <T> T jsonToBean(String json, Class<T> valueType) throws IOException {
|
|
||||||
return mapper.readValue(json, valueType);
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
|
||||||
* spring json 转换为List
|
|
||||||
*/
|
|
||||||
public static <T> List<T> jsonToList(String json, Class<T> valueType) throws IOException {
|
|
||||||
List<Map<String, Object>> list = mapper.readValue(json, new TypeReference<List<T>>() {
|
|
||||||
});
|
|
||||||
return list.stream().map(value -> mapToBean(value, valueType)).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
|
||||||
* spring json 转换为Map (map的value为基本类型)
|
|
||||||
*/
|
|
||||||
public static Map jsonToMap(String json) throws IOException {
|
|
||||||
return mapper.readValue(json, Map.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
|
||||||
* spring json 转换为Map (map的value为bean)
|
|
||||||
*/
|
|
||||||
public static <T> Map<String, T> jsonToMap(String json, Class<T> clazz) throws IOException {
|
|
||||||
Map<String, Map<String, Object>> map = mapper.readValue(json,
|
|
||||||
new TypeReference<Map<String, T>>() {
|
|
||||||
});
|
|
||||||
Map<String, T> result = new HashMap<>();
|
|
||||||
for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
|
|
||||||
result.put(entry.getKey(), mapToBean(entry.getValue(), clazz));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/***
|
|
||||||
* map 转换为 bean
|
|
||||||
*/
|
|
||||||
public static <T> T mapToBean(Map map, Class<T> valueType) {
|
|
||||||
return mapper.convertValue(map, valueType);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.heibaiying.bean.User;
|
|
||||||
import com.heibaiying.jackson.JacksonUtils;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author : heibaiying
|
|
||||||
* @description : jackson 测试
|
|
||||||
*/
|
|
||||||
public class JacksonTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() throws IOException {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user