perf: 调整jackson依赖,引入json处理
parent
88dd323aab
commit
386074e878
@ -0,0 +1,87 @@
|
||||
package com.shxy.xymanager_common.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class JSONProcessor {
|
||||
|
||||
|
||||
public Map<String, Object> fromJSON2Map(String json) throws JsonParseException, JsonMappingException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> productMap = mapper.readValue(json, Map.class);
|
||||
return productMap;
|
||||
}
|
||||
|
||||
public Map<String, Object> fromJSON2Map(Object obj) throws JsonParseException, JsonMappingException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> productMap = mapper.convertValue(obj, Map.class);
|
||||
return productMap;
|
||||
}
|
||||
|
||||
public String buildJSONFromJSONObject(Object obj) {
|
||||
String jsonInString = null;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
jsonInString = mapper.writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
// LOG.error("JSON transform failed. ", e);
|
||||
}
|
||||
return jsonInString;
|
||||
}
|
||||
|
||||
public String buildJSONFromJSONObject(Object obj, boolean prettyPrinter) {
|
||||
String jsonInString = null;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
if (prettyPrinter) {
|
||||
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||
} else {
|
||||
jsonInString = mapper.writeValueAsString(obj);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// LOG.error("JSON transform failed. ", e);
|
||||
}
|
||||
return jsonInString;
|
||||
}
|
||||
|
||||
public <T> T buildJSONObjectFromJSON(String json, Class<T> clazz) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(json, clazz);
|
||||
} catch (UnrecognizedPropertyException e) {
|
||||
throw new Exception(e.getPropertyName(), e);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("JSON Object transform failed. ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T buildJSONObjectFromJSON(File file, Class<T> clazz) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(file, clazz);
|
||||
} catch (UnrecognizedPropertyException e) {
|
||||
throw new Exception(e.getPropertyName(), e);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("JSON Object transform failed. ", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @author roger - Sep 9, 2016
|
||||
* @version 2.0
|
||||
* file name: JSONProcessor.java
|
||||
* package name: com.roam2free.rest.util
|
||||
*/
|
||||
|
||||
package com.shxy.xymanager_common.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class JSONUtil {
|
||||
|
||||
private static JSONProcessor jSONProcessor = new JSONProcessor();
|
||||
|
||||
public static String object2Json(Object obj) {
|
||||
return jSONProcessor.buildJSONFromJSONObject(obj);
|
||||
}
|
||||
|
||||
public static String object2Json(Object obj, boolean prettyPrinter) {
|
||||
return jSONProcessor.buildJSONFromJSONObject(obj, prettyPrinter);
|
||||
}
|
||||
|
||||
public static <T> T json2Object(String json, Class<T> clazz) throws Exception {
|
||||
return jSONProcessor.buildJSONObjectFromJSON(json, clazz);
|
||||
}
|
||||
|
||||
public static <T> T file2Object(File file, Class<T> clazz) throws Exception {
|
||||
return jSONProcessor.buildJSONObjectFromJSON(file, clazz);
|
||||
}
|
||||
|
||||
public static Map<String, Object> object2Map(Object obj) {
|
||||
try {
|
||||
return jSONProcessor.fromJSON2Map(obj);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue