|
@@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.JavaType;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
import com.fasterxml.jackson.databind.util.JSONPObject;
|
|
|
-import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -18,10 +17,6 @@ public class JsonMapper {
|
|
|
private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
|
|
|
private ObjectMapper mapper;
|
|
|
|
|
|
- public JsonMapper() {
|
|
|
- this((JsonInclude.Include)null);
|
|
|
- }
|
|
|
-
|
|
|
public JsonMapper(JsonInclude.Include include) {
|
|
|
this.mapper = new ObjectMapper();
|
|
|
if (include != null) {
|
|
@@ -61,49 +56,6 @@ public class JsonMapper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public <T> T fromJson(String jsonString, JavaType javaType) {
|
|
|
- if (StringUtils.isEmpty(jsonString)) {
|
|
|
- return null;
|
|
|
- } else {
|
|
|
- try {
|
|
|
- return this.mapper.readValue(jsonString, javaType);
|
|
|
- } catch (IOException var4) {
|
|
|
- logger.warn("parse json string error:" + jsonString, var4);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public JavaType createCollectionType(Class<?> collectionClass, Class... elementClasses) {
|
|
|
- return this.mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
|
|
|
- }
|
|
|
-
|
|
|
- public <T> T update(String jsonString, T object) {
|
|
|
- try {
|
|
|
- return this.mapper.readerForUpdating(object).readValue(jsonString);
|
|
|
- } catch (JsonProcessingException var4) {
|
|
|
- logger.warn("update json string:" + jsonString + " to object:" + object + " error.", var4);
|
|
|
- } catch (IOException var5) {
|
|
|
- logger.warn("update json string:" + jsonString + " to object:" + object + " error.", var5);
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public String toJsonP(String functionName, Object object) {
|
|
|
- return this.toJson(new JSONPObject(functionName, object));
|
|
|
- }
|
|
|
-
|
|
|
- public void enableEnumUseToString() {
|
|
|
- this.mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
|
|
- this.mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
|
|
|
- }
|
|
|
-
|
|
|
- public void enableJaxbAnnotation() {
|
|
|
- JaxbAnnotationModule module = new JaxbAnnotationModule();
|
|
|
- this.mapper.registerModule(module);
|
|
|
- }
|
|
|
-
|
|
|
public ObjectMapper getMapper() {
|
|
|
return this.mapper;
|
|
|
}
|