package org.sxkj.common.utils;
|
|
import com.alibaba.nacos.shaded.com.google.gson.FieldNamingPolicy;
|
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import org.springframework.core.io.ClassPathResource;
|
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.Iterator;
|
import java.util.Map;
|
|
public class JsonUtils {
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
/**
|
* json字符串转驼峰
|
*
|
* @param json String
|
* @return String
|
*/
|
public static String convertToCamelCaseJson(String json) {
|
JsonNode jsonNode = null;
|
try {
|
jsonNode = objectMapper.readTree(json);
|
} catch (JsonProcessingException e) {
|
e.printStackTrace();
|
}
|
|
jsonNode = convertKeysToCamelCase(jsonNode);
|
|
try {
|
return objectMapper.writeValueAsString(jsonNode);
|
} catch (JsonProcessingException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* JsonNode的key转驼峰
|
*
|
* @param jsonNode JsonNode
|
* @return JsonNode
|
*/
|
public static JsonNode convertKeysToCamelCase(JsonNode jsonNode) {
|
if (jsonNode.isObject()) {
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
jsonNode.fields().forEachRemaining(entry -> {
|
objectNode.set(snakeToCamel(entry.getKey()), convertKeysToCamelCase(entry.getValue()));
|
});
|
return objectNode;
|
}
|
|
if (jsonNode.isArray()) {
|
ArrayNode arrayNode = objectMapper.createArrayNode();
|
jsonNode.elements().forEachRemaining(entry -> {
|
arrayNode.add(convertKeysToCamelCase(entry));
|
});
|
|
return arrayNode;
|
}
|
|
return jsonNode;
|
}
|
|
public static JsonNode convertKeysToSnake(JsonNode jsonNode) {
|
if (jsonNode.isObject()) {
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
jsonNode.fields().forEachRemaining(entry -> {
|
objectNode.set(camelToSnake(entry.getKey()), convertKeysToCamelCase(entry.getValue()));
|
});
|
return objectNode;
|
}
|
|
if (jsonNode.isArray()) {
|
ArrayNode arrayNode = objectMapper.createArrayNode();
|
jsonNode.elements().forEachRemaining(entry -> {
|
arrayNode.add(convertKeysToSnake(entry));
|
});
|
|
return arrayNode;
|
}
|
|
return jsonNode;
|
}
|
|
|
/**
|
* 下划线转驼峰
|
*
|
* @param key String
|
* @return String
|
*/
|
public static String snakeToCamel(String key) {
|
if (key.indexOf("_") != 0) {
|
String[] s = key.split("_");
|
StringBuilder stringBuilder = new StringBuilder(s[0]);
|
for (int i = 1; i < s.length; i++) {
|
stringBuilder.append(s[i].substring(0, 1).toUpperCase()).
|
append(s[i].substring(1));
|
}
|
return stringBuilder.toString();
|
}
|
|
return key;
|
}
|
|
|
/**
|
* 驼峰转下划线
|
*
|
* @param key String
|
* @return String
|
*/
|
public static String camelToSnake(String key) {
|
int length = key.length();
|
StringBuilder sb = new StringBuilder(length);
|
for (int i = 0; i < length; i++) {
|
char current = key.charAt(i);
|
if (Character.isUpperCase(current)) {
|
if (i > 0) {
|
sb.append('_');
|
}
|
sb.append(Character.toLowerCase(current));
|
continue;
|
}
|
sb.append(current);
|
}
|
return sb.toString();
|
}
|
|
|
public static String toCamelCaseJson(String json) {
|
// 原始 JSON 字符串(驼峰命名)
|
|
// 原始 JSON 字符串(驼峰命名)
|
|
// 创建 ObjectMapper
|
ObjectMapper mapper = new ObjectMapper();
|
|
// 将 JSON 字符串转换为 JsonNode
|
JsonNode rootNode = null;
|
try {
|
rootNode = mapper.readTree(json);
|
} catch (JsonProcessingException e) {
|
throw new RuntimeException(e);
|
}
|
|
// 递归处理 JSON 键
|
JsonNode convertedNode = convertKeysToSnakeCase(rootNode);
|
|
// 将转换后的 JsonNode 重新序列化为 JSON 字符串
|
String convertedJson = null;
|
try {
|
convertedJson = mapper.writeValueAsString(convertedNode);
|
} catch (JsonProcessingException e) {
|
throw new RuntimeException(e);
|
}
|
return convertedJson;
|
}
|
|
private static JsonNode convertKeysToSnakeCase(JsonNode node) {
|
if (node.isObject()) {
|
ObjectNode objectNode = (ObjectNode) node;
|
ObjectNode newObjectNode = objectNode.objectNode();
|
|
Iterator<Map.Entry<String, JsonNode>> fields = objectNode.fields();
|
while (fields.hasNext()) {
|
Map.Entry<String, JsonNode> field = fields.next();
|
String newKey = camelToSnakeCase(field.getKey()); // 转换键
|
newObjectNode.set(newKey, convertKeysToSnakeCase(field.getValue())); // 递归处理值
|
}
|
|
return newObjectNode;
|
} else if (node.isArray()) {
|
for (int i = 0; i < node.size(); i++) {
|
((ObjectNode) node).set(Integer.toString(i), convertKeysToSnakeCase(node.get(i)));
|
}
|
}
|
|
return node;
|
}
|
|
private static String camelToSnakeCase(String camelCase) {
|
StringBuilder snakeCase = new StringBuilder();
|
for (char ch : camelCase.toCharArray()) {
|
if (Character.isUpperCase(ch)) {
|
snakeCase.append('_').append(Character.toLowerCase(ch));
|
} else {
|
snakeCase.append(ch);
|
}
|
}
|
return snakeCase.toString();
|
}
|
|
/**
|
* 根据路径获取文件中的json
|
* @param path resource 路径
|
* @return json数据
|
*/
|
public static JsonNode loadJsonFile(String path) {
|
try (InputStream inputStream = new ClassPathResource(path).getInputStream()) {
|
ObjectMapper mapper = new ObjectMapper();
|
return mapper.readTree(inputStream);
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
}
|