fastjson没有jackjson稳定,所以换成jackjson来处理对象转json和json转对象问题。
首先下载jackjson包,三个都要引用
然后修改实现类
package JRT.Core.Util;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.lang.reflect.Field;
import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;
/**
* json工具类,处理对象和json
*/
public class JsonUtil {
/**
* Object对象转Json串
*
* @param object
* @return
*/
public static String Object2Json(Object object) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(object);
}
/**
* Object转Json串
*
* @param object 对象
* @param null2empty null值是否转空""
* @return
*/
public static String Object2Json(Object object, boolean null2empty) throws Exception {
if (!null2empty) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(object);
} else {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString("");
}
});
return objectMapper.writeValueAsString(object);
}
}
/**
* Json串转Object
*
* @param json json串
* @param c 类型
* @return
*/
public static Object Json2Object(String json, Class c) throws Exception {
if (json.isEmpty()) {
return null;
} else {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper.readValue(json, c);
}
}
/**
* 克隆一个对象
*
* @param obj 要克隆的对象
* @return
*/
public static Object CloneObject(Object obj) throws Exception {
if (obj==null) {
return null;
}
else {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String json=Object2Json(obj);
return objectMapper.readValue(json, obj.getClass());
}
}
}
老的fastjson的包装
package JRT.Core.Util;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.lang.reflect.Field;
/**
* json工具类,处理对象和json
*/
public class JsonUtil
{
/**
* Object对象转Json串
* @param object
* @return
*/
public static String Object2Json(Object object)
{
return JSONObject.toJSONString(object, SerializerFeature.PrettyFormat);
}
/**
* Object转Json串
* @param object 对象
* @param null2empty null值是否转空""
* @return
*/
public static String Object2Json(Object object, boolean null2empty)
{
if (!null2empty)
{
return JSON.toJSONString(object);
}
else
{
return JSON.toJSONString(object, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.PrettyFormat);
}
}
/**
* Json串转Object
* @param json json串
* @param c 类型
* @return
*/
public static Object Json2Object(String json,Class c)
{
if (json.isEmpty())
{
return null;
}
else
{
return JSON.parseObject(json,c);
}
}
}
以上就是fastjson换jackjson的改动