增加jackson用例

This commit is contained in:
罗祥 2019-01-09 17:58:45 +08:00
parent 8bd210aaef
commit a206999739
8 changed files with 166 additions and 1 deletions

View File

@ -1,6 +1,6 @@
# Spring-Samples-For-All
![spring](https://img.shields.io/badge/spring-5.1.3.RELEASE-brightgreen.svg)![springboot](https://img.shields.io/badge/springboot-2.1.1.RELEASE-brightgreen.svg)![springcloud](https://img.shields.io/badge/springcloud-Finchley.SR2-brightgreen.svg)![jdk](https://img.shields.io/badge/jdk-1.8-blue.svg)
![spring](https://img.shields.io/badge/spring-5.1.3.RELEASE-brightgreen.svg)![springboot](https://img.shields.io/badge/springboot-2.1.1.RELEASE-brightgreen.svg)![springcloud](https://img.shields.io/badge/springcloud-Finchley.SR2-brightgreen.svg)![jdk](https://img.shields.io/badge/jdk->=1.8-blue.svg)![author](https://img.shields.io/badge/author-heibaiying-orange.svg)
<br/>

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="205.3" height="184.28" viewBox="0 0 205.3 184.28"><defs><style>.a{fill:#68bd45;}</style></defs><title>Asset 1</title><path class="a" d="M202.66,81.08l-40.43-70C158.72,5,150.1,0,143.08,0H62.22c-7,0-15.64,5-19.15,11.06l-40.43,70c-3.52,6.08-3.52,16,0,22.12l40.43,70c3.51,6.08,12.13,11.06,19.15,11.06h80.86c7,0,15.63-5,19.15-11.06l40.43-70C206.18,97.11,206.18,87.17,202.66,81.08ZM93.91,38.72a8.34,8.34,0,1,1,16.67,0V88.53a8.34,8.34,0,1,1-16.67,0ZM102.25,145A56.51,56.51,0,0,1,68.54,43.17a7.41,7.41,0,1,1,8.85,11.89,41.69,41.69,0,1,0,66.54,33.47A41.89,41.89,0,0,0,127,55,7.41,7.41,0,0,1,135.76,43a56.52,56.52,0,0,1-33.51,102Z"/></svg>

After

Width:  |  Height:  |  Size: 676 B

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="244.11" height="161.44" viewBox="0 0 244.11 161.44"><defs><style>.a{fill:#77bc1f;}</style></defs><title>Asset 1</title><path class="a" d="M208.36,61c-2.28-15.29-7-32.54-13.39-51.64A75.79,75.79,0,0,1,186.19,25,82.37,82.37,0,0,0,56.42,40.1,54.57,54.57,0,0,1,90.09,54.25a56.91,56.91,0,0,0-31.81-9.38,58.28,58.28,0,0,0,0,116.57h134.4A51.42,51.42,0,0,0,208.36,61ZM82.79,143.47a7,7,0,1,1-1-9.91A7,7,0,0,1,82.79,143.47Zm111.83-24.68c-20.34,27.09-63.76,18-91.61,19.26,0,0-4.93.28-9.9,1.1,0,0,1.88-.8,4.28-1.63,19.55-6.76,28.8-8.12,40.69-14.23,22.32-11.44,44.55-36.35,49.06-62.23-8.5,24.9-34.35,46.32-57.88,55C113.14,122,84,127.8,84,127.81l-1.18-.63C63,117.54,62.45,74.63,98.43,60.8c15.78-6.07,30.87-2.73,47.92-6.8,18.19-4.32,39.25-18,47.81-35.79C203.77,46.69,215.3,91.24,194.62,118.79Z"/></svg>

After

Width:  |  Height:  |  Size: 833 B

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="208.47" height="208.47" viewBox="0 0 208.47 208.47"><defs><style>.a{fill:#68bd45;}.b{fill:#fff;}</style></defs><title>Asset 1</title><path class="a" d="M196.09,66.62c8,11.26,12.59,24.14,12.59,37.83s-4.59,26.65-12.67,37.93c-2.27,13.68-8.15,26.07-17.86,35.78-9.54,9.53-21.78,15.6-35.86,17.91-11.26,8-24.15,12.61-37.84,12.61s-26.6-4.57-37.85-12.61c-14.08-2.31-26.32-8.38-35.86-17.91-9.71-9.71-15.59-22.1-17.85-35.78C4.81,131.09.21,118.17.21,104.45s4.6-26.66,12.68-37.93C15.14,52.84,21,40.45,30.74,30.74c9.54-9.54,21.78-15.6,35.86-17.91C77.85,4.78,90.75.21,104.45.21S131,4.78,142.29,12.83c14.08,2.31,26.32,8.37,35.86,17.91S193.79,52.62,196.09,66.62Zm9.84,37.83a61,61,0,0,0-9.08-31.65,88.8,88.8,0,0,1-.57,19.43,103.3,103.3,0,0,1-2.5,12.32,93.61,93.61,0,0,1,3,31.68A61,61,0,0,0,205.92,104.45ZM193.53,91.86a83,83,0,0,0-.08-24.2,86.34,86.34,0,0,0-23.17-21.5,120.09,120.09,0,0,1,9.92,26.91,119.67,119.67,0,0,1,12,26.1C192.75,96.73,193.2,94.3,193.53,91.86Zm-.13,49.43a86.5,86.5,0,0,0-1.18-31.42,119.35,119.35,0,0,1-12.05,26,120.5,120.5,0,0,1-9.9,26.83A86.75,86.75,0,0,0,193.41,141.29Zm-2.47-36.78a113.13,113.13,0,0,0-9-22.72,135.21,135.21,0,0,1,0,45.46A112.78,112.78,0,0,0,190.94,104.51Zm1.34-42.77a60,60,0,0,0-45.12-45.12A93.42,93.42,0,0,1,167.59,41.3,93.59,93.59,0,0,1,192.27,61.75ZM176.2,176.2a61.05,61.05,0,0,0,16-29,93.79,93.79,0,0,1-24.61,20.36,93.56,93.56,0,0,1-20.43,24.67A60.89,60.89,0,0,0,176.2,176.2Zm-.91-110.56a113,113,0,0,0-9.69-22.34,113.18,113.18,0,0,0-22.27-9.67,137.37,137.37,0,0,1,17.29,14.65A134.72,134.72,0,0,1,175.28,65.65Zm-9.69,99.94a113.19,113.19,0,0,0,9.67-22.29,136.49,136.49,0,0,1-31.95,32A113.54,113.54,0,0,0,165.59,165.59Zm-2.86-127a86.51,86.51,0,0,0-21.48-23.16,79.11,79.11,0,0,0-12.17-.94,92.78,92.78,0,0,0-19.29,2.11,118.82,118.82,0,0,1,26.13,12.1A120.34,120.34,0,0,1,162.73,38.62ZM141.25,193.44a86.35,86.35,0,0,0,21.47-23.16,120.12,120.12,0,0,1-26.8,9.89,119.21,119.21,0,0,1-26.13,12.1,92.77,92.77,0,0,0,19.29,2.1A79.12,79.12,0,0,0,141.25,193.44Zm3.48-65.84c14.64-17.07,9-46.55,4-65.51-6.33,11.25-20.63,19.25-32.64,21.28-11.28,1.93-23.61-1.07-34.13,2.21-24,7.49-25.47,35.44-13,42.55l.6.49c.1,0,22-2.38,32.74-5.56,15.7-4.67,33.42-17.51,40-33.35-4,16.66-19.56,31.92-34.58,38.43-8,3.47-13.92,4.49-27.09,7.54a26.25,26.25,0,0,0-8.72,4C86.79,130.2,126.7,148.59,144.74,127.59ZM129.08,11.76c2.38,0,4.71.1,7,.29a59.69,59.69,0,0,0-63.27,0c2.29-.19,4.63-.29,7-.29a97,97,0,0,1,24.63,3.31A96.9,96.9,0,0,1,129.08,11.76Zm7,185.08q-3.44.29-7,.3a97.27,97.27,0,0,1-24.63-3.31,97.38,97.38,0,0,1-24.63,3.31c-2.38,0-4.71-.1-7-.29a59.66,59.66,0,0,0,63.27,0ZM127.26,26.95a111.86,111.86,0,0,0-22.82-9,111.67,111.67,0,0,0-22.82,9,135.38,135.38,0,0,1,45.63,0ZM104.45,191a111.86,111.86,0,0,0,22.82-9,135.38,135.38,0,0,1-45.63,0A111.67,111.67,0,0,0,104.45,191ZM99.1,16.62a92.7,92.7,0,0,0-19.28-2.11,79,79,0,0,0-12.17.94A86.37,86.37,0,0,0,46.16,38.62,120.27,120.27,0,0,1,73,28.72,118.82,118.82,0,0,1,99.1,16.62ZM79.81,194.37a92.69,92.69,0,0,0,19.28-2.1A119,119,0,0,1,73,180.17a120.34,120.34,0,0,1-26.81-9.89,86.51,86.51,0,0,0,21.48,23.16A79,79,0,0,0,79.81,194.37ZM61.16,141.82a6.27,6.27,0,1,0-6.38-6.17A6.25,6.25,0,0,0,61.16,141.82ZM65.57,33.63A113,113,0,0,0,43.31,43.3a113,113,0,0,0-9.7,22.34,136.36,136.36,0,0,1,32-32Zm-22.27,132a113.46,113.46,0,0,0,22.27,9.67,137.48,137.48,0,0,1-17.29-14.65,135.55,135.55,0,0,1-14.67-17.38A113.07,113.07,0,0,0,43.31,165.59ZM41.3,41.3A93.75,93.75,0,0,1,61.73,16.63a59.83,59.83,0,0,0-45,45A93.58,93.58,0,0,1,41.3,41.3Zm20.43,151A93.88,93.88,0,0,1,41.3,167.59a93.75,93.75,0,0,1-24.6-20.36,59.83,59.83,0,0,0,45,45Zm-33-119.19a120.11,120.11,0,0,1,9.93-26.91A86.22,86.22,0,0,0,15.49,67.6a86.47,86.47,0,0,0,1.19,31.51A120.37,120.37,0,0,1,28.69,73.07Zm9.93,89.66a120.11,120.11,0,0,1-9.93-26.91,120.37,120.37,0,0,1-12-26,86.46,86.46,0,0,0-1.19,31.51A86.36,86.36,0,0,0,38.62,162.73ZM25,104.45a134.91,134.91,0,0,1,1.92-22.66A113.2,113.2,0,0,0,18,104.45a113.2,113.2,0,0,0,8.95,22.66A134.91,134.91,0,0,1,25,104.45Zm-9.86,0a93.64,93.64,0,0,1-3-31.79,59.7,59.7,0,0,0,0,63.58A93.64,93.64,0,0,1,15.14,104.45Z" transform="translate(-0.21 -0.21)"/><path class="b" d="M148.71,62.09c5,19,10.66,48.44-4,65.51-18,21-57.95,2.6-72.81,12a26.25,26.25,0,0,1,8.72-4c13.17-3,19.1-4.07,27.09-7.54,15-6.51,30.56-21.77,34.58-38.43-6.6,15.84-24.31,28.69-40,33.35-10.7,3.18-32.63,5.56-32.74,5.56l-.6-.49c-12.49-7.1-11-35.05,13-42.55,10.52-3.28,22.85-.28,34.13-2.21C128.09,81.33,142.39,73.33,148.71,62.09Z" transform="translate(-0.21 -0.21)"/></svg>

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -0,0 +1,45 @@
<?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>

View File

@ -0,0 +1,25 @@
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;
}

View File

@ -0,0 +1,71 @@
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);
}
}

View File

@ -0,0 +1,21 @@
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 {
}
}