feat: 增加钉钉发送
parent
a7d0730768
commit
a9e92c5db0
@ -0,0 +1,87 @@
|
||||
package com.xydl.cac.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,60 @@
|
||||
package com.xydl.cac.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public static String object2Xml(Object obj) throws JsonProcessingException {
|
||||
XmlMapper xmlMapper = XmlMapper.builder()
|
||||
.enable(SerializationFeature.INDENT_OUTPUT)
|
||||
.build();
|
||||
return xmlMapper.writeValueAsString(obj);
|
||||
}
|
||||
|
||||
public static <T> T xml2Object(String xml, Class<T> clazz) throws JsonProcessingException {
|
||||
XmlMapper xmlMapper = XmlMapper.builder()
|
||||
.build();
|
||||
return xmlMapper.readValue(xml, clazz);
|
||||
}
|
||||
|
||||
public static JsonNode xml2JsonNode(String xml) throws JsonProcessingException {
|
||||
XmlMapper xmlMapper = XmlMapper.builder()
|
||||
.build();
|
||||
return xmlMapper.readTree(xml);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue