一、ObjectMapper
ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构,反之亦然。它使用JsonParser和JsonGenerator的实例实现JSON实际的读/写。
maven依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
二、代码
import com.google.common.collect.Lists;
import com.mmall.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Slf4j
public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);
objectMapper.configure(SerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false);
objectMapper.setDateFormat(new SimpleDateFormat(DateTimeUtil.STANDARD_FORMAT));
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES,false);
}
public static <T> String obj2String(T obj){
if (obj == null){
return null;
}
try {
return obj instanceof String ? (String) obj:objectMapper.writeValueAsString(obj);
} catch (IOException e) {
log.warn("Parse object to String error", e);
return null;
}
}
public static <T> String obj2StringPretty(T obj){
if (obj == null){
return null;
}
try {
return obj instanceof String ? (String) obj:objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (IOException e) {
log.warn("Parse object to String error", e);
return null;
}
}
public static <T> T string2Obj(String str, Class<T> clazz){
if (StringUtils.isEmpty(str) || clazz == null){
return null;
}
try {
return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str,clazz);
} catch (IOException e) {
log.warn("Parse object to Object error", e);
return null;
}
}
public static <T> T string2Obj(String str, TypeReference<T> typeReference){
if (StringUtils.isEmpty(str) || typeReference == null){
return null;
}
try {
return typeReference.getType().equals(String.class) ? (T) str : (T) objectMapper.readValue(str, typeReference);
} catch (IOException e) {
log.warn("Parse object to Object error", e);
return null;
}
}
public static <T> T string2Obj(String str, Class<T> collectionClass, Class<?>... elementClasses){
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
try {
return objectMapper.readValue(str, javaType);
} catch (IOException e) {
log.warn("Parse object to Object error", e);
return null;
}
}
public static void main(String[] args) {
}
}
三、扩展
利用对象转换json。可以做单点登录,将用户的信息转换为json数据,作为redis的value值;将用户的sessionid作为key,存储到redis中。
|