package com.qianwen.core.tool.jackson; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.CollectionLikeType; import com.fasterxml.jackson.databind.type.MapType; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.TimeZone; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.qianwen.core.tool.utils.Exceptions; import com.qianwen.core.tool.utils.ObjectUtil; import com.qianwen.core.tool.utils.StringPool; import com.qianwen.core.tool.utils.StringUtil; import org.springframework.lang.Nullable; /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/jackson/JsonUtil.class */ public class JsonUtil { private static final Logger log = LoggerFactory.getLogger(JsonUtil.class); public static String toJson(T value) { try { return getInstance().writeValueAsString(value); } catch (Exception e) { log.error(e.getMessage(), e); return null; } } public static byte[] toJsonAsBytes(Object object) { try { return getInstance().writeValueAsBytes(object); } catch (JsonProcessingException e) { throw Exceptions.unchecked(e); } } public static T parse(String content, Class valueType) { try { return (T) getInstance().readValue(content, valueType); } catch (Exception e) { log.error(e.getMessage(), e); return null; } } public static T parse(String content, TypeReference typeReference) { try { return (T) getInstance().readValue(content, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static T parse(byte[] bytes, Class valueType) { try { return (T) getInstance().readValue(bytes, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static T parse(byte[] bytes, TypeReference typeReference) { try { return (T) getInstance().readValue(bytes, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static T parse(InputStream in, Class valueType) { try { return (T) getInstance().readValue(in, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static T parse(InputStream in, TypeReference typeReference) { try { return (T) getInstance().readValue(in, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static List parseArray(String content, Class valueTypeRef) { try { if (!StringUtil.startsWithIgnoreCase(content, StringPool.LEFT_SQ_BRACKET)) { content = StringPool.LEFT_SQ_BRACKET + content + StringPool.RIGHT_SQ_BRACKET; } List> list = (List) getInstance().readValue(content, new TypeReference>>() { // from class: org.springblade.core.tool.jackson.JsonUtil.1 }); ArrayList arrayList = new ArrayList(); for (Map map : list) { arrayList.add(toPojo(map, valueTypeRef)); } return arrayList; } catch (IOException e) { log.error(e.getMessage(), e); return null; } } public static JsonNode readTree(String jsonString) { Objects.requireNonNull(jsonString, "jsonString is null"); try { return getInstance().readTree(jsonString); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static JsonNode readTree(InputStream in) { Objects.requireNonNull(in, "InputStream in is null"); try { return getInstance().readTree(in); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static JsonNode readTree(byte[] content) { Objects.requireNonNull(content, "byte[] content is null"); try { return getInstance().readTree(content); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static JsonNode readTree(JsonParser jsonParser) { Objects.requireNonNull(jsonParser, "jsonParser is null"); try { return getInstance().readTree(jsonParser); } catch (IOException e) { throw Exceptions.unchecked(e); } } @Nullable public static T readValue(@Nullable byte[] content, Class valueType) { if (ObjectUtil.isEmpty(content)) { return null; } try { return (T) getInstance().readValue(content, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } @Nullable public static T readValue(@Nullable String jsonString, Class valueType) { if (StringUtil.isBlank(jsonString)) { return null; } try { return (T) getInstance().readValue(jsonString, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } @Nullable public static T readValue(@Nullable InputStream in, Class valueType) { if (in == null) { return null; } try { return (T) getInstance().readValue(in, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } @Nullable public static T readValue(@Nullable byte[] content, TypeReference typeReference) { if (ObjectUtil.isEmpty(content)) { return null; } try { return (T) getInstance().readValue(content, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } @Nullable public static T readValue(@Nullable String jsonString, TypeReference typeReference) { if (StringUtil.isBlank(jsonString)) { return null; } try { return (T) getInstance().readValue(jsonString, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } @Nullable public static T readValue(@Nullable InputStream in, TypeReference typeReference) { if (in == null) { return null; } try { return (T) getInstance().readValue(in, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static MapType getMapType(Class keyClass, Class valueClass) { return getInstance().getTypeFactory().constructMapType(Map.class, keyClass, valueClass); } public static CollectionLikeType getListType(Class elementClass) { return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass); } public static List readList(@Nullable byte[] content, Class elementClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyList(); } try { return (List) getInstance().readValue(content, getListType(elementClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static List readList(@Nullable InputStream content, Class elementClass) { if (content == null) { return Collections.emptyList(); } try { return (List) getInstance().readValue(content, getListType(elementClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static List readList(@Nullable String content, Class elementClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyList(); } try { return (List) getInstance().readValue(content, getListType(elementClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static Map readMap(@Nullable byte[] content, Class keyClass, Class valueClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyMap(); } try { return (Map) getInstance().readValue(content, getMapType(keyClass, valueClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static Map readMap(@Nullable InputStream content, Class keyClass, Class valueClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyMap(); } try { return (Map) getInstance().readValue(content, getMapType(keyClass, valueClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static Map readMap(@Nullable String content, Class keyClass, Class valueClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyMap(); } try { return (Map) getInstance().readValue(content, getMapType(keyClass, valueClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static T convertValue(Object fromValue, Class toValueType) { return (T) getInstance().convertValue(fromValue, toValueType); } public static T convertValue(Object fromValue, JavaType toValueType) { return (T) getInstance().convertValue(fromValue, toValueType); } public static T convertValue(Object fromValue, TypeReference toValueTypeRef) { return (T) getInstance().convertValue(fromValue, toValueTypeRef); } public static T treeToValue(TreeNode treeNode, Class valueType) { try { return (T) getInstance().treeToValue(treeNode, valueType); } catch (JsonProcessingException e) { throw Exceptions.unchecked(e); } } public static JsonNode valueToTree(@Nullable Object value) { return getInstance().valueToTree(value); } public static boolean canSerialize(@Nullable Object value) { if (value == null) { return true; } return getInstance().canSerialize(value.getClass()); } public static Map toMap(String content) { try { return (Map) getInstance().readValue(content, Map.class); } catch (IOException e) { log.error(e.getMessage(), e); return null; } } public static Map toMap(String content, Class valueTypeRef) { try { Map> map = (Map) getInstance().readValue(content, new TypeReference>>() { // from class: org.springblade.core.tool.jackson.JsonUtil.2 }); HashMap hashMap = new HashMap(16); for (Map.Entry> entry : map.entrySet()) { hashMap.put(entry.getKey(), toPojo(entry.getValue(), valueTypeRef)); } return hashMap; } catch (IOException e) { log.error(e.getMessage(), e); return null; } } public static T toPojo(Map fromValue, Class toValueType) { return (T) getInstance().convertValue(fromValue, toValueType); } public static ObjectMapper getInstance() { return JacksonHolder.INSTANCE; } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/jackson/JsonUtil$JacksonHolder.class */ public static class JacksonHolder { private static final ObjectMapper INSTANCE = new JacksonObjectMapper(); private JacksonHolder() { } } /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/jackson/JsonUtil$JacksonObjectMapper.class */ private static class JacksonObjectMapper extends ObjectMapper { private static final long serialVersionUID = 4288193147502386170L; private static final Locale CHINA = Locale.CHINA; public JacksonObjectMapper(ObjectMapper src) { super(src); } public JacksonObjectMapper() { super.setLocale(CHINA); super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); super.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); super.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)); super.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); super.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true); super.configure(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER.mappedFeature(), true); super.findAndRegisterModules(); super.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); super.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), true); super.getDeserializationConfig().withoutFeatures(new DeserializationFeature[]{DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES}); super.registerModule(new BladeJavaTimeModule()); super.findAndRegisterModules(); } public ObjectMapper copy() { return new JacksonObjectMapper(this); } } }