package tech.powerjob.common.serialize; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.json.JsonMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import tech.powerjob.common.exception.ImpossibleException; import tech.powerjob.common.exception.PowerJobException; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * JSON工具类 * * @author tjq * @since 2020/4/16 */ @Slf4j public class JsonUtils { private static final JsonMapper JSON_MAPPER = JsonMapper.builder() .configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true) .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) .configure(JsonParser.Feature.IGNORE_UNDEFINED, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .build(); static { JSON_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 非核心功能可降级,尽可能降低依赖冲突概率 try { JSON_MAPPER.registerModule(new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule()); } catch (Exception e) { log.warn("[JsonUtils] registerJavaTimeModule failed, PowerJob can't process Java 8 date/time type now!", e); } } private static final TypeReference> MAP_TYPE_REFERENCE = new TypeReference> () {}; private JsonUtils(){ } public static String toJSONString(Object obj) { if (obj == null) { return null; } if (obj instanceof String) { return (String) obj; } try { return JSON_MAPPER.writeValueAsString(obj); }catch (Exception e) { log.error("[PowerJob] toJSONString failed", e); } return null; } public static String toJSONStringUnsafe(Object obj) { if (obj instanceof String) { return (String) obj; } try { return JSON_MAPPER.writeValueAsString(obj); }catch (Exception e) { ExceptionUtils.rethrow(e); } throw new ImpossibleException(); } public static byte[] toBytes(Object obj) { try { return JSON_MAPPER.writeValueAsBytes(obj); }catch (Exception e) { log.error("[PowerJob] serialize failed", e); } return null; } public static T parseObject(String json, Class clz) throws Exception { return JSON_MAPPER.readValue(json, clz); } public static Map parseMap(String json) { if (StringUtils.isEmpty(json)) { return new HashMap<>(); } try { return JSON_MAPPER.readValue(json, MAP_TYPE_REFERENCE); } catch (Exception e) { ExceptionUtils.rethrow(e); } throw new ImpossibleException(); } public static T parseObject(byte[] b, Class clz) throws IOException { return JSON_MAPPER.readValue(b, clz); } public static T parseObject(byte[] b, TypeReference typeReference) throws IOException { return JSON_MAPPER.readValue(b, typeReference); } public static T parseObject(String json, TypeReference typeReference) throws IOException { return JSON_MAPPER.readValue(json, typeReference); } public static T parseObjectIgnoreException(String json, Class clz) { if (StringUtils.isEmpty(json)) { return null; } try { return JSON_MAPPER.readValue(json, clz); }catch (Exception e) { log.error("unable to parse json string to object,current string:{}",json,e); return null; } } public static T parseObjectUnsafe(String json, Class clz) { try { return JSON_MAPPER.readValue(json, clz); }catch (Exception e) { ExceptionUtils.rethrow(e); } throw new PowerJobException("impossible"); } }