作者:cndz 围观群众:1152 更新于 标签:Java工具类yamljsonproperties转换工具类
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.14.0-rc3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-properties -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-properties</artifactId>
<version>2.14.0-rc3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.7</version>
</dependency>
package com.demo.utils;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.dataformat.yaml.util.StringQuotingChecker;
import lombok.SneakyThrows;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
/**
* @author:zzy
* @date:Created in 2023/5/3 19:29
* @modified By:zzy
*/
public class ContentConverUtil {
private static ObjectMapper getYamlMappper() {
YAMLFactory yamlFactory = YAMLFactory.builder()
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.stringQuotingChecker(new StringQuotingChecker.Default() {
private static final long serialVersionUID = 1324053635146744895L;
@Override
public boolean needToQuoteValue(String value) {
return false;
}
}).build();
return new YAMLMapper(yamlFactory);
}
/**
* properties转yaml
* @param propertiesContent
* @return
*/
@SneakyThrows
public static String propertiesToYaml(String propertiesContent) {
if(StrUtil.isBlank(propertiesContent)) {return propertiesContent;}
//仅有单层的map
Properties properties = new Properties();
properties.load(IoUtil.toStream(propertiesContent.getBytes(StandardCharsets.UTF_8)));
//具体层级的Map
Dict propertiesDict = Dict.create();
properties.entrySet().stream()
.sorted((mapEntry1,mapEntry2) -> -CompareUtil.compare(Convert.toStr(mapEntry1.getKey()).length(), Convert.toStr(mapEntry2.getKey()).length()))
.forEach(mapEntry -> {
Object KeyName = mapEntry.getKey();
Object keyValue = mapEntry.getValue();
BeanPath.create(KeyName.toString()).set(propertiesDict,keyValue);
});
ObjectMapper yamlMapper = getYamlMappper();
return yamlMapper.writeValueAsString(propertiesDict);
}
/**
* yaml转properties
* @param yamlContent
* @return
*/
@SneakyThrows
public static String yamlToProperties(String yamlContent) {
YAMLMapper yamlMapper = new YAMLMapper();
JsonNode jsonNode = yamlMapper.readTree(yamlContent);
JSONObject entries = JSONUtil.parseObj(jsonNode.toString());
JavaPropsMapper mapper = new JavaPropsMapper();
return mapper.writeValueAsString(entries);
}
/**
* json转properties
* @param json
* @return
*/
@SneakyThrows
public static String jsonToProperties(String json) {
if(JSONUtil.isTypeJSON(json)) {
JavaPropsMapper javaPropsMapper = new JavaPropsMapper();
JSONObject jsonObject = JSONUtil.parseObj(json);
return javaPropsMapper.writeValueAsString(jsonObject);
}
return null;
}
/**
* properties转json
* @param propertiesContent
* @return
*/
@SneakyThrows
public static String propertiesToJson(String propertiesContent) {
if(StrUtil.isBlank(propertiesContent)) {return propertiesContent;}
//仅有单层的map
Properties properties = new Properties();
properties.load(IoUtil.toStream(propertiesContent.getBytes(StandardCharsets.UTF_8)));
//具体层级的Map
Dict propertiesDict = Dict.create();
properties.entrySet().stream()
.sorted((mapEntry1,mapEntry2) -> -CompareUtil.compare(Convert.toStr(mapEntry1.getKey()).length(), Convert.toStr(mapEntry2.getKey()).length()))
.forEach(mapEntry -> {
Object KeyName = mapEntry.getKey();
Object keyValue = mapEntry.getValue();
BeanPath.create(KeyName.toString()).set(propertiesDict,keyValue);
});
return JSONUtil.toJsonPrettyStr(propertiesDict);
}
/**
* json转yaml
* @param json
* @return
*/
@SneakyThrows
public static String jsonToYaml(String json) {
if(JSONUtil.isTypeJSON(json)) {
ObjectMapper yamlMappper = getYamlMappper();
JSONObject jsonObject = JSONUtil.parseObj(json);
return yamlMappper.writeValueAsString(jsonObject);
}
return null;
}
/**
* yaml转json
* @param yaml
* @return
*/
@SneakyThrows
public static String yamlToJson(String yaml) {
if(StrUtil.isBlank(yaml)) { return yaml;}
YAMLMapper yamlMapper = new YAMLMapper();
JsonNode jsonNode = yamlMapper.readTree(yaml);
return JSONUtil.formatJsonStr(jsonNode.toString());
}
}