2018-11-28 16:22:33 +08:00
|
|
|
package com.gzzn.omms.msgexchangeapi.utils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonParseException;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
|
|
|
|
|
|
|
|
|
public class JsonUtil {
|
2018-11-30 11:30:08 +08:00
|
|
|
public static String getString(Object object)
|
2018-11-28 16:22:33 +08:00
|
|
|
{
|
2018-11-30 11:30:08 +08:00
|
|
|
try {
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
|
|
|
|
|
java.lang.String jsonStr;
|
|
|
|
|
jsonStr = ow.writeValueAsString(object);
|
|
|
|
|
return jsonStr;
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
} //end function
|
2018-11-28 16:22:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> T getObject(String str,Class<T> valueType) throws JsonParseException, JsonMappingException, IOException
|
|
|
|
|
{
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
T obj = mapper.readValue(str, valueType);
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|