|
|
|
@ -6,6 +6,8 @@ import org.codehaus.jackson.JsonParseException;
|
|
|
|
|
import org.codehaus.jackson.map.JsonMappingException;
|
|
|
|
|
import org.codehaus.jackson.map.ObjectMapper;
|
|
|
|
|
import org.codehaus.jackson.type.TypeReference;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -20,6 +22,8 @@ import java.util.Map;
|
|
|
|
|
* @author xuxueli 2015-9-25 18:02:56
|
|
|
|
|
*/
|
|
|
|
|
public class JacksonUtil {
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
|
|
|
|
|
|
|
|
|
|
private final static ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
public static ObjectMapper getInstance() {
|
|
|
|
|
return objectMapper;
|
|
|
|
@ -36,11 +40,11 @@ public class JacksonUtil {
|
|
|
|
|
try {
|
|
|
|
|
return getInstance().writeValueAsString(obj);
|
|
|
|
|
} catch (JsonGenerationException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} catch (JsonMappingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -57,11 +61,11 @@ public class JacksonUtil {
|
|
|
|
|
try {
|
|
|
|
|
return getInstance().readValue(jsonStr, clazz);
|
|
|
|
|
} catch (JsonParseException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} catch (JsonMappingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -69,11 +73,11 @@ public class JacksonUtil {
|
|
|
|
|
try {
|
|
|
|
|
return getInstance().readValue(jsonStr, new TypeReference<T>() { });
|
|
|
|
|
} catch (JsonParseException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} catch (JsonMappingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -87,7 +91,7 @@ public class JacksonUtil {
|
|
|
|
|
System.out.println(json);
|
|
|
|
|
System.out.println(readValue(json, Map.class));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|