基于com.fasterxml.jackson的JSON数据处理工具类

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 基于com.fasterxml.jackson的JSON数据处理工具类
 */
@Slf4j
public class ObjectMapperUtils {
	
	/**
	 * 将对象序列化成json字符串
	 * 
	 * @param value
	 * @return
	 */
	public static String toJSON(Object object) {
		try {
			return getInstance().writeValueAsString(object);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public static String toJSON(Object object, Class serializationView) {
		try {
			return getInstance().writerWithView(serializationView).writeValueAsString(object);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 将对象序列化成JSON byte数组
	 * 
	 * @param object
	 * @return JSON byte数组
	 */
	public static byte[] toJSONAsBytes(Object object) {
		try {
			return getInstance().writeValueAsBytes(object);
		} catch (JsonProcessingException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将JSON反序列化成对象
	 * 
	 * @param content
	 * @param valueType
	 * @return
	 */
	public static  T fromJSON(String content, Class valueType) {
		try {
			return getInstance().readValue(content, valueType);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 将JSON反序列化成对象
	 * @param content
	 * @param typeReference
	 *        泛型类型 new TypeReference<People<Chinese>>(){}
	 * @return
	 */
	public static  T fromJSON(String content, TypeReference typeReference) {
		try {
			return getInstance().readValue(content, typeReference);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将JSON byte数组反序列化成对象
	 * 
	 * @param bytes
	 * @param valueType
	 * @return
	 */
	public static  T fromJSON(byte[] bytes, Class valueType) {
		try {
			return getInstance().readValue(bytes, valueType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将JSON byte数组反序列化成对象
	 * 
	 * @param bytes
	 * @param typeReference
	 *        泛型类型 new TypeReference<People<Chinese>>(){}
	 * @return
	 */
	public static  T fromJSON(byte[] bytes, TypeReference typeReference) {
		try {
			return getInstance().readValue(bytes, typeReference);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将JSON反序列化成对象
	 * 
	 * @param in
	 * @param valueType
	 * @return
	 */
	public static  T fromJSON(InputStream in, Class valueType) {
		try {
			return getInstance().readValue(in, valueType);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将JSON反序列化成对象
	 * 
	 * @param in
	 * @param typeReference
	 *        泛型类型 new TypeReference<People<Chinese>>(){}
	 * @return
	 */
	public static  T fromJSON(InputStream in, TypeReference typeReference) {
		try {
			return getInstance().readValue(in, typeReference);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  T toList(String json, Class collectionType, Class valueType) {
		if (StrUtil.isBlank(json)) {
			try {
				return (T) collectionType.newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		try {
			return (T) getInstance().readValue(json, TypeFactory.defaultInstance().constructCollectionType(collectionType, valueType));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  T toMap(String json, Class mapType, Class keyType, Class valueType) {
		if (StrUtil.isBlank(json)) {
			try {
				return (T) mapType.newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		try {
			return (T) getInstance().readValue(json, TypeFactory.defaultInstance().constructMapType(mapType, keyType, valueType));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static  T toPojo(Map fromValue, Class toValueType) {
		return getInstance().convertValue(fromValue, toValueType);
	}

	/**
	 * 将JSON字符串转成JsonNode
	 * 
	 * 
	 * @param json
	 * @return
	 */
	public static JsonNode readTree(String json) {
		try {
			return getInstance().readTree(json);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	/**
	 * 将JSON字符串转成JsonNode
	 * 
	 * 
	 * @param in
	 * @return
	 */
	public static JsonNode readTree(InputStream in) {
		try {
			return getInstance().readTree(in);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	public static JsonNode readTree(byte[] content) {
		try {
			return getInstance().readTree(content);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	public static JsonNode readTree(JsonParser jsonParser) {
		try {
			return getInstance().readTree(jsonParser);
		} catch (IOException e) {
			throw Exceptions.unchecked(e);
		}
	}

	public static ObjectMapper getInstance() {
		return JacksonHolder.INSTANCE;
	}

	private static class JacksonHolder {
		private static ObjectMapper INSTANCE = Jackson2ObjectMapperBuilder.json().createXmlMapper(false).build();
	}

	/**
	 * 使用alibaba FastJSON格式化JSON
	 * 
	 * @param json
	 * @return
	 */
	public static Object formatJSON(String json) {
		return JSON.toJSONString(JSONObject.parseObject(json), SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, 
	            SerializerFeature.WriteDateUseDateFormat);
	}

你可能感兴趣的:(Java,java)