From a2069997391c945c52ee3da56d44513e99803908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E7=A5=A5?= <1366971433@qq.com> Date: Wed, 9 Jan 2019 17:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jackson=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pictures/icon-spring-boot.svg | 1 + pictures/icon-spring-cloud.svg | 1 + pictures/icon-spring-framework.svg | 1 + .../jackson-samples/pom.xml | 45 ++++++++++++ .../main/java/com/heibaiying/bean/User.java | 25 +++++++ .../com/heibaiying/jackson/JacksonUtils.java | 71 +++++++++++++++++++ .../src/test/java/JacksonTests.java | 21 ++++++ 8 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 pictures/icon-spring-boot.svg create mode 100644 pictures/icon-spring-cloud.svg create mode 100644 pictures/icon-spring-framework.svg create mode 100644 third-party frameworks or libraries/jackson-samples/pom.xml create mode 100644 third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/bean/User.java create mode 100644 third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/jackson/JacksonUtils.java create mode 100644 third-party frameworks or libraries/jackson-samples/src/test/java/JacksonTests.java diff --git a/README.md b/README.md index db5026f..25b4323 100644 --- a/README.md +++ b/README.md @@ -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)
diff --git a/pictures/icon-spring-boot.svg b/pictures/icon-spring-boot.svg new file mode 100644 index 0000000..f71953e --- /dev/null +++ b/pictures/icon-spring-boot.svg @@ -0,0 +1 @@ +Asset 1 \ No newline at end of file diff --git a/pictures/icon-spring-cloud.svg b/pictures/icon-spring-cloud.svg new file mode 100644 index 0000000..b88888c --- /dev/null +++ b/pictures/icon-spring-cloud.svg @@ -0,0 +1 @@ +Asset 1 \ No newline at end of file diff --git a/pictures/icon-spring-framework.svg b/pictures/icon-spring-framework.svg new file mode 100644 index 0000000..5f55eef --- /dev/null +++ b/pictures/icon-spring-framework.svg @@ -0,0 +1 @@ +Asset 1 \ No newline at end of file diff --git a/third-party frameworks or libraries/jackson-samples/pom.xml b/third-party frameworks or libraries/jackson-samples/pom.xml new file mode 100644 index 0000000..ff876f1 --- /dev/null +++ b/third-party frameworks or libraries/jackson-samples/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.heibaiying + jackson-samples + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.8 + + + org.projectlombok + lombok + 1.18.4 + provided + + + junit + junit + 4.12 + test + + + + + \ No newline at end of file diff --git a/third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/bean/User.java b/third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/bean/User.java new file mode 100644 index 0000000..5d2dbab --- /dev/null +++ b/third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/bean/User.java @@ -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; +} diff --git a/third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/jackson/JacksonUtils.java b/third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/jackson/JacksonUtils.java new file mode 100644 index 0000000..126aa77 --- /dev/null +++ b/third-party frameworks or libraries/jackson-samples/src/main/java/com/heibaiying/jackson/JacksonUtils.java @@ -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 jsonToBean(String json, Class valueType) throws IOException { + return mapper.readValue(json, valueType); + } + + /*** + * spring json 转换为List + */ + public static List jsonToList(String json, Class valueType) throws IOException { + List> list = mapper.readValue(json, new TypeReference>() { + }); + 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 Map jsonToMap(String json, Class clazz) throws IOException { + Map> map = mapper.readValue(json, + new TypeReference>() { + }); + Map result = new HashMap<>(); + for (Map.Entry> entry : map.entrySet()) { + result.put(entry.getKey(), mapToBean(entry.getValue(), clazz)); + } + return result; + } + + /*** + * map 转换为 bean + */ + public static T mapToBean(Map map, Class valueType) { + return mapper.convertValue(map, valueType); + } +} diff --git a/third-party frameworks or libraries/jackson-samples/src/test/java/JacksonTests.java b/third-party frameworks or libraries/jackson-samples/src/test/java/JacksonTests.java new file mode 100644 index 0000000..f1e0bbc --- /dev/null +++ b/third-party frameworks or libraries/jackson-samples/src/test/java/JacksonTests.java @@ -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 { + + } +}