|
@@ -0,0 +1,285 @@
|
|
|
+package com.factory.base.util;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+
|
|
|
+import org.springframework.util.NumberUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
+import com.fasterxml.jackson.core.JsonParser.Feature;
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.MapperFeature;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class JacksonJsonUtils {
|
|
|
+
|
|
|
+ private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+
|
|
|
+ static {
|
|
|
+ //JsonReadFeature
|
|
|
+ objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
|
|
+ objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); // 设置日期解析格式
|
|
|
+ objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 设置null值为不序列化
|
|
|
+ objectMapper.configure(Feature.ALLOW_COMMENTS, true);//可以解析C或者java 的注释
|
|
|
+ objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);//可以解析无双引号的
|
|
|
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);//忽略位置字段
|
|
|
+ objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);// 忽略 transient 修饰的属性
|
|
|
+ SimpleModule simpleModule = new SimpleModule();
|
|
|
+ simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
|
|
|
+ objectMapper.registerModule(simpleModule);//防止Long超出JS识别范围
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ArrayNode getArrayNode() {
|
|
|
+ return objectMapper.createArrayNode();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ObjectNode getObjectNode() {
|
|
|
+ return objectMapper.createObjectNode();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过path选择JsonNode 可能返回一个MissingNode
|
|
|
+ *
|
|
|
+ * @param json {"name":"Jack","age":20,"class":["1","2","3"],"father":{"name":"old
|
|
|
+ * Jack","age":50}} father/name 可以选择到old Jack class/1 可以选举到"2"
|
|
|
+ * @return
|
|
|
+ * TypeReference<T> valueTypeRef
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T treeNodeToObject(JsonNode node, Class<T> classes) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ if (node != null && !node.isMissingNode() && !node.isNull()) {
|
|
|
+ result = objectMapper.treeToValue(node, classes);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("treeNodeToObject error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过path选择JsonNode 可能返回一个MissingNode
|
|
|
+ *
|
|
|
+ * @param json {"name":"Jack","age":20,"class":["1","2","3"],"father":{"name":"old
|
|
|
+ * Jack","age":50}} father/name 可以选择到old Jack class/2 可以选举到"2"
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JsonNode pathSelectNode(JsonNode node, String path) {
|
|
|
+ JsonNode result = node;
|
|
|
+ try {
|
|
|
+ if (!StringUtils.hasText(path)) {
|
|
|
+ return node;
|
|
|
+ }
|
|
|
+ String[] paths = StringUtils.split(path, "/");
|
|
|
+ for (String item : paths) {
|
|
|
+
|
|
|
+ Integer num = null;
|
|
|
+ try {
|
|
|
+ num = NumberUtils.parseNumber(item, Integer.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ if (num != null) {
|
|
|
+ result = result.path(num);
|
|
|
+ } else {
|
|
|
+ result = result.path(item);
|
|
|
+ }
|
|
|
+ if (result.isMissingNode() || result.isNull()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("pathSelectNode error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成JsonNode并获取路径下的节点
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JsonNode readTree(String json, String path) {
|
|
|
+ JsonNode result = null;
|
|
|
+ try {
|
|
|
+ result = pathSelectNode(objectMapper.readTree(json), path);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json readTree error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成JsonNode
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JsonNode readTree(String json) {
|
|
|
+ return readTree(json, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成对象
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param classes 对象类型
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T readObject(String json, Class<T> classes) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.readValue(json, classes);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成对象
|
|
|
+ * TypeReference<T> r = new TypeReference<T>(){} ;
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param classes 对象类型
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T readObject(String json,TypeReference<T> valueTypeRef) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.readValue(json, valueTypeRef);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成对象
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param classes 对象类型
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T readObject(File file, Class<T> classes) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.readValue(file, classes);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成对象
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param classes 对象类型
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T readObject(File file, TypeReference<T> valueTypeRef) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.readValue(file, valueTypeRef);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成对象
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param classes 对象类型
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T readObject(InputStream in, Class<T> classes) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.readValue(in, classes);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将JSON串解析成对象
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param classes 对象类型
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T readObject(InputStream in, TypeReference<T> valueTypeRef) {
|
|
|
+ T result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.readValue(in, valueTypeRef);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象串解析成JSON字符串
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String writeObject(Object object) {
|
|
|
+ String result = null;
|
|
|
+ try {
|
|
|
+ result = objectMapper.writeValueAsString(object);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象串写到文件
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static void writeObject(File file, Object object) {
|
|
|
+ try {
|
|
|
+ objectMapper.writeValue(file, object);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将对象串写到文件
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static void writeObject(OutputStream out, Object object) {
|
|
|
+ try {
|
|
|
+ objectMapper.writeValue(out, object);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("parse json object error:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|