标签:序列化 https pen OLE base condition des lse tor
直接见代码,需要引入的包如下文,需要谷歌的包。
1 package com.cxf.value; 2 3 import com.fasterxml.jackson.core.type.TypeReference; 4 import com.fasterxml.jackson.databind.DeserializationFeature; 5 import com.fasterxml.jackson.databind.ObjectMapper; 6 import com.fasterxml.jackson.databind.type.TypeFactory; 7 import com.google.common.base.Preconditions; 8 9 import java.io.IOException; 10 import java.io.StringWriter; 11 import java.io.Writer; 12 13 public class JsonUtils { 14 private static ObjectMapper objectMapper = new ObjectMapper(); 15 16 public JsonUtils() { 17 } 18 19 public static ObjectMapper getMapper() { 20 return objectMapper; 21 } 22 23 public static String serialize(Object object) throws IOException { 24 return serialize(object, false); 25 } 26 27 public static String serialize(Object object, boolean withPretty) throws IOException { 28 Writer write = new StringWriter(); 29 if (withPretty) { 30 objectMapper.writerWithDefaultPrettyPrinter().writeValue(write, object); 31 } else { 32 objectMapper.writeValue(write, object); 33 } 34 35 return write.toString(); 36 } 37 38 public static String serialize(ObjectMapper objectMapper, Object object, boolean withPretty) throws IOException { 39 Writer write = new StringWriter(); 40 if (withPretty) { 41 objectMapper.writerWithDefaultPrettyPrinter().writeValue(write, object); 42 } else { 43 objectMapper.writeValue(write, object); 44 } 45 46 return write.toString(); 47 } 48 49 public static <T> T deserialize(String json, Class<T> classType) throws IOException { 50 Preconditions.checkNotNull(json); 51 return (T) objectMapper.readValue(json, TypeFactory.rawClass(classType)); 52 } 53 54 public static <T> T deserialize(String json, TypeReference<T> typeRef) throws IOException { 55 return objectMapper.readValue(json, typeRef); 56 } 57 58 public static <T> T deserialize(ObjectMapper objectMapper, String json, TypeReference<T> typeRef) throws IOException { 59 return objectMapper.readValue(json, typeRef); 60 } 61 62 static { 63 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 64 } 65 }
1 <!-- use jackson lib--> 2 <dependency> 3 <groupId>com.alibaba</groupId> 4 <artifactId>fastjson</artifactId> 5 <version>1.2.44</version> 6 </dependency> 7 <dependency> 8 <groupId>com.google.code.gson</groupId> 9 <artifactId>gson</artifactId> 10 <version>2.8.2</version> 11 </dependency> 12 <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> 13 <dependency> 14 <groupId>com.google.guava</groupId> 15 <artifactId>guava</artifactId> 16 <version>27.0-jre</version> 17 </dependency>
标签:序列化 https pen OLE base condition des lse tor
原文地址:https://www.cnblogs.com/javallh/p/9976917.html