/* * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). *

* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.gnu.org/licenses/lgpl.html *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.dreamlu.mica.core.utils; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; 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 lombok.experimental.UtilityClass; import net.dreamlu.mica.core.jackson.MicaJavaTimeModule; import org.springframework.lang.Nullable; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.util.*; /** * json 工具类 * * @author L.cm */ @UtilityClass public class JsonUtil { /** * 将对象序列化成json字符串 * * @param object javaBean * @return jsonString json字符串 */ @Nullable public static String toJson(@Nullable Object object) { if (object == null) { return null; } try { return getInstance().writeValueAsString(object); } catch (JsonProcessingException e) { throw Exceptions.unchecked(e); } } /** * 将对象序列化成 json byte 数组 * * @param object javaBean * @return jsonString json字符串 */ @Nullable public static byte[] toJsonAsBytes(@Nullable Object object) { if (object == null) { return null; } try { return getInstance().writeValueAsBytes(object); } catch (JsonProcessingException e) { throw Exceptions.unchecked(e); } } /** * 将json字符串转成 JsonNode * * @param jsonString jsonString * @return jsonString json字符串 */ public static JsonNode readTree(String jsonString) { Objects.requireNonNull(jsonString, "jsonString is null"); try { return getInstance().readTree(jsonString); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json字符串转成 JsonNode * * @param in InputStream * @return jsonString json字符串 */ 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); } } /** * 将json字符串转成 JsonNode * * @param content content * @return jsonString json字符串 */ 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); } } /** * 将json字符串转成 JsonNode * * @param jsonParser JsonParser * @return jsonString json字符串 */ public static JsonNode readTree(JsonParser jsonParser) { Objects.requireNonNull(jsonParser, "jsonParser is null"); try { return getInstance().readTree(jsonParser); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json byte 数组反序列化成对象 * * @param content json bytes * @param valueType class * @param T 泛型标记 * @return Bean */ @Nullable public static T readValue(@Nullable byte[] content, Class valueType) { if (ObjectUtil.isEmpty(content)) { return null; } try { return getInstance().readValue(content, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json反序列化成对象 * * @param jsonString jsonString * @param valueType class * @param T 泛型标记 * @return Bean */ @Nullable public static T readValue(@Nullable String jsonString, Class valueType) { if (StringUtil.isBlank(jsonString)) { return null; } try { return getInstance().readValue(jsonString, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json反序列化成对象 * * @param in InputStream * @param valueType class * @param T 泛型标记 * @return Bean */ @Nullable public static T readValue(@Nullable InputStream in, Class valueType) { if (in == null) { return null; } try { return getInstance().readValue(in, valueType); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json反序列化成对象 * * @param content bytes * @param typeReference 泛型类型 * @param T 泛型标记 * @return Bean */ @Nullable public static T readValue(@Nullable byte[] content, TypeReference typeReference) { if (ObjectUtil.isEmpty(content)) { return null; } try { return getInstance().readValue(content, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json反序列化成对象 * * @param jsonString jsonString * @param typeReference 泛型类型 * @param T 泛型标记 * @return Bean */ @Nullable public static T readValue(@Nullable String jsonString, TypeReference typeReference) { if (StringUtil.isBlank(jsonString)) { return null; } try { return getInstance().readValue(jsonString, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 将json反序列化成对象 * * @param in InputStream * @param typeReference 泛型类型 * @param T 泛型标记 * @return Bean */ @Nullable public static T readValue(@Nullable InputStream in, TypeReference typeReference) { if (in == null) { return null; } try { return getInstance().readValue(in, typeReference); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 封装 map type * * @param keyClass key 类型 * @param valueClass value 类型 * @return MapType */ public static MapType getMapType(Class keyClass, Class valueClass) { return getInstance().getTypeFactory().constructMapType(Map.class, keyClass, valueClass); } /** * 封装 map type * * @param elementClass 集合值类型 * @return CollectionLikeType */ public static CollectionLikeType getListType(Class elementClass) { return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass); } /** * 读取集合 * * @param content bytes * @param elementClass elementClass * @param 泛型 * @return 集合 */ @Nullable public static List readList(@Nullable byte[] content, Class elementClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyList(); } try { return getInstance().readValue(content, getListType(elementClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 读取集合 * * @param content InputStream * @param elementClass elementClass * @param 泛型 * @return 集合 */ @Nullable public static List readList(@Nullable InputStream content, Class elementClass) { if (content == null) { return Collections.emptyList(); } try { return getInstance().readValue(content, getListType(elementClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 读取集合 * * @param content bytes * @param elementClass elementClass * @param 泛型 * @return 集合 */ @Nullable private static List readList(@Nullable String content, Class elementClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyList(); } try { return getInstance().readValue(content, getListType(elementClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 读取集合 * * @param content bytes * @param keyClass key类型 * @param valueClass 值类型 * @param 泛型 * @param 泛型 * @return 集合 */ @Nullable public static Map readMap(@Nullable byte[] content, Class keyClass, Class valueClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyMap(); } try { return getInstance().readValue(content, getMapType(keyClass, valueClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 读取集合 * * @param content InputStream * @param keyClass key类型 * @param valueClass 值类型 * @param 泛型 * @param 泛型 * @return 集合 */ @Nullable public static Map readMap(@Nullable InputStream content, Class keyClass, Class valueClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyMap(); } try { return getInstance().readValue(content, getMapType(keyClass, valueClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } /** * 读取集合 * * @param content bytes * @param keyClass key类型 * @param valueClass 值类型 * @param 泛型 * @param 泛型 * @return 集合 */ @Nullable private static Map readMap(@Nullable String content, Class keyClass, Class valueClass) { if (ObjectUtil.isEmpty(content)) { return Collections.emptyMap(); } try { return getInstance().readValue(content, getMapType(keyClass, valueClass)); } catch (IOException e) { throw Exceptions.unchecked(e); } } public static ObjectMapper getInstance() { return JacksonHolder.INSTANCE; } private static class JacksonHolder { private static ObjectMapper INSTANCE = new JacksonObjectMapper(); } private static class JacksonObjectMapper extends ObjectMapper { private static final long serialVersionUID = 4288193147502386170L; private static final Locale CHINA = Locale.CHINA; JacksonObjectMapper() { super(); super.setLocale(CHINA); super.setDateFormat(new SimpleDateFormat(DateUtil.PATTERN_DATETIME, CHINA)); // 单引号 super.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); super.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true); // 允许JSON字符串包含非引号控制字符(值小于32的ASCII字符,包含制表符和换行符) super.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); // 忽略json字符串中不识别的属性 super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 忽略无法转换的对象 super.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); super.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); super.findAndRegisterModules(); super.registerModule(new MicaJavaTimeModule()); } } }